blob: 37304781e0c771a7286ee57ea3a18f287239e6ec [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) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100487 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100488 __ gs()->call(entry_point);
489 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100490}
491
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000492static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000493// Use a fake return address register to mimic Quick.
494static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400495CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
496 const X86_64InstructionSetFeatures& isa_features,
497 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000498 : CodeGenerator(graph,
499 kNumberOfCpuRegisters,
500 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000502 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
503 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000504 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000505 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
506 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000507 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100508 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000510 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400511 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400512 isa_features_(isa_features),
513 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000514 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
515}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100516
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100517InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
518 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100519 : HGraphVisitor(graph),
520 assembler_(codegen->GetAssembler()),
521 codegen_(codegen) {}
522
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100523Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100524 switch (type) {
525 case Primitive::kPrimLong:
526 case Primitive::kPrimByte:
527 case Primitive::kPrimBoolean:
528 case Primitive::kPrimChar:
529 case Primitive::kPrimShort:
530 case Primitive::kPrimInt:
531 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100532 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100533 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100534 }
535
536 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100538 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100539 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100540 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541
542 case Primitive::kPrimVoid:
543 LOG(FATAL) << "Unreachable type " << type;
544 }
545
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100546 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547}
548
Nicolas Geoffray98893962015-01-21 12:32:32 +0000549void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100550 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100551 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100552
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000553 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100554 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000555
Nicolas Geoffray98893962015-01-21 12:32:32 +0000556 if (is_baseline) {
557 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
558 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
559 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000560 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
561 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
562 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000563 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564}
565
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100566static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100567 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100568}
David Srbecky9d8606d2015-04-12 09:35:32 +0100569
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100570static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100571 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100572}
573
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100575 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000576 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100577 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700578 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000579 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100580
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000581 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100582 __ testq(CpuRegister(RAX), Address(
583 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100584 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100585 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000586
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000587 if (HasEmptyFrame()) {
588 return;
589 }
590
Nicolas Geoffray98893962015-01-21 12:32:32 +0000591 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000592 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000593 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000594 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100595 __ cfi().AdjustCFAOffset(kX86_64WordSize);
596 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000597 }
598 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100599
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100600 int adjust = GetFrameSize() - GetCoreSpillSize();
601 __ subq(CpuRegister(RSP), Immediate(adjust));
602 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000603 uint32_t xmm_spill_location = GetFpuSpillStart();
604 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100605
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000606 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
607 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100608 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
609 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
610 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000611 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100612 }
613
Mathieu Chartiere401d142015-04-22 13:56:20 -0700614 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100615 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100616}
617
618void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100619 __ cfi().RememberState();
620 if (!HasEmptyFrame()) {
621 uint32_t xmm_spill_location = GetFpuSpillStart();
622 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
623 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
624 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
625 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
626 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
627 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
628 }
629 }
630
631 int adjust = GetFrameSize() - GetCoreSpillSize();
632 __ addq(CpuRegister(RSP), Immediate(adjust));
633 __ cfi().AdjustCFAOffset(-adjust);
634
635 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
636 Register reg = kCoreCalleeSaves[i];
637 if (allocated_registers_.ContainsCoreRegister(reg)) {
638 __ popq(CpuRegister(reg));
639 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
640 __ cfi().Restore(DWARFReg(reg));
641 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000642 }
643 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100644 __ ret();
645 __ cfi().RestoreState();
646 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100647}
648
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100649void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
650 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100651}
652
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100653Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
654 switch (load->GetType()) {
655 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100656 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100657 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100658
659 case Primitive::kPrimInt:
660 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100661 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100662 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663
664 case Primitive::kPrimBoolean:
665 case Primitive::kPrimByte:
666 case Primitive::kPrimChar:
667 case Primitive::kPrimShort:
668 case Primitive::kPrimVoid:
669 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700670 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100671 }
672
673 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700674 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675}
676
677void CodeGeneratorX86_64::Move(Location destination, Location source) {
678 if (source.Equals(destination)) {
679 return;
680 }
681 if (destination.IsRegister()) {
682 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000683 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100684 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000685 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000687 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100689 } else {
690 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000691 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 Address(CpuRegister(RSP), source.GetStackIndex()));
693 }
694 } else if (destination.IsFpuRegister()) {
695 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000696 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000698 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100699 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000700 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 Address(CpuRegister(RSP), source.GetStackIndex()));
702 } else {
703 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000704 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100705 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100706 }
707 } else if (destination.IsStackSlot()) {
708 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 } else if (source.IsFpuRegister()) {
712 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000713 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500714 } else if (source.IsConstant()) {
715 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000716 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500717 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100718 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500719 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000720 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
721 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722 }
723 } else {
724 DCHECK(destination.IsDoubleStackSlot());
725 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000727 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100728 } else if (source.IsFpuRegister()) {
729 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000730 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500731 } else if (source.IsConstant()) {
732 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800733 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500734 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000735 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500736 } else {
737 DCHECK(constant->IsLongConstant());
738 value = constant->AsLongConstant()->GetValue();
739 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400740 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100741 } else {
742 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000743 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
744 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100745 }
746 }
747}
748
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100749void CodeGeneratorX86_64::Move(HInstruction* instruction,
750 Location location,
751 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000752 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100753 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700754 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100755 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000756 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100757 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000758 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000759 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
760 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000761 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000762 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000763 } else if (location.IsStackSlot()) {
764 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
765 } else {
766 DCHECK(location.IsConstant());
767 DCHECK_EQ(location.GetConstant(), const_to_move);
768 }
769 } else if (const_to_move->IsLongConstant()) {
770 int64_t value = const_to_move->AsLongConstant()->GetValue();
771 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400772 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000773 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400774 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000775 } else {
776 DCHECK(location.IsConstant());
777 DCHECK_EQ(location.GetConstant(), const_to_move);
778 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100779 }
Roland Levillain476df552014-10-09 17:51:36 +0100780 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100781 switch (instruction->GetType()) {
782 case Primitive::kPrimBoolean:
783 case Primitive::kPrimByte:
784 case Primitive::kPrimChar:
785 case Primitive::kPrimShort:
786 case Primitive::kPrimInt:
787 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100789 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
790 break;
791
792 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000794 Move(location,
795 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100796 break;
797
798 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100799 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100800 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000801 } else if (instruction->IsTemporary()) {
802 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
803 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100804 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100805 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100806 switch (instruction->GetType()) {
807 case Primitive::kPrimBoolean:
808 case Primitive::kPrimByte:
809 case Primitive::kPrimChar:
810 case Primitive::kPrimShort:
811 case Primitive::kPrimInt:
812 case Primitive::kPrimNot:
813 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 case Primitive::kPrimFloat:
815 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000816 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100817 break;
818
819 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100820 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100821 }
822 }
823}
824
David Brazdilfc6a86a2015-06-26 10:33:45 +0000825void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100826 DCHECK(!successor->IsExitBlock());
827
828 HBasicBlock* block = got->GetBlock();
829 HInstruction* previous = got->GetPrevious();
830
831 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000832 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100833 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
834 return;
835 }
836
837 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
838 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
839 }
840 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100841 __ jmp(codegen_->GetLabelOf(successor));
842 }
843}
844
David Brazdilfc6a86a2015-06-26 10:33:45 +0000845void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
846 got->SetLocations(nullptr);
847}
848
849void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
850 HandleGoto(got, got->GetSuccessor());
851}
852
853void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
854 try_boundary->SetLocations(nullptr);
855}
856
857void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
858 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
859 if (!successor->IsExitBlock()) {
860 HandleGoto(try_boundary, successor);
861 }
862}
863
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100864void LocationsBuilderX86_64::VisitExit(HExit* exit) {
865 exit->SetLocations(nullptr);
866}
867
868void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700869 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100870}
871
Mark Mendellc4701932015-04-10 13:18:51 -0400872void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
873 Label* true_label,
874 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100875 if (cond->IsFPConditionTrueIfNaN()) {
876 __ j(kUnordered, true_label);
877 } else if (cond->IsFPConditionFalseIfNaN()) {
878 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400879 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100880 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400881}
882
883void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
884 HCondition* condition,
885 Label* true_target,
886 Label* false_target,
887 Label* always_true_target) {
888 LocationSummary* locations = condition->GetLocations();
889 Location left = locations->InAt(0);
890 Location right = locations->InAt(1);
891
892 // We don't want true_target as a nullptr.
893 if (true_target == nullptr) {
894 true_target = always_true_target;
895 }
896 bool falls_through = (false_target == nullptr);
897
898 // FP compares don't like null false_targets.
899 if (false_target == nullptr) {
900 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
901 }
902
903 Primitive::Type type = condition->InputAt(0)->GetType();
904 switch (type) {
905 case Primitive::kPrimLong: {
906 CpuRegister left_reg = left.AsRegister<CpuRegister>();
907 if (right.IsConstant()) {
908 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
909 if (IsInt<32>(value)) {
910 if (value == 0) {
911 __ testq(left_reg, left_reg);
912 } else {
913 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
914 }
915 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100916 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -0400917 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
918 }
919 } else if (right.IsDoubleStackSlot()) {
920 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
921 } else {
922 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
923 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100924 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -0400925 break;
926 }
927 case Primitive::kPrimFloat: {
928 if (right.IsFpuRegister()) {
929 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
930 } else if (right.IsConstant()) {
931 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
932 codegen_->LiteralFloatAddress(
933 right.GetConstant()->AsFloatConstant()->GetValue()));
934 } else {
935 DCHECK(right.IsStackSlot());
936 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
937 Address(CpuRegister(RSP), right.GetStackIndex()));
938 }
939 GenerateFPJumps(condition, true_target, false_target);
940 break;
941 }
942 case Primitive::kPrimDouble: {
943 if (right.IsFpuRegister()) {
944 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
945 } else if (right.IsConstant()) {
946 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
947 codegen_->LiteralDoubleAddress(
948 right.GetConstant()->AsDoubleConstant()->GetValue()));
949 } else {
950 DCHECK(right.IsDoubleStackSlot());
951 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
952 Address(CpuRegister(RSP), right.GetStackIndex()));
953 }
954 GenerateFPJumps(condition, true_target, false_target);
955 break;
956 }
957 default:
958 LOG(FATAL) << "Unexpected condition type " << type;
959 }
960
961 if (!falls_through) {
962 __ jmp(false_target);
963 }
964}
965
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700966void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
967 Label* true_target,
968 Label* false_target,
969 Label* always_true_target) {
970 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100971 if (cond->IsIntConstant()) {
972 // Constant condition, statically compared against 1.
973 int32_t cond_value = cond->AsIntConstant()->GetValue();
974 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700975 if (always_true_target != nullptr) {
976 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100977 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100978 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100979 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100980 DCHECK_EQ(cond_value, 0);
981 }
982 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100983 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100984 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
985 // Moves do not affect the eflags register, so if the condition is
986 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -0400987 // again. We can't use the eflags on FP conditions if they are
988 // materialized due to the complex branching.
989 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100990 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -0400991 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
992 && !Primitive::IsFloatingPointType(type);
993
Roland Levillain4fa13f62015-07-06 18:11:54 +0100994 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100995 if (!eflags_set) {
996 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700997 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100998 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000999 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001000 } else {
1001 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1002 Immediate(0));
1003 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001004 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001005 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001006 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001007 }
1008 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001009 // Condition has not been materialized, use its inputs as the
1010 // comparison and its condition as the branch condition.
1011
Mark Mendellc4701932015-04-10 13:18:51 -04001012 // Is this a long or FP comparison that has been folded into the HCondition?
1013 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001014 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001015 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1016 true_target, false_target, always_true_target);
1017 return;
1018 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001019
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001020 Location lhs = cond->GetLocations()->InAt(0);
1021 Location rhs = cond->GetLocations()->InAt(1);
1022 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001024 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001025 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001026 if (constant == 0) {
1027 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1028 } else {
1029 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1030 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001031 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001033 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1034 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001035 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001036 }
Dave Allison20dfc792014-06-16 20:44:29 -07001037 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001038 if (false_target != nullptr) {
1039 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001040 }
1041}
1042
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001043void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1044 LocationSummary* locations =
1045 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1046 HInstruction* cond = if_instr->InputAt(0);
1047 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1048 locations->SetInAt(0, Location::Any());
1049 }
1050}
1051
1052void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1053 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1054 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1055 Label* always_true_target = true_target;
1056 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1057 if_instr->IfTrueSuccessor())) {
1058 always_true_target = nullptr;
1059 }
1060 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1061 if_instr->IfFalseSuccessor())) {
1062 false_target = nullptr;
1063 }
1064 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1065}
1066
1067void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1068 LocationSummary* locations = new (GetGraph()->GetArena())
1069 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1070 HInstruction* cond = deoptimize->InputAt(0);
1071 DCHECK(cond->IsCondition());
1072 if (cond->AsCondition()->NeedsMaterialization()) {
1073 locations->SetInAt(0, Location::Any());
1074 }
1075}
1076
1077void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1078 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1079 DeoptimizationSlowPathX86_64(deoptimize);
1080 codegen_->AddSlowPath(slow_path);
1081 Label* slow_path_entry = slow_path->GetEntryLabel();
1082 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1083}
1084
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001085void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1086 local->SetLocations(nullptr);
1087}
1088
1089void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1090 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1091}
1092
1093void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1094 local->SetLocations(nullptr);
1095}
1096
1097void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1098 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001099 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100}
1101
1102void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001103 LocationSummary* locations =
1104 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105 switch (store->InputAt(1)->GetType()) {
1106 case Primitive::kPrimBoolean:
1107 case Primitive::kPrimByte:
1108 case Primitive::kPrimChar:
1109 case Primitive::kPrimShort:
1110 case Primitive::kPrimInt:
1111 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001113 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1114 break;
1115
1116 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1119 break;
1120
1121 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001123 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001124}
1125
1126void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001127 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001128}
1129
Roland Levillain0d37cd02015-05-27 16:39:19 +01001130void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001131 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001132 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001133 // Handle the long/FP comparisons made in instruction simplification.
1134 switch (cond->InputAt(0)->GetType()) {
1135 case Primitive::kPrimLong:
1136 locations->SetInAt(0, Location::RequiresRegister());
1137 locations->SetInAt(1, Location::Any());
1138 break;
1139 case Primitive::kPrimFloat:
1140 case Primitive::kPrimDouble:
1141 locations->SetInAt(0, Location::RequiresFpuRegister());
1142 locations->SetInAt(1, Location::Any());
1143 break;
1144 default:
1145 locations->SetInAt(0, Location::RequiresRegister());
1146 locations->SetInAt(1, Location::Any());
1147 break;
1148 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001149 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001150 locations->SetOut(Location::RequiresRegister());
1151 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152}
1153
Roland Levillain0d37cd02015-05-27 16:39:19 +01001154void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001155 if (!cond->NeedsMaterialization()) {
1156 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001157 }
Mark Mendellc4701932015-04-10 13:18:51 -04001158
1159 LocationSummary* locations = cond->GetLocations();
1160 Location lhs = locations->InAt(0);
1161 Location rhs = locations->InAt(1);
1162 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1163 Label true_label, false_label;
1164
1165 switch (cond->InputAt(0)->GetType()) {
1166 default:
1167 // Integer case.
1168
1169 // Clear output register: setcc only sets the low byte.
1170 __ xorl(reg, reg);
1171
1172 if (rhs.IsRegister()) {
1173 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1174 } else if (rhs.IsConstant()) {
1175 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1176 if (constant == 0) {
1177 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1178 } else {
1179 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1180 }
1181 } else {
1182 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1183 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001184 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001185 return;
1186 case Primitive::kPrimLong:
1187 // Clear output register: setcc only sets the low byte.
1188 __ xorl(reg, reg);
1189
1190 if (rhs.IsRegister()) {
1191 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1192 } else if (rhs.IsConstant()) {
1193 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1194 if (IsInt<32>(value)) {
1195 if (value == 0) {
1196 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1197 } else {
1198 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1199 }
1200 } else {
1201 // Value won't fit in an int.
1202 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1203 }
1204 } else {
1205 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1206 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001207 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001208 return;
1209 case Primitive::kPrimFloat: {
1210 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1211 if (rhs.IsConstant()) {
1212 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1213 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1214 } else if (rhs.IsStackSlot()) {
1215 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1216 } else {
1217 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1218 }
1219 GenerateFPJumps(cond, &true_label, &false_label);
1220 break;
1221 }
1222 case Primitive::kPrimDouble: {
1223 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1224 if (rhs.IsConstant()) {
1225 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1226 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1227 } else if (rhs.IsDoubleStackSlot()) {
1228 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1229 } else {
1230 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1231 }
1232 GenerateFPJumps(cond, &true_label, &false_label);
1233 break;
1234 }
1235 }
1236
1237 // Convert the jumps into the result.
1238 Label done_label;
1239
Roland Levillain4fa13f62015-07-06 18:11:54 +01001240 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001241 __ Bind(&false_label);
1242 __ xorl(reg, reg);
1243 __ jmp(&done_label);
1244
Roland Levillain4fa13f62015-07-06 18:11:54 +01001245 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001246 __ Bind(&true_label);
1247 __ movl(reg, Immediate(1));
1248 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001249}
1250
1251void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1252 VisitCondition(comp);
1253}
1254
1255void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1256 VisitCondition(comp);
1257}
1258
1259void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1260 VisitCondition(comp);
1261}
1262
1263void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1264 VisitCondition(comp);
1265}
1266
1267void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1268 VisitCondition(comp);
1269}
1270
1271void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1272 VisitCondition(comp);
1273}
1274
1275void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1276 VisitCondition(comp);
1277}
1278
1279void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1280 VisitCondition(comp);
1281}
1282
1283void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1284 VisitCondition(comp);
1285}
1286
1287void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1288 VisitCondition(comp);
1289}
1290
1291void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1292 VisitCondition(comp);
1293}
1294
1295void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1296 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001297}
1298
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001299void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001300 LocationSummary* locations =
1301 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001302 switch (compare->InputAt(0)->GetType()) {
1303 case Primitive::kPrimLong: {
1304 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001305 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1307 break;
1308 }
1309 case Primitive::kPrimFloat:
1310 case Primitive::kPrimDouble: {
1311 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001312 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001313 locations->SetOut(Location::RequiresRegister());
1314 break;
1315 }
1316 default:
1317 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1318 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001319}
1320
1321void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001322 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001323 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001324 Location left = locations->InAt(0);
1325 Location right = locations->InAt(1);
1326
1327 Label less, greater, done;
1328 Primitive::Type type = compare->InputAt(0)->GetType();
1329 switch (type) {
1330 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001331 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1332 if (right.IsConstant()) {
1333 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001334 if (IsInt<32>(value)) {
1335 if (value == 0) {
1336 __ testq(left_reg, left_reg);
1337 } else {
1338 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1339 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001340 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001341 // Value won't fit in an int.
1342 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001343 }
Mark Mendell40741f32015-04-20 22:10:34 -04001344 } else if (right.IsDoubleStackSlot()) {
1345 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001346 } else {
1347 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1348 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001349 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001350 }
1351 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001352 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1353 if (right.IsConstant()) {
1354 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1355 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1356 } else if (right.IsStackSlot()) {
1357 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1358 } else {
1359 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1360 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001361 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1362 break;
1363 }
1364 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001365 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1366 if (right.IsConstant()) {
1367 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1368 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1369 } else if (right.IsDoubleStackSlot()) {
1370 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1371 } else {
1372 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1373 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001374 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1375 break;
1376 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001377 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001378 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001379 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001380 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001381 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001382 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001383
Calin Juravle91debbc2014-11-26 19:01:09 +00001384 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001385 __ movl(out, Immediate(1));
1386 __ jmp(&done);
1387
1388 __ Bind(&less);
1389 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001390
1391 __ Bind(&done);
1392}
1393
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001394void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001395 LocationSummary* locations =
1396 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001397 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001398}
1399
1400void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001401 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001402 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001403}
1404
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001405void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1406 LocationSummary* locations =
1407 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1408 locations->SetOut(Location::ConstantLocation(constant));
1409}
1410
1411void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1412 // Will be generated at use site.
1413 UNUSED(constant);
1414}
1415
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001416void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001417 LocationSummary* locations =
1418 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001419 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001420}
1421
1422void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001423 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001424 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001425}
1426
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001427void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1428 LocationSummary* locations =
1429 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1430 locations->SetOut(Location::ConstantLocation(constant));
1431}
1432
1433void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1434 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001435 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001436}
1437
1438void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1439 LocationSummary* locations =
1440 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1441 locations->SetOut(Location::ConstantLocation(constant));
1442}
1443
1444void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1445 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001446 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001447}
1448
Calin Juravle27df7582015-04-17 19:12:31 +01001449void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1450 memory_barrier->SetLocations(nullptr);
1451}
1452
1453void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1454 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1455}
1456
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001457void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1458 ret->SetLocations(nullptr);
1459}
1460
1461void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001462 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001463 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001464}
1465
1466void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001467 LocationSummary* locations =
1468 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001469 switch (ret->InputAt(0)->GetType()) {
1470 case Primitive::kPrimBoolean:
1471 case Primitive::kPrimByte:
1472 case Primitive::kPrimChar:
1473 case Primitive::kPrimShort:
1474 case Primitive::kPrimInt:
1475 case Primitive::kPrimNot:
1476 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001477 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001478 break;
1479
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001480 case Primitive::kPrimFloat:
1481 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001482 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001483 break;
1484
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001485 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001486 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001487 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001488}
1489
1490void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1491 if (kIsDebugBuild) {
1492 switch (ret->InputAt(0)->GetType()) {
1493 case Primitive::kPrimBoolean:
1494 case Primitive::kPrimByte:
1495 case Primitive::kPrimChar:
1496 case Primitive::kPrimShort:
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimNot:
1499 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001500 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001501 break;
1502
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001503 case Primitive::kPrimFloat:
1504 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001505 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001506 XMM0);
1507 break;
1508
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001509 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001510 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001511 }
1512 }
1513 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001514}
1515
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001516Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1517 switch (type) {
1518 case Primitive::kPrimBoolean:
1519 case Primitive::kPrimByte:
1520 case Primitive::kPrimChar:
1521 case Primitive::kPrimShort:
1522 case Primitive::kPrimInt:
1523 case Primitive::kPrimNot:
1524 case Primitive::kPrimLong:
1525 return Location::RegisterLocation(RAX);
1526
1527 case Primitive::kPrimVoid:
1528 return Location::NoLocation();
1529
1530 case Primitive::kPrimDouble:
1531 case Primitive::kPrimFloat:
1532 return Location::FpuRegisterLocation(XMM0);
1533 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001534
1535 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001536}
1537
1538Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1539 return Location::RegisterLocation(kMethodRegisterArgument);
1540}
1541
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001542Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001543 switch (type) {
1544 case Primitive::kPrimBoolean:
1545 case Primitive::kPrimByte:
1546 case Primitive::kPrimChar:
1547 case Primitive::kPrimShort:
1548 case Primitive::kPrimInt:
1549 case Primitive::kPrimNot: {
1550 uint32_t index = gp_index_++;
1551 stack_index_++;
1552 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001553 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001554 } else {
1555 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1556 }
1557 }
1558
1559 case Primitive::kPrimLong: {
1560 uint32_t index = gp_index_;
1561 stack_index_ += 2;
1562 if (index < calling_convention.GetNumberOfRegisters()) {
1563 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001564 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001565 } else {
1566 gp_index_ += 2;
1567 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1568 }
1569 }
1570
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001571 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001572 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001573 stack_index_++;
1574 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001575 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001576 } else {
1577 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1578 }
1579 }
1580
1581 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001582 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001583 stack_index_ += 2;
1584 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001585 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001586 } else {
1587 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1588 }
1589 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001590
1591 case Primitive::kPrimVoid:
1592 LOG(FATAL) << "Unexpected parameter type " << type;
1593 break;
1594 }
1595 return Location();
1596}
1597
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001598void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001599 // When we do not run baseline, explicit clinit checks triggered by static
1600 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1601 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001602
Mark Mendellfb8d2792015-03-31 22:16:59 -04001603 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001604 if (intrinsic.TryDispatch(invoke)) {
1605 return;
1606 }
1607
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001608 HandleInvoke(invoke);
1609}
1610
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001611static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1612 if (invoke->GetLocations()->Intrinsified()) {
1613 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1614 intrinsic.Dispatch(invoke);
1615 return true;
1616 }
1617 return false;
1618}
1619
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001620void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001621 // When we do not run baseline, explicit clinit checks triggered by static
1622 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1623 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001624
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001625 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1626 return;
1627 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001628
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001629 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001630 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001631 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001632 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001633}
1634
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001635void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001636 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001637 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001638}
1639
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001640void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001641 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001642 if (intrinsic.TryDispatch(invoke)) {
1643 return;
1644 }
1645
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001646 HandleInvoke(invoke);
1647}
1648
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001649void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001650 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1651 return;
1652 }
1653
Roland Levillain271ab9c2014-11-27 15:23:57 +00001654 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001655 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1656 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001657 LocationSummary* locations = invoke->GetLocations();
1658 Location receiver = locations->InAt(0);
1659 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1660 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001661 DCHECK(receiver.IsRegister());
1662 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001663 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001664 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001665 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001666 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001667 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001668 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001669 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001670
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001671 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001672 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001673}
1674
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001675void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1676 HandleInvoke(invoke);
1677 // Add the hidden argument.
1678 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1679}
1680
1681void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1682 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001684 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1685 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001686 LocationSummary* locations = invoke->GetLocations();
1687 Location receiver = locations->InAt(0);
1688 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1689
1690 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001691 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1692 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001693
1694 // temp = object->GetClass();
1695 if (receiver.IsStackSlot()) {
1696 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1697 __ movl(temp, Address(temp, class_offset));
1698 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001699 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001700 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001701 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001702 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001703 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001704 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001705 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001706 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001707 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001708
1709 DCHECK(!codegen_->IsLeafMethod());
1710 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1711}
1712
Roland Levillain88cb1752014-10-20 16:36:47 +01001713void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1714 LocationSummary* locations =
1715 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1716 switch (neg->GetResultType()) {
1717 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001718 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001719 locations->SetInAt(0, Location::RequiresRegister());
1720 locations->SetOut(Location::SameAsFirstInput());
1721 break;
1722
Roland Levillain88cb1752014-10-20 16:36:47 +01001723 case Primitive::kPrimFloat:
1724 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001725 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001726 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001727 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001728 break;
1729
1730 default:
1731 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1732 }
1733}
1734
1735void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1736 LocationSummary* locations = neg->GetLocations();
1737 Location out = locations->Out();
1738 Location in = locations->InAt(0);
1739 switch (neg->GetResultType()) {
1740 case Primitive::kPrimInt:
1741 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001742 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001743 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001744 break;
1745
1746 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001747 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001748 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001749 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001750 break;
1751
Roland Levillain5368c212014-11-27 15:03:41 +00001752 case Primitive::kPrimFloat: {
1753 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001754 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001755 // Implement float negation with an exclusive or with value
1756 // 0x80000000 (mask for bit 31, representing the sign of a
1757 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001758 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001759 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001760 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001761 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001762
Roland Levillain5368c212014-11-27 15:03:41 +00001763 case Primitive::kPrimDouble: {
1764 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001765 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001766 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001767 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001768 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001769 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001770 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001771 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001772 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001773
1774 default:
1775 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1776 }
1777}
1778
Roland Levillaindff1f282014-11-05 14:15:05 +00001779void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1780 LocationSummary* locations =
1781 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1782 Primitive::Type result_type = conversion->GetResultType();
1783 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001784 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001785
David Brazdilb2bd1c52015-03-25 11:17:37 +00001786 // The Java language does not allow treating boolean as an integral type but
1787 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001788
Roland Levillaindff1f282014-11-05 14:15:05 +00001789 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001790 case Primitive::kPrimByte:
1791 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001792 case Primitive::kPrimBoolean:
1793 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001794 case Primitive::kPrimShort:
1795 case Primitive::kPrimInt:
1796 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001797 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001798 locations->SetInAt(0, Location::Any());
1799 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1800 break;
1801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillain01a8d712014-11-14 16:27:39 +00001808 case Primitive::kPrimShort:
1809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimInt:
1814 case Primitive::kPrimChar:
1815 // Processing a Dex `int-to-short' instruction.
1816 locations->SetInAt(0, Location::Any());
1817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1818 break;
1819
1820 default:
1821 LOG(FATAL) << "Unexpected type conversion from " << input_type
1822 << " to " << result_type;
1823 }
1824 break;
1825
Roland Levillain946e1432014-11-11 17:35:19 +00001826 case Primitive::kPrimInt:
1827 switch (input_type) {
1828 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001829 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001830 locations->SetInAt(0, Location::Any());
1831 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1832 break;
1833
1834 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001835 // Processing a Dex `float-to-int' instruction.
1836 locations->SetInAt(0, Location::RequiresFpuRegister());
1837 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001838 break;
1839
Roland Levillain946e1432014-11-11 17:35:19 +00001840 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001841 // Processing a Dex `double-to-int' instruction.
1842 locations->SetInAt(0, Location::RequiresFpuRegister());
1843 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001844 break;
1845
1846 default:
1847 LOG(FATAL) << "Unexpected type conversion from " << input_type
1848 << " to " << result_type;
1849 }
1850 break;
1851
Roland Levillaindff1f282014-11-05 14:15:05 +00001852 case Primitive::kPrimLong:
1853 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001854 case Primitive::kPrimBoolean:
1855 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001856 case Primitive::kPrimByte:
1857 case Primitive::kPrimShort:
1858 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001859 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001860 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 // TODO: We would benefit from a (to-be-implemented)
1862 // Location::RegisterOrStackSlot requirement for this input.
1863 locations->SetInAt(0, Location::RequiresRegister());
1864 locations->SetOut(Location::RequiresRegister());
1865 break;
1866
1867 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001868 // Processing a Dex `float-to-long' instruction.
1869 locations->SetInAt(0, Location::RequiresFpuRegister());
1870 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001871 break;
1872
Roland Levillaindff1f282014-11-05 14:15:05 +00001873 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001874 // Processing a Dex `double-to-long' instruction.
1875 locations->SetInAt(0, Location::RequiresFpuRegister());
1876 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001877 break;
1878
1879 default:
1880 LOG(FATAL) << "Unexpected type conversion from " << input_type
1881 << " to " << result_type;
1882 }
1883 break;
1884
Roland Levillain981e4542014-11-14 11:47:14 +00001885 case Primitive::kPrimChar:
1886 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001887 case Primitive::kPrimBoolean:
1888 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001889 case Primitive::kPrimByte:
1890 case Primitive::kPrimShort:
1891 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001892 // Processing a Dex `int-to-char' instruction.
1893 locations->SetInAt(0, Location::Any());
1894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1895 break;
1896
1897 default:
1898 LOG(FATAL) << "Unexpected type conversion from " << input_type
1899 << " to " << result_type;
1900 }
1901 break;
1902
Roland Levillaindff1f282014-11-05 14:15:05 +00001903 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001904 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001905 case Primitive::kPrimBoolean:
1906 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001907 case Primitive::kPrimByte:
1908 case Primitive::kPrimShort:
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimChar:
1911 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001912 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001913 locations->SetOut(Location::RequiresFpuRegister());
1914 break;
1915
1916 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001917 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001918 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001919 locations->SetOut(Location::RequiresFpuRegister());
1920 break;
1921
Roland Levillaincff13742014-11-17 14:32:17 +00001922 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001923 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001924 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001925 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001926 break;
1927
1928 default:
1929 LOG(FATAL) << "Unexpected type conversion from " << input_type
1930 << " to " << result_type;
1931 };
1932 break;
1933
Roland Levillaindff1f282014-11-05 14:15:05 +00001934 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001935 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001936 case Primitive::kPrimBoolean:
1937 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001938 case Primitive::kPrimByte:
1939 case Primitive::kPrimShort:
1940 case Primitive::kPrimInt:
1941 case Primitive::kPrimChar:
1942 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001943 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001944 locations->SetOut(Location::RequiresFpuRegister());
1945 break;
1946
1947 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001948 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001949 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001950 locations->SetOut(Location::RequiresFpuRegister());
1951 break;
1952
Roland Levillaincff13742014-11-17 14:32:17 +00001953 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001954 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001955 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001956 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001957 break;
1958
1959 default:
1960 LOG(FATAL) << "Unexpected type conversion from " << input_type
1961 << " to " << result_type;
1962 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001963 break;
1964
1965 default:
1966 LOG(FATAL) << "Unexpected type conversion from " << input_type
1967 << " to " << result_type;
1968 }
1969}
1970
1971void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1972 LocationSummary* locations = conversion->GetLocations();
1973 Location out = locations->Out();
1974 Location in = locations->InAt(0);
1975 Primitive::Type result_type = conversion->GetResultType();
1976 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001977 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001978 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001979 case Primitive::kPrimByte:
1980 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001981 case Primitive::kPrimBoolean:
1982 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001983 case Primitive::kPrimShort:
1984 case Primitive::kPrimInt:
1985 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001986 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001987 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001988 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001989 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001991 Address(CpuRegister(RSP), in.GetStackIndex()));
1992 } else {
1993 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001994 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001995 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1996 }
1997 break;
1998
1999 default:
2000 LOG(FATAL) << "Unexpected type conversion from " << input_type
2001 << " to " << result_type;
2002 }
2003 break;
2004
Roland Levillain01a8d712014-11-14 16:27:39 +00002005 case Primitive::kPrimShort:
2006 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002007 case Primitive::kPrimBoolean:
2008 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002009 case Primitive::kPrimByte:
2010 case Primitive::kPrimInt:
2011 case Primitive::kPrimChar:
2012 // Processing a Dex `int-to-short' instruction.
2013 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002014 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002015 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002016 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002017 Address(CpuRegister(RSP), in.GetStackIndex()));
2018 } else {
2019 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002021 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2022 }
2023 break;
2024
2025 default:
2026 LOG(FATAL) << "Unexpected type conversion from " << input_type
2027 << " to " << result_type;
2028 }
2029 break;
2030
Roland Levillain946e1432014-11-11 17:35:19 +00002031 case Primitive::kPrimInt:
2032 switch (input_type) {
2033 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002034 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002035 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002036 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002037 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002038 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002039 Address(CpuRegister(RSP), in.GetStackIndex()));
2040 } else {
2041 DCHECK(in.IsConstant());
2042 DCHECK(in.GetConstant()->IsLongConstant());
2043 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002045 }
2046 break;
2047
Roland Levillain3f8f9362014-12-02 17:45:01 +00002048 case Primitive::kPrimFloat: {
2049 // Processing a Dex `float-to-int' instruction.
2050 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2051 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain3f8f9362014-12-02 17:45:01 +00002052 Label done, nan;
2053
2054 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002055 // if input >= (float)INT_MAX goto done
2056 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002057 __ j(kAboveEqual, &done);
2058 // if input == NaN goto nan
2059 __ j(kUnordered, &nan);
2060 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002061 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002062 __ jmp(&done);
2063 __ Bind(&nan);
2064 // output = 0
2065 __ xorl(output, output);
2066 __ Bind(&done);
2067 break;
2068 }
2069
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002070 case Primitive::kPrimDouble: {
2071 // Processing a Dex `double-to-int' instruction.
2072 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2073 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002074 Label done, nan;
2075
2076 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002077 // if input >= (double)INT_MAX goto done
2078 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002079 __ j(kAboveEqual, &done);
2080 // if input == NaN goto nan
2081 __ j(kUnordered, &nan);
2082 // output = double-to-int-truncate(input)
2083 __ cvttsd2si(output, input);
2084 __ jmp(&done);
2085 __ Bind(&nan);
2086 // output = 0
2087 __ xorl(output, output);
2088 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002089 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002090 }
Roland Levillain946e1432014-11-11 17:35:19 +00002091
2092 default:
2093 LOG(FATAL) << "Unexpected type conversion from " << input_type
2094 << " to " << result_type;
2095 }
2096 break;
2097
Roland Levillaindff1f282014-11-05 14:15:05 +00002098 case Primitive::kPrimLong:
2099 switch (input_type) {
2100 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002101 case Primitive::kPrimBoolean:
2102 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002103 case Primitive::kPrimByte:
2104 case Primitive::kPrimShort:
2105 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002106 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002107 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002108 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002109 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002110 break;
2111
Roland Levillain624279f2014-12-04 11:54:28 +00002112 case Primitive::kPrimFloat: {
2113 // Processing a Dex `float-to-long' instruction.
2114 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2115 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain624279f2014-12-04 11:54:28 +00002116 Label done, nan;
2117
Mark Mendell92e83bf2015-05-07 11:25:03 -04002118 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002119 // if input >= (float)LONG_MAX goto done
2120 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002121 __ j(kAboveEqual, &done);
2122 // if input == NaN goto nan
2123 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002124 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002125 __ cvttss2si(output, input, true);
2126 __ jmp(&done);
2127 __ Bind(&nan);
2128 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002129 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002130 __ Bind(&done);
2131 break;
2132 }
2133
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002134 case Primitive::kPrimDouble: {
2135 // Processing a Dex `double-to-long' instruction.
2136 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2137 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002138 Label done, nan;
2139
Mark Mendell92e83bf2015-05-07 11:25:03 -04002140 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002141 // if input >= (double)LONG_MAX goto done
2142 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002143 __ j(kAboveEqual, &done);
2144 // if input == NaN goto nan
2145 __ j(kUnordered, &nan);
2146 // output = double-to-long-truncate(input)
2147 __ cvttsd2si(output, input, true);
2148 __ jmp(&done);
2149 __ Bind(&nan);
2150 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002151 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002152 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002153 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002154 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002155
2156 default:
2157 LOG(FATAL) << "Unexpected type conversion from " << input_type
2158 << " to " << result_type;
2159 }
2160 break;
2161
Roland Levillain981e4542014-11-14 11:47:14 +00002162 case Primitive::kPrimChar:
2163 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002164 case Primitive::kPrimBoolean:
2165 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002166 case Primitive::kPrimByte:
2167 case Primitive::kPrimShort:
2168 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002169 // Processing a Dex `int-to-char' instruction.
2170 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002172 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002173 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002174 Address(CpuRegister(RSP), in.GetStackIndex()));
2175 } else {
2176 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002177 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002178 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2179 }
2180 break;
2181
2182 default:
2183 LOG(FATAL) << "Unexpected type conversion from " << input_type
2184 << " to " << result_type;
2185 }
2186 break;
2187
Roland Levillaindff1f282014-11-05 14:15:05 +00002188 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002189 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002190 case Primitive::kPrimBoolean:
2191 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002192 case Primitive::kPrimByte:
2193 case Primitive::kPrimShort:
2194 case Primitive::kPrimInt:
2195 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002196 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002197 if (in.IsRegister()) {
2198 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2199 } else if (in.IsConstant()) {
2200 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2201 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2202 if (v == 0) {
2203 __ xorps(dest, dest);
2204 } else {
2205 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2206 }
2207 } else {
2208 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2209 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2210 }
Roland Levillaincff13742014-11-17 14:32:17 +00002211 break;
2212
2213 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002214 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002215 if (in.IsRegister()) {
2216 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2217 } else if (in.IsConstant()) {
2218 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2219 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2220 if (v == 0) {
2221 __ xorps(dest, dest);
2222 } else {
2223 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2224 }
2225 } else {
2226 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2227 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2228 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002229 break;
2230
Roland Levillaincff13742014-11-17 14:32:17 +00002231 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002232 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002233 if (in.IsFpuRegister()) {
2234 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2235 } else if (in.IsConstant()) {
2236 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2237 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2238 if (bit_cast<int64_t, double>(v) == 0) {
2239 __ xorps(dest, dest);
2240 } else {
2241 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2242 }
2243 } else {
2244 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2245 Address(CpuRegister(RSP), in.GetStackIndex()));
2246 }
Roland Levillaincff13742014-11-17 14:32:17 +00002247 break;
2248
2249 default:
2250 LOG(FATAL) << "Unexpected type conversion from " << input_type
2251 << " to " << result_type;
2252 };
2253 break;
2254
Roland Levillaindff1f282014-11-05 14:15:05 +00002255 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002256 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002257 case Primitive::kPrimBoolean:
2258 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002259 case Primitive::kPrimByte:
2260 case Primitive::kPrimShort:
2261 case Primitive::kPrimInt:
2262 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002263 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002264 if (in.IsRegister()) {
2265 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2266 } else if (in.IsConstant()) {
2267 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2268 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2269 if (v == 0) {
2270 __ xorpd(dest, dest);
2271 } else {
2272 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2273 }
2274 } else {
2275 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2276 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2277 }
Roland Levillaincff13742014-11-17 14:32:17 +00002278 break;
2279
2280 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002281 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002282 if (in.IsRegister()) {
2283 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2284 } else if (in.IsConstant()) {
2285 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2286 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2287 if (v == 0) {
2288 __ xorpd(dest, dest);
2289 } else {
2290 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2291 }
2292 } else {
2293 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2294 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2295 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002296 break;
2297
Roland Levillaincff13742014-11-17 14:32:17 +00002298 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002299 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002300 if (in.IsFpuRegister()) {
2301 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2302 } else if (in.IsConstant()) {
2303 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2304 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2305 if (bit_cast<int32_t, float>(v) == 0) {
2306 __ xorpd(dest, dest);
2307 } else {
2308 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2309 }
2310 } else {
2311 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2312 Address(CpuRegister(RSP), in.GetStackIndex()));
2313 }
Roland Levillaincff13742014-11-17 14:32:17 +00002314 break;
2315
2316 default:
2317 LOG(FATAL) << "Unexpected type conversion from " << input_type
2318 << " to " << result_type;
2319 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002320 break;
2321
2322 default:
2323 LOG(FATAL) << "Unexpected type conversion from " << input_type
2324 << " to " << result_type;
2325 }
2326}
2327
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002328void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002329 LocationSummary* locations =
2330 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002331 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002332 case Primitive::kPrimInt: {
2333 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002334 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2335 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002336 break;
2337 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002338
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002339 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002340 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002341 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002342 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002344 break;
2345 }
2346
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002347 case Primitive::kPrimDouble:
2348 case Primitive::kPrimFloat: {
2349 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002350 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002351 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002352 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002353 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002354
2355 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002356 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002357 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002358}
2359
2360void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2361 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002362 Location first = locations->InAt(0);
2363 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002364 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002365
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002366 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002367 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002368 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002369 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2370 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002371 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2372 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002373 } else {
2374 __ leal(out.AsRegister<CpuRegister>(), Address(
2375 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2376 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002377 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002378 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2379 __ addl(out.AsRegister<CpuRegister>(),
2380 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2381 } else {
2382 __ leal(out.AsRegister<CpuRegister>(), Address(
2383 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2384 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002385 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002386 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002387 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002388 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002389 break;
2390 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002391
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002392 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002393 if (second.IsRegister()) {
2394 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2395 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002396 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2397 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002398 } else {
2399 __ leaq(out.AsRegister<CpuRegister>(), Address(
2400 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2401 }
2402 } else {
2403 DCHECK(second.IsConstant());
2404 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2405 int32_t int32_value = Low32Bits(value);
2406 DCHECK_EQ(int32_value, value);
2407 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2408 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2409 } else {
2410 __ leaq(out.AsRegister<CpuRegister>(), Address(
2411 first.AsRegister<CpuRegister>(), int32_value));
2412 }
2413 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002414 break;
2415 }
2416
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002417 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002418 if (second.IsFpuRegister()) {
2419 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2420 } else if (second.IsConstant()) {
2421 __ addss(first.AsFpuRegister<XmmRegister>(),
2422 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2423 } else {
2424 DCHECK(second.IsStackSlot());
2425 __ addss(first.AsFpuRegister<XmmRegister>(),
2426 Address(CpuRegister(RSP), second.GetStackIndex()));
2427 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002428 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002429 }
2430
2431 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002432 if (second.IsFpuRegister()) {
2433 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2434 } else if (second.IsConstant()) {
2435 __ addsd(first.AsFpuRegister<XmmRegister>(),
2436 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2437 } else {
2438 DCHECK(second.IsDoubleStackSlot());
2439 __ addsd(first.AsFpuRegister<XmmRegister>(),
2440 Address(CpuRegister(RSP), second.GetStackIndex()));
2441 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002442 break;
2443 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002444
2445 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002446 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002447 }
2448}
2449
2450void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002451 LocationSummary* locations =
2452 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002453 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002454 case Primitive::kPrimInt: {
2455 locations->SetInAt(0, Location::RequiresRegister());
2456 locations->SetInAt(1, Location::Any());
2457 locations->SetOut(Location::SameAsFirstInput());
2458 break;
2459 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002460 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002461 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002462 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002463 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002464 break;
2465 }
Calin Juravle11351682014-10-23 15:38:15 +01002466 case Primitive::kPrimFloat:
2467 case Primitive::kPrimDouble: {
2468 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002469 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002470 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002471 break;
Calin Juravle11351682014-10-23 15:38:15 +01002472 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002473 default:
Calin Juravle11351682014-10-23 15:38:15 +01002474 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002475 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002476}
2477
2478void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2479 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002480 Location first = locations->InAt(0);
2481 Location second = locations->InAt(1);
2482 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002483 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002484 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002485 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002487 } else if (second.IsConstant()) {
2488 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002489 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002490 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002491 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002492 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002493 break;
2494 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002495 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002496 if (second.IsConstant()) {
2497 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2498 DCHECK(IsInt<32>(value));
2499 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2500 } else {
2501 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2502 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002503 break;
2504 }
2505
Calin Juravle11351682014-10-23 15:38:15 +01002506 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002507 if (second.IsFpuRegister()) {
2508 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2509 } else if (second.IsConstant()) {
2510 __ subss(first.AsFpuRegister<XmmRegister>(),
2511 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2512 } else {
2513 DCHECK(second.IsStackSlot());
2514 __ subss(first.AsFpuRegister<XmmRegister>(),
2515 Address(CpuRegister(RSP), second.GetStackIndex()));
2516 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002517 break;
Calin Juravle11351682014-10-23 15:38:15 +01002518 }
2519
2520 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002521 if (second.IsFpuRegister()) {
2522 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2523 } else if (second.IsConstant()) {
2524 __ subsd(first.AsFpuRegister<XmmRegister>(),
2525 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2526 } else {
2527 DCHECK(second.IsDoubleStackSlot());
2528 __ subsd(first.AsFpuRegister<XmmRegister>(),
2529 Address(CpuRegister(RSP), second.GetStackIndex()));
2530 }
Calin Juravle11351682014-10-23 15:38:15 +01002531 break;
2532 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002533
2534 default:
Calin Juravle11351682014-10-23 15:38:15 +01002535 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002536 }
2537}
2538
Calin Juravle34bacdf2014-10-07 20:23:36 +01002539void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2540 LocationSummary* locations =
2541 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2542 switch (mul->GetResultType()) {
2543 case Primitive::kPrimInt: {
2544 locations->SetInAt(0, Location::RequiresRegister());
2545 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002546 if (mul->InputAt(1)->IsIntConstant()) {
2547 // Can use 3 operand multiply.
2548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2549 } else {
2550 locations->SetOut(Location::SameAsFirstInput());
2551 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002552 break;
2553 }
2554 case Primitive::kPrimLong: {
2555 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002556 locations->SetInAt(1, Location::Any());
2557 if (mul->InputAt(1)->IsLongConstant() &&
2558 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002559 // 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 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002566 case Primitive::kPrimFloat:
2567 case Primitive::kPrimDouble: {
2568 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002569 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002570 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002571 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002572 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002573
2574 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002575 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002576 }
2577}
2578
2579void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2580 LocationSummary* locations = mul->GetLocations();
2581 Location first = locations->InAt(0);
2582 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002583 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002584 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002585 case Primitive::kPrimInt:
2586 // The constant may have ended up in a register, so test explicitly to avoid
2587 // problems where the output may not be the same as the first operand.
2588 if (mul->InputAt(1)->IsIntConstant()) {
2589 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2590 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2591 } else if (second.IsRegister()) {
2592 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002594 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002595 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002596 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002597 __ imull(first.AsRegister<CpuRegister>(),
2598 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002599 }
2600 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002601 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002602 // The constant may have ended up in a register, so test explicitly to avoid
2603 // problems where the output may not be the same as the first operand.
2604 if (mul->InputAt(1)->IsLongConstant()) {
2605 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2606 if (IsInt<32>(value)) {
2607 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2608 Immediate(static_cast<int32_t>(value)));
2609 } else {
2610 // Have to use the constant area.
2611 DCHECK(first.Equals(out));
2612 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2613 }
2614 } else if (second.IsRegister()) {
2615 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002616 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002617 } else {
2618 DCHECK(second.IsDoubleStackSlot());
2619 DCHECK(first.Equals(out));
2620 __ imulq(first.AsRegister<CpuRegister>(),
2621 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002622 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002623 break;
2624 }
2625
Calin Juravleb5bfa962014-10-21 18:02:24 +01002626 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002627 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002628 if (second.IsFpuRegister()) {
2629 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2630 } else if (second.IsConstant()) {
2631 __ mulss(first.AsFpuRegister<XmmRegister>(),
2632 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2633 } else {
2634 DCHECK(second.IsStackSlot());
2635 __ mulss(first.AsFpuRegister<XmmRegister>(),
2636 Address(CpuRegister(RSP), second.GetStackIndex()));
2637 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002638 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002639 }
2640
2641 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002642 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002643 if (second.IsFpuRegister()) {
2644 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2645 } else if (second.IsConstant()) {
2646 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2647 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2648 } else {
2649 DCHECK(second.IsDoubleStackSlot());
2650 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2651 Address(CpuRegister(RSP), second.GetStackIndex()));
2652 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002653 break;
2654 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002655
2656 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002657 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658 }
2659}
2660
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002661void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2662 uint32_t stack_adjustment, bool is_float) {
2663 if (source.IsStackSlot()) {
2664 DCHECK(is_float);
2665 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2666 } else if (source.IsDoubleStackSlot()) {
2667 DCHECK(!is_float);
2668 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2669 } else {
2670 // Write the value to the temporary location on the stack and load to FP stack.
2671 if (is_float) {
2672 Location stack_temp = Location::StackSlot(temp_offset);
2673 codegen_->Move(stack_temp, source);
2674 __ flds(Address(CpuRegister(RSP), temp_offset));
2675 } else {
2676 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2677 codegen_->Move(stack_temp, source);
2678 __ fldl(Address(CpuRegister(RSP), temp_offset));
2679 }
2680 }
2681}
2682
2683void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2684 Primitive::Type type = rem->GetResultType();
2685 bool is_float = type == Primitive::kPrimFloat;
2686 size_t elem_size = Primitive::ComponentSize(type);
2687 LocationSummary* locations = rem->GetLocations();
2688 Location first = locations->InAt(0);
2689 Location second = locations->InAt(1);
2690 Location out = locations->Out();
2691
2692 // Create stack space for 2 elements.
2693 // TODO: enhance register allocator to ask for stack temporaries.
2694 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2695
2696 // Load the values to the FP stack in reverse order, using temporaries if needed.
2697 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2698 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2699
2700 // Loop doing FPREM until we stabilize.
2701 Label retry;
2702 __ Bind(&retry);
2703 __ fprem();
2704
2705 // Move FP status to AX.
2706 __ fstsw();
2707
2708 // And see if the argument reduction is complete. This is signaled by the
2709 // C2 FPU flag bit set to 0.
2710 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2711 __ j(kNotEqual, &retry);
2712
2713 // We have settled on the final value. Retrieve it into an XMM register.
2714 // Store FP top of stack to real stack.
2715 if (is_float) {
2716 __ fsts(Address(CpuRegister(RSP), 0));
2717 } else {
2718 __ fstl(Address(CpuRegister(RSP), 0));
2719 }
2720
2721 // Pop the 2 items from the FP stack.
2722 __ fucompp();
2723
2724 // Load the value from the stack into an XMM register.
2725 DCHECK(out.IsFpuRegister()) << out;
2726 if (is_float) {
2727 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2728 } else {
2729 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2730 }
2731
2732 // And remove the temporary stack space we allocated.
2733 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2734}
2735
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002736void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2737 DCHECK(instruction->IsDiv() || instruction->IsRem());
2738
2739 LocationSummary* locations = instruction->GetLocations();
2740 Location second = locations->InAt(1);
2741 DCHECK(second.IsConstant());
2742
2743 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2744 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002745 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002746
2747 DCHECK(imm == 1 || imm == -1);
2748
2749 switch (instruction->GetResultType()) {
2750 case Primitive::kPrimInt: {
2751 if (instruction->IsRem()) {
2752 __ xorl(output_register, output_register);
2753 } else {
2754 __ movl(output_register, input_register);
2755 if (imm == -1) {
2756 __ negl(output_register);
2757 }
2758 }
2759 break;
2760 }
2761
2762 case Primitive::kPrimLong: {
2763 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002764 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002765 } else {
2766 __ movq(output_register, input_register);
2767 if (imm == -1) {
2768 __ negq(output_register);
2769 }
2770 }
2771 break;
2772 }
2773
2774 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002775 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002776 }
2777}
2778
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002779void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002780 LocationSummary* locations = instruction->GetLocations();
2781 Location second = locations->InAt(1);
2782
2783 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2784 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2785
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002786 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002787
2788 DCHECK(IsPowerOfTwo(std::abs(imm)));
2789
2790 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2791
2792 if (instruction->GetResultType() == Primitive::kPrimInt) {
2793 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2794 __ testl(numerator, numerator);
2795 __ cmov(kGreaterEqual, tmp, numerator);
2796 int shift = CTZ(imm);
2797 __ sarl(tmp, Immediate(shift));
2798
2799 if (imm < 0) {
2800 __ negl(tmp);
2801 }
2802
2803 __ movl(output_register, tmp);
2804 } else {
2805 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2806 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2807
Mark Mendell92e83bf2015-05-07 11:25:03 -04002808 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002809 __ addq(rdx, numerator);
2810 __ testq(numerator, numerator);
2811 __ cmov(kGreaterEqual, rdx, numerator);
2812 int shift = CTZ(imm);
2813 __ sarq(rdx, Immediate(shift));
2814
2815 if (imm < 0) {
2816 __ negq(rdx);
2817 }
2818
2819 __ movq(output_register, rdx);
2820 }
2821}
2822
2823void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2824 DCHECK(instruction->IsDiv() || instruction->IsRem());
2825
2826 LocationSummary* locations = instruction->GetLocations();
2827 Location second = locations->InAt(1);
2828
2829 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2830 : locations->GetTemp(0).AsRegister<CpuRegister>();
2831 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2832 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2833 : locations->Out().AsRegister<CpuRegister>();
2834 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2835
2836 DCHECK_EQ(RAX, eax.AsRegister());
2837 DCHECK_EQ(RDX, edx.AsRegister());
2838 if (instruction->IsDiv()) {
2839 DCHECK_EQ(RAX, out.AsRegister());
2840 } else {
2841 DCHECK_EQ(RDX, out.AsRegister());
2842 }
2843
2844 int64_t magic;
2845 int shift;
2846
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002847 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002848 if (instruction->GetResultType() == Primitive::kPrimInt) {
2849 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2850
2851 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2852
2853 __ movl(numerator, eax);
2854
2855 Label no_div;
2856 Label end;
2857 __ testl(eax, eax);
2858 __ j(kNotEqual, &no_div);
2859
2860 __ xorl(out, out);
2861 __ jmp(&end);
2862
2863 __ Bind(&no_div);
2864
2865 __ movl(eax, Immediate(magic));
2866 __ imull(numerator);
2867
2868 if (imm > 0 && magic < 0) {
2869 __ addl(edx, numerator);
2870 } else if (imm < 0 && magic > 0) {
2871 __ subl(edx, numerator);
2872 }
2873
2874 if (shift != 0) {
2875 __ sarl(edx, Immediate(shift));
2876 }
2877
2878 __ movl(eax, edx);
2879 __ shrl(edx, Immediate(31));
2880 __ addl(edx, eax);
2881
2882 if (instruction->IsRem()) {
2883 __ movl(eax, numerator);
2884 __ imull(edx, Immediate(imm));
2885 __ subl(eax, edx);
2886 __ movl(edx, eax);
2887 } else {
2888 __ movl(eax, edx);
2889 }
2890 __ Bind(&end);
2891 } else {
2892 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2893
2894 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2895
2896 CpuRegister rax = eax;
2897 CpuRegister rdx = edx;
2898
2899 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2900
2901 // Save the numerator.
2902 __ movq(numerator, rax);
2903
2904 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002905 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002906
2907 // RDX:RAX = magic * numerator
2908 __ imulq(numerator);
2909
2910 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002911 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002912 __ addq(rdx, numerator);
2913 } else if (imm < 0 && magic > 0) {
2914 // RDX -= numerator
2915 __ subq(rdx, numerator);
2916 }
2917
2918 // Shift if needed.
2919 if (shift != 0) {
2920 __ sarq(rdx, Immediate(shift));
2921 }
2922
2923 // RDX += 1 if RDX < 0
2924 __ movq(rax, rdx);
2925 __ shrq(rdx, Immediate(63));
2926 __ addq(rdx, rax);
2927
2928 if (instruction->IsRem()) {
2929 __ movq(rax, numerator);
2930
2931 if (IsInt<32>(imm)) {
2932 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2933 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002934 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002935 }
2936
2937 __ subq(rax, rdx);
2938 __ movq(rdx, rax);
2939 } else {
2940 __ movq(rax, rdx);
2941 }
2942 }
2943}
2944
Calin Juravlebacfec32014-11-14 15:54:36 +00002945void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2946 DCHECK(instruction->IsDiv() || instruction->IsRem());
2947 Primitive::Type type = instruction->GetResultType();
2948 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2949
2950 bool is_div = instruction->IsDiv();
2951 LocationSummary* locations = instruction->GetLocations();
2952
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002953 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2954 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002955
Roland Levillain271ab9c2014-11-27 15:23:57 +00002956 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002957 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002958
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002959 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002960 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002961
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002962 if (imm == 0) {
2963 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2964 } else if (imm == 1 || imm == -1) {
2965 DivRemOneOrMinusOne(instruction);
2966 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002967 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002968 } else {
2969 DCHECK(imm <= -2 || imm >= 2);
2970 GenerateDivRemWithAnyConstant(instruction);
2971 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002972 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002973 SlowPathCodeX86_64* slow_path =
2974 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2975 out.AsRegister(), type, is_div);
2976 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002977
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002978 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2979 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2980 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2981 // so it's safe to just use negl instead of more complex comparisons.
2982 if (type == Primitive::kPrimInt) {
2983 __ cmpl(second_reg, Immediate(-1));
2984 __ j(kEqual, slow_path->GetEntryLabel());
2985 // edx:eax <- sign-extended of eax
2986 __ cdq();
2987 // eax = quotient, edx = remainder
2988 __ idivl(second_reg);
2989 } else {
2990 __ cmpq(second_reg, Immediate(-1));
2991 __ j(kEqual, slow_path->GetEntryLabel());
2992 // rdx:rax <- sign-extended of rax
2993 __ cqo();
2994 // rax = quotient, rdx = remainder
2995 __ idivq(second_reg);
2996 }
2997 __ Bind(slow_path->GetExitLabel());
2998 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002999}
3000
Calin Juravle7c4954d2014-10-28 16:57:40 +00003001void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3002 LocationSummary* locations =
3003 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3004 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003005 case Primitive::kPrimInt:
3006 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003007 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003008 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003009 locations->SetOut(Location::SameAsFirstInput());
3010 // Intel uses edx:eax as the dividend.
3011 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003012 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3013 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3014 // output and request another temp.
3015 if (div->InputAt(1)->IsConstant()) {
3016 locations->AddTemp(Location::RequiresRegister());
3017 }
Calin Juravled0d48522014-11-04 16:40:20 +00003018 break;
3019 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003020
Calin Juravle7c4954d2014-10-28 16:57:40 +00003021 case Primitive::kPrimFloat:
3022 case Primitive::kPrimDouble: {
3023 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003024 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003025 locations->SetOut(Location::SameAsFirstInput());
3026 break;
3027 }
3028
3029 default:
3030 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3031 }
3032}
3033
3034void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3035 LocationSummary* locations = div->GetLocations();
3036 Location first = locations->InAt(0);
3037 Location second = locations->InAt(1);
3038 DCHECK(first.Equals(locations->Out()));
3039
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003040 Primitive::Type type = div->GetResultType();
3041 switch (type) {
3042 case Primitive::kPrimInt:
3043 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003044 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003045 break;
3046 }
3047
Calin Juravle7c4954d2014-10-28 16:57:40 +00003048 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003049 if (second.IsFpuRegister()) {
3050 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3051 } else if (second.IsConstant()) {
3052 __ divss(first.AsFpuRegister<XmmRegister>(),
3053 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3054 } else {
3055 DCHECK(second.IsStackSlot());
3056 __ divss(first.AsFpuRegister<XmmRegister>(),
3057 Address(CpuRegister(RSP), second.GetStackIndex()));
3058 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003059 break;
3060 }
3061
3062 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003063 if (second.IsFpuRegister()) {
3064 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3065 } else if (second.IsConstant()) {
3066 __ divsd(first.AsFpuRegister<XmmRegister>(),
3067 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3068 } else {
3069 DCHECK(second.IsDoubleStackSlot());
3070 __ divsd(first.AsFpuRegister<XmmRegister>(),
3071 Address(CpuRegister(RSP), second.GetStackIndex()));
3072 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003073 break;
3074 }
3075
3076 default:
3077 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3078 }
3079}
3080
Calin Juravlebacfec32014-11-14 15:54:36 +00003081void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003082 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003083 LocationSummary* locations =
3084 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003085
3086 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003087 case Primitive::kPrimInt:
3088 case Primitive::kPrimLong: {
3089 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003090 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003091 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3092 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003093 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3094 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3095 // output and request another temp.
3096 if (rem->InputAt(1)->IsConstant()) {
3097 locations->AddTemp(Location::RequiresRegister());
3098 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003099 break;
3100 }
3101
3102 case Primitive::kPrimFloat:
3103 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003104 locations->SetInAt(0, Location::Any());
3105 locations->SetInAt(1, Location::Any());
3106 locations->SetOut(Location::RequiresFpuRegister());
3107 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003108 break;
3109 }
3110
3111 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003112 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003113 }
3114}
3115
3116void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3117 Primitive::Type type = rem->GetResultType();
3118 switch (type) {
3119 case Primitive::kPrimInt:
3120 case Primitive::kPrimLong: {
3121 GenerateDivRemIntegral(rem);
3122 break;
3123 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003124 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003125 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003126 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003127 break;
3128 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003129 default:
3130 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3131 }
3132}
3133
Calin Juravled0d48522014-11-04 16:40:20 +00003134void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3135 LocationSummary* locations =
3136 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3137 locations->SetInAt(0, Location::Any());
3138 if (instruction->HasUses()) {
3139 locations->SetOut(Location::SameAsFirstInput());
3140 }
3141}
3142
3143void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3144 SlowPathCodeX86_64* slow_path =
3145 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3146 codegen_->AddSlowPath(slow_path);
3147
3148 LocationSummary* locations = instruction->GetLocations();
3149 Location value = locations->InAt(0);
3150
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003151 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003152 case Primitive::kPrimByte:
3153 case Primitive::kPrimChar:
3154 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003155 case Primitive::kPrimInt: {
3156 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003158 __ j(kEqual, slow_path->GetEntryLabel());
3159 } else if (value.IsStackSlot()) {
3160 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3161 __ j(kEqual, slow_path->GetEntryLabel());
3162 } else {
3163 DCHECK(value.IsConstant()) << value;
3164 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3165 __ jmp(slow_path->GetEntryLabel());
3166 }
3167 }
3168 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003169 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003170 case Primitive::kPrimLong: {
3171 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003172 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003173 __ j(kEqual, slow_path->GetEntryLabel());
3174 } else if (value.IsDoubleStackSlot()) {
3175 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3176 __ j(kEqual, slow_path->GetEntryLabel());
3177 } else {
3178 DCHECK(value.IsConstant()) << value;
3179 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3180 __ jmp(slow_path->GetEntryLabel());
3181 }
3182 }
3183 break;
3184 }
3185 default:
3186 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003187 }
Calin Juravled0d48522014-11-04 16:40:20 +00003188}
3189
Calin Juravle9aec02f2014-11-18 23:06:35 +00003190void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3191 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3192
3193 LocationSummary* locations =
3194 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3195
3196 switch (op->GetResultType()) {
3197 case Primitive::kPrimInt:
3198 case Primitive::kPrimLong: {
3199 locations->SetInAt(0, Location::RequiresRegister());
3200 // The shift count needs to be in CL.
3201 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3202 locations->SetOut(Location::SameAsFirstInput());
3203 break;
3204 }
3205 default:
3206 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3207 }
3208}
3209
3210void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3211 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3212
3213 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003215 Location second = locations->InAt(1);
3216
3217 switch (op->GetResultType()) {
3218 case Primitive::kPrimInt: {
3219 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003220 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003221 if (op->IsShl()) {
3222 __ shll(first_reg, second_reg);
3223 } else if (op->IsShr()) {
3224 __ sarl(first_reg, second_reg);
3225 } else {
3226 __ shrl(first_reg, second_reg);
3227 }
3228 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003229 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003230 if (op->IsShl()) {
3231 __ shll(first_reg, imm);
3232 } else if (op->IsShr()) {
3233 __ sarl(first_reg, imm);
3234 } else {
3235 __ shrl(first_reg, imm);
3236 }
3237 }
3238 break;
3239 }
3240 case Primitive::kPrimLong: {
3241 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003243 if (op->IsShl()) {
3244 __ shlq(first_reg, second_reg);
3245 } else if (op->IsShr()) {
3246 __ sarq(first_reg, second_reg);
3247 } else {
3248 __ shrq(first_reg, second_reg);
3249 }
3250 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003251 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003252 if (op->IsShl()) {
3253 __ shlq(first_reg, imm);
3254 } else if (op->IsShr()) {
3255 __ sarq(first_reg, imm);
3256 } else {
3257 __ shrq(first_reg, imm);
3258 }
3259 }
3260 break;
3261 }
3262 default:
3263 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3264 }
3265}
3266
3267void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3268 HandleShift(shl);
3269}
3270
3271void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3272 HandleShift(shl);
3273}
3274
3275void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3276 HandleShift(shr);
3277}
3278
3279void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3280 HandleShift(shr);
3281}
3282
3283void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3284 HandleShift(ushr);
3285}
3286
3287void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3288 HandleShift(ushr);
3289}
3290
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003291void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003292 LocationSummary* locations =
3293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003294 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003295 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003296 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003297 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003298}
3299
3300void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3301 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003302 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3303 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003304 // Note: if heap poisoning is enabled, the entry point takes cares
3305 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003306
3307 codegen_->InvokeRuntime(
3308 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3309 instruction,
3310 instruction->GetDexPc(),
3311 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003312
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003313 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003314}
3315
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003316void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3317 LocationSummary* locations =
3318 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3319 InvokeRuntimeCallingConvention calling_convention;
3320 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003321 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003322 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003323 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003324}
3325
3326void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3327 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003328 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3329 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003330
Roland Levillain4d027112015-07-01 15:41:14 +01003331 // Note: if heap poisoning is enabled, the entry point takes cares
3332 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003333 codegen_->InvokeRuntime(
3334 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3335 instruction,
3336 instruction->GetDexPc(),
3337 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003338
3339 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003340}
3341
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003342void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003343 LocationSummary* locations =
3344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003345 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3346 if (location.IsStackSlot()) {
3347 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3348 } else if (location.IsDoubleStackSlot()) {
3349 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3350 }
3351 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003352}
3353
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003354void InstructionCodeGeneratorX86_64::VisitParameterValue(
3355 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003356 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003357}
3358
3359void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3360 LocationSummary* locations =
3361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3362 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3363}
3364
3365void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3366 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3367 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003368}
3369
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003370void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003371 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003372 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003373 locations->SetInAt(0, Location::RequiresRegister());
3374 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003375}
3376
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003377void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3378 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3380 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003381 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003382 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003383 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003384 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003385 break;
3386
3387 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003388 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003389 break;
3390
3391 default:
3392 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3393 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003394}
3395
David Brazdil66d126e2015-04-03 16:02:44 +01003396void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3397 LocationSummary* locations =
3398 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3399 locations->SetInAt(0, Location::RequiresRegister());
3400 locations->SetOut(Location::SameAsFirstInput());
3401}
3402
3403void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003404 LocationSummary* locations = bool_not->GetLocations();
3405 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3406 locations->Out().AsRegister<CpuRegister>().AsRegister());
3407 Location out = locations->Out();
3408 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3409}
3410
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003411void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003412 LocationSummary* locations =
3413 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003414 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3415 locations->SetInAt(i, Location::Any());
3416 }
3417 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003418}
3419
3420void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003421 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003422 LOG(FATAL) << "Unimplemented";
3423}
3424
Calin Juravle52c48962014-12-16 17:02:57 +00003425void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3426 /*
3427 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3428 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3429 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3430 */
3431 switch (kind) {
3432 case MemBarrierKind::kAnyAny: {
3433 __ mfence();
3434 break;
3435 }
3436 case MemBarrierKind::kAnyStore:
3437 case MemBarrierKind::kLoadAny:
3438 case MemBarrierKind::kStoreStore: {
3439 // nop
3440 break;
3441 }
3442 default:
3443 LOG(FATAL) << "Unexpected memory barier " << kind;
3444 }
3445}
3446
3447void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3448 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3449
Nicolas Geoffray39468442014-09-02 15:17:15 +01003450 LocationSummary* locations =
3451 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003452 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003453 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3454 locations->SetOut(Location::RequiresFpuRegister());
3455 } else {
3456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3457 }
Calin Juravle52c48962014-12-16 17:02:57 +00003458}
3459
3460void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3461 const FieldInfo& field_info) {
3462 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3463
3464 LocationSummary* locations = instruction->GetLocations();
3465 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3466 Location out = locations->Out();
3467 bool is_volatile = field_info.IsVolatile();
3468 Primitive::Type field_type = field_info.GetFieldType();
3469 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3470
3471 switch (field_type) {
3472 case Primitive::kPrimBoolean: {
3473 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3474 break;
3475 }
3476
3477 case Primitive::kPrimByte: {
3478 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3479 break;
3480 }
3481
3482 case Primitive::kPrimShort: {
3483 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3484 break;
3485 }
3486
3487 case Primitive::kPrimChar: {
3488 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3489 break;
3490 }
3491
3492 case Primitive::kPrimInt:
3493 case Primitive::kPrimNot: {
3494 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3495 break;
3496 }
3497
3498 case Primitive::kPrimLong: {
3499 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3500 break;
3501 }
3502
3503 case Primitive::kPrimFloat: {
3504 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3505 break;
3506 }
3507
3508 case Primitive::kPrimDouble: {
3509 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3510 break;
3511 }
3512
3513 case Primitive::kPrimVoid:
3514 LOG(FATAL) << "Unreachable type " << field_type;
3515 UNREACHABLE();
3516 }
3517
Calin Juravle77520bc2015-01-12 18:45:46 +00003518 codegen_->MaybeRecordImplicitNullCheck(instruction);
3519
Calin Juravle52c48962014-12-16 17:02:57 +00003520 if (is_volatile) {
3521 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3522 }
Roland Levillain4d027112015-07-01 15:41:14 +01003523
3524 if (field_type == Primitive::kPrimNot) {
3525 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3526 }
Calin Juravle52c48962014-12-16 17:02:57 +00003527}
3528
3529void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3530 const FieldInfo& field_info) {
3531 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3532
3533 LocationSummary* locations =
3534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003535 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003536 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003537 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003538
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003539 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003540 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3541 locations->SetInAt(1, Location::RequiresFpuRegister());
3542 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003543 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003544 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003545 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003546 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003547 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003548 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003549 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3550 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003551 locations->AddTemp(Location::RequiresRegister());
3552 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003553}
3554
Calin Juravle52c48962014-12-16 17:02:57 +00003555void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003556 const FieldInfo& field_info,
3557 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003558 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3559
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003560 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003561 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3562 Location value = locations->InAt(1);
3563 bool is_volatile = field_info.IsVolatile();
3564 Primitive::Type field_type = field_info.GetFieldType();
3565 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3566
3567 if (is_volatile) {
3568 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3569 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003570
3571 switch (field_type) {
3572 case Primitive::kPrimBoolean:
3573 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003574 if (value.IsConstant()) {
3575 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3576 __ movb(Address(base, offset), Immediate(v));
3577 } else {
3578 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3579 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003580 break;
3581 }
3582
3583 case Primitive::kPrimShort:
3584 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003585 if (value.IsConstant()) {
3586 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3587 __ movw(Address(base, offset), Immediate(v));
3588 } else {
3589 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3590 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003591 break;
3592 }
3593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003594 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003595 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003596 if (value.IsConstant()) {
3597 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003598 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3599 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3600 // Note: if heap poisoning is enabled, no need to poison
3601 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003602 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003603 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003604 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3605 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3606 __ movl(temp, value.AsRegister<CpuRegister>());
3607 __ PoisonHeapReference(temp);
3608 __ movl(Address(base, offset), temp);
3609 } else {
3610 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3611 }
Mark Mendell40741f32015-04-20 22:10:34 -04003612 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003613 break;
3614 }
3615
3616 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003617 if (value.IsConstant()) {
3618 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3619 DCHECK(IsInt<32>(v));
3620 int32_t v_32 = v;
3621 __ movq(Address(base, offset), Immediate(v_32));
3622 } else {
3623 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3624 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003625 break;
3626 }
3627
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003628 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003629 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003630 break;
3631 }
3632
3633 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003634 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003635 break;
3636 }
3637
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003638 case Primitive::kPrimVoid:
3639 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003640 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003641 }
Calin Juravle52c48962014-12-16 17:02:57 +00003642
Calin Juravle77520bc2015-01-12 18:45:46 +00003643 codegen_->MaybeRecordImplicitNullCheck(instruction);
3644
3645 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3646 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3647 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003648 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003649 }
3650
Calin Juravle52c48962014-12-16 17:02:57 +00003651 if (is_volatile) {
3652 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3653 }
3654}
3655
3656void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3657 HandleFieldSet(instruction, instruction->GetFieldInfo());
3658}
3659
3660void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003661 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662}
3663
3664void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003665 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003666}
3667
3668void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003669 HandleFieldGet(instruction, instruction->GetFieldInfo());
3670}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003671
Calin Juravle52c48962014-12-16 17:02:57 +00003672void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3673 HandleFieldGet(instruction);
3674}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003675
Calin Juravle52c48962014-12-16 17:02:57 +00003676void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3677 HandleFieldGet(instruction, instruction->GetFieldInfo());
3678}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003679
Calin Juravle52c48962014-12-16 17:02:57 +00003680void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3681 HandleFieldSet(instruction, instruction->GetFieldInfo());
3682}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003683
Calin Juravle52c48962014-12-16 17:02:57 +00003684void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003685 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003686}
3687
3688void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003689 LocationSummary* locations =
3690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003691 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3692 ? Location::RequiresRegister()
3693 : Location::Any();
3694 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003695 if (instruction->HasUses()) {
3696 locations->SetOut(Location::SameAsFirstInput());
3697 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003698}
3699
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003700void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003701 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3702 return;
3703 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003704 LocationSummary* locations = instruction->GetLocations();
3705 Location obj = locations->InAt(0);
3706
3707 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3708 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3709}
3710
3711void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003712 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003713 codegen_->AddSlowPath(slow_path);
3714
3715 LocationSummary* locations = instruction->GetLocations();
3716 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003717
3718 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003719 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003720 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003721 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003722 } else {
3723 DCHECK(obj.IsConstant()) << obj;
3724 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3725 __ jmp(slow_path->GetEntryLabel());
3726 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003727 }
3728 __ j(kEqual, slow_path->GetEntryLabel());
3729}
3730
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003731void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3732 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3733 GenerateImplicitNullCheck(instruction);
3734 } else {
3735 GenerateExplicitNullCheck(instruction);
3736 }
3737}
3738
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003739void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003740 LocationSummary* locations =
3741 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003742 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003743 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003744 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3745 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3746 } else {
3747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3748 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003749}
3750
3751void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3752 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003754 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003755 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756
Roland Levillain4d027112015-07-01 15:41:14 +01003757 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758 case Primitive::kPrimBoolean: {
3759 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003761 if (index.IsConstant()) {
3762 __ movzxb(out, Address(obj,
3763 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3764 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003765 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003766 }
3767 break;
3768 }
3769
3770 case Primitive::kPrimByte: {
3771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003773 if (index.IsConstant()) {
3774 __ movsxb(out, Address(obj,
3775 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3776 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003777 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 }
3779 break;
3780 }
3781
3782 case Primitive::kPrimShort: {
3783 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003784 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003785 if (index.IsConstant()) {
3786 __ movsxw(out, Address(obj,
3787 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3788 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003789 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003790 }
3791 break;
3792 }
3793
3794 case Primitive::kPrimChar: {
3795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003796 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003797 if (index.IsConstant()) {
3798 __ movzxw(out, Address(obj,
3799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003801 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802 }
3803 break;
3804 }
3805
3806 case Primitive::kPrimInt:
3807 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003808 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3809 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003810 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812 if (index.IsConstant()) {
3813 __ movl(out, Address(obj,
3814 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3815 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003816 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003817 }
3818 break;
3819 }
3820
3821 case Primitive::kPrimLong: {
3822 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003823 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824 if (index.IsConstant()) {
3825 __ movq(out, Address(obj,
3826 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3827 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003829 }
3830 break;
3831 }
3832
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003833 case Primitive::kPrimFloat: {
3834 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003836 if (index.IsConstant()) {
3837 __ movss(out, Address(obj,
3838 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3839 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003840 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003841 }
3842 break;
3843 }
3844
3845 case Primitive::kPrimDouble: {
3846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003847 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003848 if (index.IsConstant()) {
3849 __ movsd(out, Address(obj,
3850 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3851 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003853 }
3854 break;
3855 }
3856
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003857 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003858 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003859 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003860 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003861 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003862
3863 if (type == Primitive::kPrimNot) {
3864 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3865 __ MaybeUnpoisonHeapReference(out);
3866 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003867}
3868
3869void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003870 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003871
3872 bool needs_write_barrier =
3873 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3874 bool needs_runtime_call = instruction->NeedsTypeCheck();
3875
Nicolas Geoffray39468442014-09-02 15:17:15 +01003876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003877 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3878 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003880 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3881 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3882 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003883 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003884 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003885 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003886 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3887 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003888 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003889 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003890 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3891 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003892 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003893 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003894 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003895
3896 if (needs_write_barrier) {
3897 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003898 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003899 locations->AddTemp(Location::RequiresRegister());
3900 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003901 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003902}
3903
3904void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3905 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003907 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003908 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003909 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003910 bool needs_runtime_call = locations->WillCall();
3911 bool needs_write_barrier =
3912 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913
3914 switch (value_type) {
3915 case Primitive::kPrimBoolean:
3916 case Primitive::kPrimByte: {
3917 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 if (index.IsConstant()) {
3919 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003920 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003921 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003922 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003923 __ movb(Address(obj, offset),
3924 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003925 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003926 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003927 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003928 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3929 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003930 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003932 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003934 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003935 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003936 break;
3937 }
3938
3939 case Primitive::kPrimShort:
3940 case Primitive::kPrimChar: {
3941 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942 if (index.IsConstant()) {
3943 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003944 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003945 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003946 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003947 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003948 __ movw(Address(obj, offset),
3949 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003950 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003951 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003952 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003953 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003954 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3955 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003956 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003957 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003958 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003959 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3960 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003962 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003963 break;
3964 }
3965
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003966 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003967 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003968 if (!needs_runtime_call) {
3969 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3970 if (index.IsConstant()) {
3971 size_t offset =
3972 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3973 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003974 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3975 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3976 __ movl(temp, value.AsRegister<CpuRegister>());
3977 __ PoisonHeapReference(temp);
3978 __ movl(Address(obj, offset), temp);
3979 } else {
3980 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
3981 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003982 } else {
3983 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003984 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003985 // `value_type == Primitive::kPrimNot` implies `v == 0`.
3986 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
3987 // Note: if heap poisoning is enabled, no need to poison
3988 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04003989 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003990 }
3991 } else {
3992 DCHECK(index.IsRegister()) << index;
3993 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003994 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3995 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3996 __ movl(temp, value.AsRegister<CpuRegister>());
3997 __ PoisonHeapReference(temp);
3998 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
3999 } else {
4000 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4001 value.AsRegister<CpuRegister>());
4002 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004003 } else {
4004 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004005 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004006 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4007 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4008 // Note: if heap poisoning is enabled, no need to poison
4009 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004010 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004011 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004012 }
4013 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004014 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004015 if (needs_write_barrier) {
4016 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004017 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4018 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004019 codegen_->MarkGCCard(
4020 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004021 }
4022 } else {
4023 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004024 // Note: if heap poisoning is enabled, pAputObject takes cares
4025 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004026 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4027 instruction,
4028 instruction->GetDexPc(),
4029 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004030 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004031 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004032 break;
4033 }
4034
4035 case Primitive::kPrimLong: {
4036 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004037 if (index.IsConstant()) {
4038 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004039 if (value.IsRegister()) {
4040 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4041 } else {
4042 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4043 DCHECK(IsInt<32>(v));
4044 int32_t v_32 = v;
4045 __ movq(Address(obj, offset), Immediate(v_32));
4046 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004047 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004048 if (value.IsRegister()) {
4049 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4050 value.AsRegister<CpuRegister>());
4051 } else {
4052 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4053 DCHECK(IsInt<32>(v));
4054 int32_t v_32 = v;
4055 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4056 Immediate(v_32));
4057 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004058 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004059 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004060 break;
4061 }
4062
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004063 case Primitive::kPrimFloat: {
4064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4065 if (index.IsConstant()) {
4066 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4067 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004068 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004069 } else {
4070 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4072 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004073 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004074 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004075 break;
4076 }
4077
4078 case Primitive::kPrimDouble: {
4079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4080 if (index.IsConstant()) {
4081 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4082 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004083 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004084 } else {
4085 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004086 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4087 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004088 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004089 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004090 break;
4091 }
4092
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004093 case Primitive::kPrimVoid:
4094 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004095 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004096 }
4097}
4098
4099void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004100 LocationSummary* locations =
4101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004102 locations->SetInAt(0, Location::RequiresRegister());
4103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104}
4105
4106void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4107 LocationSummary* locations = instruction->GetLocations();
4108 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4110 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004111 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004112 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004113}
4114
4115void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004116 LocationSummary* locations =
4117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004118 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004119 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004120 if (instruction->HasUses()) {
4121 locations->SetOut(Location::SameAsFirstInput());
4122 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004123}
4124
4125void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4126 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004127 Location index_loc = locations->InAt(0);
4128 Location length_loc = locations->InAt(1);
4129 SlowPathCodeX86_64* slow_path =
4130 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004131
Mark Mendell99dbd682015-04-22 16:18:52 -04004132 if (length_loc.IsConstant()) {
4133 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4134 if (index_loc.IsConstant()) {
4135 // BCE will remove the bounds check if we are guarenteed to pass.
4136 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4137 if (index < 0 || index >= length) {
4138 codegen_->AddSlowPath(slow_path);
4139 __ jmp(slow_path->GetEntryLabel());
4140 } else {
4141 // Some optimization after BCE may have generated this, and we should not
4142 // generate a bounds check if it is a valid range.
4143 }
4144 return;
4145 }
4146
4147 // We have to reverse the jump condition because the length is the constant.
4148 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4149 __ cmpl(index_reg, Immediate(length));
4150 codegen_->AddSlowPath(slow_path);
4151 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004152 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004153 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4154 if (index_loc.IsConstant()) {
4155 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4156 __ cmpl(length, Immediate(value));
4157 } else {
4158 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4159 }
4160 codegen_->AddSlowPath(slow_path);
4161 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004162 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004163}
4164
4165void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4166 CpuRegister card,
4167 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004168 CpuRegister value,
4169 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004171 if (value_can_be_null) {
4172 __ testl(value, value);
4173 __ j(kEqual, &is_null);
4174 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004175 __ gs()->movq(card, Address::Absolute(
4176 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4177 __ movq(temp, object);
4178 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004179 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004180 if (value_can_be_null) {
4181 __ Bind(&is_null);
4182 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004183}
4184
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4186 temp->SetLocations(nullptr);
4187}
4188
4189void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4190 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004191 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004192}
4193
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004194void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004195 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004196 LOG(FATAL) << "Unimplemented";
4197}
4198
4199void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004200 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4201}
4202
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004203void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4205}
4206
4207void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004208 HBasicBlock* block = instruction->GetBlock();
4209 if (block->GetLoopInformation() != nullptr) {
4210 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4211 // The back edge will generate the suspend check.
4212 return;
4213 }
4214 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4215 // The goto will generate the suspend check.
4216 return;
4217 }
4218 GenerateSuspendCheck(instruction, nullptr);
4219}
4220
4221void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4222 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004223 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004224 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4225 if (slow_path == nullptr) {
4226 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4227 instruction->SetSlowPath(slow_path);
4228 codegen_->AddSlowPath(slow_path);
4229 if (successor != nullptr) {
4230 DCHECK(successor->IsLoopHeader());
4231 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4232 }
4233 } else {
4234 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4235 }
4236
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004237 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004238 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004239 if (successor == nullptr) {
4240 __ j(kNotEqual, slow_path->GetEntryLabel());
4241 __ Bind(slow_path->GetReturnLabel());
4242 } else {
4243 __ j(kEqual, codegen_->GetLabelOf(successor));
4244 __ jmp(slow_path->GetEntryLabel());
4245 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004246}
4247
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004248X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4249 return codegen_->GetAssembler();
4250}
4251
4252void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4253 MoveOperands* move = moves_.Get(index);
4254 Location source = move->GetSource();
4255 Location destination = move->GetDestination();
4256
4257 if (source.IsRegister()) {
4258 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004259 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004260 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004261 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004262 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004263 } else {
4264 DCHECK(destination.IsDoubleStackSlot());
4265 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004266 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004267 }
4268 } else if (source.IsStackSlot()) {
4269 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004270 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004271 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004272 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004273 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004274 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004275 } else {
4276 DCHECK(destination.IsStackSlot());
4277 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4278 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4279 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004280 } else if (source.IsDoubleStackSlot()) {
4281 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004282 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004283 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004284 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004285 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4286 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004287 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004288 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004289 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4290 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4291 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004292 } else if (source.IsConstant()) {
4293 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004294 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4295 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004296 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004297 if (value == 0) {
4298 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4299 } else {
4300 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4301 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004302 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004303 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004304 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004305 }
4306 } else if (constant->IsLongConstant()) {
4307 int64_t value = constant->AsLongConstant()->GetValue();
4308 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004309 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004310 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004311 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004312 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004313 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004314 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004315 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004316 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004317 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004318 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4319 if (value == 0) {
4320 // easy FP 0.0.
4321 __ xorps(dest, dest);
4322 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004323 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004324 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004325 } else {
4326 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004327 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004328 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4329 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004330 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004331 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004332 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004333 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004334 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004335 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4336 if (value == 0) {
4337 __ xorpd(dest, dest);
4338 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004339 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004340 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004341 } else {
4342 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004343 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004344 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004345 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004346 } else if (source.IsFpuRegister()) {
4347 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004348 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004349 } else if (destination.IsStackSlot()) {
4350 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004351 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004352 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004353 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004354 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004355 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004356 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004357 }
4358}
4359
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004360void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004361 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004362 __ movl(Address(CpuRegister(RSP), mem), reg);
4363 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004364}
4365
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004366void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004367 ScratchRegisterScope ensure_scratch(
4368 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4369
4370 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4371 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4372 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4373 Address(CpuRegister(RSP), mem2 + stack_offset));
4374 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4375 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4376 CpuRegister(ensure_scratch.GetRegister()));
4377}
4378
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004379void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4380 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4381 __ movq(Address(CpuRegister(RSP), mem), reg);
4382 __ movq(reg, CpuRegister(TMP));
4383}
4384
4385void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4386 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004387 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004388
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004389 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4390 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4391 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4392 Address(CpuRegister(RSP), mem2 + stack_offset));
4393 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4394 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4395 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004396}
4397
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004398void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4399 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4400 __ movss(Address(CpuRegister(RSP), mem), reg);
4401 __ movd(reg, CpuRegister(TMP));
4402}
4403
4404void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4405 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4406 __ movsd(Address(CpuRegister(RSP), mem), reg);
4407 __ movd(reg, CpuRegister(TMP));
4408}
4409
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004410void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4411 MoveOperands* move = moves_.Get(index);
4412 Location source = move->GetSource();
4413 Location destination = move->GetDestination();
4414
4415 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004416 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004417 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004418 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004419 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004420 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004421 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004422 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4423 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004424 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004425 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004426 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004427 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4428 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004429 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004430 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4431 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4432 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004433 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004434 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004435 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004436 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004437 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004438 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004439 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004440 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004441 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004442 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004443 }
4444}
4445
4446
4447void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4448 __ pushq(CpuRegister(reg));
4449}
4450
4451
4452void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4453 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004454}
4455
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004456void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4457 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4458 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4459 Immediate(mirror::Class::kStatusInitialized));
4460 __ j(kLess, slow_path->GetEntryLabel());
4461 __ Bind(slow_path->GetExitLabel());
4462 // No need for memory fence, thanks to the X86_64 memory model.
4463}
4464
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004465void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004466 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4467 ? LocationSummary::kCallOnSlowPath
4468 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004469 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004470 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004471 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004472 locations->SetOut(Location::RequiresRegister());
4473}
4474
4475void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004476 LocationSummary* locations = cls->GetLocations();
4477 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4478 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004479 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004480 DCHECK(!cls->CanCallRuntime());
4481 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004482 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004483 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004484 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004485 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004486 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004487 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004488 __ MaybeUnpoisonHeapReference(out);
4489
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004490 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4491 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4492 codegen_->AddSlowPath(slow_path);
4493 __ testl(out, out);
4494 __ j(kEqual, slow_path->GetEntryLabel());
4495 if (cls->MustGenerateClinitCheck()) {
4496 GenerateClassInitializationCheck(slow_path, out);
4497 } else {
4498 __ Bind(slow_path->GetExitLabel());
4499 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004500 }
4501}
4502
4503void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4504 LocationSummary* locations =
4505 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4506 locations->SetInAt(0, Location::RequiresRegister());
4507 if (check->HasUses()) {
4508 locations->SetOut(Location::SameAsFirstInput());
4509 }
4510}
4511
4512void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004513 // We assume the class to not be null.
4514 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4515 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004516 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004517 GenerateClassInitializationCheck(slow_path,
4518 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004519}
4520
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004521void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4522 LocationSummary* locations =
4523 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004524 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004525 locations->SetOut(Location::RequiresRegister());
4526}
4527
4528void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4529 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4530 codegen_->AddSlowPath(slow_path);
4531
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004532 LocationSummary* locations = load->GetLocations();
4533 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4534 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004535 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004536 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004537 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004538 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004539 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004540 __ testl(out, out);
4541 __ j(kEqual, slow_path->GetEntryLabel());
4542 __ Bind(slow_path->GetExitLabel());
4543}
4544
David Brazdilcb1c0552015-08-04 16:22:25 +01004545static Address GetExceptionTlsAddress() {
4546 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4547}
4548
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004549void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4550 LocationSummary* locations =
4551 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4552 locations->SetOut(Location::RequiresRegister());
4553}
4554
4555void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004556 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4557}
4558
4559void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4560 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4561}
4562
4563void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4564 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004565}
4566
4567void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4568 LocationSummary* locations =
4569 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4570 InvokeRuntimeCallingConvention calling_convention;
4571 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4572}
4573
4574void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004575 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4576 instruction,
4577 instruction->GetDexPc(),
4578 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004579}
4580
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004581void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004582 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4583 ? LocationSummary::kNoCall
4584 : LocationSummary::kCallOnSlowPath;
4585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4586 locations->SetInAt(0, Location::RequiresRegister());
4587 locations->SetInAt(1, Location::Any());
4588 locations->SetOut(Location::RequiresRegister());
4589}
4590
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004591void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004592 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004593 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004594 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004595 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004596 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4597 Label done, zero;
4598 SlowPathCodeX86_64* slow_path = nullptr;
4599
4600 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004601 // Avoid null check if we know obj is not null.
4602 if (instruction->MustDoNullCheck()) {
4603 __ testl(obj, obj);
4604 __ j(kEqual, &zero);
4605 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004606 // Compare the class of `obj` with `cls`.
4607 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004608 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004609 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004610 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004611 } else {
4612 DCHECK(cls.IsStackSlot()) << cls;
4613 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4614 }
4615 if (instruction->IsClassFinal()) {
4616 // Classes must be equal for the instanceof to succeed.
4617 __ j(kNotEqual, &zero);
4618 __ movl(out, Immediate(1));
4619 __ jmp(&done);
4620 } else {
4621 // If the classes are not equal, we go into a slow path.
4622 DCHECK(locations->OnlyCallsOnSlowPath());
4623 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004624 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004625 codegen_->AddSlowPath(slow_path);
4626 __ j(kNotEqual, slow_path->GetEntryLabel());
4627 __ movl(out, Immediate(1));
4628 __ jmp(&done);
4629 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004630
4631 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4632 __ Bind(&zero);
4633 __ movl(out, Immediate(0));
4634 }
4635
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004636 if (slow_path != nullptr) {
4637 __ Bind(slow_path->GetExitLabel());
4638 }
4639 __ Bind(&done);
4640}
4641
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004642void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4644 instruction, LocationSummary::kCallOnSlowPath);
4645 locations->SetInAt(0, Location::RequiresRegister());
4646 locations->SetInAt(1, Location::Any());
4647 locations->AddTemp(Location::RequiresRegister());
4648}
4649
4650void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4651 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004652 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004653 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004654 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004655 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4656 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4657 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4658 codegen_->AddSlowPath(slow_path);
4659
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004660 // Avoid null check if we know obj is not null.
4661 if (instruction->MustDoNullCheck()) {
4662 __ testl(obj, obj);
4663 __ j(kEqual, slow_path->GetExitLabel());
4664 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004665 // Compare the class of `obj` with `cls`.
4666 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004667 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004668 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004669 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004670 } else {
4671 DCHECK(cls.IsStackSlot()) << cls;
4672 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4673 }
Roland Levillain4d027112015-07-01 15:41:14 +01004674 // The checkcast succeeds if the classes are equal (fast path).
4675 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004676 __ j(kNotEqual, slow_path->GetEntryLabel());
4677 __ Bind(slow_path->GetExitLabel());
4678}
4679
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004680void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4681 LocationSummary* locations =
4682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4683 InvokeRuntimeCallingConvention calling_convention;
4684 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4685}
4686
4687void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004688 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4689 : QUICK_ENTRY_POINT(pUnlockObject),
4690 instruction,
4691 instruction->GetDexPc(),
4692 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004693}
4694
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004695void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4696void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4697void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4698
4699void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4700 LocationSummary* locations =
4701 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4702 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4703 || instruction->GetResultType() == Primitive::kPrimLong);
4704 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004705 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004706 locations->SetOut(Location::SameAsFirstInput());
4707}
4708
4709void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4710 HandleBitwiseOperation(instruction);
4711}
4712
4713void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4714 HandleBitwiseOperation(instruction);
4715}
4716
4717void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4718 HandleBitwiseOperation(instruction);
4719}
4720
4721void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4722 LocationSummary* locations = instruction->GetLocations();
4723 Location first = locations->InAt(0);
4724 Location second = locations->InAt(1);
4725 DCHECK(first.Equals(locations->Out()));
4726
4727 if (instruction->GetResultType() == Primitive::kPrimInt) {
4728 if (second.IsRegister()) {
4729 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004731 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004732 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004733 } else {
4734 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004735 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004736 }
4737 } else if (second.IsConstant()) {
4738 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4739 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004740 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004741 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004742 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004743 } else {
4744 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004745 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004746 }
4747 } else {
4748 Address address(CpuRegister(RSP), second.GetStackIndex());
4749 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004750 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004751 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004752 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004753 } else {
4754 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004755 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004756 }
4757 }
4758 } else {
4759 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004760 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4761 bool second_is_constant = false;
4762 int64_t value = 0;
4763 if (second.IsConstant()) {
4764 second_is_constant = true;
4765 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004766 }
Mark Mendell40741f32015-04-20 22:10:34 -04004767 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004768
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004769 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004770 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004771 if (is_int32_value) {
4772 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4773 } else {
4774 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4775 }
4776 } else if (second.IsDoubleStackSlot()) {
4777 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004778 } else {
4779 __ andq(first_reg, second.AsRegister<CpuRegister>());
4780 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004781 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004782 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004783 if (is_int32_value) {
4784 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4785 } else {
4786 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4787 }
4788 } else if (second.IsDoubleStackSlot()) {
4789 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004790 } else {
4791 __ orq(first_reg, second.AsRegister<CpuRegister>());
4792 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004793 } else {
4794 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004795 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004796 if (is_int32_value) {
4797 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4798 } else {
4799 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4800 }
4801 } else if (second.IsDoubleStackSlot()) {
4802 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004803 } else {
4804 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4805 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004806 }
4807 }
4808}
4809
Calin Juravleb1498f62015-02-16 13:13:29 +00004810void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4811 // Nothing to do, this should be removed during prepare for register allocator.
4812 UNUSED(instruction);
4813 LOG(FATAL) << "Unreachable";
4814}
4815
4816void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4817 // Nothing to do, this should be removed during prepare for register allocator.
4818 UNUSED(instruction);
4819 LOG(FATAL) << "Unreachable";
4820}
4821
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004822void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4823 DCHECK(codegen_->IsBaseline());
4824 LocationSummary* locations =
4825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4826 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4827}
4828
4829void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4830 DCHECK(codegen_->IsBaseline());
4831 // Will be generated at use site.
4832}
4833
Mark Mendell92e83bf2015-05-07 11:25:03 -04004834void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4835 if (value == 0) {
4836 __ xorl(dest, dest);
4837 } else if (value > 0 && IsInt<32>(value)) {
4838 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4839 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4840 } else {
4841 __ movq(dest, Immediate(value));
4842 }
4843}
4844
Mark Mendellcfa410b2015-05-25 16:02:44 -04004845void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4846 DCHECK(dest.IsDoubleStackSlot());
4847 if (IsInt<32>(value)) {
4848 // Can move directly as an int32 constant.
4849 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4850 Immediate(static_cast<int32_t>(value)));
4851 } else {
4852 Load64BitValue(CpuRegister(TMP), value);
4853 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4854 }
4855}
4856
Mark Mendellf55c3e02015-03-26 21:07:46 -04004857void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4858 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004859 X86_64Assembler* assembler = GetAssembler();
4860 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004861 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4862 // byte values. If used for vectors at a later time, this will need to be
4863 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004864 assembler->Align(4, 0);
4865 constant_area_start_ = assembler->CodeSize();
4866 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004867 }
4868
4869 // And finish up.
4870 CodeGenerator::Finalize(allocator);
4871}
4872
4873/**
4874 * Class to handle late fixup of offsets into constant area.
4875 */
4876class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4877 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004878 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004879 : codegen_(codegen), offset_into_constant_area_(offset) {}
4880
4881 private:
4882 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4883 // Patch the correct offset for the instruction. We use the address of the
4884 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4885 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4886 int relative_position = constant_offset - pos;
4887
4888 // Patch in the right value.
4889 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4890 }
4891
Mark Mendell39dcf552015-04-09 20:42:42 -04004892 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004893
4894 // Location in constant area that the fixup refers to.
4895 int offset_into_constant_area_;
4896};
4897
4898Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4899 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4900 return Address::RIP(fixup);
4901}
4902
4903Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4904 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4905 return Address::RIP(fixup);
4906}
4907
4908Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4909 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4910 return Address::RIP(fixup);
4911}
4912
4913Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4914 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4915 return Address::RIP(fixup);
4916}
4917
Roland Levillain4d027112015-07-01 15:41:14 +01004918#undef __
4919
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004920} // namespace x86_64
4921} // namespace art