blob: c1f601e6d4256944361710af13ba8677d2f649a2 [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
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "intrinsics.h"
22#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070023#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010025#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "mirror/object_reference.h"
27#include "thread.h"
28#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/x86_64/assembler_x86_64.h"
31#include "utils/x86_64/managed_register_x86_64.h"
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace art {
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace x86_64 {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr int kCurrentMethodStackOffset = 0;
41
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
43static constexpr size_t kRuntimeParameterCoreRegistersLength =
44 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000045static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
46static constexpr size_t kRuntimeParameterFpuRegistersLength =
47 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000048static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050
Mark Mendell24f2dfa2015-01-14 19:51:45 -050051static constexpr int kC2ConditionMask = 0x400;
52
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054 public:
55 InvokeRuntimeCallingConvention()
56 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057 kRuntimeParameterCoreRegistersLength,
58 kRuntimeParameterFpuRegisters,
59 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010060
61 private:
62 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
63};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
66
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010067class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
Alexandre Rames2ed20af2015-03-06 13:55:35 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010073 __ gs()->call(
74 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000075 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 }
77
78 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
81};
82
Calin Juravled0d48522014-11-04 16:40:20 +000083class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
84 public:
85 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
86
Alexandre Rames2ed20af2015-03-06 13:55:35 +000087 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ Bind(GetEntryLabel());
89 __ gs()->call(
90 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000091 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000092 }
93
94 private:
95 HDivZeroCheck* const instruction_;
96 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
97};
98
Calin Juravlebacfec32014-11-14 15:54:36 +000099class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000101 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
102 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000106 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000107 if (is_div_) {
108 __ negl(cpu_reg_);
109 } else {
110 __ movl(cpu_reg_, Immediate(0));
111 }
112
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000113 } else {
114 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 if (is_div_) {
116 __ negq(cpu_reg_);
117 } else {
118 __ movq(cpu_reg_, Immediate(0));
119 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000120 }
Calin Juravled0d48522014-11-04 16:40:20 +0000121 __ jmp(GetExitLabel());
122 }
123
124 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 const bool is_div_;
128 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000129};
130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
134 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000136 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000139 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000141 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
142 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100143 if (successor_ == nullptr) {
144 __ jmp(GetReturnLabel());
145 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 }
149
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150 Label* GetReturnLabel() {
151 DCHECK(successor_ == nullptr);
152 return &return_label_;
153 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154
155 private:
156 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 Label return_label_;
159
160 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
161};
162
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100165 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
166 Location index_location,
167 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100168 : instruction_(instruction),
169 index_location_(index_location),
170 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 codegen->EmitParallelMoves(
178 index_location_,
179 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
180 length_location_,
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 __ gs()->call(Address::Absolute(
183 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 }
186
187 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100188 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 const Location index_location_;
190 const Location length_location_;
191
192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
193};
194
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 LoadClassSlowPathX86_64(HLoadClass* cls,
198 HInstruction* at,
199 uint32_t dex_pc,
200 bool do_clinit)
201 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
202 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000205 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
208 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ gs()->call(Address::Absolute((do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000218 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000220 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
226
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 __ jmp(GetExitLabel());
229 }
230
231 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 // The class this slow path will load.
233 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100246};
247
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000248class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
249 public:
250 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
251
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000252 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 LocationSummary* locations = instruction_->GetLocations();
254 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
255
256 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
257 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000258 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000259
260 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800261 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
262 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263 Immediate(instruction_->GetStringIndex()));
264 __ gs()->call(Address::Absolute(
265 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000267 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000268 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000269 __ jmp(GetExitLabel());
270 }
271
272 private:
273 HLoadString* const instruction_;
274
275 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
276};
277
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
279 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 TypeCheckSlowPathX86_64(HInstruction* instruction,
281 Location class_to_check,
282 Location object_class,
283 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 class_to_check_(class_to_check),
286 object_class_(object_class),
287 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 DCHECK(instruction_->IsCheckCast()
292 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
294 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
295 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000296 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297
298 // We're moving two locations to locations that could overlap, so we need a parallel
299 // move resolver.
300 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000301 codegen->EmitParallelMoves(
302 class_to_check_,
303 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
304 object_class_,
305 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 if (instruction_->IsInstanceOf()) {
308 __ gs()->call(
309 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
310 } else {
311 DCHECK(instruction_->IsCheckCast());
312 __ gs()->call(
313 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
314 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000315 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316
317 if (instruction_->IsInstanceOf()) {
318 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
319 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000321 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 __ jmp(GetExitLabel());
323 }
324
325 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 HInstruction* const instruction_;
327 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
331 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
332};
333
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100334#undef __
335#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
336
Dave Allison20dfc792014-06-16 20:44:29 -0700337inline Condition X86_64Condition(IfCondition cond) {
338 switch (cond) {
339 case kCondEQ: return kEqual;
340 case kCondNE: return kNotEqual;
341 case kCondLT: return kLess;
342 case kCondLE: return kLessEqual;
343 case kCondGT: return kGreater;
344 case kCondGE: return kGreaterEqual;
345 default:
346 LOG(FATAL) << "Unknown if condition";
347 }
348 return kEqual;
349}
350
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800351void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
352 CpuRegister temp) {
353 // All registers are assumed to be correctly set up.
354
355 // TODO: Implement all kinds of calls:
356 // 1) boot -> boot
357 // 2) app -> boot
358 // 3) app -> app
359 //
360 // Currently we implement the app -> app logic, which looks up in the resolve cache.
361
362 // temp = method;
363 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000364 if (!invoke->IsRecursive()) {
365 // temp = temp->dex_cache_resolved_methods_;
366 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
367 // temp = temp[index_in_cache]
368 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
369 // (temp + offset_of_quick_compiled_code)()
370 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
371 kX86_64WordSize).SizeValue()));
372 } else {
373 __ call(&frame_entry_label_);
374 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800375
376 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800377}
378
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100379void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
380 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
381}
382
383void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
384 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
385}
386
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100387size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
388 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
389 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100390}
391
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100392size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
393 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
394 return kX86_64WordSize;
395}
396
397size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
398 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
399 return kX86_64WordSize;
400}
401
402size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
403 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
404 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100405}
406
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000407static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000408// Use a fake return address register to mimic Quick.
409static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000410CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000411 : CodeGenerator(graph,
412 kNumberOfCpuRegisters,
413 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000414 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000415 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
416 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000417 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000418 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
419 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000420 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100421 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100422 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000423 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000424 move_resolver_(graph->GetArena(), this) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000425 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
426}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100427
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100428InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
429 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430 : HGraphVisitor(graph),
431 assembler_(codegen->GetAssembler()),
432 codegen_(codegen) {}
433
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100434Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100435 switch (type) {
436 case Primitive::kPrimLong:
437 case Primitive::kPrimByte:
438 case Primitive::kPrimBoolean:
439 case Primitive::kPrimChar:
440 case Primitive::kPrimShort:
441 case Primitive::kPrimInt:
442 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100444 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 }
446
447 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100448 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100449 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100450 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100451 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100452
453 case Primitive::kPrimVoid:
454 LOG(FATAL) << "Unreachable type " << type;
455 }
456
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100457 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100458}
459
Nicolas Geoffray98893962015-01-21 12:32:32 +0000460void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100461 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100462 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100463
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000464 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100465 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000466
Nicolas Geoffray98893962015-01-21 12:32:32 +0000467 if (is_baseline) {
468 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
469 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
470 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000471 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
472 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
473 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000474 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100475}
476
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100477void CodeGeneratorX86_64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000478 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100479 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700480 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000481 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100482
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000483 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100484 __ testq(CpuRegister(RAX), Address(
485 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100486 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100487 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000488
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000489 if (HasEmptyFrame()) {
490 return;
491 }
492
Nicolas Geoffray98893962015-01-21 12:32:32 +0000493 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000494 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000495 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000496 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000497 }
498 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100499
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000500 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
501 uint32_t xmm_spill_location = GetFpuSpillStart();
502 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100503
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000504 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
505 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
506 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
507 XmmRegister(kFpuCalleeSaves[i]));
508 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100509 }
510
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
512}
513
514void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000515 if (HasEmptyFrame()) {
516 return;
517 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000518 uint32_t xmm_spill_location = GetFpuSpillStart();
519 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
520 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
521 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
522 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
523 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
524 }
525 }
526
527 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000528
529 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000530 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000531 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000532 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000533 }
534 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100535}
536
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100537void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
538 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100539}
540
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100541void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000542 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
544}
545
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100546Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
547 switch (load->GetType()) {
548 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100549 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100550 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
551 break;
552
553 case Primitive::kPrimInt:
554 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100555 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100557
558 case Primitive::kPrimBoolean:
559 case Primitive::kPrimByte:
560 case Primitive::kPrimChar:
561 case Primitive::kPrimShort:
562 case Primitive::kPrimVoid:
563 LOG(FATAL) << "Unexpected type " << load->GetType();
564 }
565
566 LOG(FATAL) << "Unreachable";
567 return Location();
568}
569
570void CodeGeneratorX86_64::Move(Location destination, Location source) {
571 if (source.Equals(destination)) {
572 return;
573 }
574 if (destination.IsRegister()) {
575 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000576 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100577 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000578 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000580 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100582 } else {
583 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000584 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 Address(CpuRegister(RSP), source.GetStackIndex()));
586 }
587 } else if (destination.IsFpuRegister()) {
588 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000589 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000591 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100592 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000593 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100594 Address(CpuRegister(RSP), source.GetStackIndex()));
595 } else {
596 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000597 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100598 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100599 }
600 } else if (destination.IsStackSlot()) {
601 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100602 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000603 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100604 } else if (source.IsFpuRegister()) {
605 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000606 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500607 } else if (source.IsConstant()) {
608 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000609 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500610 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100611 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500612 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000613 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
614 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615 }
616 } else {
617 DCHECK(destination.IsDoubleStackSlot());
618 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000620 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100621 } else if (source.IsFpuRegister()) {
622 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000623 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500624 } else if (source.IsConstant()) {
625 HConstant* constant = source.GetConstant();
626 int64_t value = constant->AsLongConstant()->GetValue();
627 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000628 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500629 } else {
630 DCHECK(constant->IsLongConstant());
631 value = constant->AsLongConstant()->GetValue();
632 }
633 __ movq(CpuRegister(TMP), Immediate(value));
634 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100635 } else {
636 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000637 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
638 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100639 }
640 }
641}
642
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100643void CodeGeneratorX86_64::Move(HInstruction* instruction,
644 Location location,
645 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000646 LocationSummary* locations = instruction->GetLocations();
647 if (locations != nullptr && locations->Out().Equals(location)) {
648 return;
649 }
650
651 if (locations != nullptr && locations->Out().IsConstant()) {
652 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000653 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
654 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000655 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000656 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000657 } else if (location.IsStackSlot()) {
658 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
659 } else {
660 DCHECK(location.IsConstant());
661 DCHECK_EQ(location.GetConstant(), const_to_move);
662 }
663 } else if (const_to_move->IsLongConstant()) {
664 int64_t value = const_to_move->AsLongConstant()->GetValue();
665 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000666 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000667 } else if (location.IsDoubleStackSlot()) {
668 __ movq(CpuRegister(TMP), Immediate(value));
669 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
670 } else {
671 DCHECK(location.IsConstant());
672 DCHECK_EQ(location.GetConstant(), const_to_move);
673 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100674 }
Roland Levillain476df552014-10-09 17:51:36 +0100675 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100676 switch (instruction->GetType()) {
677 case Primitive::kPrimBoolean:
678 case Primitive::kPrimByte:
679 case Primitive::kPrimChar:
680 case Primitive::kPrimShort:
681 case Primitive::kPrimInt:
682 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100684 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
685 break;
686
687 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000689 Move(location,
690 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100691 break;
692
693 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100694 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100695 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000696 } else if (instruction->IsTemporary()) {
697 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
698 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100699 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100700 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100701 switch (instruction->GetType()) {
702 case Primitive::kPrimBoolean:
703 case Primitive::kPrimByte:
704 case Primitive::kPrimChar:
705 case Primitive::kPrimShort:
706 case Primitive::kPrimInt:
707 case Primitive::kPrimNot:
708 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 case Primitive::kPrimFloat:
710 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000711 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100712 break;
713
714 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100716 }
717 }
718}
719
720void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
721 got->SetLocations(nullptr);
722}
723
724void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
725 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100726 DCHECK(!successor->IsExitBlock());
727
728 HBasicBlock* block = got->GetBlock();
729 HInstruction* previous = got->GetPrevious();
730
731 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000732 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100733 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
734 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
735 return;
736 }
737
738 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
739 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
740 }
741 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742 __ jmp(codegen_->GetLabelOf(successor));
743 }
744}
745
746void LocationsBuilderX86_64::VisitExit(HExit* exit) {
747 exit->SetLocations(nullptr);
748}
749
750void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700751 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752}
753
Andreas Gampe0ba62732015-03-24 02:39:46 +0000754void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
755 LocationSummary* locations =
756 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
757 HInstruction* cond = if_instr->InputAt(0);
758 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
759 locations->SetInAt(0, Location::Any());
760 }
761}
762
763void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
764 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100765 if (cond->IsIntConstant()) {
766 // Constant condition, statically compared against 1.
767 int32_t cond_value = cond->AsIntConstant()->GetValue();
768 if (cond_value == 1) {
Andreas Gampe0ba62732015-03-24 02:39:46 +0000769 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
770 if_instr->IfTrueSuccessor())) {
771 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100772 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100773 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100774 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100775 DCHECK_EQ(cond_value, 0);
776 }
777 } else {
778 bool materialized =
779 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
780 // Moves do not affect the eflags register, so if the condition is
781 // evaluated just before the if, we don't need to evaluate it
782 // again.
783 bool eflags_set = cond->IsCondition()
Andreas Gampe0ba62732015-03-24 02:39:46 +0000784 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100785 if (materialized) {
786 if (!eflags_set) {
787 // Materialized condition, compare against 0.
Andreas Gampe0ba62732015-03-24 02:39:46 +0000788 Location lhs = if_instr->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100789 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000790 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100791 } else {
792 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
793 Immediate(0));
794 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000795 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 } else {
Andreas Gampe0ba62732015-03-24 02:39:46 +0000797 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
798 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100799 }
800 } else {
801 Location lhs = cond->GetLocations()->InAt(0);
802 Location rhs = cond->GetLocations()->InAt(1);
803 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000804 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000806 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000807 if (constant == 0) {
808 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
809 } else {
810 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
811 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100812 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000813 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 Address(CpuRegister(RSP), rhs.GetStackIndex()));
815 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000816 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
817 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700818 }
Dave Allison20dfc792014-06-16 20:44:29 -0700819 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000820 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
821 if_instr->IfFalseSuccessor())) {
822 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100823 }
824}
825
826void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
827 local->SetLocations(nullptr);
828}
829
830void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
831 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
832}
833
834void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
835 local->SetLocations(nullptr);
836}
837
838void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
839 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700840 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100841}
842
843void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100844 LocationSummary* locations =
845 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 switch (store->InputAt(1)->GetType()) {
847 case Primitive::kPrimBoolean:
848 case Primitive::kPrimByte:
849 case Primitive::kPrimChar:
850 case Primitive::kPrimShort:
851 case Primitive::kPrimInt:
852 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100854 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
855 break;
856
857 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
860 break;
861
862 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100864 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100865}
866
867void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700868 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869}
870
Dave Allison20dfc792014-06-16 20:44:29 -0700871void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100872 LocationSummary* locations =
873 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100874 locations->SetInAt(0, Location::RequiresRegister());
875 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100876 if (comp->NeedsMaterialization()) {
877 locations->SetOut(Location::RequiresRegister());
878 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100879}
880
Dave Allison20dfc792014-06-16 20:44:29 -0700881void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
882 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100883 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000884 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100885 // Clear register: setcc only sets the low byte.
886 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000887 Location lhs = locations->InAt(0);
888 Location rhs = locations->InAt(1);
889 if (rhs.IsRegister()) {
890 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
891 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800892 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000893 if (constant == 0) {
894 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
895 } else {
896 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
897 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100898 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000899 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100900 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100901 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700902 }
903}
904
905void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
906 VisitCondition(comp);
907}
908
909void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
910 VisitCondition(comp);
911}
912
913void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
914 VisitCondition(comp);
915}
916
917void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
918 VisitCondition(comp);
919}
920
921void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
922 VisitCondition(comp);
923}
924
925void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
926 VisitCondition(comp);
927}
928
929void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
930 VisitCondition(comp);
931}
932
933void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
934 VisitCondition(comp);
935}
936
937void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
938 VisitCondition(comp);
939}
940
941void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
942 VisitCondition(comp);
943}
944
945void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
946 VisitCondition(comp);
947}
948
949void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
950 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100951}
952
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100953void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100954 LocationSummary* locations =
955 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000956 switch (compare->InputAt(0)->GetType()) {
957 case Primitive::kPrimLong: {
958 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -0400959 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +0000960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
961 break;
962 }
963 case Primitive::kPrimFloat:
964 case Primitive::kPrimDouble: {
965 locations->SetInAt(0, Location::RequiresFpuRegister());
966 locations->SetInAt(1, Location::RequiresFpuRegister());
967 locations->SetOut(Location::RequiresRegister());
968 break;
969 }
970 default:
971 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
972 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100973}
974
975void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100976 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000977 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000978 Location left = locations->InAt(0);
979 Location right = locations->InAt(1);
980
981 Label less, greater, done;
982 Primitive::Type type = compare->InputAt(0)->GetType();
983 switch (type) {
984 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -0400985 CpuRegister left_reg = left.AsRegister<CpuRegister>();
986 if (right.IsConstant()) {
987 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
988 DCHECK(IsInt<32>(value));
989 if (value == 0) {
990 __ testq(left_reg, left_reg);
991 } else {
992 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
993 }
994 } else {
995 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
996 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100997 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000998 }
999 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001000 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001001 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1002 break;
1003 }
1004 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001005 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001006 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1007 break;
1008 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001009 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001010 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001011 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001012 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001013 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001014 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001015
Calin Juravle91debbc2014-11-26 19:01:09 +00001016 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001017 __ movl(out, Immediate(1));
1018 __ jmp(&done);
1019
1020 __ Bind(&less);
1021 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001022
1023 __ Bind(&done);
1024}
1025
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001026void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001027 LocationSummary* locations =
1028 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001029 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001030}
1031
1032void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001033 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001034 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001035}
1036
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001037void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1038 LocationSummary* locations =
1039 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1040 locations->SetOut(Location::ConstantLocation(constant));
1041}
1042
1043void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1044 // Will be generated at use site.
1045 UNUSED(constant);
1046}
1047
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001048void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001049 LocationSummary* locations =
1050 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001051 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052}
1053
1054void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001055 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001056 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001057}
1058
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001059void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1060 LocationSummary* locations =
1061 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1062 locations->SetOut(Location::ConstantLocation(constant));
1063}
1064
1065void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1066 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001067 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001068}
1069
1070void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1071 LocationSummary* locations =
1072 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1073 locations->SetOut(Location::ConstantLocation(constant));
1074}
1075
1076void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1077 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001078 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001079}
1080
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1082 ret->SetLocations(nullptr);
1083}
1084
1085void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001086 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001087 codegen_->GenerateFrameExit();
1088 __ ret();
1089}
1090
1091void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001092 LocationSummary* locations =
1093 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094 switch (ret->InputAt(0)->GetType()) {
1095 case Primitive::kPrimBoolean:
1096 case Primitive::kPrimByte:
1097 case Primitive::kPrimChar:
1098 case Primitive::kPrimShort:
1099 case Primitive::kPrimInt:
1100 case Primitive::kPrimNot:
1101 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103 break;
1104
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 case Primitive::kPrimFloat:
1106 case Primitive::kPrimDouble:
1107 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001108 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 break;
1110
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001111 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001113 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114}
1115
1116void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1117 if (kIsDebugBuild) {
1118 switch (ret->InputAt(0)->GetType()) {
1119 case Primitive::kPrimBoolean:
1120 case Primitive::kPrimByte:
1121 case Primitive::kPrimChar:
1122 case Primitive::kPrimShort:
1123 case Primitive::kPrimInt:
1124 case Primitive::kPrimNot:
1125 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001126 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001127 break;
1128
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001129 case Primitive::kPrimFloat:
1130 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001131 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 XMM0);
1133 break;
1134
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001136 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137 }
1138 }
1139 codegen_->GenerateFrameExit();
1140 __ ret();
1141}
1142
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001143Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1144 switch (type) {
1145 case Primitive::kPrimBoolean:
1146 case Primitive::kPrimByte:
1147 case Primitive::kPrimChar:
1148 case Primitive::kPrimShort:
1149 case Primitive::kPrimInt:
1150 case Primitive::kPrimNot: {
1151 uint32_t index = gp_index_++;
1152 stack_index_++;
1153 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001155 } else {
1156 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1157 }
1158 }
1159
1160 case Primitive::kPrimLong: {
1161 uint32_t index = gp_index_;
1162 stack_index_ += 2;
1163 if (index < calling_convention.GetNumberOfRegisters()) {
1164 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001166 } else {
1167 gp_index_ += 2;
1168 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1169 }
1170 }
1171
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 case Primitive::kPrimFloat: {
1173 uint32_t index = fp_index_++;
1174 stack_index_++;
1175 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001177 } else {
1178 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1179 }
1180 }
1181
1182 case Primitive::kPrimDouble: {
1183 uint32_t index = fp_index_++;
1184 stack_index_ += 2;
1185 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001186 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 } else {
1188 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1189 }
1190 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191
1192 case Primitive::kPrimVoid:
1193 LOG(FATAL) << "Unexpected parameter type " << type;
1194 break;
1195 }
1196 return Location();
1197}
1198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001199void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001200 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1201 if (intrinsic.TryDispatch(invoke)) {
1202 return;
1203 }
1204
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001205 HandleInvoke(invoke);
1206}
1207
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001208static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1209 if (invoke->GetLocations()->Intrinsified()) {
1210 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1211 intrinsic.Dispatch(invoke);
1212 return true;
1213 }
1214 return false;
1215}
1216
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001217void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001218 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1219 return;
1220 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001221
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001222 codegen_->GenerateStaticOrDirectCall(
1223 invoke,
1224 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001225 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001226}
1227
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001228void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001229 LocationSummary* locations =
1230 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001231 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001232
1233 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001234 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001235 HInstruction* input = invoke->InputAt(i);
1236 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1237 }
1238
1239 switch (invoke->GetType()) {
1240 case Primitive::kPrimBoolean:
1241 case Primitive::kPrimByte:
1242 case Primitive::kPrimChar:
1243 case Primitive::kPrimShort:
1244 case Primitive::kPrimInt:
1245 case Primitive::kPrimNot:
1246 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001247 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001248 break;
1249
1250 case Primitive::kPrimVoid:
1251 break;
1252
1253 case Primitive::kPrimDouble:
1254 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001255 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001256 break;
1257 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001258}
1259
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001260void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001261 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1262 if (intrinsic.TryDispatch(invoke)) {
1263 return;
1264 }
1265
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001266 HandleInvoke(invoke);
1267}
1268
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001269void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001270 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1271 return;
1272 }
1273
Roland Levillain271ab9c2014-11-27 15:23:57 +00001274 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001275 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1276 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1277 LocationSummary* locations = invoke->GetLocations();
1278 Location receiver = locations->InAt(0);
1279 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1280 // temp = object->GetClass();
1281 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001282 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1283 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001284 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001286 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001287 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288 // temp = temp->GetMethodAt(method_offset);
1289 __ movl(temp, Address(temp, method_offset));
1290 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001291 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001292 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001293
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001294 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001295 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296}
1297
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001298void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1299 HandleInvoke(invoke);
1300 // Add the hidden argument.
1301 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1302}
1303
1304void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1305 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1308 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1309 LocationSummary* locations = invoke->GetLocations();
1310 Location receiver = locations->InAt(0);
1311 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1312
1313 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001315 Immediate(invoke->GetDexMethodIndex()));
1316
1317 // temp = object->GetClass();
1318 if (receiver.IsStackSlot()) {
1319 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1320 __ movl(temp, Address(temp, class_offset));
1321 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001322 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001324 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001325 // temp = temp->GetImtEntryAt(method_offset);
1326 __ movl(temp, Address(temp, method_offset));
1327 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001328 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001329 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001330
1331 DCHECK(!codegen_->IsLeafMethod());
1332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1333}
1334
Roland Levillain88cb1752014-10-20 16:36:47 +01001335void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1336 LocationSummary* locations =
1337 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1338 switch (neg->GetResultType()) {
1339 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001340 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001341 locations->SetInAt(0, Location::RequiresRegister());
1342 locations->SetOut(Location::SameAsFirstInput());
1343 break;
1344
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001347 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001348 locations->SetOut(Location::SameAsFirstInput());
1349 locations->AddTemp(Location::RequiresRegister());
1350 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001351 break;
1352
1353 default:
1354 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1355 }
1356}
1357
1358void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1359 LocationSummary* locations = neg->GetLocations();
1360 Location out = locations->Out();
1361 Location in = locations->InAt(0);
1362 switch (neg->GetResultType()) {
1363 case Primitive::kPrimInt:
1364 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001365 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001366 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001367 break;
1368
1369 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001370 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001371 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001372 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001373 break;
1374
Roland Levillain5368c212014-11-27 15:03:41 +00001375 case Primitive::kPrimFloat: {
1376 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1378 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001379 // Implement float negation with an exclusive or with value
1380 // 0x80000000 (mask for bit 31, representing the sign of a
1381 // single-precision floating-point number).
1382 __ movq(constant, Immediate(INT64_C(0x80000000)));
1383 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001384 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001385 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001386 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001387
Roland Levillain5368c212014-11-27 15:03:41 +00001388 case Primitive::kPrimDouble: {
1389 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1391 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001392 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001393 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001394 // a double-precision floating-point number).
1395 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1396 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001397 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001398 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001399 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001400
1401 default:
1402 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1403 }
1404}
1405
Roland Levillaindff1f282014-11-05 14:15:05 +00001406void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1407 LocationSummary* locations =
1408 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1409 Primitive::Type result_type = conversion->GetResultType();
1410 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001411 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001412
David Brazdilb2bd1c52015-03-25 11:17:37 +00001413 // The Java language does not allow treating boolean as an integral type but
1414 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001415
Roland Levillaindff1f282014-11-05 14:15:05 +00001416 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001417 case Primitive::kPrimByte:
1418 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001419 case Primitive::kPrimBoolean:
1420 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001421 case Primitive::kPrimShort:
1422 case Primitive::kPrimInt:
1423 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001424 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001425 locations->SetInAt(0, Location::Any());
1426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1427 break;
1428
1429 default:
1430 LOG(FATAL) << "Unexpected type conversion from " << input_type
1431 << " to " << result_type;
1432 }
1433 break;
1434
Roland Levillain01a8d712014-11-14 16:27:39 +00001435 case Primitive::kPrimShort:
1436 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001437 case Primitive::kPrimBoolean:
1438 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001439 case Primitive::kPrimByte:
1440 case Primitive::kPrimInt:
1441 case Primitive::kPrimChar:
1442 // Processing a Dex `int-to-short' instruction.
1443 locations->SetInAt(0, Location::Any());
1444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1445 break;
1446
1447 default:
1448 LOG(FATAL) << "Unexpected type conversion from " << input_type
1449 << " to " << result_type;
1450 }
1451 break;
1452
Roland Levillain946e1432014-11-11 17:35:19 +00001453 case Primitive::kPrimInt:
1454 switch (input_type) {
1455 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001456 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001457 locations->SetInAt(0, Location::Any());
1458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1459 break;
1460
1461 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001462 // Processing a Dex `float-to-int' instruction.
1463 locations->SetInAt(0, Location::RequiresFpuRegister());
1464 locations->SetOut(Location::RequiresRegister());
1465 locations->AddTemp(Location::RequiresFpuRegister());
1466 break;
1467
Roland Levillain946e1432014-11-11 17:35:19 +00001468 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001469 // Processing a Dex `double-to-int' instruction.
1470 locations->SetInAt(0, Location::RequiresFpuRegister());
1471 locations->SetOut(Location::RequiresRegister());
1472 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001473 break;
1474
1475 default:
1476 LOG(FATAL) << "Unexpected type conversion from " << input_type
1477 << " to " << result_type;
1478 }
1479 break;
1480
Roland Levillaindff1f282014-11-05 14:15:05 +00001481 case Primitive::kPrimLong:
1482 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001483 case Primitive::kPrimBoolean:
1484 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001485 case Primitive::kPrimByte:
1486 case Primitive::kPrimShort:
1487 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001488 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001489 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001490 // TODO: We would benefit from a (to-be-implemented)
1491 // Location::RegisterOrStackSlot requirement for this input.
1492 locations->SetInAt(0, Location::RequiresRegister());
1493 locations->SetOut(Location::RequiresRegister());
1494 break;
1495
1496 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001497 // Processing a Dex `float-to-long' instruction.
1498 locations->SetInAt(0, Location::RequiresFpuRegister());
1499 locations->SetOut(Location::RequiresRegister());
1500 locations->AddTemp(Location::RequiresFpuRegister());
1501 break;
1502
Roland Levillaindff1f282014-11-05 14:15:05 +00001503 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001504 // Processing a Dex `double-to-long' instruction.
1505 locations->SetInAt(0, Location::RequiresFpuRegister());
1506 locations->SetOut(Location::RequiresRegister());
1507 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001508 break;
1509
1510 default:
1511 LOG(FATAL) << "Unexpected type conversion from " << input_type
1512 << " to " << result_type;
1513 }
1514 break;
1515
Roland Levillain981e4542014-11-14 11:47:14 +00001516 case Primitive::kPrimChar:
1517 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001518 case Primitive::kPrimBoolean:
1519 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001520 case Primitive::kPrimByte:
1521 case Primitive::kPrimShort:
1522 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001523 // Processing a Dex `int-to-char' instruction.
1524 locations->SetInAt(0, Location::Any());
1525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1526 break;
1527
1528 default:
1529 LOG(FATAL) << "Unexpected type conversion from " << input_type
1530 << " to " << result_type;
1531 }
1532 break;
1533
Roland Levillaindff1f282014-11-05 14:15:05 +00001534 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001535 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001536 case Primitive::kPrimBoolean:
1537 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001538 case Primitive::kPrimByte:
1539 case Primitive::kPrimShort:
1540 case Primitive::kPrimInt:
1541 case Primitive::kPrimChar:
1542 // Processing a Dex `int-to-float' instruction.
1543 locations->SetInAt(0, Location::RequiresRegister());
1544 locations->SetOut(Location::RequiresFpuRegister());
1545 break;
1546
1547 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001548 // Processing a Dex `long-to-float' instruction.
1549 locations->SetInAt(0, Location::RequiresRegister());
1550 locations->SetOut(Location::RequiresFpuRegister());
1551 break;
1552
Roland Levillaincff13742014-11-17 14:32:17 +00001553 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001554 // Processing a Dex `double-to-float' instruction.
1555 locations->SetInAt(0, Location::RequiresFpuRegister());
1556 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001557 break;
1558
1559 default:
1560 LOG(FATAL) << "Unexpected type conversion from " << input_type
1561 << " to " << result_type;
1562 };
1563 break;
1564
Roland Levillaindff1f282014-11-05 14:15:05 +00001565 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001566 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001567 case Primitive::kPrimBoolean:
1568 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001569 case Primitive::kPrimByte:
1570 case Primitive::kPrimShort:
1571 case Primitive::kPrimInt:
1572 case Primitive::kPrimChar:
1573 // Processing a Dex `int-to-double' instruction.
1574 locations->SetInAt(0, Location::RequiresRegister());
1575 locations->SetOut(Location::RequiresFpuRegister());
1576 break;
1577
1578 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001579 // Processing a Dex `long-to-double' instruction.
1580 locations->SetInAt(0, Location::RequiresRegister());
1581 locations->SetOut(Location::RequiresFpuRegister());
1582 break;
1583
Roland Levillaincff13742014-11-17 14:32:17 +00001584 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001585 // Processing a Dex `float-to-double' instruction.
1586 locations->SetInAt(0, Location::RequiresFpuRegister());
1587 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001588 break;
1589
1590 default:
1591 LOG(FATAL) << "Unexpected type conversion from " << input_type
1592 << " to " << result_type;
1593 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001594 break;
1595
1596 default:
1597 LOG(FATAL) << "Unexpected type conversion from " << input_type
1598 << " to " << result_type;
1599 }
1600}
1601
1602void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1603 LocationSummary* locations = conversion->GetLocations();
1604 Location out = locations->Out();
1605 Location in = locations->InAt(0);
1606 Primitive::Type result_type = conversion->GetResultType();
1607 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001608 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001609 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001610 case Primitive::kPrimByte:
1611 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001612 case Primitive::kPrimBoolean:
1613 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001614 case Primitive::kPrimShort:
1615 case Primitive::kPrimInt:
1616 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001617 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001618 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001619 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001620 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001621 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001622 Address(CpuRegister(RSP), in.GetStackIndex()));
1623 } else {
1624 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001625 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001626 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1627 }
1628 break;
1629
1630 default:
1631 LOG(FATAL) << "Unexpected type conversion from " << input_type
1632 << " to " << result_type;
1633 }
1634 break;
1635
Roland Levillain01a8d712014-11-14 16:27:39 +00001636 case Primitive::kPrimShort:
1637 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001638 case Primitive::kPrimBoolean:
1639 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001640 case Primitive::kPrimByte:
1641 case Primitive::kPrimInt:
1642 case Primitive::kPrimChar:
1643 // Processing a Dex `int-to-short' instruction.
1644 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001645 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001646 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001647 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001648 Address(CpuRegister(RSP), in.GetStackIndex()));
1649 } else {
1650 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001651 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001652 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1653 }
1654 break;
1655
1656 default:
1657 LOG(FATAL) << "Unexpected type conversion from " << input_type
1658 << " to " << result_type;
1659 }
1660 break;
1661
Roland Levillain946e1432014-11-11 17:35:19 +00001662 case Primitive::kPrimInt:
1663 switch (input_type) {
1664 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001665 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001666 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001667 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001668 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001669 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001670 Address(CpuRegister(RSP), in.GetStackIndex()));
1671 } else {
1672 DCHECK(in.IsConstant());
1673 DCHECK(in.GetConstant()->IsLongConstant());
1674 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001676 }
1677 break;
1678
Roland Levillain3f8f9362014-12-02 17:45:01 +00001679 case Primitive::kPrimFloat: {
1680 // Processing a Dex `float-to-int' instruction.
1681 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1682 CpuRegister output = out.AsRegister<CpuRegister>();
1683 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1684 Label done, nan;
1685
1686 __ movl(output, Immediate(kPrimIntMax));
1687 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001688 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001689 // if input >= temp goto done
1690 __ comiss(input, temp);
1691 __ j(kAboveEqual, &done);
1692 // if input == NaN goto nan
1693 __ j(kUnordered, &nan);
1694 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001695 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001696 __ jmp(&done);
1697 __ Bind(&nan);
1698 // output = 0
1699 __ xorl(output, output);
1700 __ Bind(&done);
1701 break;
1702 }
1703
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001704 case Primitive::kPrimDouble: {
1705 // Processing a Dex `double-to-int' instruction.
1706 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1707 CpuRegister output = out.AsRegister<CpuRegister>();
1708 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1709 Label done, nan;
1710
1711 __ movl(output, Immediate(kPrimIntMax));
1712 // temp = int-to-double(output)
1713 __ cvtsi2sd(temp, output);
1714 // if input >= temp goto done
1715 __ comisd(input, temp);
1716 __ j(kAboveEqual, &done);
1717 // if input == NaN goto nan
1718 __ j(kUnordered, &nan);
1719 // output = double-to-int-truncate(input)
1720 __ cvttsd2si(output, input);
1721 __ jmp(&done);
1722 __ Bind(&nan);
1723 // output = 0
1724 __ xorl(output, output);
1725 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001726 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001727 }
Roland Levillain946e1432014-11-11 17:35:19 +00001728
1729 default:
1730 LOG(FATAL) << "Unexpected type conversion from " << input_type
1731 << " to " << result_type;
1732 }
1733 break;
1734
Roland Levillaindff1f282014-11-05 14:15:05 +00001735 case Primitive::kPrimLong:
1736 switch (input_type) {
1737 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001738 case Primitive::kPrimBoolean:
1739 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001740 case Primitive::kPrimByte:
1741 case Primitive::kPrimShort:
1742 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001743 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001744 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001745 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001746 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001747 break;
1748
Roland Levillain624279f2014-12-04 11:54:28 +00001749 case Primitive::kPrimFloat: {
1750 // Processing a Dex `float-to-long' instruction.
1751 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1752 CpuRegister output = out.AsRegister<CpuRegister>();
1753 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1754 Label done, nan;
1755
1756 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001757 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001758 __ cvtsi2ss(temp, output, true);
1759 // if input >= temp goto done
1760 __ comiss(input, temp);
1761 __ j(kAboveEqual, &done);
1762 // if input == NaN goto nan
1763 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001764 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001765 __ cvttss2si(output, input, true);
1766 __ jmp(&done);
1767 __ Bind(&nan);
1768 // output = 0
1769 __ xorq(output, output);
1770 __ Bind(&done);
1771 break;
1772 }
1773
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001774 case Primitive::kPrimDouble: {
1775 // Processing a Dex `double-to-long' instruction.
1776 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1777 CpuRegister output = out.AsRegister<CpuRegister>();
1778 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1779 Label done, nan;
1780
1781 __ movq(output, Immediate(kPrimLongMax));
1782 // temp = long-to-double(output)
1783 __ cvtsi2sd(temp, output, true);
1784 // if input >= temp goto done
1785 __ comisd(input, temp);
1786 __ j(kAboveEqual, &done);
1787 // if input == NaN goto nan
1788 __ j(kUnordered, &nan);
1789 // output = double-to-long-truncate(input)
1790 __ cvttsd2si(output, input, true);
1791 __ jmp(&done);
1792 __ Bind(&nan);
1793 // output = 0
1794 __ xorq(output, output);
1795 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001796 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001797 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001798
1799 default:
1800 LOG(FATAL) << "Unexpected type conversion from " << input_type
1801 << " to " << result_type;
1802 }
1803 break;
1804
Roland Levillain981e4542014-11-14 11:47:14 +00001805 case Primitive::kPrimChar:
1806 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001807 case Primitive::kPrimBoolean:
1808 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001809 case Primitive::kPrimByte:
1810 case Primitive::kPrimShort:
1811 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001812 // Processing a Dex `int-to-char' instruction.
1813 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001814 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001815 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001816 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001817 Address(CpuRegister(RSP), in.GetStackIndex()));
1818 } else {
1819 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001820 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001821 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1822 }
1823 break;
1824
1825 default:
1826 LOG(FATAL) << "Unexpected type conversion from " << input_type
1827 << " to " << result_type;
1828 }
1829 break;
1830
Roland Levillaindff1f282014-11-05 14:15:05 +00001831 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001832 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001833 case Primitive::kPrimBoolean:
1834 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001835 case Primitive::kPrimByte:
1836 case Primitive::kPrimShort:
1837 case Primitive::kPrimInt:
1838 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001839 // Processing a Dex `int-to-float' instruction.
1840 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001841 break;
1842
1843 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001844 // Processing a Dex `long-to-float' instruction.
1845 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1846 break;
1847
Roland Levillaincff13742014-11-17 14:32:17 +00001848 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001849 // Processing a Dex `double-to-float' instruction.
1850 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001851 break;
1852
1853 default:
1854 LOG(FATAL) << "Unexpected type conversion from " << input_type
1855 << " to " << result_type;
1856 };
1857 break;
1858
Roland Levillaindff1f282014-11-05 14:15:05 +00001859 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001860 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001861 case Primitive::kPrimBoolean:
1862 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001863 case Primitive::kPrimByte:
1864 case Primitive::kPrimShort:
1865 case Primitive::kPrimInt:
1866 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001867 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001869 break;
1870
1871 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001872 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001873 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001874 break;
1875
Roland Levillaincff13742014-11-17 14:32:17 +00001876 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001877 // Processing a Dex `float-to-double' instruction.
1878 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001879 break;
1880
1881 default:
1882 LOG(FATAL) << "Unexpected type conversion from " << input_type
1883 << " to " << result_type;
1884 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001885 break;
1886
1887 default:
1888 LOG(FATAL) << "Unexpected type conversion from " << input_type
1889 << " to " << result_type;
1890 }
1891}
1892
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001894 LocationSummary* locations =
1895 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001896 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001897 case Primitive::kPrimInt: {
1898 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001899 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1900 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001901 break;
1902 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001903
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001904 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001905 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001906 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001907 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001909 break;
1910 }
1911
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001912 case Primitive::kPrimDouble:
1913 case Primitive::kPrimFloat: {
1914 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001915 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001916 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001917 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001918 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001919
1920 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001921 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001922 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001923}
1924
1925void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1926 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 Location first = locations->InAt(0);
1928 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001929 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001930
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001931 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001932 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001934 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1935 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1936 } else {
1937 __ leal(out.AsRegister<CpuRegister>(), Address(
1938 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1939 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001941 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1942 __ addl(out.AsRegister<CpuRegister>(),
1943 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
1944 } else {
1945 __ leal(out.AsRegister<CpuRegister>(), Address(
1946 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
1947 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001948 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001949 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001950 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001951 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001952 break;
1953 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001955 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05001956 if (second.IsRegister()) {
1957 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1958 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1959 } else {
1960 __ leaq(out.AsRegister<CpuRegister>(), Address(
1961 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1962 }
1963 } else {
1964 DCHECK(second.IsConstant());
1965 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1966 int32_t int32_value = Low32Bits(value);
1967 DCHECK_EQ(int32_value, value);
1968 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1969 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
1970 } else {
1971 __ leaq(out.AsRegister<CpuRegister>(), Address(
1972 first.AsRegister<CpuRegister>(), int32_value));
1973 }
1974 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001975 break;
1976 }
1977
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001978 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001979 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001980 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001981 }
1982
1983 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001984 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001985 break;
1986 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001987
1988 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001989 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001990 }
1991}
1992
1993void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001994 LocationSummary* locations =
1995 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001996 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001997 case Primitive::kPrimInt: {
1998 locations->SetInAt(0, Location::RequiresRegister());
1999 locations->SetInAt(1, Location::Any());
2000 locations->SetOut(Location::SameAsFirstInput());
2001 break;
2002 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002003 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002004 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002005 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002006 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002007 break;
2008 }
Calin Juravle11351682014-10-23 15:38:15 +01002009 case Primitive::kPrimFloat:
2010 case Primitive::kPrimDouble: {
2011 locations->SetInAt(0, Location::RequiresFpuRegister());
2012 locations->SetInAt(1, Location::RequiresFpuRegister());
2013 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014 break;
Calin Juravle11351682014-10-23 15:38:15 +01002015 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016 default:
Calin Juravle11351682014-10-23 15:38:15 +01002017 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002018 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002019}
2020
2021void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2022 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002023 Location first = locations->InAt(0);
2024 Location second = locations->InAt(1);
2025 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002026 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002027 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002028 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002029 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002030 } else if (second.IsConstant()) {
2031 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002032 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002033 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002034 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002035 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002036 break;
2037 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002038 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002039 if (second.IsConstant()) {
2040 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2041 DCHECK(IsInt<32>(value));
2042 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2043 } else {
2044 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2045 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002046 break;
2047 }
2048
Calin Juravle11351682014-10-23 15:38:15 +01002049 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002051 break;
Calin Juravle11351682014-10-23 15:38:15 +01002052 }
2053
2054 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002056 break;
2057 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002058
2059 default:
Calin Juravle11351682014-10-23 15:38:15 +01002060 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002061 }
2062}
2063
Calin Juravle34bacdf2014-10-07 20:23:36 +01002064void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2065 LocationSummary* locations =
2066 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2067 switch (mul->GetResultType()) {
2068 case Primitive::kPrimInt: {
2069 locations->SetInAt(0, Location::RequiresRegister());
2070 locations->SetInAt(1, Location::Any());
2071 locations->SetOut(Location::SameAsFirstInput());
2072 break;
2073 }
2074 case Primitive::kPrimLong: {
2075 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002076 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2077 if (locations->InAt(1).IsConstant()) {
2078 // Can use 3 operand multiply.
2079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2080 } else {
2081 locations->SetOut(Location::SameAsFirstInput());
2082 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002083 break;
2084 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002085 case Primitive::kPrimFloat:
2086 case Primitive::kPrimDouble: {
2087 locations->SetInAt(0, Location::RequiresFpuRegister());
2088 locations->SetInAt(1, Location::RequiresFpuRegister());
2089 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002090 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002091 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002092
2093 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002094 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002095 }
2096}
2097
2098void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2099 LocationSummary* locations = mul->GetLocations();
2100 Location first = locations->InAt(0);
2101 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002102 switch (mul->GetResultType()) {
2103 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002104 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002105 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002106 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002107 } else if (second.IsConstant()) {
2108 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002109 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002110 } else {
2111 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002112 __ imull(first.AsRegister<CpuRegister>(),
2113 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002114 }
2115 break;
2116 }
2117 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002118 if (second.IsConstant()) {
2119 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2120 DCHECK(IsInt<32>(value));
2121 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2122 first.AsRegister<CpuRegister>(),
2123 Immediate(static_cast<int32_t>(value)));
2124 } else {
2125 DCHECK(first.Equals(locations->Out()));
2126 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2127 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002128 break;
2129 }
2130
Calin Juravleb5bfa962014-10-21 18:02:24 +01002131 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002132 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002135 }
2136
2137 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002138 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002140 break;
2141 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002142
2143 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002144 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002145 }
2146}
2147
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002148void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2149 uint32_t stack_adjustment, bool is_float) {
2150 if (source.IsStackSlot()) {
2151 DCHECK(is_float);
2152 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2153 } else if (source.IsDoubleStackSlot()) {
2154 DCHECK(!is_float);
2155 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2156 } else {
2157 // Write the value to the temporary location on the stack and load to FP stack.
2158 if (is_float) {
2159 Location stack_temp = Location::StackSlot(temp_offset);
2160 codegen_->Move(stack_temp, source);
2161 __ flds(Address(CpuRegister(RSP), temp_offset));
2162 } else {
2163 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2164 codegen_->Move(stack_temp, source);
2165 __ fldl(Address(CpuRegister(RSP), temp_offset));
2166 }
2167 }
2168}
2169
2170void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2171 Primitive::Type type = rem->GetResultType();
2172 bool is_float = type == Primitive::kPrimFloat;
2173 size_t elem_size = Primitive::ComponentSize(type);
2174 LocationSummary* locations = rem->GetLocations();
2175 Location first = locations->InAt(0);
2176 Location second = locations->InAt(1);
2177 Location out = locations->Out();
2178
2179 // Create stack space for 2 elements.
2180 // TODO: enhance register allocator to ask for stack temporaries.
2181 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2182
2183 // Load the values to the FP stack in reverse order, using temporaries if needed.
2184 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2185 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2186
2187 // Loop doing FPREM until we stabilize.
2188 Label retry;
2189 __ Bind(&retry);
2190 __ fprem();
2191
2192 // Move FP status to AX.
2193 __ fstsw();
2194
2195 // And see if the argument reduction is complete. This is signaled by the
2196 // C2 FPU flag bit set to 0.
2197 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2198 __ j(kNotEqual, &retry);
2199
2200 // We have settled on the final value. Retrieve it into an XMM register.
2201 // Store FP top of stack to real stack.
2202 if (is_float) {
2203 __ fsts(Address(CpuRegister(RSP), 0));
2204 } else {
2205 __ fstl(Address(CpuRegister(RSP), 0));
2206 }
2207
2208 // Pop the 2 items from the FP stack.
2209 __ fucompp();
2210
2211 // Load the value from the stack into an XMM register.
2212 DCHECK(out.IsFpuRegister()) << out;
2213 if (is_float) {
2214 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2215 } else {
2216 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2217 }
2218
2219 // And remove the temporary stack space we allocated.
2220 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2221}
2222
Calin Juravlebacfec32014-11-14 15:54:36 +00002223void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2224 DCHECK(instruction->IsDiv() || instruction->IsRem());
2225 Primitive::Type type = instruction->GetResultType();
2226 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2227
2228 bool is_div = instruction->IsDiv();
2229 LocationSummary* locations = instruction->GetLocations();
2230
Roland Levillain271ab9c2014-11-27 15:23:57 +00002231 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2232 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002233
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002235 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2236
2237 SlowPathCodeX86_64* slow_path =
2238 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2239 out_reg.AsRegister(), type, is_div);
2240 codegen_->AddSlowPath(slow_path);
2241
2242 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2243 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2244 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002245 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002246 __ cmpl(second_reg, Immediate(-1));
2247 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002248 // edx:eax <- sign-extended of eax
2249 __ cdq();
2250 // eax = quotient, edx = remainder
2251 __ idivl(second_reg);
2252 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002253 __ cmpq(second_reg, Immediate(-1));
2254 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002255 // rdx:rax <- sign-extended of rax
2256 __ cqo();
2257 // rax = quotient, rdx = remainder
2258 __ idivq(second_reg);
2259 }
2260
2261 __ Bind(slow_path->GetExitLabel());
2262}
2263
Calin Juravle7c4954d2014-10-28 16:57:40 +00002264void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2265 LocationSummary* locations =
2266 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2267 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002268 case Primitive::kPrimInt:
2269 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002270 locations->SetInAt(0, Location::RegisterLocation(RAX));
2271 locations->SetInAt(1, Location::RequiresRegister());
2272 locations->SetOut(Location::SameAsFirstInput());
2273 // Intel uses edx:eax as the dividend.
2274 locations->AddTemp(Location::RegisterLocation(RDX));
2275 break;
2276 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002277
Calin Juravle7c4954d2014-10-28 16:57:40 +00002278 case Primitive::kPrimFloat:
2279 case Primitive::kPrimDouble: {
2280 locations->SetInAt(0, Location::RequiresFpuRegister());
2281 locations->SetInAt(1, Location::RequiresFpuRegister());
2282 locations->SetOut(Location::SameAsFirstInput());
2283 break;
2284 }
2285
2286 default:
2287 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2288 }
2289}
2290
2291void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2292 LocationSummary* locations = div->GetLocations();
2293 Location first = locations->InAt(0);
2294 Location second = locations->InAt(1);
2295 DCHECK(first.Equals(locations->Out()));
2296
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002297 Primitive::Type type = div->GetResultType();
2298 switch (type) {
2299 case Primitive::kPrimInt:
2300 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002301 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002302 break;
2303 }
2304
Calin Juravle7c4954d2014-10-28 16:57:40 +00002305 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002307 break;
2308 }
2309
2310 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002311 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002312 break;
2313 }
2314
2315 default:
2316 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2317 }
2318}
2319
Calin Juravlebacfec32014-11-14 15:54:36 +00002320void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002321 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002322 LocationSummary* locations =
2323 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002324
2325 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002326 case Primitive::kPrimInt:
2327 case Primitive::kPrimLong: {
2328 locations->SetInAt(0, Location::RegisterLocation(RAX));
2329 locations->SetInAt(1, Location::RequiresRegister());
2330 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2331 locations->SetOut(Location::RegisterLocation(RDX));
2332 break;
2333 }
2334
2335 case Primitive::kPrimFloat:
2336 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002337 locations->SetInAt(0, Location::Any());
2338 locations->SetInAt(1, Location::Any());
2339 locations->SetOut(Location::RequiresFpuRegister());
2340 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002341 break;
2342 }
2343
2344 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002345 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002346 }
2347}
2348
2349void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2350 Primitive::Type type = rem->GetResultType();
2351 switch (type) {
2352 case Primitive::kPrimInt:
2353 case Primitive::kPrimLong: {
2354 GenerateDivRemIntegral(rem);
2355 break;
2356 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002357 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002358 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002359 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002360 break;
2361 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002362 default:
2363 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2364 }
2365}
2366
Calin Juravled0d48522014-11-04 16:40:20 +00002367void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2368 LocationSummary* locations =
2369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2370 locations->SetInAt(0, Location::Any());
2371 if (instruction->HasUses()) {
2372 locations->SetOut(Location::SameAsFirstInput());
2373 }
2374}
2375
2376void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2377 SlowPathCodeX86_64* slow_path =
2378 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2379 codegen_->AddSlowPath(slow_path);
2380
2381 LocationSummary* locations = instruction->GetLocations();
2382 Location value = locations->InAt(0);
2383
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002384 switch (instruction->GetType()) {
2385 case Primitive::kPrimInt: {
2386 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002387 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002388 __ j(kEqual, slow_path->GetEntryLabel());
2389 } else if (value.IsStackSlot()) {
2390 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2391 __ j(kEqual, slow_path->GetEntryLabel());
2392 } else {
2393 DCHECK(value.IsConstant()) << value;
2394 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2395 __ jmp(slow_path->GetEntryLabel());
2396 }
2397 }
2398 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002399 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002400 case Primitive::kPrimLong: {
2401 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002402 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002403 __ j(kEqual, slow_path->GetEntryLabel());
2404 } else if (value.IsDoubleStackSlot()) {
2405 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2406 __ j(kEqual, slow_path->GetEntryLabel());
2407 } else {
2408 DCHECK(value.IsConstant()) << value;
2409 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2410 __ jmp(slow_path->GetEntryLabel());
2411 }
2412 }
2413 break;
2414 }
2415 default:
2416 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002417 }
Calin Juravled0d48522014-11-04 16:40:20 +00002418}
2419
Calin Juravle9aec02f2014-11-18 23:06:35 +00002420void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2421 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2422
2423 LocationSummary* locations =
2424 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2425
2426 switch (op->GetResultType()) {
2427 case Primitive::kPrimInt:
2428 case Primitive::kPrimLong: {
2429 locations->SetInAt(0, Location::RequiresRegister());
2430 // The shift count needs to be in CL.
2431 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2432 locations->SetOut(Location::SameAsFirstInput());
2433 break;
2434 }
2435 default:
2436 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2437 }
2438}
2439
2440void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2441 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2442
2443 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002445 Location second = locations->InAt(1);
2446
2447 switch (op->GetResultType()) {
2448 case Primitive::kPrimInt: {
2449 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002451 if (op->IsShl()) {
2452 __ shll(first_reg, second_reg);
2453 } else if (op->IsShr()) {
2454 __ sarl(first_reg, second_reg);
2455 } else {
2456 __ shrl(first_reg, second_reg);
2457 }
2458 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002459 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002460 if (op->IsShl()) {
2461 __ shll(first_reg, imm);
2462 } else if (op->IsShr()) {
2463 __ sarl(first_reg, imm);
2464 } else {
2465 __ shrl(first_reg, imm);
2466 }
2467 }
2468 break;
2469 }
2470 case Primitive::kPrimLong: {
2471 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002473 if (op->IsShl()) {
2474 __ shlq(first_reg, second_reg);
2475 } else if (op->IsShr()) {
2476 __ sarq(first_reg, second_reg);
2477 } else {
2478 __ shrq(first_reg, second_reg);
2479 }
2480 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002481 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002482 if (op->IsShl()) {
2483 __ shlq(first_reg, imm);
2484 } else if (op->IsShr()) {
2485 __ sarq(first_reg, imm);
2486 } else {
2487 __ shrq(first_reg, imm);
2488 }
2489 }
2490 break;
2491 }
2492 default:
2493 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2494 }
2495}
2496
2497void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2498 HandleShift(shl);
2499}
2500
2501void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2502 HandleShift(shl);
2503}
2504
2505void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2506 HandleShift(shr);
2507}
2508
2509void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2510 HandleShift(shr);
2511}
2512
2513void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2514 HandleShift(ushr);
2515}
2516
2517void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2518 HandleShift(ushr);
2519}
2520
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002522 LocationSummary* locations =
2523 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002524 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002525 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2526 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2527 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002528}
2529
2530void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2531 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002532 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002533 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2534
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002535 __ gs()->call(
2536 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002537
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002538 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002539 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002540}
2541
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002542void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2543 LocationSummary* locations =
2544 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2545 InvokeRuntimeCallingConvention calling_convention;
2546 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002547 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002548 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002549 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002550}
2551
2552void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2553 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002554 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002555 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2556
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002557 __ gs()->call(
2558 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002559
2560 DCHECK(!codegen_->IsLeafMethod());
2561 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2562}
2563
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002564void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002565 LocationSummary* locations =
2566 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002567 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2568 if (location.IsStackSlot()) {
2569 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2570 } else if (location.IsDoubleStackSlot()) {
2571 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2572 }
2573 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002574}
2575
2576void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2577 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002578 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002579}
2580
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002581void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002582 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002583 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002584 locations->SetInAt(0, Location::RequiresRegister());
2585 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002586}
2587
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002588void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2589 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2591 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002592 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002593 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002594 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002596 break;
2597
2598 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002600 break;
2601
2602 default:
2603 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2604 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002605}
2606
2607void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002608 LocationSummary* locations =
2609 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002610 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2611 locations->SetInAt(i, Location::Any());
2612 }
2613 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002614}
2615
2616void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002617 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002618 LOG(FATAL) << "Unimplemented";
2619}
2620
Calin Juravle52c48962014-12-16 17:02:57 +00002621void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2622 /*
2623 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2624 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2625 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2626 */
2627 switch (kind) {
2628 case MemBarrierKind::kAnyAny: {
2629 __ mfence();
2630 break;
2631 }
2632 case MemBarrierKind::kAnyStore:
2633 case MemBarrierKind::kLoadAny:
2634 case MemBarrierKind::kStoreStore: {
2635 // nop
2636 break;
2637 }
2638 default:
2639 LOG(FATAL) << "Unexpected memory barier " << kind;
2640 }
2641}
2642
2643void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2644 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2645
Nicolas Geoffray39468442014-09-02 15:17:15 +01002646 LocationSummary* locations =
2647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002648 locations->SetInAt(0, Location::RequiresRegister());
2649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2650}
2651
2652void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2653 const FieldInfo& field_info) {
2654 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2655
2656 LocationSummary* locations = instruction->GetLocations();
2657 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2658 Location out = locations->Out();
2659 bool is_volatile = field_info.IsVolatile();
2660 Primitive::Type field_type = field_info.GetFieldType();
2661 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2662
2663 switch (field_type) {
2664 case Primitive::kPrimBoolean: {
2665 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2666 break;
2667 }
2668
2669 case Primitive::kPrimByte: {
2670 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2671 break;
2672 }
2673
2674 case Primitive::kPrimShort: {
2675 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2676 break;
2677 }
2678
2679 case Primitive::kPrimChar: {
2680 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2681 break;
2682 }
2683
2684 case Primitive::kPrimInt:
2685 case Primitive::kPrimNot: {
2686 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2687 break;
2688 }
2689
2690 case Primitive::kPrimLong: {
2691 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2692 break;
2693 }
2694
2695 case Primitive::kPrimFloat: {
2696 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2697 break;
2698 }
2699
2700 case Primitive::kPrimDouble: {
2701 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2702 break;
2703 }
2704
2705 case Primitive::kPrimVoid:
2706 LOG(FATAL) << "Unreachable type " << field_type;
2707 UNREACHABLE();
2708 }
2709
Calin Juravle77520bc2015-01-12 18:45:46 +00002710 codegen_->MaybeRecordImplicitNullCheck(instruction);
2711
Calin Juravle52c48962014-12-16 17:02:57 +00002712 if (is_volatile) {
2713 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2714 }
2715}
2716
2717void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2718 const FieldInfo& field_info) {
2719 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2720
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002723 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002724 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2725
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002726 locations->SetInAt(0, Location::RequiresRegister());
2727 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002728 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002729 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002730 locations->AddTemp(Location::RequiresRegister());
2731 locations->AddTemp(Location::RequiresRegister());
2732 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002733}
2734
Calin Juravle52c48962014-12-16 17:02:57 +00002735void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2736 const FieldInfo& field_info) {
2737 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2738
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002739 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002740 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2741 Location value = locations->InAt(1);
2742 bool is_volatile = field_info.IsVolatile();
2743 Primitive::Type field_type = field_info.GetFieldType();
2744 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2745
2746 if (is_volatile) {
2747 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2748 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002749
2750 switch (field_type) {
2751 case Primitive::kPrimBoolean:
2752 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002753 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002754 break;
2755 }
2756
2757 case Primitive::kPrimShort:
2758 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002759 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002760 break;
2761 }
2762
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002763 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002764 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002765 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002766 break;
2767 }
2768
2769 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002770 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002771 break;
2772 }
2773
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002774 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002775 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002776 break;
2777 }
2778
2779 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002780 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002781 break;
2782 }
2783
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002784 case Primitive::kPrimVoid:
2785 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002786 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002787 }
Calin Juravle52c48962014-12-16 17:02:57 +00002788
Calin Juravle77520bc2015-01-12 18:45:46 +00002789 codegen_->MaybeRecordImplicitNullCheck(instruction);
2790
2791 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2792 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2793 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2794 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2795 }
2796
Calin Juravle52c48962014-12-16 17:02:57 +00002797 if (is_volatile) {
2798 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2799 }
2800}
2801
2802void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2803 HandleFieldSet(instruction, instruction->GetFieldInfo());
2804}
2805
2806void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2807 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002808}
2809
2810void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002811 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002812}
2813
2814void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002815 HandleFieldGet(instruction, instruction->GetFieldInfo());
2816}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002817
Calin Juravle52c48962014-12-16 17:02:57 +00002818void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2819 HandleFieldGet(instruction);
2820}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002821
Calin Juravle52c48962014-12-16 17:02:57 +00002822void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2823 HandleFieldGet(instruction, instruction->GetFieldInfo());
2824}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002825
Calin Juravle52c48962014-12-16 17:02:57 +00002826void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2827 HandleFieldSet(instruction, instruction->GetFieldInfo());
2828}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002829
Calin Juravle52c48962014-12-16 17:02:57 +00002830void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2831 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002832}
2833
2834void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002835 LocationSummary* locations =
2836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002837 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2838 ? Location::RequiresRegister()
2839 : Location::Any();
2840 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002841 if (instruction->HasUses()) {
2842 locations->SetOut(Location::SameAsFirstInput());
2843 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002844}
2845
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002846void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002847 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2848 return;
2849 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002850 LocationSummary* locations = instruction->GetLocations();
2851 Location obj = locations->InAt(0);
2852
2853 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2854 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2855}
2856
2857void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002858 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002859 codegen_->AddSlowPath(slow_path);
2860
2861 LocationSummary* locations = instruction->GetLocations();
2862 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002863
2864 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002865 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002866 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002867 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002868 } else {
2869 DCHECK(obj.IsConstant()) << obj;
2870 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2871 __ jmp(slow_path->GetEntryLabel());
2872 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002873 }
2874 __ j(kEqual, slow_path->GetEntryLabel());
2875}
2876
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002877void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2878 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2879 GenerateImplicitNullCheck(instruction);
2880 } else {
2881 GenerateExplicitNullCheck(instruction);
2882 }
2883}
2884
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002885void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002886 LocationSummary* locations =
2887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002888 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002889 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002890 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2891 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002892}
2893
2894void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2895 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002896 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002897 Location index = locations->InAt(1);
2898
2899 switch (instruction->GetType()) {
2900 case Primitive::kPrimBoolean: {
2901 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002902 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002903 if (index.IsConstant()) {
2904 __ movzxb(out, Address(obj,
2905 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2906 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002908 }
2909 break;
2910 }
2911
2912 case Primitive::kPrimByte: {
2913 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002914 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002915 if (index.IsConstant()) {
2916 __ movsxb(out, Address(obj,
2917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2918 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002920 }
2921 break;
2922 }
2923
2924 case Primitive::kPrimShort: {
2925 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927 if (index.IsConstant()) {
2928 __ movsxw(out, Address(obj,
2929 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2930 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002931 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002932 }
2933 break;
2934 }
2935
2936 case Primitive::kPrimChar: {
2937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002938 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002939 if (index.IsConstant()) {
2940 __ movzxw(out, Address(obj,
2941 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2942 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002943 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002944 }
2945 break;
2946 }
2947
2948 case Primitive::kPrimInt:
2949 case Primitive::kPrimNot: {
2950 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2951 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002952 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002953 if (index.IsConstant()) {
2954 __ movl(out, Address(obj,
2955 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2956 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002958 }
2959 break;
2960 }
2961
2962 case Primitive::kPrimLong: {
2963 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002964 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002965 if (index.IsConstant()) {
2966 __ movq(out, Address(obj,
2967 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2968 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002970 }
2971 break;
2972 }
2973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002974 case Primitive::kPrimFloat: {
2975 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002976 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002977 if (index.IsConstant()) {
2978 __ movss(out, Address(obj,
2979 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2980 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002982 }
2983 break;
2984 }
2985
2986 case Primitive::kPrimDouble: {
2987 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002989 if (index.IsConstant()) {
2990 __ movsd(out, Address(obj,
2991 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2992 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002993 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002994 }
2995 break;
2996 }
2997
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002998 case Primitive::kPrimVoid:
2999 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003000 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003002 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003003}
3004
3005void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003006 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003007
3008 bool needs_write_barrier =
3009 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3010 bool needs_runtime_call = instruction->NeedsTypeCheck();
3011
Nicolas Geoffray39468442014-09-02 15:17:15 +01003012 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003013 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3014 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003015 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3017 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3018 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003020 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003021 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003022 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3023 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003024 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003025 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003026 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3027 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003028 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003029 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003030 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003031
3032 if (needs_write_barrier) {
3033 // Temporary registers for the write barrier.
3034 locations->AddTemp(Location::RequiresRegister());
3035 locations->AddTemp(Location::RequiresRegister());
3036 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003037 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038}
3039
3040void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3041 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003043 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003044 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003045 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003046 bool needs_runtime_call = locations->WillCall();
3047 bool needs_write_barrier =
3048 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003049
3050 switch (value_type) {
3051 case Primitive::kPrimBoolean:
3052 case Primitive::kPrimByte: {
3053 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054 if (index.IsConstant()) {
3055 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003056 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003058 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003059 __ movb(Address(obj, offset),
3060 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003061 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003062 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003063 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3065 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003066 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003067 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003068 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3069 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003071 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003072 break;
3073 }
3074
3075 case Primitive::kPrimShort:
3076 case Primitive::kPrimChar: {
3077 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078 if (index.IsConstant()) {
3079 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003080 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003081 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003082 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003083 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003084 __ movw(Address(obj, offset),
3085 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003086 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003087 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003088 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003089 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3091 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003092 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003093 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003095 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3096 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003098 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003099 break;
3100 }
3101
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003102 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003103 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003104 if (!needs_runtime_call) {
3105 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3106 if (index.IsConstant()) {
3107 size_t offset =
3108 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3109 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003110 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003111 } else {
3112 DCHECK(value.IsConstant()) << value;
3113 __ movl(Address(obj, offset),
3114 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3115 }
3116 } else {
3117 DCHECK(index.IsRegister()) << index;
3118 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3120 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003121 } else {
3122 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003124 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3125 }
3126 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003127 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003128 if (needs_write_barrier) {
3129 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003130 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3131 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3132 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003133 }
3134 } else {
3135 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003136 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3137 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003138 DCHECK(!codegen_->IsLeafMethod());
3139 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3140 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003141 break;
3142 }
3143
3144 case Primitive::kPrimLong: {
3145 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 if (index.IsConstant()) {
3147 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003148 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003149 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003150 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003151 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003152 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3153 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003154 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003156 break;
3157 }
3158
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003159 case Primitive::kPrimFloat: {
3160 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3161 if (index.IsConstant()) {
3162 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3163 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003164 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003165 } else {
3166 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003167 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3168 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003169 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003170 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003171 break;
3172 }
3173
3174 case Primitive::kPrimDouble: {
3175 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3176 if (index.IsConstant()) {
3177 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3178 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003179 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003180 } else {
3181 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3183 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003184 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003185 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003186 break;
3187 }
3188
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003189 case Primitive::kPrimVoid:
3190 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003191 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003192 }
3193}
3194
3195void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003196 LocationSummary* locations =
3197 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003198 locations->SetInAt(0, Location::RequiresRegister());
3199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003200}
3201
3202void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3203 LocationSummary* locations = instruction->GetLocations();
3204 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003205 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3206 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003207 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003208 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209}
3210
3211void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003212 LocationSummary* locations =
3213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003214 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003216 if (instruction->HasUses()) {
3217 locations->SetOut(Location::SameAsFirstInput());
3218 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003219}
3220
3221void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3222 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003223 Location index_loc = locations->InAt(0);
3224 Location length_loc = locations->InAt(1);
3225 SlowPathCodeX86_64* slow_path =
3226 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003227 codegen_->AddSlowPath(slow_path);
3228
Mark Mendellf60c90b2015-03-04 15:12:59 -05003229 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3230 if (index_loc.IsConstant()) {
3231 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3232 __ cmpl(length, Immediate(value));
3233 } else {
3234 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3235 }
3236 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003237}
3238
3239void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3240 CpuRegister card,
3241 CpuRegister object,
3242 CpuRegister value) {
3243 Label is_null;
3244 __ testl(value, value);
3245 __ j(kEqual, &is_null);
3246 __ gs()->movq(card, Address::Absolute(
3247 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3248 __ movq(temp, object);
3249 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3250 __ movb(Address(temp, card, TIMES_1, 0), card);
3251 __ Bind(&is_null);
3252}
3253
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003254void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3255 temp->SetLocations(nullptr);
3256}
3257
3258void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3259 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003260 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003261}
3262
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003263void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003264 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003265 LOG(FATAL) << "Unimplemented";
3266}
3267
3268void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003269 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3270}
3271
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003272void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3273 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3274}
3275
3276void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003277 HBasicBlock* block = instruction->GetBlock();
3278 if (block->GetLoopInformation() != nullptr) {
3279 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3280 // The back edge will generate the suspend check.
3281 return;
3282 }
3283 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3284 // The goto will generate the suspend check.
3285 return;
3286 }
3287 GenerateSuspendCheck(instruction, nullptr);
3288}
3289
3290void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3291 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003292 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003293 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003294 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003295 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003296 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003297 if (successor == nullptr) {
3298 __ j(kNotEqual, slow_path->GetEntryLabel());
3299 __ Bind(slow_path->GetReturnLabel());
3300 } else {
3301 __ j(kEqual, codegen_->GetLabelOf(successor));
3302 __ jmp(slow_path->GetEntryLabel());
3303 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003304}
3305
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003306X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3307 return codegen_->GetAssembler();
3308}
3309
3310void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3311 MoveOperands* move = moves_.Get(index);
3312 Location source = move->GetSource();
3313 Location destination = move->GetDestination();
3314
3315 if (source.IsRegister()) {
3316 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003317 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003318 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003319 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003320 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003321 } else {
3322 DCHECK(destination.IsDoubleStackSlot());
3323 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003325 }
3326 } else if (source.IsStackSlot()) {
3327 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003328 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003329 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003330 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003332 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003333 } else {
3334 DCHECK(destination.IsStackSlot());
3335 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3336 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3337 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003338 } else if (source.IsDoubleStackSlot()) {
3339 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003340 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003341 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003342 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003343 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3344 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003345 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003346 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003347 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3348 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3349 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003350 } else if (source.IsConstant()) {
3351 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003352 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3353 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003354 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003355 if (value == 0) {
3356 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3357 } else {
3358 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3359 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003360 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003361 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003362 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003363 }
3364 } else if (constant->IsLongConstant()) {
3365 int64_t value = constant->AsLongConstant()->GetValue();
3366 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003367 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003368 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003369 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003370 __ movq(CpuRegister(TMP), Immediate(value));
3371 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3372 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003373 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003374 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003375 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003376 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003377 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003378 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3379 if (value == 0) {
3380 // easy FP 0.0.
3381 __ xorps(dest, dest);
3382 } else {
3383 __ movl(CpuRegister(TMP), imm);
3384 __ movd(dest, CpuRegister(TMP));
3385 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003386 } else {
3387 DCHECK(destination.IsStackSlot()) << destination;
3388 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3389 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003390 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003391 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003392 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003393 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003394 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003395 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003396 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3397 if (value == 0) {
3398 __ xorpd(dest, dest);
3399 } else {
3400 __ movq(CpuRegister(TMP), imm);
3401 __ movd(dest, CpuRegister(TMP));
3402 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003403 } else {
3404 DCHECK(destination.IsDoubleStackSlot()) << destination;
3405 __ movq(CpuRegister(TMP), imm);
3406 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3407 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003408 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003409 } else if (source.IsFpuRegister()) {
3410 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003411 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003412 } else if (destination.IsStackSlot()) {
3413 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003414 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003415 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003416 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003417 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003418 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003419 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003420 }
3421}
3422
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003423void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003424 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003425 __ movl(Address(CpuRegister(RSP), mem), reg);
3426 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003427}
3428
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003429void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003430 ScratchRegisterScope ensure_scratch(
3431 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3432
3433 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3434 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3435 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3436 Address(CpuRegister(RSP), mem2 + stack_offset));
3437 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3438 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3439 CpuRegister(ensure_scratch.GetRegister()));
3440}
3441
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003442void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3443 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3444 __ movq(Address(CpuRegister(RSP), mem), reg);
3445 __ movq(reg, CpuRegister(TMP));
3446}
3447
3448void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3449 ScratchRegisterScope ensure_scratch(
3450 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3451
3452 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3453 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3454 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3455 Address(CpuRegister(RSP), mem2 + stack_offset));
3456 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3457 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3458 CpuRegister(ensure_scratch.GetRegister()));
3459}
3460
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003461void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3462 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3463 __ movss(Address(CpuRegister(RSP), mem), reg);
3464 __ movd(reg, CpuRegister(TMP));
3465}
3466
3467void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3468 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3469 __ movsd(Address(CpuRegister(RSP), mem), reg);
3470 __ movd(reg, CpuRegister(TMP));
3471}
3472
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003473void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3474 MoveOperands* move = moves_.Get(index);
3475 Location source = move->GetSource();
3476 Location destination = move->GetDestination();
3477
3478 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003480 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003482 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003483 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003484 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003485 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3486 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003488 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003490 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3491 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003492 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003493 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3494 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3495 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003496 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003497 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003498 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003500 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003501 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003502 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003504 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003505 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003506 }
3507}
3508
3509
3510void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3511 __ pushq(CpuRegister(reg));
3512}
3513
3514
3515void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3516 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003517}
3518
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003519void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3520 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3521 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3522 Immediate(mirror::Class::kStatusInitialized));
3523 __ j(kLess, slow_path->GetEntryLabel());
3524 __ Bind(slow_path->GetExitLabel());
3525 // No need for memory fence, thanks to the X86_64 memory model.
3526}
3527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003528void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003529 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3530 ? LocationSummary::kCallOnSlowPath
3531 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003532 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003533 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003534 locations->SetOut(Location::RequiresRegister());
3535}
3536
3537void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003538 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003539 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003540 DCHECK(!cls->CanCallRuntime());
3541 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003542 codegen_->LoadCurrentMethod(out);
3543 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3544 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003545 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003546 codegen_->LoadCurrentMethod(out);
3547 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3548 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003549 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3550 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3551 codegen_->AddSlowPath(slow_path);
3552 __ testl(out, out);
3553 __ j(kEqual, slow_path->GetEntryLabel());
3554 if (cls->MustGenerateClinitCheck()) {
3555 GenerateClassInitializationCheck(slow_path, out);
3556 } else {
3557 __ Bind(slow_path->GetExitLabel());
3558 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003559 }
3560}
3561
3562void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3563 LocationSummary* locations =
3564 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3565 locations->SetInAt(0, Location::RequiresRegister());
3566 if (check->HasUses()) {
3567 locations->SetOut(Location::SameAsFirstInput());
3568 }
3569}
3570
3571void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003572 // We assume the class to not be null.
3573 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3574 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003575 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003576 GenerateClassInitializationCheck(slow_path,
3577 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003578}
3579
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003580void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3581 LocationSummary* locations =
3582 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3583 locations->SetOut(Location::RequiresRegister());
3584}
3585
3586void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3587 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3588 codegen_->AddSlowPath(slow_path);
3589
Roland Levillain271ab9c2014-11-27 15:23:57 +00003590 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003591 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003592 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3593 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003594 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3595 __ testl(out, out);
3596 __ j(kEqual, slow_path->GetEntryLabel());
3597 __ Bind(slow_path->GetExitLabel());
3598}
3599
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003600void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3601 LocationSummary* locations =
3602 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3603 locations->SetOut(Location::RequiresRegister());
3604}
3605
3606void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3607 Address address = Address::Absolute(
3608 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003610 __ gs()->movl(address, Immediate(0));
3611}
3612
3613void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3614 LocationSummary* locations =
3615 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3616 InvokeRuntimeCallingConvention calling_convention;
3617 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3618}
3619
3620void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3621 __ gs()->call(
3622 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3623 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3624}
3625
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003626void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003627 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3628 ? LocationSummary::kNoCall
3629 : LocationSummary::kCallOnSlowPath;
3630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3631 locations->SetInAt(0, Location::RequiresRegister());
3632 locations->SetInAt(1, Location::Any());
3633 locations->SetOut(Location::RequiresRegister());
3634}
3635
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003636void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003637 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003638 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003639 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003641 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3642 Label done, zero;
3643 SlowPathCodeX86_64* slow_path = nullptr;
3644
3645 // Return 0 if `obj` is null.
3646 // TODO: avoid this check if we know obj is not null.
3647 __ testl(obj, obj);
3648 __ j(kEqual, &zero);
3649 // Compare the class of `obj` with `cls`.
3650 __ movl(out, Address(obj, class_offset));
3651 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003653 } else {
3654 DCHECK(cls.IsStackSlot()) << cls;
3655 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3656 }
3657 if (instruction->IsClassFinal()) {
3658 // Classes must be equal for the instanceof to succeed.
3659 __ j(kNotEqual, &zero);
3660 __ movl(out, Immediate(1));
3661 __ jmp(&done);
3662 } else {
3663 // If the classes are not equal, we go into a slow path.
3664 DCHECK(locations->OnlyCallsOnSlowPath());
3665 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003666 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003667 codegen_->AddSlowPath(slow_path);
3668 __ j(kNotEqual, slow_path->GetEntryLabel());
3669 __ movl(out, Immediate(1));
3670 __ jmp(&done);
3671 }
3672 __ Bind(&zero);
3673 __ movl(out, Immediate(0));
3674 if (slow_path != nullptr) {
3675 __ Bind(slow_path->GetExitLabel());
3676 }
3677 __ Bind(&done);
3678}
3679
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003680void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3681 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3682 instruction, LocationSummary::kCallOnSlowPath);
3683 locations->SetInAt(0, Location::RequiresRegister());
3684 locations->SetInAt(1, Location::Any());
3685 locations->AddTemp(Location::RequiresRegister());
3686}
3687
3688void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3689 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003690 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003691 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003693 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3694 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3695 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3696 codegen_->AddSlowPath(slow_path);
3697
3698 // TODO: avoid this check if we know obj is not null.
3699 __ testl(obj, obj);
3700 __ j(kEqual, slow_path->GetExitLabel());
3701 // Compare the class of `obj` with `cls`.
3702 __ movl(temp, Address(obj, class_offset));
3703 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003705 } else {
3706 DCHECK(cls.IsStackSlot()) << cls;
3707 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3708 }
3709 // Classes must be equal for the checkcast to succeed.
3710 __ j(kNotEqual, slow_path->GetEntryLabel());
3711 __ Bind(slow_path->GetExitLabel());
3712}
3713
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003714void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3715 LocationSummary* locations =
3716 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3717 InvokeRuntimeCallingConvention calling_convention;
3718 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3719}
3720
3721void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3722 __ gs()->call(Address::Absolute(instruction->IsEnter()
3723 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3724 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3725 true));
3726 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3727}
3728
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003729void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3730void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3731void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3732
3733void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3734 LocationSummary* locations =
3735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3736 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3737 || instruction->GetResultType() == Primitive::kPrimLong);
3738 locations->SetInAt(0, Location::RequiresRegister());
3739 if (instruction->GetType() == Primitive::kPrimInt) {
3740 locations->SetInAt(1, Location::Any());
3741 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003742 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003743 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003744 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003745 }
3746 locations->SetOut(Location::SameAsFirstInput());
3747}
3748
3749void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3750 HandleBitwiseOperation(instruction);
3751}
3752
3753void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3754 HandleBitwiseOperation(instruction);
3755}
3756
3757void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3758 HandleBitwiseOperation(instruction);
3759}
3760
3761void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3762 LocationSummary* locations = instruction->GetLocations();
3763 Location first = locations->InAt(0);
3764 Location second = locations->InAt(1);
3765 DCHECK(first.Equals(locations->Out()));
3766
3767 if (instruction->GetResultType() == Primitive::kPrimInt) {
3768 if (second.IsRegister()) {
3769 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003770 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003771 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003773 } else {
3774 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003776 }
3777 } else if (second.IsConstant()) {
3778 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3779 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003781 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003783 } else {
3784 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003785 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003786 }
3787 } else {
3788 Address address(CpuRegister(RSP), second.GetStackIndex());
3789 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003791 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003792 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003793 } else {
3794 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003795 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003796 }
3797 }
3798 } else {
3799 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003800 CpuRegister first_reg = first.AsRegister<CpuRegister>();
3801 bool second_is_constant = false;
3802 int64_t value = 0;
3803 if (second.IsConstant()) {
3804 second_is_constant = true;
3805 value = second.GetConstant()->AsLongConstant()->GetValue();
3806 DCHECK(IsInt<32>(value));
3807 }
3808
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003809 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003810 if (second_is_constant) {
3811 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
3812 } else {
3813 __ andq(first_reg, second.AsRegister<CpuRegister>());
3814 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003815 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003816 if (second_is_constant) {
3817 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
3818 } else {
3819 __ orq(first_reg, second.AsRegister<CpuRegister>());
3820 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003821 } else {
3822 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003823 if (second_is_constant) {
3824 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
3825 } else {
3826 __ xorq(first_reg, second.AsRegister<CpuRegister>());
3827 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003828 }
3829 }
3830}
3831
Calin Juravleb1498f62015-02-16 13:13:29 +00003832void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
3833 // Nothing to do, this should be removed during prepare for register allocator.
3834 UNUSED(instruction);
3835 LOG(FATAL) << "Unreachable";
3836}
3837
3838void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
3839 // Nothing to do, this should be removed during prepare for register allocator.
3840 UNUSED(instruction);
3841 LOG(FATAL) << "Unreachable";
3842}
3843
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003844} // namespace x86_64
3845} // namespace art