blob: 07ba95dcfbc55355c8d739d7430dd6e481343b63 [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 Geoffray39468442014-09-02 15:17:15 +010075 codegen->RecordPcInfo(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));
91 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
92 }
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 Geoffray3bca0df2014-09-19 11:01:00 +0100139 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
141 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100142 codegen->RestoreLiveRegisters(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 Geoffray39468442014-09-02 15:17:15 +0100184 codegen->RecordPcInfo(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 Geoffray424f6762014-11-03 14:51:25 +0000210 codegen->SaveLiveRegisters(locations);
211
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));
218 codegen->RecordPcInfo(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
227 codegen->RestoreLiveRegisters(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());
258 codegen->SaveLiveRegisters(locations);
259
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));
266 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
267 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
268 codegen->RestoreLiveRegisters(locations);
269 __ 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());
296 codegen->SaveLiveRegisters(locations);
297
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 }
315 codegen->RecordPcInfo(instruction_, dex_pc_);
316
317 if (instruction_->IsInstanceOf()) {
318 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
319 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 codegen->RestoreLiveRegisters(locations);
322 __ 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());
377 RecordPcInfo(invoke, invoke->GetDexPc());
378}
379
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100380void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
381 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
382}
383
384void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
385 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
386}
387
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100388size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
389 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
390 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100391}
392
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100393size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
394 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
395 return kX86_64WordSize;
396}
397
398size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
399 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
400 return kX86_64WordSize;
401}
402
403size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
404 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
405 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100406}
407
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000408static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000409// Use a fake return address register to mimic Quick.
410static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000411CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000412 : CodeGenerator(graph,
413 kNumberOfCpuRegisters,
414 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000415 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000416 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
417 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000418 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000419 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
420 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000421 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100422 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000424 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000425 move_resolver_(graph->GetArena(), this) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000426 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
427}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100428
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100429InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
430 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100431 : HGraphVisitor(graph),
432 assembler_(codegen->GetAssembler()),
433 codegen_(codegen) {}
434
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436 switch (type) {
437 case Primitive::kPrimLong:
438 case Primitive::kPrimByte:
439 case Primitive::kPrimBoolean:
440 case Primitive::kPrimChar:
441 case Primitive::kPrimShort:
442 case Primitive::kPrimInt:
443 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100444 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100445 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100446 }
447
448 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100449 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100451 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100452 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453
454 case Primitive::kPrimVoid:
455 LOG(FATAL) << "Unreachable type " << type;
456 }
457
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100458 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459}
460
Nicolas Geoffray98893962015-01-21 12:32:32 +0000461void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100462 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100463 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100464
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000465 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100466 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000467
Nicolas Geoffray98893962015-01-21 12:32:32 +0000468 if (is_baseline) {
469 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
470 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
471 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000472 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
473 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
474 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000475 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100476}
477
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478void CodeGeneratorX86_64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000479 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100480 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700481 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000482 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100483
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000484 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100485 __ testq(CpuRegister(RAX), Address(
486 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100487 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100488 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000489
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000490 if (HasEmptyFrame()) {
491 return;
492 }
493
Nicolas Geoffray98893962015-01-21 12:32:32 +0000494 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000495 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000496 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000497 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000498 }
499 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100500
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
502 uint32_t xmm_spill_location = GetFpuSpillStart();
503 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100504
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000505 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
506 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
507 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
508 XmmRegister(kFpuCalleeSaves[i]));
509 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100510 }
511
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
513}
514
515void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000516 if (HasEmptyFrame()) {
517 return;
518 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000519 uint32_t xmm_spill_location = GetFpuSpillStart();
520 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
521 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
522 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
523 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
524 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
525 }
526 }
527
528 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000529
530 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000532 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000534 }
535 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536}
537
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100538void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
539 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100540}
541
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100542void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000543 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
545}
546
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
548 switch (load->GetType()) {
549 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
552 break;
553
554 case Primitive::kPrimInt:
555 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100556 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558
559 case Primitive::kPrimBoolean:
560 case Primitive::kPrimByte:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimShort:
563 case Primitive::kPrimVoid:
564 LOG(FATAL) << "Unexpected type " << load->GetType();
565 }
566
567 LOG(FATAL) << "Unreachable";
568 return Location();
569}
570
571void CodeGeneratorX86_64::Move(Location destination, Location source) {
572 if (source.Equals(destination)) {
573 return;
574 }
575 if (destination.IsRegister()) {
576 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 } else {
584 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 Address(CpuRegister(RSP), source.GetStackIndex()));
587 }
588 } else if (destination.IsFpuRegister()) {
589 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000592 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000594 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100595 Address(CpuRegister(RSP), source.GetStackIndex()));
596 } else {
597 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000598 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 }
601 } else if (destination.IsStackSlot()) {
602 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000604 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 } else if (source.IsFpuRegister()) {
606 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500608 } else if (source.IsConstant()) {
609 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000610 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500611 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100612 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500613 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000614 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
615 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100616 }
617 } else {
618 DCHECK(destination.IsDoubleStackSlot());
619 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (source.IsFpuRegister()) {
623 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500625 } else if (source.IsConstant()) {
626 HConstant* constant = source.GetConstant();
627 int64_t value = constant->AsLongConstant()->GetValue();
628 if (constant->IsDoubleConstant()) {
629 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
630 } else {
631 DCHECK(constant->IsLongConstant());
632 value = constant->AsLongConstant()->GetValue();
633 }
634 __ movq(CpuRegister(TMP), Immediate(value));
635 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636 } else {
637 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000638 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
639 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100640 }
641 }
642}
643
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100644void CodeGeneratorX86_64::Move(HInstruction* instruction,
645 Location location,
646 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000647 LocationSummary* locations = instruction->GetLocations();
648 if (locations != nullptr && locations->Out().Equals(location)) {
649 return;
650 }
651
652 if (locations != nullptr && locations->Out().IsConstant()) {
653 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000654 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
655 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000656 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000657 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000658 } else if (location.IsStackSlot()) {
659 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
660 } else {
661 DCHECK(location.IsConstant());
662 DCHECK_EQ(location.GetConstant(), const_to_move);
663 }
664 } else if (const_to_move->IsLongConstant()) {
665 int64_t value = const_to_move->AsLongConstant()->GetValue();
666 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000667 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000668 } else if (location.IsDoubleStackSlot()) {
669 __ movq(CpuRegister(TMP), Immediate(value));
670 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
671 } else {
672 DCHECK(location.IsConstant());
673 DCHECK_EQ(location.GetConstant(), const_to_move);
674 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675 }
Roland Levillain476df552014-10-09 17:51:36 +0100676 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100677 switch (instruction->GetType()) {
678 case Primitive::kPrimBoolean:
679 case Primitive::kPrimByte:
680 case Primitive::kPrimChar:
681 case Primitive::kPrimShort:
682 case Primitive::kPrimInt:
683 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100684 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
686 break;
687
688 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100689 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000690 Move(location,
691 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100692 break;
693
694 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100696 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000697 } else if (instruction->IsTemporary()) {
698 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
699 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100700 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100701 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702 switch (instruction->GetType()) {
703 case Primitive::kPrimBoolean:
704 case Primitive::kPrimByte:
705 case Primitive::kPrimChar:
706 case Primitive::kPrimShort:
707 case Primitive::kPrimInt:
708 case Primitive::kPrimNot:
709 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 case Primitive::kPrimFloat:
711 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000712 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100713 break;
714
715 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 }
718 }
719}
720
721void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
722 got->SetLocations(nullptr);
723}
724
725void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
726 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100727 DCHECK(!successor->IsExitBlock());
728
729 HBasicBlock* block = got->GetBlock();
730 HInstruction* previous = got->GetPrevious();
731
732 HLoopInformation* info = block->GetLoopInformation();
733 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
734 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
735 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
736 return;
737 }
738
739 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
740 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
741 }
742 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743 __ jmp(codegen_->GetLabelOf(successor));
744 }
745}
746
747void LocationsBuilderX86_64::VisitExit(HExit* exit) {
748 exit->SetLocations(nullptr);
749}
750
751void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700752 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100753 if (kIsDebugBuild) {
754 __ Comment("Unreachable");
755 __ int3();
756 }
757}
758
759void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100760 LocationSummary* locations =
761 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100762 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100763 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100764 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100765 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100766}
767
768void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700769 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100770 if (cond->IsIntConstant()) {
771 // Constant condition, statically compared against 1.
772 int32_t cond_value = cond->AsIntConstant()->GetValue();
773 if (cond_value == 1) {
774 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
775 if_instr->IfTrueSuccessor())) {
776 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100777 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100778 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100780 DCHECK_EQ(cond_value, 0);
781 }
782 } else {
783 bool materialized =
784 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
785 // Moves do not affect the eflags register, so if the condition is
786 // evaluated just before the if, we don't need to evaluate it
787 // again.
788 bool eflags_set = cond->IsCondition()
789 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
790 if (materialized) {
791 if (!eflags_set) {
792 // Materialized condition, compare against 0.
793 Location lhs = if_instr->GetLocations()->InAt(0);
794 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000795 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 } else {
797 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
798 Immediate(0));
799 }
800 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
801 } else {
802 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
803 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
804 }
805 } else {
806 Location lhs = cond->GetLocations()->InAt(0);
807 Location rhs = cond->GetLocations()->InAt(1);
808 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000809 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100810 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000811 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000812 if (constant == 0) {
813 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
814 } else {
815 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
816 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000818 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 Address(CpuRegister(RSP), rhs.GetStackIndex()));
820 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100821 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
822 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700823 }
Dave Allison20dfc792014-06-16 20:44:29 -0700824 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
826 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700827 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100828 }
829}
830
831void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
832 local->SetLocations(nullptr);
833}
834
835void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
836 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
837}
838
839void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
840 local->SetLocations(nullptr);
841}
842
843void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
844 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700845 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846}
847
848void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100849 LocationSummary* locations =
850 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851 switch (store->InputAt(1)->GetType()) {
852 case Primitive::kPrimBoolean:
853 case Primitive::kPrimByte:
854 case Primitive::kPrimChar:
855 case Primitive::kPrimShort:
856 case Primitive::kPrimInt:
857 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
860 break;
861
862 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100864 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
865 break;
866
867 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100870}
871
872void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700873 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100874}
875
Dave Allison20dfc792014-06-16 20:44:29 -0700876void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100877 LocationSummary* locations =
878 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100879 locations->SetInAt(0, Location::RequiresRegister());
880 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100881 if (comp->NeedsMaterialization()) {
882 locations->SetOut(Location::RequiresRegister());
883 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100884}
885
Dave Allison20dfc792014-06-16 20:44:29 -0700886void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
887 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100888 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000889 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100890 // Clear register: setcc only sets the low byte.
891 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000892 Location lhs = locations->InAt(0);
893 Location rhs = locations->InAt(1);
894 if (rhs.IsRegister()) {
895 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
896 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800897 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000898 if (constant == 0) {
899 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
900 } else {
901 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
902 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100903 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000904 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100905 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100906 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700907 }
908}
909
910void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
911 VisitCondition(comp);
912}
913
914void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
915 VisitCondition(comp);
916}
917
918void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
919 VisitCondition(comp);
920}
921
922void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
923 VisitCondition(comp);
924}
925
926void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
927 VisitCondition(comp);
928}
929
930void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
931 VisitCondition(comp);
932}
933
934void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
935 VisitCondition(comp);
936}
937
938void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
939 VisitCondition(comp);
940}
941
942void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
943 VisitCondition(comp);
944}
945
946void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
947 VisitCondition(comp);
948}
949
950void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
951 VisitCondition(comp);
952}
953
954void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
955 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100956}
957
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100958void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100959 LocationSummary* locations =
960 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000961 switch (compare->InputAt(0)->GetType()) {
962 case Primitive::kPrimLong: {
963 locations->SetInAt(0, Location::RequiresRegister());
964 locations->SetInAt(1, Location::RequiresRegister());
965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
966 break;
967 }
968 case Primitive::kPrimFloat:
969 case Primitive::kPrimDouble: {
970 locations->SetInAt(0, Location::RequiresFpuRegister());
971 locations->SetInAt(1, Location::RequiresFpuRegister());
972 locations->SetOut(Location::RequiresRegister());
973 break;
974 }
975 default:
976 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
977 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100978}
979
980void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100981 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000982 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000983 Location left = locations->InAt(0);
984 Location right = locations->InAt(1);
985
986 Label less, greater, done;
987 Primitive::Type type = compare->InputAt(0)->GetType();
988 switch (type) {
989 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000990 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100991 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000992 }
993 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000994 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000995 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
996 break;
997 }
998 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000999 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001000 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1001 break;
1002 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001003 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001004 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001005 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001006 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001007 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001008 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001009
Calin Juravle91debbc2014-11-26 19:01:09 +00001010 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001011 __ movl(out, Immediate(1));
1012 __ jmp(&done);
1013
1014 __ Bind(&less);
1015 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001016
1017 __ Bind(&done);
1018}
1019
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001020void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001021 LocationSummary* locations =
1022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001024}
1025
1026void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001027 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029}
1030
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001031void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1034 locations->SetOut(Location::ConstantLocation(constant));
1035}
1036
1037void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1038 // Will be generated at use site.
1039 UNUSED(constant);
1040}
1041
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001043 LocationSummary* locations =
1044 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001045 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001046}
1047
1048void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001049 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001050 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001051}
1052
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001053void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1054 LocationSummary* locations =
1055 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1056 locations->SetOut(Location::ConstantLocation(constant));
1057}
1058
1059void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1060 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001061 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001062}
1063
1064void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1065 LocationSummary* locations =
1066 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1067 locations->SetOut(Location::ConstantLocation(constant));
1068}
1069
1070void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1071 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001072 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001073}
1074
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1076 ret->SetLocations(nullptr);
1077}
1078
1079void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001080 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081 codegen_->GenerateFrameExit();
1082 __ ret();
1083}
1084
1085void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001086 LocationSummary* locations =
1087 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088 switch (ret->InputAt(0)->GetType()) {
1089 case Primitive::kPrimBoolean:
1090 case Primitive::kPrimByte:
1091 case Primitive::kPrimChar:
1092 case Primitive::kPrimShort:
1093 case Primitive::kPrimInt:
1094 case Primitive::kPrimNot:
1095 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097 break;
1098
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 case Primitive::kPrimFloat:
1100 case Primitive::kPrimDouble:
1101 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 break;
1104
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001107 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108}
1109
1110void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1111 if (kIsDebugBuild) {
1112 switch (ret->InputAt(0)->GetType()) {
1113 case Primitive::kPrimBoolean:
1114 case Primitive::kPrimByte:
1115 case Primitive::kPrimChar:
1116 case Primitive::kPrimShort:
1117 case Primitive::kPrimInt:
1118 case Primitive::kPrimNot:
1119 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001120 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001121 break;
1122
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001123 case Primitive::kPrimFloat:
1124 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001125 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001126 XMM0);
1127 break;
1128
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001130 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131 }
1132 }
1133 codegen_->GenerateFrameExit();
1134 __ ret();
1135}
1136
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1138 switch (type) {
1139 case Primitive::kPrimBoolean:
1140 case Primitive::kPrimByte:
1141 case Primitive::kPrimChar:
1142 case Primitive::kPrimShort:
1143 case Primitive::kPrimInt:
1144 case Primitive::kPrimNot: {
1145 uint32_t index = gp_index_++;
1146 stack_index_++;
1147 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149 } else {
1150 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1151 }
1152 }
1153
1154 case Primitive::kPrimLong: {
1155 uint32_t index = gp_index_;
1156 stack_index_ += 2;
1157 if (index < calling_convention.GetNumberOfRegisters()) {
1158 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160 } else {
1161 gp_index_ += 2;
1162 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1163 }
1164 }
1165
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 case Primitive::kPrimFloat: {
1167 uint32_t index = fp_index_++;
1168 stack_index_++;
1169 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 } else {
1172 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1173 }
1174 }
1175
1176 case Primitive::kPrimDouble: {
1177 uint32_t index = fp_index_++;
1178 stack_index_ += 2;
1179 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001181 } else {
1182 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1183 }
1184 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185
1186 case Primitive::kPrimVoid:
1187 LOG(FATAL) << "Unexpected parameter type " << type;
1188 break;
1189 }
1190 return Location();
1191}
1192
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001193void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001194 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1195 if (intrinsic.TryDispatch(invoke)) {
1196 return;
1197 }
1198
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001199 HandleInvoke(invoke);
1200}
1201
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001202static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1203 if (invoke->GetLocations()->Intrinsified()) {
1204 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1205 intrinsic.Dispatch(invoke);
1206 return true;
1207 }
1208 return false;
1209}
1210
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001211void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001212 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1213 return;
1214 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001215
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001216 codegen_->GenerateStaticOrDirectCall(
1217 invoke,
1218 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001219}
1220
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001221void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001222 LocationSummary* locations =
1223 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001224 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001225
1226 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001227 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 HInstruction* input = invoke->InputAt(i);
1229 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1230 }
1231
1232 switch (invoke->GetType()) {
1233 case Primitive::kPrimBoolean:
1234 case Primitive::kPrimByte:
1235 case Primitive::kPrimChar:
1236 case Primitive::kPrimShort:
1237 case Primitive::kPrimInt:
1238 case Primitive::kPrimNot:
1239 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001240 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 break;
1242
1243 case Primitive::kPrimVoid:
1244 break;
1245
1246 case Primitive::kPrimDouble:
1247 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001248 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 break;
1250 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251}
1252
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001253void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001254 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1255 if (intrinsic.TryDispatch(invoke)) {
1256 return;
1257 }
1258
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001259 HandleInvoke(invoke);
1260}
1261
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001262void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001263 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1264 return;
1265 }
1266
Roland Levillain271ab9c2014-11-27 15:23:57 +00001267 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1269 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1270 LocationSummary* locations = invoke->GetLocations();
1271 Location receiver = locations->InAt(0);
1272 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1273 // temp = object->GetClass();
1274 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001275 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1276 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001280 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001281 // temp = temp->GetMethodAt(method_offset);
1282 __ movl(temp, Address(temp, method_offset));
1283 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001284 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001285 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001286
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001287 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001288 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001289}
1290
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001291void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1292 HandleInvoke(invoke);
1293 // Add the hidden argument.
1294 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1295}
1296
1297void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1298 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001299 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001300 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1301 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1302 LocationSummary* locations = invoke->GetLocations();
1303 Location receiver = locations->InAt(0);
1304 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1305
1306 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001308 Immediate(invoke->GetDexMethodIndex()));
1309
1310 // temp = object->GetClass();
1311 if (receiver.IsStackSlot()) {
1312 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1313 __ movl(temp, Address(temp, class_offset));
1314 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001316 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001317 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001318 // temp = temp->GetImtEntryAt(method_offset);
1319 __ movl(temp, Address(temp, method_offset));
1320 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001321 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001322 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001323
1324 DCHECK(!codegen_->IsLeafMethod());
1325 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1326}
1327
Roland Levillain88cb1752014-10-20 16:36:47 +01001328void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1329 LocationSummary* locations =
1330 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1331 switch (neg->GetResultType()) {
1332 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001333 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001334 locations->SetInAt(0, Location::RequiresRegister());
1335 locations->SetOut(Location::SameAsFirstInput());
1336 break;
1337
Roland Levillain88cb1752014-10-20 16:36:47 +01001338 case Primitive::kPrimFloat:
1339 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001340 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001341 locations->SetOut(Location::SameAsFirstInput());
1342 locations->AddTemp(Location::RequiresRegister());
1343 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 break;
1345
1346 default:
1347 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1348 }
1349}
1350
1351void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1352 LocationSummary* locations = neg->GetLocations();
1353 Location out = locations->Out();
1354 Location in = locations->InAt(0);
1355 switch (neg->GetResultType()) {
1356 case Primitive::kPrimInt:
1357 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001358 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001359 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001360 break;
1361
1362 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001363 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001364 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001366 break;
1367
Roland Levillain5368c212014-11-27 15:03:41 +00001368 case Primitive::kPrimFloat: {
1369 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1371 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001372 // Implement float negation with an exclusive or with value
1373 // 0x80000000 (mask for bit 31, representing the sign of a
1374 // single-precision floating-point number).
1375 __ movq(constant, Immediate(INT64_C(0x80000000)));
1376 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001378 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001379 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380
Roland Levillain5368c212014-11-27 15:03:41 +00001381 case Primitive::kPrimDouble: {
1382 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001383 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1384 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001385 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001386 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001387 // a double-precision floating-point number).
1388 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1389 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001391 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001392 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001393
1394 default:
1395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1396 }
1397}
1398
Roland Levillaindff1f282014-11-05 14:15:05 +00001399void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1400 LocationSummary* locations =
1401 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1402 Primitive::Type result_type = conversion->GetResultType();
1403 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001404 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001405 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001406 case Primitive::kPrimByte:
1407 switch (input_type) {
1408 case Primitive::kPrimShort:
1409 case Primitive::kPrimInt:
1410 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001411 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001412 locations->SetInAt(0, Location::Any());
1413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1414 break;
1415
1416 default:
1417 LOG(FATAL) << "Unexpected type conversion from " << input_type
1418 << " to " << result_type;
1419 }
1420 break;
1421
Roland Levillain01a8d712014-11-14 16:27:39 +00001422 case Primitive::kPrimShort:
1423 switch (input_type) {
1424 case Primitive::kPrimByte:
1425 case Primitive::kPrimInt:
1426 case Primitive::kPrimChar:
1427 // Processing a Dex `int-to-short' instruction.
1428 locations->SetInAt(0, Location::Any());
1429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1430 break;
1431
1432 default:
1433 LOG(FATAL) << "Unexpected type conversion from " << input_type
1434 << " to " << result_type;
1435 }
1436 break;
1437
Roland Levillain946e1432014-11-11 17:35:19 +00001438 case Primitive::kPrimInt:
1439 switch (input_type) {
1440 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001441 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001442 locations->SetInAt(0, Location::Any());
1443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1444 break;
1445
1446 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001447 // Processing a Dex `float-to-int' instruction.
1448 locations->SetInAt(0, Location::RequiresFpuRegister());
1449 locations->SetOut(Location::RequiresRegister());
1450 locations->AddTemp(Location::RequiresFpuRegister());
1451 break;
1452
Roland Levillain946e1432014-11-11 17:35:19 +00001453 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001454 // Processing a Dex `double-to-int' instruction.
1455 locations->SetInAt(0, Location::RequiresFpuRegister());
1456 locations->SetOut(Location::RequiresRegister());
1457 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001458 break;
1459
1460 default:
1461 LOG(FATAL) << "Unexpected type conversion from " << input_type
1462 << " to " << result_type;
1463 }
1464 break;
1465
Roland Levillaindff1f282014-11-05 14:15:05 +00001466 case Primitive::kPrimLong:
1467 switch (input_type) {
1468 case Primitive::kPrimByte:
1469 case Primitive::kPrimShort:
1470 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001471 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001472 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001473 // TODO: We would benefit from a (to-be-implemented)
1474 // Location::RegisterOrStackSlot requirement for this input.
1475 locations->SetInAt(0, Location::RequiresRegister());
1476 locations->SetOut(Location::RequiresRegister());
1477 break;
1478
1479 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001480 // Processing a Dex `float-to-long' instruction.
1481 locations->SetInAt(0, Location::RequiresFpuRegister());
1482 locations->SetOut(Location::RequiresRegister());
1483 locations->AddTemp(Location::RequiresFpuRegister());
1484 break;
1485
Roland Levillaindff1f282014-11-05 14:15:05 +00001486 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001487 // Processing a Dex `double-to-long' instruction.
1488 locations->SetInAt(0, Location::RequiresFpuRegister());
1489 locations->SetOut(Location::RequiresRegister());
1490 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001491 break;
1492
1493 default:
1494 LOG(FATAL) << "Unexpected type conversion from " << input_type
1495 << " to " << result_type;
1496 }
1497 break;
1498
Roland Levillain981e4542014-11-14 11:47:14 +00001499 case Primitive::kPrimChar:
1500 switch (input_type) {
1501 case Primitive::kPrimByte:
1502 case Primitive::kPrimShort:
1503 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001504 // Processing a Dex `int-to-char' instruction.
1505 locations->SetInAt(0, Location::Any());
1506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1507 break;
1508
1509 default:
1510 LOG(FATAL) << "Unexpected type conversion from " << input_type
1511 << " to " << result_type;
1512 }
1513 break;
1514
Roland Levillaindff1f282014-11-05 14:15:05 +00001515 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001516 switch (input_type) {
1517 case Primitive::kPrimByte:
1518 case Primitive::kPrimShort:
1519 case Primitive::kPrimInt:
1520 case Primitive::kPrimChar:
1521 // Processing a Dex `int-to-float' instruction.
1522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetOut(Location::RequiresFpuRegister());
1524 break;
1525
1526 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001527 // Processing a Dex `long-to-float' instruction.
1528 locations->SetInAt(0, Location::RequiresRegister());
1529 locations->SetOut(Location::RequiresFpuRegister());
1530 break;
1531
Roland Levillaincff13742014-11-17 14:32:17 +00001532 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001533 // Processing a Dex `double-to-float' instruction.
1534 locations->SetInAt(0, Location::RequiresFpuRegister());
1535 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 };
1542 break;
1543
Roland Levillaindff1f282014-11-05 14:15:05 +00001544 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001545 switch (input_type) {
1546 case Primitive::kPrimByte:
1547 case Primitive::kPrimShort:
1548 case Primitive::kPrimInt:
1549 case Primitive::kPrimChar:
1550 // Processing a Dex `int-to-double' instruction.
1551 locations->SetInAt(0, Location::RequiresRegister());
1552 locations->SetOut(Location::RequiresFpuRegister());
1553 break;
1554
1555 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001556 // Processing a Dex `long-to-double' instruction.
1557 locations->SetInAt(0, Location::RequiresRegister());
1558 locations->SetOut(Location::RequiresFpuRegister());
1559 break;
1560
Roland Levillaincff13742014-11-17 14:32:17 +00001561 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001562 // Processing a Dex `float-to-double' instruction.
1563 locations->SetInAt(0, Location::RequiresFpuRegister());
1564 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001565 break;
1566
1567 default:
1568 LOG(FATAL) << "Unexpected type conversion from " << input_type
1569 << " to " << result_type;
1570 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001571 break;
1572
1573 default:
1574 LOG(FATAL) << "Unexpected type conversion from " << input_type
1575 << " to " << result_type;
1576 }
1577}
1578
1579void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1580 LocationSummary* locations = conversion->GetLocations();
1581 Location out = locations->Out();
1582 Location in = locations->InAt(0);
1583 Primitive::Type result_type = conversion->GetResultType();
1584 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001585 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001586 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001587 case Primitive::kPrimByte:
1588 switch (input_type) {
1589 case Primitive::kPrimShort:
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001592 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001593 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001594 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001595 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001596 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001597 Address(CpuRegister(RSP), in.GetStackIndex()));
1598 } else {
1599 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001600 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001601 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1602 }
1603 break;
1604
1605 default:
1606 LOG(FATAL) << "Unexpected type conversion from " << input_type
1607 << " to " << result_type;
1608 }
1609 break;
1610
Roland Levillain01a8d712014-11-14 16:27:39 +00001611 case Primitive::kPrimShort:
1612 switch (input_type) {
1613 case Primitive::kPrimByte:
1614 case Primitive::kPrimInt:
1615 case Primitive::kPrimChar:
1616 // Processing a Dex `int-to-short' instruction.
1617 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001618 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001619 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001620 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001621 Address(CpuRegister(RSP), in.GetStackIndex()));
1622 } else {
1623 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001624 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001625 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1626 }
1627 break;
1628
1629 default:
1630 LOG(FATAL) << "Unexpected type conversion from " << input_type
1631 << " to " << result_type;
1632 }
1633 break;
1634
Roland Levillain946e1432014-11-11 17:35:19 +00001635 case Primitive::kPrimInt:
1636 switch (input_type) {
1637 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001638 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001639 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001640 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001641 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001642 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001643 Address(CpuRegister(RSP), in.GetStackIndex()));
1644 } else {
1645 DCHECK(in.IsConstant());
1646 DCHECK(in.GetConstant()->IsLongConstant());
1647 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001648 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001649 }
1650 break;
1651
Roland Levillain3f8f9362014-12-02 17:45:01 +00001652 case Primitive::kPrimFloat: {
1653 // Processing a Dex `float-to-int' instruction.
1654 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1655 CpuRegister output = out.AsRegister<CpuRegister>();
1656 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1657 Label done, nan;
1658
1659 __ movl(output, Immediate(kPrimIntMax));
1660 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001661 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001662 // if input >= temp goto done
1663 __ comiss(input, temp);
1664 __ j(kAboveEqual, &done);
1665 // if input == NaN goto nan
1666 __ j(kUnordered, &nan);
1667 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001668 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001669 __ jmp(&done);
1670 __ Bind(&nan);
1671 // output = 0
1672 __ xorl(output, output);
1673 __ Bind(&done);
1674 break;
1675 }
1676
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001677 case Primitive::kPrimDouble: {
1678 // Processing a Dex `double-to-int' instruction.
1679 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1680 CpuRegister output = out.AsRegister<CpuRegister>();
1681 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1682 Label done, nan;
1683
1684 __ movl(output, Immediate(kPrimIntMax));
1685 // temp = int-to-double(output)
1686 __ cvtsi2sd(temp, output);
1687 // if input >= temp goto done
1688 __ comisd(input, temp);
1689 __ j(kAboveEqual, &done);
1690 // if input == NaN goto nan
1691 __ j(kUnordered, &nan);
1692 // output = double-to-int-truncate(input)
1693 __ cvttsd2si(output, input);
1694 __ jmp(&done);
1695 __ Bind(&nan);
1696 // output = 0
1697 __ xorl(output, output);
1698 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001699 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001700 }
Roland Levillain946e1432014-11-11 17:35:19 +00001701
1702 default:
1703 LOG(FATAL) << "Unexpected type conversion from " << input_type
1704 << " to " << result_type;
1705 }
1706 break;
1707
Roland Levillaindff1f282014-11-05 14:15:05 +00001708 case Primitive::kPrimLong:
1709 switch (input_type) {
1710 DCHECK(out.IsRegister());
1711 case Primitive::kPrimByte:
1712 case Primitive::kPrimShort:
1713 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001714 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001715 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001716 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001717 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001718 break;
1719
Roland Levillain624279f2014-12-04 11:54:28 +00001720 case Primitive::kPrimFloat: {
1721 // Processing a Dex `float-to-long' instruction.
1722 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1723 CpuRegister output = out.AsRegister<CpuRegister>();
1724 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1725 Label done, nan;
1726
1727 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001728 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001729 __ cvtsi2ss(temp, output, true);
1730 // if input >= temp goto done
1731 __ comiss(input, temp);
1732 __ j(kAboveEqual, &done);
1733 // if input == NaN goto nan
1734 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001735 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001736 __ cvttss2si(output, input, true);
1737 __ jmp(&done);
1738 __ Bind(&nan);
1739 // output = 0
1740 __ xorq(output, output);
1741 __ Bind(&done);
1742 break;
1743 }
1744
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001745 case Primitive::kPrimDouble: {
1746 // Processing a Dex `double-to-long' instruction.
1747 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1748 CpuRegister output = out.AsRegister<CpuRegister>();
1749 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1750 Label done, nan;
1751
1752 __ movq(output, Immediate(kPrimLongMax));
1753 // temp = long-to-double(output)
1754 __ cvtsi2sd(temp, output, true);
1755 // if input >= temp goto done
1756 __ comisd(input, temp);
1757 __ j(kAboveEqual, &done);
1758 // if input == NaN goto nan
1759 __ j(kUnordered, &nan);
1760 // output = double-to-long-truncate(input)
1761 __ cvttsd2si(output, input, true);
1762 __ jmp(&done);
1763 __ Bind(&nan);
1764 // output = 0
1765 __ xorq(output, output);
1766 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001767 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001768 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001769
1770 default:
1771 LOG(FATAL) << "Unexpected type conversion from " << input_type
1772 << " to " << result_type;
1773 }
1774 break;
1775
Roland Levillain981e4542014-11-14 11:47:14 +00001776 case Primitive::kPrimChar:
1777 switch (input_type) {
1778 case Primitive::kPrimByte:
1779 case Primitive::kPrimShort:
1780 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001781 // Processing a Dex `int-to-char' instruction.
1782 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001783 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001784 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001785 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001786 Address(CpuRegister(RSP), in.GetStackIndex()));
1787 } else {
1788 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001789 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001790 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1791 }
1792 break;
1793
1794 default:
1795 LOG(FATAL) << "Unexpected type conversion from " << input_type
1796 << " to " << result_type;
1797 }
1798 break;
1799
Roland Levillaindff1f282014-11-05 14:15:05 +00001800 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001801 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimByte:
1803 case Primitive::kPrimShort:
1804 case Primitive::kPrimInt:
1805 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001806 // Processing a Dex `int-to-float' instruction.
1807 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001808 break;
1809
1810 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001811 // Processing a Dex `long-to-float' instruction.
1812 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1813 break;
1814
Roland Levillaincff13742014-11-17 14:32:17 +00001815 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001816 // Processing a Dex `double-to-float' instruction.
1817 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001818 break;
1819
1820 default:
1821 LOG(FATAL) << "Unexpected type conversion from " << input_type
1822 << " to " << result_type;
1823 };
1824 break;
1825
Roland Levillaindff1f282014-11-05 14:15:05 +00001826 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001827 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001828 case Primitive::kPrimByte:
1829 case Primitive::kPrimShort:
1830 case Primitive::kPrimInt:
1831 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001832 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001833 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001834 break;
1835
1836 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001837 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001838 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001839 break;
1840
Roland Levillaincff13742014-11-17 14:32:17 +00001841 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001842 // Processing a Dex `float-to-double' instruction.
1843 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001844 break;
1845
1846 default:
1847 LOG(FATAL) << "Unexpected type conversion from " << input_type
1848 << " to " << result_type;
1849 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001850 break;
1851
1852 default:
1853 LOG(FATAL) << "Unexpected type conversion from " << input_type
1854 << " to " << result_type;
1855 }
1856}
1857
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001858void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001861 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001862 case Primitive::kPrimInt: {
1863 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001864 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001866 break;
1867 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001868
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001870 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001871 // We can use a leaq or addq if the constant can fit in an immediate.
1872 HInstruction* rhs = add->InputAt(1);
1873 bool is_int32_constant = false;
1874 if (rhs->IsLongConstant()) {
1875 int64_t value = rhs->AsLongConstant()->GetValue();
1876 if (static_cast<int32_t>(value) == value) {
1877 is_int32_constant = true;
1878 }
1879 }
1880 locations->SetInAt(1,
1881 is_int32_constant ? Location::RegisterOrConstant(rhs) :
1882 Location::RequiresRegister());
1883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001884 break;
1885 }
1886
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887 case Primitive::kPrimDouble:
1888 case Primitive::kPrimFloat: {
1889 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001890 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001892 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001893 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001894
1895 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001896 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001897 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001898}
1899
1900void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1901 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001902 Location first = locations->InAt(0);
1903 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001904 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001905
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001906 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001907 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001908 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001909 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1910 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1911 } else {
1912 __ leal(out.AsRegister<CpuRegister>(), Address(
1913 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1914 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001915 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001916 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1917 __ addl(out.AsRegister<CpuRegister>(),
1918 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
1919 } else {
1920 __ leal(out.AsRegister<CpuRegister>(), Address(
1921 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
1922 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001923 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001924 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001925 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001926 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001927 break;
1928 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001929
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001930 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05001931 if (second.IsRegister()) {
1932 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1933 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1934 } else {
1935 __ leaq(out.AsRegister<CpuRegister>(), Address(
1936 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1937 }
1938 } else {
1939 DCHECK(second.IsConstant());
1940 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1941 int32_t int32_value = Low32Bits(value);
1942 DCHECK_EQ(int32_value, value);
1943 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1944 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
1945 } else {
1946 __ leaq(out.AsRegister<CpuRegister>(), Address(
1947 first.AsRegister<CpuRegister>(), int32_value));
1948 }
1949 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001950 break;
1951 }
1952
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001955 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001956 }
1957
1958 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001959 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 break;
1961 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001962
1963 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001964 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001965 }
1966}
1967
1968void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001969 LocationSummary* locations =
1970 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001971 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001972 case Primitive::kPrimInt: {
1973 locations->SetInAt(0, Location::RequiresRegister());
1974 locations->SetInAt(1, Location::Any());
1975 locations->SetOut(Location::SameAsFirstInput());
1976 break;
1977 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001978 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001979 locations->SetInAt(0, Location::RequiresRegister());
1980 locations->SetInAt(1, Location::RequiresRegister());
1981 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001982 break;
1983 }
Calin Juravle11351682014-10-23 15:38:15 +01001984 case Primitive::kPrimFloat:
1985 case Primitive::kPrimDouble: {
1986 locations->SetInAt(0, Location::RequiresFpuRegister());
1987 locations->SetInAt(1, Location::RequiresFpuRegister());
1988 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001989 break;
Calin Juravle11351682014-10-23 15:38:15 +01001990 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001991 default:
Calin Juravle11351682014-10-23 15:38:15 +01001992 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001993 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001994}
1995
1996void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1997 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001998 Location first = locations->InAt(0);
1999 Location second = locations->InAt(1);
2000 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002001 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002002 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002003 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002004 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002005 } else if (second.IsConstant()) {
2006 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002008 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002009 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002010 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002011 break;
2012 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002013 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002014 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002015 break;
2016 }
2017
Calin Juravle11351682014-10-23 15:38:15 +01002018 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002019 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002020 break;
Calin Juravle11351682014-10-23 15:38:15 +01002021 }
2022
2023 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002024 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002025 break;
2026 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002027
2028 default:
Calin Juravle11351682014-10-23 15:38:15 +01002029 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030 }
2031}
2032
Calin Juravle34bacdf2014-10-07 20:23:36 +01002033void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2036 switch (mul->GetResultType()) {
2037 case Primitive::kPrimInt: {
2038 locations->SetInAt(0, Location::RequiresRegister());
2039 locations->SetInAt(1, Location::Any());
2040 locations->SetOut(Location::SameAsFirstInput());
2041 break;
2042 }
2043 case Primitive::kPrimLong: {
2044 locations->SetInAt(0, Location::RequiresRegister());
2045 locations->SetInAt(1, Location::RequiresRegister());
2046 locations->SetOut(Location::SameAsFirstInput());
2047 break;
2048 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002049 case Primitive::kPrimFloat:
2050 case Primitive::kPrimDouble: {
2051 locations->SetInAt(0, Location::RequiresFpuRegister());
2052 locations->SetInAt(1, Location::RequiresFpuRegister());
2053 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002054 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002055 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002056
2057 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002058 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002059 }
2060}
2061
2062void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2063 LocationSummary* locations = mul->GetLocations();
2064 Location first = locations->InAt(0);
2065 Location second = locations->InAt(1);
2066 DCHECK(first.Equals(locations->Out()));
2067 switch (mul->GetResultType()) {
2068 case Primitive::kPrimInt: {
2069 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002070 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002071 } else if (second.IsConstant()) {
2072 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002073 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002074 } else {
2075 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002076 __ imull(first.AsRegister<CpuRegister>(),
2077 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002078 }
2079 break;
2080 }
2081 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002083 break;
2084 }
2085
Calin Juravleb5bfa962014-10-21 18:02:24 +01002086 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002088 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002089 }
2090
2091 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002093 break;
2094 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002095
2096 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002097 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002098 }
2099}
2100
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002101void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2102 uint32_t stack_adjustment, bool is_float) {
2103 if (source.IsStackSlot()) {
2104 DCHECK(is_float);
2105 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2106 } else if (source.IsDoubleStackSlot()) {
2107 DCHECK(!is_float);
2108 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2109 } else {
2110 // Write the value to the temporary location on the stack and load to FP stack.
2111 if (is_float) {
2112 Location stack_temp = Location::StackSlot(temp_offset);
2113 codegen_->Move(stack_temp, source);
2114 __ flds(Address(CpuRegister(RSP), temp_offset));
2115 } else {
2116 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2117 codegen_->Move(stack_temp, source);
2118 __ fldl(Address(CpuRegister(RSP), temp_offset));
2119 }
2120 }
2121}
2122
2123void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2124 Primitive::Type type = rem->GetResultType();
2125 bool is_float = type == Primitive::kPrimFloat;
2126 size_t elem_size = Primitive::ComponentSize(type);
2127 LocationSummary* locations = rem->GetLocations();
2128 Location first = locations->InAt(0);
2129 Location second = locations->InAt(1);
2130 Location out = locations->Out();
2131
2132 // Create stack space for 2 elements.
2133 // TODO: enhance register allocator to ask for stack temporaries.
2134 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2135
2136 // Load the values to the FP stack in reverse order, using temporaries if needed.
2137 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2138 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2139
2140 // Loop doing FPREM until we stabilize.
2141 Label retry;
2142 __ Bind(&retry);
2143 __ fprem();
2144
2145 // Move FP status to AX.
2146 __ fstsw();
2147
2148 // And see if the argument reduction is complete. This is signaled by the
2149 // C2 FPU flag bit set to 0.
2150 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2151 __ j(kNotEqual, &retry);
2152
2153 // We have settled on the final value. Retrieve it into an XMM register.
2154 // Store FP top of stack to real stack.
2155 if (is_float) {
2156 __ fsts(Address(CpuRegister(RSP), 0));
2157 } else {
2158 __ fstl(Address(CpuRegister(RSP), 0));
2159 }
2160
2161 // Pop the 2 items from the FP stack.
2162 __ fucompp();
2163
2164 // Load the value from the stack into an XMM register.
2165 DCHECK(out.IsFpuRegister()) << out;
2166 if (is_float) {
2167 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2168 } else {
2169 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2170 }
2171
2172 // And remove the temporary stack space we allocated.
2173 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2174}
2175
Calin Juravlebacfec32014-11-14 15:54:36 +00002176void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2177 DCHECK(instruction->IsDiv() || instruction->IsRem());
2178 Primitive::Type type = instruction->GetResultType();
2179 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2180
2181 bool is_div = instruction->IsDiv();
2182 LocationSummary* locations = instruction->GetLocations();
2183
Roland Levillain271ab9c2014-11-27 15:23:57 +00002184 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2185 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002186
Roland Levillain271ab9c2014-11-27 15:23:57 +00002187 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002188 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2189
2190 SlowPathCodeX86_64* slow_path =
2191 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2192 out_reg.AsRegister(), type, is_div);
2193 codegen_->AddSlowPath(slow_path);
2194
2195 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2196 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2197 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002198 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002199 __ cmpl(second_reg, Immediate(-1));
2200 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002201 // edx:eax <- sign-extended of eax
2202 __ cdq();
2203 // eax = quotient, edx = remainder
2204 __ idivl(second_reg);
2205 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002206 __ cmpq(second_reg, Immediate(-1));
2207 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002208 // rdx:rax <- sign-extended of rax
2209 __ cqo();
2210 // rax = quotient, rdx = remainder
2211 __ idivq(second_reg);
2212 }
2213
2214 __ Bind(slow_path->GetExitLabel());
2215}
2216
Calin Juravle7c4954d2014-10-28 16:57:40 +00002217void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2218 LocationSummary* locations =
2219 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2220 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002221 case Primitive::kPrimInt:
2222 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002223 locations->SetInAt(0, Location::RegisterLocation(RAX));
2224 locations->SetInAt(1, Location::RequiresRegister());
2225 locations->SetOut(Location::SameAsFirstInput());
2226 // Intel uses edx:eax as the dividend.
2227 locations->AddTemp(Location::RegisterLocation(RDX));
2228 break;
2229 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002230
Calin Juravle7c4954d2014-10-28 16:57:40 +00002231 case Primitive::kPrimFloat:
2232 case Primitive::kPrimDouble: {
2233 locations->SetInAt(0, Location::RequiresFpuRegister());
2234 locations->SetInAt(1, Location::RequiresFpuRegister());
2235 locations->SetOut(Location::SameAsFirstInput());
2236 break;
2237 }
2238
2239 default:
2240 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2241 }
2242}
2243
2244void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2245 LocationSummary* locations = div->GetLocations();
2246 Location first = locations->InAt(0);
2247 Location second = locations->InAt(1);
2248 DCHECK(first.Equals(locations->Out()));
2249
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002250 Primitive::Type type = div->GetResultType();
2251 switch (type) {
2252 case Primitive::kPrimInt:
2253 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002254 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002255 break;
2256 }
2257
Calin Juravle7c4954d2014-10-28 16:57:40 +00002258 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002259 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002260 break;
2261 }
2262
2263 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002264 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002265 break;
2266 }
2267
2268 default:
2269 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2270 }
2271}
2272
Calin Juravlebacfec32014-11-14 15:54:36 +00002273void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002274 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002275 LocationSummary* locations =
2276 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002277
2278 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002279 case Primitive::kPrimInt:
2280 case Primitive::kPrimLong: {
2281 locations->SetInAt(0, Location::RegisterLocation(RAX));
2282 locations->SetInAt(1, Location::RequiresRegister());
2283 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2284 locations->SetOut(Location::RegisterLocation(RDX));
2285 break;
2286 }
2287
2288 case Primitive::kPrimFloat:
2289 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002290 locations->SetInAt(0, Location::Any());
2291 locations->SetInAt(1, Location::Any());
2292 locations->SetOut(Location::RequiresFpuRegister());
2293 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002294 break;
2295 }
2296
2297 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002298 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002299 }
2300}
2301
2302void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2303 Primitive::Type type = rem->GetResultType();
2304 switch (type) {
2305 case Primitive::kPrimInt:
2306 case Primitive::kPrimLong: {
2307 GenerateDivRemIntegral(rem);
2308 break;
2309 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002310 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002311 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002312 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002313 break;
2314 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002315 default:
2316 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2317 }
2318}
2319
Calin Juravled0d48522014-11-04 16:40:20 +00002320void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2321 LocationSummary* locations =
2322 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2323 locations->SetInAt(0, Location::Any());
2324 if (instruction->HasUses()) {
2325 locations->SetOut(Location::SameAsFirstInput());
2326 }
2327}
2328
2329void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2330 SlowPathCodeX86_64* slow_path =
2331 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2332 codegen_->AddSlowPath(slow_path);
2333
2334 LocationSummary* locations = instruction->GetLocations();
2335 Location value = locations->InAt(0);
2336
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002337 switch (instruction->GetType()) {
2338 case Primitive::kPrimInt: {
2339 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002340 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002341 __ j(kEqual, slow_path->GetEntryLabel());
2342 } else if (value.IsStackSlot()) {
2343 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2344 __ j(kEqual, slow_path->GetEntryLabel());
2345 } else {
2346 DCHECK(value.IsConstant()) << value;
2347 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2348 __ jmp(slow_path->GetEntryLabel());
2349 }
2350 }
2351 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002352 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002353 case Primitive::kPrimLong: {
2354 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002356 __ j(kEqual, slow_path->GetEntryLabel());
2357 } else if (value.IsDoubleStackSlot()) {
2358 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2359 __ j(kEqual, slow_path->GetEntryLabel());
2360 } else {
2361 DCHECK(value.IsConstant()) << value;
2362 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2363 __ jmp(slow_path->GetEntryLabel());
2364 }
2365 }
2366 break;
2367 }
2368 default:
2369 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002370 }
Calin Juravled0d48522014-11-04 16:40:20 +00002371}
2372
Calin Juravle9aec02f2014-11-18 23:06:35 +00002373void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2374 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2375
2376 LocationSummary* locations =
2377 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2378
2379 switch (op->GetResultType()) {
2380 case Primitive::kPrimInt:
2381 case Primitive::kPrimLong: {
2382 locations->SetInAt(0, Location::RequiresRegister());
2383 // The shift count needs to be in CL.
2384 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2385 locations->SetOut(Location::SameAsFirstInput());
2386 break;
2387 }
2388 default:
2389 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2390 }
2391}
2392
2393void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2394 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2395
2396 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002397 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002398 Location second = locations->InAt(1);
2399
2400 switch (op->GetResultType()) {
2401 case Primitive::kPrimInt: {
2402 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002403 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002404 if (op->IsShl()) {
2405 __ shll(first_reg, second_reg);
2406 } else if (op->IsShr()) {
2407 __ sarl(first_reg, second_reg);
2408 } else {
2409 __ shrl(first_reg, second_reg);
2410 }
2411 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002412 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002413 if (op->IsShl()) {
2414 __ shll(first_reg, imm);
2415 } else if (op->IsShr()) {
2416 __ sarl(first_reg, imm);
2417 } else {
2418 __ shrl(first_reg, imm);
2419 }
2420 }
2421 break;
2422 }
2423 case Primitive::kPrimLong: {
2424 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002425 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002426 if (op->IsShl()) {
2427 __ shlq(first_reg, second_reg);
2428 } else if (op->IsShr()) {
2429 __ sarq(first_reg, second_reg);
2430 } else {
2431 __ shrq(first_reg, second_reg);
2432 }
2433 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002434 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002435 if (op->IsShl()) {
2436 __ shlq(first_reg, imm);
2437 } else if (op->IsShr()) {
2438 __ sarq(first_reg, imm);
2439 } else {
2440 __ shrq(first_reg, imm);
2441 }
2442 }
2443 break;
2444 }
2445 default:
2446 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2447 }
2448}
2449
2450void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2451 HandleShift(shl);
2452}
2453
2454void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2455 HandleShift(shl);
2456}
2457
2458void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2459 HandleShift(shr);
2460}
2461
2462void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2463 HandleShift(shr);
2464}
2465
2466void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2467 HandleShift(ushr);
2468}
2469
2470void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2471 HandleShift(ushr);
2472}
2473
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002474void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002475 LocationSummary* locations =
2476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002477 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002478 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2479 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2480 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002481}
2482
2483void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2484 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002485 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002486 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2487
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002488 __ gs()->call(
2489 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002490
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002491 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002492 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002493}
2494
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002495void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2496 LocationSummary* locations =
2497 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2498 InvokeRuntimeCallingConvention calling_convention;
2499 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002500 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002501 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002502 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002503}
2504
2505void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2506 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002507 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002508 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2509
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002510 __ gs()->call(
2511 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002512
2513 DCHECK(!codegen_->IsLeafMethod());
2514 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2515}
2516
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002517void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002518 LocationSummary* locations =
2519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002520 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2521 if (location.IsStackSlot()) {
2522 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2523 } else if (location.IsDoubleStackSlot()) {
2524 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2525 }
2526 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002527}
2528
2529void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2530 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002531 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002532}
2533
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002534void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002535 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002536 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002537 locations->SetInAt(0, Location::RequiresRegister());
2538 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002539}
2540
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002541void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2542 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2544 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002545 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002546 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002547 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002548 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002549 break;
2550
2551 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002552 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002553 break;
2554
2555 default:
2556 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2557 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002558}
2559
2560void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002561 LocationSummary* locations =
2562 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002563 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2564 locations->SetInAt(i, Location::Any());
2565 }
2566 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002567}
2568
2569void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002570 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002571 LOG(FATAL) << "Unimplemented";
2572}
2573
Calin Juravle52c48962014-12-16 17:02:57 +00002574void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2575 /*
2576 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2577 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2578 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2579 */
2580 switch (kind) {
2581 case MemBarrierKind::kAnyAny: {
2582 __ mfence();
2583 break;
2584 }
2585 case MemBarrierKind::kAnyStore:
2586 case MemBarrierKind::kLoadAny:
2587 case MemBarrierKind::kStoreStore: {
2588 // nop
2589 break;
2590 }
2591 default:
2592 LOG(FATAL) << "Unexpected memory barier " << kind;
2593 }
2594}
2595
2596void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2597 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2598
Nicolas Geoffray39468442014-09-02 15:17:15 +01002599 LocationSummary* locations =
2600 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002601 locations->SetInAt(0, Location::RequiresRegister());
2602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2603}
2604
2605void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2606 const FieldInfo& field_info) {
2607 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2608
2609 LocationSummary* locations = instruction->GetLocations();
2610 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2611 Location out = locations->Out();
2612 bool is_volatile = field_info.IsVolatile();
2613 Primitive::Type field_type = field_info.GetFieldType();
2614 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2615
2616 switch (field_type) {
2617 case Primitive::kPrimBoolean: {
2618 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2619 break;
2620 }
2621
2622 case Primitive::kPrimByte: {
2623 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2624 break;
2625 }
2626
2627 case Primitive::kPrimShort: {
2628 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2629 break;
2630 }
2631
2632 case Primitive::kPrimChar: {
2633 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2634 break;
2635 }
2636
2637 case Primitive::kPrimInt:
2638 case Primitive::kPrimNot: {
2639 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2640 break;
2641 }
2642
2643 case Primitive::kPrimLong: {
2644 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2645 break;
2646 }
2647
2648 case Primitive::kPrimFloat: {
2649 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2650 break;
2651 }
2652
2653 case Primitive::kPrimDouble: {
2654 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2655 break;
2656 }
2657
2658 case Primitive::kPrimVoid:
2659 LOG(FATAL) << "Unreachable type " << field_type;
2660 UNREACHABLE();
2661 }
2662
Calin Juravle77520bc2015-01-12 18:45:46 +00002663 codegen_->MaybeRecordImplicitNullCheck(instruction);
2664
Calin Juravle52c48962014-12-16 17:02:57 +00002665 if (is_volatile) {
2666 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2667 }
2668}
2669
2670void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2671 const FieldInfo& field_info) {
2672 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2673
2674 LocationSummary* locations =
2675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002676 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002677 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2678
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002679 locations->SetInAt(0, Location::RequiresRegister());
2680 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002681 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002682 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002683 locations->AddTemp(Location::RequiresRegister());
2684 locations->AddTemp(Location::RequiresRegister());
2685 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002686}
2687
Calin Juravle52c48962014-12-16 17:02:57 +00002688void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2689 const FieldInfo& field_info) {
2690 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2691
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002692 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002693 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2694 Location value = locations->InAt(1);
2695 bool is_volatile = field_info.IsVolatile();
2696 Primitive::Type field_type = field_info.GetFieldType();
2697 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2698
2699 if (is_volatile) {
2700 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2701 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702
2703 switch (field_type) {
2704 case Primitive::kPrimBoolean:
2705 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002706 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707 break;
2708 }
2709
2710 case Primitive::kPrimShort:
2711 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002712 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002713 break;
2714 }
2715
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002716 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002717 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002718 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002719 break;
2720 }
2721
2722 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002723 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002724 break;
2725 }
2726
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002727 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002728 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002729 break;
2730 }
2731
2732 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002733 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002734 break;
2735 }
2736
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002737 case Primitive::kPrimVoid:
2738 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002739 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740 }
Calin Juravle52c48962014-12-16 17:02:57 +00002741
Calin Juravle77520bc2015-01-12 18:45:46 +00002742 codegen_->MaybeRecordImplicitNullCheck(instruction);
2743
2744 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2745 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2746 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2747 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2748 }
2749
Calin Juravle52c48962014-12-16 17:02:57 +00002750 if (is_volatile) {
2751 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2752 }
2753}
2754
2755void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2756 HandleFieldSet(instruction, instruction->GetFieldInfo());
2757}
2758
2759void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2760 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002761}
2762
2763void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002764 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002765}
2766
2767void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002768 HandleFieldGet(instruction, instruction->GetFieldInfo());
2769}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002770
Calin Juravle52c48962014-12-16 17:02:57 +00002771void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2772 HandleFieldGet(instruction);
2773}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002774
Calin Juravle52c48962014-12-16 17:02:57 +00002775void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2776 HandleFieldGet(instruction, instruction->GetFieldInfo());
2777}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002778
Calin Juravle52c48962014-12-16 17:02:57 +00002779void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2780 HandleFieldSet(instruction, instruction->GetFieldInfo());
2781}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002782
Calin Juravle52c48962014-12-16 17:02:57 +00002783void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2784 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002785}
2786
2787void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002788 LocationSummary* locations =
2789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002790 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2791 ? Location::RequiresRegister()
2792 : Location::Any();
2793 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002794 if (instruction->HasUses()) {
2795 locations->SetOut(Location::SameAsFirstInput());
2796 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797}
2798
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002799void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002800 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2801 return;
2802 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002803 LocationSummary* locations = instruction->GetLocations();
2804 Location obj = locations->InAt(0);
2805
2806 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2807 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2808}
2809
2810void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002811 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002812 codegen_->AddSlowPath(slow_path);
2813
2814 LocationSummary* locations = instruction->GetLocations();
2815 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002816
2817 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002818 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002819 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002820 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002821 } else {
2822 DCHECK(obj.IsConstant()) << obj;
2823 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2824 __ jmp(slow_path->GetEntryLabel());
2825 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002826 }
2827 __ j(kEqual, slow_path->GetEntryLabel());
2828}
2829
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002830void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2831 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2832 GenerateImplicitNullCheck(instruction);
2833 } else {
2834 GenerateExplicitNullCheck(instruction);
2835 }
2836}
2837
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002838void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002839 LocationSummary* locations =
2840 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002841 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002842 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002843 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002845}
2846
2847void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2848 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002849 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002850 Location index = locations->InAt(1);
2851
2852 switch (instruction->GetType()) {
2853 case Primitive::kPrimBoolean: {
2854 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002856 if (index.IsConstant()) {
2857 __ movzxb(out, Address(obj,
2858 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2859 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002860 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861 }
2862 break;
2863 }
2864
2865 case Primitive::kPrimByte: {
2866 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002867 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002868 if (index.IsConstant()) {
2869 __ movsxb(out, Address(obj,
2870 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2871 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002872 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002873 }
2874 break;
2875 }
2876
2877 case Primitive::kPrimShort: {
2878 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002880 if (index.IsConstant()) {
2881 __ movsxw(out, Address(obj,
2882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2883 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002885 }
2886 break;
2887 }
2888
2889 case Primitive::kPrimChar: {
2890 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002891 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002892 if (index.IsConstant()) {
2893 __ movzxw(out, Address(obj,
2894 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2895 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002896 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002897 }
2898 break;
2899 }
2900
2901 case Primitive::kPrimInt:
2902 case Primitive::kPrimNot: {
2903 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2904 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002906 if (index.IsConstant()) {
2907 __ movl(out, Address(obj,
2908 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2909 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002910 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002911 }
2912 break;
2913 }
2914
2915 case Primitive::kPrimLong: {
2916 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002917 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002918 if (index.IsConstant()) {
2919 __ movq(out, Address(obj,
2920 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002922 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002923 }
2924 break;
2925 }
2926
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002927 case Primitive::kPrimFloat: {
2928 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002929 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002930 if (index.IsConstant()) {
2931 __ movss(out, Address(obj,
2932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2933 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002934 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002935 }
2936 break;
2937 }
2938
2939 case Primitive::kPrimDouble: {
2940 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002942 if (index.IsConstant()) {
2943 __ movsd(out, Address(obj,
2944 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2945 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002946 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002947 }
2948 break;
2949 }
2950
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002951 case Primitive::kPrimVoid:
2952 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002953 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002954 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002955 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002956}
2957
2958void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002959 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002960
2961 bool needs_write_barrier =
2962 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2963 bool needs_runtime_call = instruction->NeedsTypeCheck();
2964
Nicolas Geoffray39468442014-09-02 15:17:15 +01002965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002966 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2967 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002968 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002969 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2970 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2971 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002972 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002973 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002974 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002975 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2976 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002977 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002978 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002979 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2980 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002981 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002982 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002983 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002984
2985 if (needs_write_barrier) {
2986 // Temporary registers for the write barrier.
2987 locations->AddTemp(Location::RequiresRegister());
2988 locations->AddTemp(Location::RequiresRegister());
2989 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002990 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002991}
2992
2993void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2994 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002995 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002996 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002997 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002998 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002999 bool needs_runtime_call = locations->WillCall();
3000 bool needs_write_barrier =
3001 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003002
3003 switch (value_type) {
3004 case Primitive::kPrimBoolean:
3005 case Primitive::kPrimByte: {
3006 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003007 if (index.IsConstant()) {
3008 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003009 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003011 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003012 __ movb(Address(obj, offset),
3013 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003014 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003015 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003016 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3018 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003019 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003021 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003023 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003024 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003025 break;
3026 }
3027
3028 case Primitive::kPrimShort:
3029 case Primitive::kPrimChar: {
3030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003031 if (index.IsConstant()) {
3032 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003033 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003035 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003036 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003037 __ movw(Address(obj, offset),
3038 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003039 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003040 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003041 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003042 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003043 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3044 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003045 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003046 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003048 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3049 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003051 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003052 break;
3053 }
3054
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003055 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003056 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003057 if (!needs_runtime_call) {
3058 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3059 if (index.IsConstant()) {
3060 size_t offset =
3061 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3062 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003064 } else {
3065 DCHECK(value.IsConstant()) << value;
3066 __ movl(Address(obj, offset),
3067 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3068 }
3069 } else {
3070 DCHECK(index.IsRegister()) << index;
3071 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3073 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003074 } else {
3075 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003076 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003077 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3078 }
3079 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003080 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003081 if (needs_write_barrier) {
3082 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003083 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3084 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3085 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003086 }
3087 } else {
3088 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003089 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3090 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003091 DCHECK(!codegen_->IsLeafMethod());
3092 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3093 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 break;
3095 }
3096
3097 case Primitive::kPrimLong: {
3098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003099 if (index.IsConstant()) {
3100 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003101 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003103 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003104 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3106 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003107 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003108 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003109 break;
3110 }
3111
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003112 case Primitive::kPrimFloat: {
3113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3114 if (index.IsConstant()) {
3115 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3116 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003118 } else {
3119 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003120 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3121 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003122 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003123 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003124 break;
3125 }
3126
3127 case Primitive::kPrimDouble: {
3128 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3129 if (index.IsConstant()) {
3130 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3131 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003132 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133 } else {
3134 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003135 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3136 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003137 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003138 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003139 break;
3140 }
3141
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003142 case Primitive::kPrimVoid:
3143 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003144 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003145 }
3146}
3147
3148void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003149 LocationSummary* locations =
3150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003151 locations->SetInAt(0, Location::RequiresRegister());
3152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003153}
3154
3155void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3156 LocationSummary* locations = instruction->GetLocations();
3157 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003158 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3159 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003160 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003161 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003162}
3163
3164void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003165 LocationSummary* locations =
3166 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003167 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003168 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003169 if (instruction->HasUses()) {
3170 locations->SetOut(Location::SameAsFirstInput());
3171 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003172}
3173
3174void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3175 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003176 Location index_loc = locations->InAt(0);
3177 Location length_loc = locations->InAt(1);
3178 SlowPathCodeX86_64* slow_path =
3179 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003180 codegen_->AddSlowPath(slow_path);
3181
Mark Mendellf60c90b2015-03-04 15:12:59 -05003182 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3183 if (index_loc.IsConstant()) {
3184 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3185 __ cmpl(length, Immediate(value));
3186 } else {
3187 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3188 }
3189 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003190}
3191
3192void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3193 CpuRegister card,
3194 CpuRegister object,
3195 CpuRegister value) {
3196 Label is_null;
3197 __ testl(value, value);
3198 __ j(kEqual, &is_null);
3199 __ gs()->movq(card, Address::Absolute(
3200 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3201 __ movq(temp, object);
3202 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3203 __ movb(Address(temp, card, TIMES_1, 0), card);
3204 __ Bind(&is_null);
3205}
3206
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003207void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3208 temp->SetLocations(nullptr);
3209}
3210
3211void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3212 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003213 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003214}
3215
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003216void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003217 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003218 LOG(FATAL) << "Unimplemented";
3219}
3220
3221void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003222 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3223}
3224
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003225void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3226 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3227}
3228
3229void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003230 HBasicBlock* block = instruction->GetBlock();
3231 if (block->GetLoopInformation() != nullptr) {
3232 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3233 // The back edge will generate the suspend check.
3234 return;
3235 }
3236 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3237 // The goto will generate the suspend check.
3238 return;
3239 }
3240 GenerateSuspendCheck(instruction, nullptr);
3241}
3242
3243void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3244 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003245 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003246 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003247 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003248 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003249 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003250 if (successor == nullptr) {
3251 __ j(kNotEqual, slow_path->GetEntryLabel());
3252 __ Bind(slow_path->GetReturnLabel());
3253 } else {
3254 __ j(kEqual, codegen_->GetLabelOf(successor));
3255 __ jmp(slow_path->GetEntryLabel());
3256 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003257}
3258
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003259X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3260 return codegen_->GetAssembler();
3261}
3262
3263void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3264 MoveOperands* move = moves_.Get(index);
3265 Location source = move->GetSource();
3266 Location destination = move->GetDestination();
3267
3268 if (source.IsRegister()) {
3269 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003271 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003272 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003273 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003274 } else {
3275 DCHECK(destination.IsDoubleStackSlot());
3276 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003278 }
3279 } else if (source.IsStackSlot()) {
3280 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003281 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003282 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003283 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003284 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003285 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003286 } else {
3287 DCHECK(destination.IsStackSlot());
3288 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3289 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3290 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003291 } else if (source.IsDoubleStackSlot()) {
3292 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003294 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003295 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003296 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3297 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003298 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003299 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003300 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3301 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3302 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003303 } else if (source.IsConstant()) {
3304 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003305 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3306 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003307 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003308 if (value == 0) {
3309 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3310 } else {
3311 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3312 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003313 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003314 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003315 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003316 }
3317 } else if (constant->IsLongConstant()) {
3318 int64_t value = constant->AsLongConstant()->GetValue();
3319 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003320 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003321 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003322 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003323 __ movq(CpuRegister(TMP), Immediate(value));
3324 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3325 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003326 } else if (constant->IsFloatConstant()) {
3327 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3328 if (destination.IsFpuRegister()) {
3329 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003330 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003331 } else {
3332 DCHECK(destination.IsStackSlot()) << destination;
3333 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3334 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003335 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003336 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3337 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3338 if (destination.IsFpuRegister()) {
3339 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003340 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003341 } else {
3342 DCHECK(destination.IsDoubleStackSlot()) << destination;
3343 __ movq(CpuRegister(TMP), imm);
3344 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3345 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003346 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003347 } else if (source.IsFpuRegister()) {
3348 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003350 } else if (destination.IsStackSlot()) {
3351 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003352 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003353 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003354 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003355 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003356 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003357 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003358 }
3359}
3360
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003361void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003362 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003363 __ movl(Address(CpuRegister(RSP), mem), reg);
3364 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003365}
3366
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003367void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003368 ScratchRegisterScope ensure_scratch(
3369 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3370
3371 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3372 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3373 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3374 Address(CpuRegister(RSP), mem2 + stack_offset));
3375 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3376 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3377 CpuRegister(ensure_scratch.GetRegister()));
3378}
3379
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003380void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3381 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3382 __ movq(Address(CpuRegister(RSP), mem), reg);
3383 __ movq(reg, CpuRegister(TMP));
3384}
3385
3386void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3387 ScratchRegisterScope ensure_scratch(
3388 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3389
3390 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3391 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3392 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3393 Address(CpuRegister(RSP), mem2 + stack_offset));
3394 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3395 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3396 CpuRegister(ensure_scratch.GetRegister()));
3397}
3398
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003399void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3400 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3401 __ movss(Address(CpuRegister(RSP), mem), reg);
3402 __ movd(reg, CpuRegister(TMP));
3403}
3404
3405void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3406 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3407 __ movsd(Address(CpuRegister(RSP), mem), reg);
3408 __ movd(reg, CpuRegister(TMP));
3409}
3410
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003411void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3412 MoveOperands* move = moves_.Get(index);
3413 Location source = move->GetSource();
3414 Location destination = move->GetDestination();
3415
3416 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003417 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003418 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003419 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003420 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003421 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003422 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003423 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3424 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003425 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003426 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003427 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003428 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3429 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003430 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003431 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3432 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3433 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003434 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003435 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003436 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003437 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003438 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003440 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003441 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003442 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003443 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003444 }
3445}
3446
3447
3448void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3449 __ pushq(CpuRegister(reg));
3450}
3451
3452
3453void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3454 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003455}
3456
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003457void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3458 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3459 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3460 Immediate(mirror::Class::kStatusInitialized));
3461 __ j(kLess, slow_path->GetEntryLabel());
3462 __ Bind(slow_path->GetExitLabel());
3463 // No need for memory fence, thanks to the X86_64 memory model.
3464}
3465
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003466void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003467 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3468 ? LocationSummary::kCallOnSlowPath
3469 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003470 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003471 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003472 locations->SetOut(Location::RequiresRegister());
3473}
3474
3475void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003476 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003477 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003478 DCHECK(!cls->CanCallRuntime());
3479 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003480 codegen_->LoadCurrentMethod(out);
3481 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3482 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003483 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003484 codegen_->LoadCurrentMethod(out);
3485 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3486 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003487 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3488 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3489 codegen_->AddSlowPath(slow_path);
3490 __ testl(out, out);
3491 __ j(kEqual, slow_path->GetEntryLabel());
3492 if (cls->MustGenerateClinitCheck()) {
3493 GenerateClassInitializationCheck(slow_path, out);
3494 } else {
3495 __ Bind(slow_path->GetExitLabel());
3496 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003497 }
3498}
3499
3500void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3501 LocationSummary* locations =
3502 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3503 locations->SetInAt(0, Location::RequiresRegister());
3504 if (check->HasUses()) {
3505 locations->SetOut(Location::SameAsFirstInput());
3506 }
3507}
3508
3509void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003510 // We assume the class to not be null.
3511 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3512 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003513 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003514 GenerateClassInitializationCheck(slow_path,
3515 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003516}
3517
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003518void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3519 LocationSummary* locations =
3520 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3521 locations->SetOut(Location::RequiresRegister());
3522}
3523
3524void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3525 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3526 codegen_->AddSlowPath(slow_path);
3527
Roland Levillain271ab9c2014-11-27 15:23:57 +00003528 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003529 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003530 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3531 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003532 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3533 __ testl(out, out);
3534 __ j(kEqual, slow_path->GetEntryLabel());
3535 __ Bind(slow_path->GetExitLabel());
3536}
3537
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003538void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3539 LocationSummary* locations =
3540 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3541 locations->SetOut(Location::RequiresRegister());
3542}
3543
3544void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3545 Address address = Address::Absolute(
3546 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003547 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003548 __ gs()->movl(address, Immediate(0));
3549}
3550
3551void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3554 InvokeRuntimeCallingConvention calling_convention;
3555 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3556}
3557
3558void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3559 __ gs()->call(
3560 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3561 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3562}
3563
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003564void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003565 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3566 ? LocationSummary::kNoCall
3567 : LocationSummary::kCallOnSlowPath;
3568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3569 locations->SetInAt(0, Location::RequiresRegister());
3570 locations->SetInAt(1, Location::Any());
3571 locations->SetOut(Location::RequiresRegister());
3572}
3573
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003574void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003575 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003576 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003577 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003579 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3580 Label done, zero;
3581 SlowPathCodeX86_64* slow_path = nullptr;
3582
3583 // Return 0 if `obj` is null.
3584 // TODO: avoid this check if we know obj is not null.
3585 __ testl(obj, obj);
3586 __ j(kEqual, &zero);
3587 // Compare the class of `obj` with `cls`.
3588 __ movl(out, Address(obj, class_offset));
3589 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003590 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003591 } else {
3592 DCHECK(cls.IsStackSlot()) << cls;
3593 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3594 }
3595 if (instruction->IsClassFinal()) {
3596 // Classes must be equal for the instanceof to succeed.
3597 __ j(kNotEqual, &zero);
3598 __ movl(out, Immediate(1));
3599 __ jmp(&done);
3600 } else {
3601 // If the classes are not equal, we go into a slow path.
3602 DCHECK(locations->OnlyCallsOnSlowPath());
3603 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003604 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003605 codegen_->AddSlowPath(slow_path);
3606 __ j(kNotEqual, slow_path->GetEntryLabel());
3607 __ movl(out, Immediate(1));
3608 __ jmp(&done);
3609 }
3610 __ Bind(&zero);
3611 __ movl(out, Immediate(0));
3612 if (slow_path != nullptr) {
3613 __ Bind(slow_path->GetExitLabel());
3614 }
3615 __ Bind(&done);
3616}
3617
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003618void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3620 instruction, LocationSummary::kCallOnSlowPath);
3621 locations->SetInAt(0, Location::RequiresRegister());
3622 locations->SetInAt(1, Location::Any());
3623 locations->AddTemp(Location::RequiresRegister());
3624}
3625
3626void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3627 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003629 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003630 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003631 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3632 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3633 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3634 codegen_->AddSlowPath(slow_path);
3635
3636 // TODO: avoid this check if we know obj is not null.
3637 __ testl(obj, obj);
3638 __ j(kEqual, slow_path->GetExitLabel());
3639 // Compare the class of `obj` with `cls`.
3640 __ movl(temp, Address(obj, class_offset));
3641 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003642 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003643 } else {
3644 DCHECK(cls.IsStackSlot()) << cls;
3645 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3646 }
3647 // Classes must be equal for the checkcast to succeed.
3648 __ j(kNotEqual, slow_path->GetEntryLabel());
3649 __ Bind(slow_path->GetExitLabel());
3650}
3651
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003652void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3653 LocationSummary* locations =
3654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3655 InvokeRuntimeCallingConvention calling_convention;
3656 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3657}
3658
3659void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3660 __ gs()->call(Address::Absolute(instruction->IsEnter()
3661 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3662 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3663 true));
3664 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3665}
3666
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003667void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3668void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3669void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3670
3671void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3672 LocationSummary* locations =
3673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3674 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3675 || instruction->GetResultType() == Primitive::kPrimLong);
3676 locations->SetInAt(0, Location::RequiresRegister());
3677 if (instruction->GetType() == Primitive::kPrimInt) {
3678 locations->SetInAt(1, Location::Any());
3679 } else {
3680 // Request a register to avoid loading a 64bits constant.
3681 locations->SetInAt(1, Location::RequiresRegister());
3682 }
3683 locations->SetOut(Location::SameAsFirstInput());
3684}
3685
3686void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3687 HandleBitwiseOperation(instruction);
3688}
3689
3690void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3691 HandleBitwiseOperation(instruction);
3692}
3693
3694void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3695 HandleBitwiseOperation(instruction);
3696}
3697
3698void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3699 LocationSummary* locations = instruction->GetLocations();
3700 Location first = locations->InAt(0);
3701 Location second = locations->InAt(1);
3702 DCHECK(first.Equals(locations->Out()));
3703
3704 if (instruction->GetResultType() == Primitive::kPrimInt) {
3705 if (second.IsRegister()) {
3706 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003707 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003708 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003710 } else {
3711 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003712 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003713 }
3714 } else if (second.IsConstant()) {
3715 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3716 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003717 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003718 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003719 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003720 } else {
3721 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003723 }
3724 } else {
3725 Address address(CpuRegister(RSP), second.GetStackIndex());
3726 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003727 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003728 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003729 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003730 } else {
3731 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003733 }
3734 }
3735 } else {
3736 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3737 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003739 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003740 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003741 } else {
3742 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003743 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003744 }
3745 }
3746}
3747
Calin Juravleb1498f62015-02-16 13:13:29 +00003748void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
3749 // Nothing to do, this should be removed during prepare for register allocator.
3750 UNUSED(instruction);
3751 LOG(FATAL) << "Unreachable";
3752}
3753
3754void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
3755 // Nothing to do, this should be removed during prepare for register allocator.
3756 UNUSED(instruction);
3757 LOG(FATAL) << "Unreachable";
3758}
3759
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003760} // namespace x86_64
3761} // namespace art