blob: ed1371b64caa14c07d9f1a01c8390de5b75666fe [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
71 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
72 __ 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
87 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
88 __ 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
104 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
105 __ 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
136 virtual 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
172 virtual 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
205 virtual 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
252 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
253 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
289 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
290 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 Geoffray98893962015-01-21 12:32:32 +0000490 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000491 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000492 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000493 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000494 }
495 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100496
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000497 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
498 uint32_t xmm_spill_location = GetFpuSpillStart();
499 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100500
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
502 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
503 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
504 XmmRegister(kFpuCalleeSaves[i]));
505 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100506 }
507
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100508 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
509}
510
511void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000512 uint32_t xmm_spill_location = GetFpuSpillStart();
513 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
514 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
515 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
516 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
517 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
518 }
519 }
520
521 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000522
523 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000524 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000525 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000526 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000527 }
528 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100529}
530
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100531void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
532 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100533}
534
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100535void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
537}
538
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100539Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
540 switch (load->GetType()) {
541 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100542 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
544 break;
545
546 case Primitive::kPrimInt:
547 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100548 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100549 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100550
551 case Primitive::kPrimBoolean:
552 case Primitive::kPrimByte:
553 case Primitive::kPrimChar:
554 case Primitive::kPrimShort:
555 case Primitive::kPrimVoid:
556 LOG(FATAL) << "Unexpected type " << load->GetType();
557 }
558
559 LOG(FATAL) << "Unreachable";
560 return Location();
561}
562
563void CodeGeneratorX86_64::Move(Location destination, Location source) {
564 if (source.Equals(destination)) {
565 return;
566 }
567 if (destination.IsRegister()) {
568 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000569 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100570 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000571 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100572 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000573 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100575 } else {
576 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 Address(CpuRegister(RSP), source.GetStackIndex()));
579 }
580 } else if (destination.IsFpuRegister()) {
581 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000582 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000584 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100587 Address(CpuRegister(RSP), source.GetStackIndex()));
588 } else {
589 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100592 }
593 } else if (destination.IsStackSlot()) {
594 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100595 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000596 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100597 } else if (source.IsFpuRegister()) {
598 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000599 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500600 } else if (source.IsConstant()) {
601 HConstant* constant = source.GetConstant();
602 int32_t value;
603 if (constant->IsFloatConstant()) {
604 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
605 } else {
606 DCHECK(constant->IsIntConstant());
607 value = constant->AsIntConstant()->GetValue();
608 }
609 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100610 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500611 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000612 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
613 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100614 }
615 } else {
616 DCHECK(destination.IsDoubleStackSlot());
617 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else if (source.IsFpuRegister()) {
621 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500623 } else if (source.IsConstant()) {
624 HConstant* constant = source.GetConstant();
625 int64_t value = constant->AsLongConstant()->GetValue();
626 if (constant->IsDoubleConstant()) {
627 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
628 } else {
629 DCHECK(constant->IsLongConstant());
630 value = constant->AsLongConstant()->GetValue();
631 }
632 __ movq(CpuRegister(TMP), Immediate(value));
633 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100634 } else {
635 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000636 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
637 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100638 }
639 }
640}
641
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100642void CodeGeneratorX86_64::Move(HInstruction* instruction,
643 Location location,
644 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000645 LocationSummary* locations = instruction->GetLocations();
646 if (locations != nullptr && locations->Out().Equals(location)) {
647 return;
648 }
649
650 if (locations != nullptr && locations->Out().IsConstant()) {
651 HConstant* const_to_move = locations->Out().GetConstant();
652 if (const_to_move->IsIntConstant()) {
653 Immediate imm(const_to_move->AsIntConstant()->GetValue());
654 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000655 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000656 } else if (location.IsStackSlot()) {
657 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
658 } else {
659 DCHECK(location.IsConstant());
660 DCHECK_EQ(location.GetConstant(), const_to_move);
661 }
662 } else if (const_to_move->IsLongConstant()) {
663 int64_t value = const_to_move->AsLongConstant()->GetValue();
664 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000665 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000666 } else if (location.IsDoubleStackSlot()) {
667 __ movq(CpuRegister(TMP), Immediate(value));
668 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
669 } else {
670 DCHECK(location.IsConstant());
671 DCHECK_EQ(location.GetConstant(), const_to_move);
672 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100673 }
Roland Levillain476df552014-10-09 17:51:36 +0100674 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675 switch (instruction->GetType()) {
676 case Primitive::kPrimBoolean:
677 case Primitive::kPrimByte:
678 case Primitive::kPrimChar:
679 case Primitive::kPrimShort:
680 case Primitive::kPrimInt:
681 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100683 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
684 break;
685
686 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100687 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000688 Move(location,
689 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100690 break;
691
692 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100693 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100694 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000695 } else if (instruction->IsTemporary()) {
696 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
697 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100698 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100699 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100700 switch (instruction->GetType()) {
701 case Primitive::kPrimBoolean:
702 case Primitive::kPrimByte:
703 case Primitive::kPrimChar:
704 case Primitive::kPrimShort:
705 case Primitive::kPrimInt:
706 case Primitive::kPrimNot:
707 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100708 case Primitive::kPrimFloat:
709 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000710 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100711 break;
712
713 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100714 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100715 }
716 }
717}
718
719void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
720 got->SetLocations(nullptr);
721}
722
723void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
724 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100725 DCHECK(!successor->IsExitBlock());
726
727 HBasicBlock* block = got->GetBlock();
728 HInstruction* previous = got->GetPrevious();
729
730 HLoopInformation* info = block->GetLoopInformation();
731 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
732 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
733 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
734 return;
735 }
736
737 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
738 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
739 }
740 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100741 __ jmp(codegen_->GetLabelOf(successor));
742 }
743}
744
745void LocationsBuilderX86_64::VisitExit(HExit* exit) {
746 exit->SetLocations(nullptr);
747}
748
749void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700750 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100751 if (kIsDebugBuild) {
752 __ Comment("Unreachable");
753 __ int3();
754 }
755}
756
757void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100758 LocationSummary* locations =
759 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100760 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100761 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100762 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100763 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100764}
765
766void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700767 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100768 if (cond->IsIntConstant()) {
769 // Constant condition, statically compared against 1.
770 int32_t cond_value = cond->AsIntConstant()->GetValue();
771 if (cond_value == 1) {
772 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
773 if_instr->IfTrueSuccessor())) {
774 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100775 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100776 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100777 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100778 DCHECK_EQ(cond_value, 0);
779 }
780 } else {
781 bool materialized =
782 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
783 // Moves do not affect the eflags register, so if the condition is
784 // evaluated just before the if, we don't need to evaluate it
785 // again.
786 bool eflags_set = cond->IsCondition()
787 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
788 if (materialized) {
789 if (!eflags_set) {
790 // Materialized condition, compare against 0.
791 Location lhs = if_instr->GetLocations()->InAt(0);
792 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000793 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100794 } else {
795 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
796 Immediate(0));
797 }
798 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
799 } else {
800 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
801 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
802 }
803 } else {
804 Location lhs = cond->GetLocations()->InAt(0);
805 Location rhs = cond->GetLocations()->InAt(1);
806 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000807 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100808 } else if (rhs.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000809 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
810 if (constant == 0) {
811 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
812 } else {
813 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
814 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100815 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000816 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 Address(CpuRegister(RSP), rhs.GetStackIndex()));
818 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100819 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
820 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700821 }
Dave Allison20dfc792014-06-16 20:44:29 -0700822 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100823 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
824 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700825 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100826 }
827}
828
829void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
830 local->SetLocations(nullptr);
831}
832
833void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
834 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
835}
836
837void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
838 local->SetLocations(nullptr);
839}
840
841void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
842 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700843 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100844}
845
846void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100847 LocationSummary* locations =
848 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100849 switch (store->InputAt(1)->GetType()) {
850 case Primitive::kPrimBoolean:
851 case Primitive::kPrimByte:
852 case Primitive::kPrimChar:
853 case Primitive::kPrimShort:
854 case Primitive::kPrimInt:
855 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100857 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
858 break;
859
860 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100861 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100862 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
863 break;
864
865 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100866 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100867 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100868}
869
870void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700871 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872}
873
Dave Allison20dfc792014-06-16 20:44:29 -0700874void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100875 LocationSummary* locations =
876 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100877 locations->SetInAt(0, Location::RequiresRegister());
878 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100879 if (comp->NeedsMaterialization()) {
880 locations->SetOut(Location::RequiresRegister());
881 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100882}
883
Dave Allison20dfc792014-06-16 20:44:29 -0700884void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
885 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100886 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000887 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100888 // Clear register: setcc only sets the low byte.
889 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000890 Location lhs = locations->InAt(0);
891 Location rhs = locations->InAt(1);
892 if (rhs.IsRegister()) {
893 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
894 } else if (rhs.IsConstant()) {
895 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
896 if (constant == 0) {
897 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
898 } else {
899 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
900 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100901 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000902 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100903 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100904 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700905 }
906}
907
908void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
909 VisitCondition(comp);
910}
911
912void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
913 VisitCondition(comp);
914}
915
916void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
917 VisitCondition(comp);
918}
919
920void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
921 VisitCondition(comp);
922}
923
924void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
925 VisitCondition(comp);
926}
927
928void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
929 VisitCondition(comp);
930}
931
932void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
933 VisitCondition(comp);
934}
935
936void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
937 VisitCondition(comp);
938}
939
940void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
941 VisitCondition(comp);
942}
943
944void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
945 VisitCondition(comp);
946}
947
948void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
949 VisitCondition(comp);
950}
951
952void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
953 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100954}
955
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100956void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100957 LocationSummary* locations =
958 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000959 switch (compare->InputAt(0)->GetType()) {
960 case Primitive::kPrimLong: {
961 locations->SetInAt(0, Location::RequiresRegister());
962 locations->SetInAt(1, Location::RequiresRegister());
963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
964 break;
965 }
966 case Primitive::kPrimFloat:
967 case Primitive::kPrimDouble: {
968 locations->SetInAt(0, Location::RequiresFpuRegister());
969 locations->SetInAt(1, Location::RequiresFpuRegister());
970 locations->SetOut(Location::RequiresRegister());
971 break;
972 }
973 default:
974 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
975 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100976}
977
978void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100979 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000980 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000981 Location left = locations->InAt(0);
982 Location right = locations->InAt(1);
983
984 Label less, greater, done;
985 Primitive::Type type = compare->InputAt(0)->GetType();
986 switch (type) {
987 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000988 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100989 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000990 }
991 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000992 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000993 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
994 break;
995 }
996 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000997 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000998 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
999 break;
1000 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001001 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001002 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001003 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001004 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001005 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001006 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001007
Calin Juravle91debbc2014-11-26 19:01:09 +00001008 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001009 __ movl(out, Immediate(1));
1010 __ jmp(&done);
1011
1012 __ Bind(&less);
1013 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001014
1015 __ Bind(&done);
1016}
1017
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001018void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001019 LocationSummary* locations =
1020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001021 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001022}
1023
1024void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001025 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001026 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001027}
1028
1029void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001030 LocationSummary* locations =
1031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001032 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033}
1034
1035void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001036 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001037 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001038}
1039
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001040void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1041 LocationSummary* locations =
1042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1043 locations->SetOut(Location::ConstantLocation(constant));
1044}
1045
1046void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1047 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001048 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001049}
1050
1051void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1052 LocationSummary* locations =
1053 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1054 locations->SetOut(Location::ConstantLocation(constant));
1055}
1056
1057void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1058 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001059 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001060}
1061
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1063 ret->SetLocations(nullptr);
1064}
1065
1066void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001067 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068 codegen_->GenerateFrameExit();
1069 __ ret();
1070}
1071
1072void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001073 LocationSummary* locations =
1074 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075 switch (ret->InputAt(0)->GetType()) {
1076 case Primitive::kPrimBoolean:
1077 case Primitive::kPrimByte:
1078 case Primitive::kPrimChar:
1079 case Primitive::kPrimShort:
1080 case Primitive::kPrimInt:
1081 case Primitive::kPrimNot:
1082 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001083 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001084 break;
1085
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 case Primitive::kPrimFloat:
1087 case Primitive::kPrimDouble:
1088 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 break;
1091
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095}
1096
1097void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1098 if (kIsDebugBuild) {
1099 switch (ret->InputAt(0)->GetType()) {
1100 case Primitive::kPrimBoolean:
1101 case Primitive::kPrimByte:
1102 case Primitive::kPrimChar:
1103 case Primitive::kPrimShort:
1104 case Primitive::kPrimInt:
1105 case Primitive::kPrimNot:
1106 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108 break;
1109
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 case Primitive::kPrimFloat:
1111 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001112 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 XMM0);
1114 break;
1115
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 }
1119 }
1120 codegen_->GenerateFrameExit();
1121 __ ret();
1122}
1123
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001124Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1125 switch (type) {
1126 case Primitive::kPrimBoolean:
1127 case Primitive::kPrimByte:
1128 case Primitive::kPrimChar:
1129 case Primitive::kPrimShort:
1130 case Primitive::kPrimInt:
1131 case Primitive::kPrimNot: {
1132 uint32_t index = gp_index_++;
1133 stack_index_++;
1134 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001135 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136 } else {
1137 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1138 }
1139 }
1140
1141 case Primitive::kPrimLong: {
1142 uint32_t index = gp_index_;
1143 stack_index_ += 2;
1144 if (index < calling_convention.GetNumberOfRegisters()) {
1145 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001146 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147 } else {
1148 gp_index_ += 2;
1149 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1150 }
1151 }
1152
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001153 case Primitive::kPrimFloat: {
1154 uint32_t index = fp_index_++;
1155 stack_index_++;
1156 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 } else {
1159 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1160 }
1161 }
1162
1163 case Primitive::kPrimDouble: {
1164 uint32_t index = fp_index_++;
1165 stack_index_ += 2;
1166 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else {
1169 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1170 }
1171 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001172
1173 case Primitive::kPrimVoid:
1174 LOG(FATAL) << "Unexpected parameter type " << type;
1175 break;
1176 }
1177 return Location();
1178}
1179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001180void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001181 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1182 if (intrinsic.TryDispatch(invoke)) {
1183 return;
1184 }
1185
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001186 HandleInvoke(invoke);
1187}
1188
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001189static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1190 if (invoke->GetLocations()->Intrinsified()) {
1191 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1192 intrinsic.Dispatch(invoke);
1193 return true;
1194 }
1195 return false;
1196}
1197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001198void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001199 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1200 return;
1201 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001202
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001203 codegen_->GenerateStaticOrDirectCall(
1204 invoke,
1205 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001206}
1207
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001208void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001209 LocationSummary* locations =
1210 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001211 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001212
1213 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001214 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215 HInstruction* input = invoke->InputAt(i);
1216 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1217 }
1218
1219 switch (invoke->GetType()) {
1220 case Primitive::kPrimBoolean:
1221 case Primitive::kPrimByte:
1222 case Primitive::kPrimChar:
1223 case Primitive::kPrimShort:
1224 case Primitive::kPrimInt:
1225 case Primitive::kPrimNot:
1226 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001227 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 break;
1229
1230 case Primitive::kPrimVoid:
1231 break;
1232
1233 case Primitive::kPrimDouble:
1234 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001235 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001236 break;
1237 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001238}
1239
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001240void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001241 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1242 if (intrinsic.TryDispatch(invoke)) {
1243 return;
1244 }
1245
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001246 HandleInvoke(invoke);
1247}
1248
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001249void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001250 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1251 return;
1252 }
1253
Roland Levillain271ab9c2014-11-27 15:23:57 +00001254 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001255 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1256 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1257 LocationSummary* locations = invoke->GetLocations();
1258 Location receiver = locations->InAt(0);
1259 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1260 // temp = object->GetClass();
1261 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1263 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001264 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001265 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001266 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001267 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 // temp = temp->GetMethodAt(method_offset);
1269 __ movl(temp, Address(temp, method_offset));
1270 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001271 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001272 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001274 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001275 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276}
1277
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001278void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1279 HandleInvoke(invoke);
1280 // Add the hidden argument.
1281 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1282}
1283
1284void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1285 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001286 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001287 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1288 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1289 LocationSummary* locations = invoke->GetLocations();
1290 Location receiver = locations->InAt(0);
1291 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1292
1293 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001294 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001295 Immediate(invoke->GetDexMethodIndex()));
1296
1297 // temp = object->GetClass();
1298 if (receiver.IsStackSlot()) {
1299 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1300 __ movl(temp, Address(temp, class_offset));
1301 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001302 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001303 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001304 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001305 // temp = temp->GetImtEntryAt(method_offset);
1306 __ movl(temp, Address(temp, method_offset));
1307 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001308 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001309 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310
1311 DCHECK(!codegen_->IsLeafMethod());
1312 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1313}
1314
Roland Levillain88cb1752014-10-20 16:36:47 +01001315void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1316 LocationSummary* locations =
1317 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1318 switch (neg->GetResultType()) {
1319 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001320 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001321 locations->SetInAt(0, Location::RequiresRegister());
1322 locations->SetOut(Location::SameAsFirstInput());
1323 break;
1324
Roland Levillain88cb1752014-10-20 16:36:47 +01001325 case Primitive::kPrimFloat:
1326 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001327 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001328 locations->SetOut(Location::SameAsFirstInput());
1329 locations->AddTemp(Location::RequiresRegister());
1330 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001331 break;
1332
1333 default:
1334 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1335 }
1336}
1337
1338void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1339 LocationSummary* locations = neg->GetLocations();
1340 Location out = locations->Out();
1341 Location in = locations->InAt(0);
1342 switch (neg->GetResultType()) {
1343 case Primitive::kPrimInt:
1344 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001345 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001346 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001347 break;
1348
1349 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001350 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001352 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001353 break;
1354
Roland Levillain5368c212014-11-27 15:03:41 +00001355 case Primitive::kPrimFloat: {
1356 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1358 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001359 // Implement float negation with an exclusive or with value
1360 // 0x80000000 (mask for bit 31, representing the sign of a
1361 // single-precision floating-point number).
1362 __ movq(constant, Immediate(INT64_C(0x80000000)));
1363 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001364 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001365 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001366 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001367
Roland Levillain5368c212014-11-27 15:03:41 +00001368 case Primitive::kPrimDouble: {
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 double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001373 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001374 // a double-precision floating-point number).
1375 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1376 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001378 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001379 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001380
1381 default:
1382 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1383 }
1384}
1385
Roland Levillaindff1f282014-11-05 14:15:05 +00001386void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1387 LocationSummary* locations =
1388 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1389 Primitive::Type result_type = conversion->GetResultType();
1390 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001391 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001392 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001393 case Primitive::kPrimByte:
1394 switch (input_type) {
1395 case Primitive::kPrimShort:
1396 case Primitive::kPrimInt:
1397 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001398 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001399 locations->SetInAt(0, Location::Any());
1400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1401 break;
1402
1403 default:
1404 LOG(FATAL) << "Unexpected type conversion from " << input_type
1405 << " to " << result_type;
1406 }
1407 break;
1408
Roland Levillain01a8d712014-11-14 16:27:39 +00001409 case Primitive::kPrimShort:
1410 switch (input_type) {
1411 case Primitive::kPrimByte:
1412 case Primitive::kPrimInt:
1413 case Primitive::kPrimChar:
1414 // Processing a Dex `int-to-short' instruction.
1415 locations->SetInAt(0, Location::Any());
1416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1417 break;
1418
1419 default:
1420 LOG(FATAL) << "Unexpected type conversion from " << input_type
1421 << " to " << result_type;
1422 }
1423 break;
1424
Roland Levillain946e1432014-11-11 17:35:19 +00001425 case Primitive::kPrimInt:
1426 switch (input_type) {
1427 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001428 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001429 locations->SetInAt(0, Location::Any());
1430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1431 break;
1432
1433 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001434 // Processing a Dex `float-to-int' instruction.
1435 locations->SetInAt(0, Location::RequiresFpuRegister());
1436 locations->SetOut(Location::RequiresRegister());
1437 locations->AddTemp(Location::RequiresFpuRegister());
1438 break;
1439
Roland Levillain946e1432014-11-11 17:35:19 +00001440 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001441 // Processing a Dex `double-to-int' instruction.
1442 locations->SetInAt(0, Location::RequiresFpuRegister());
1443 locations->SetOut(Location::RequiresRegister());
1444 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001445 break;
1446
1447 default:
1448 LOG(FATAL) << "Unexpected type conversion from " << input_type
1449 << " to " << result_type;
1450 }
1451 break;
1452
Roland Levillaindff1f282014-11-05 14:15:05 +00001453 case Primitive::kPrimLong:
1454 switch (input_type) {
1455 case Primitive::kPrimByte:
1456 case Primitive::kPrimShort:
1457 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001458 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001459 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001460 // TODO: We would benefit from a (to-be-implemented)
1461 // Location::RegisterOrStackSlot requirement for this input.
1462 locations->SetInAt(0, Location::RequiresRegister());
1463 locations->SetOut(Location::RequiresRegister());
1464 break;
1465
1466 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001467 // Processing a Dex `float-to-long' instruction.
1468 locations->SetInAt(0, Location::RequiresFpuRegister());
1469 locations->SetOut(Location::RequiresRegister());
1470 locations->AddTemp(Location::RequiresFpuRegister());
1471 break;
1472
Roland Levillaindff1f282014-11-05 14:15:05 +00001473 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001474 // Processing a Dex `double-to-long' instruction.
1475 locations->SetInAt(0, Location::RequiresFpuRegister());
1476 locations->SetOut(Location::RequiresRegister());
1477 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 break;
1479
1480 default:
1481 LOG(FATAL) << "Unexpected type conversion from " << input_type
1482 << " to " << result_type;
1483 }
1484 break;
1485
Roland Levillain981e4542014-11-14 11:47:14 +00001486 case Primitive::kPrimChar:
1487 switch (input_type) {
1488 case Primitive::kPrimByte:
1489 case Primitive::kPrimShort:
1490 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001491 // Processing a Dex `int-to-char' instruction.
1492 locations->SetInAt(0, Location::Any());
1493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1494 break;
1495
1496 default:
1497 LOG(FATAL) << "Unexpected type conversion from " << input_type
1498 << " to " << result_type;
1499 }
1500 break;
1501
Roland Levillaindff1f282014-11-05 14:15:05 +00001502 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001503 switch (input_type) {
1504 case Primitive::kPrimByte:
1505 case Primitive::kPrimShort:
1506 case Primitive::kPrimInt:
1507 case Primitive::kPrimChar:
1508 // Processing a Dex `int-to-float' instruction.
1509 locations->SetInAt(0, Location::RequiresRegister());
1510 locations->SetOut(Location::RequiresFpuRegister());
1511 break;
1512
1513 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001514 // Processing a Dex `long-to-float' instruction.
1515 locations->SetInAt(0, Location::RequiresRegister());
1516 locations->SetOut(Location::RequiresFpuRegister());
1517 break;
1518
Roland Levillaincff13742014-11-17 14:32:17 +00001519 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001520 // Processing a Dex `double-to-float' instruction.
1521 locations->SetInAt(0, Location::RequiresFpuRegister());
1522 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001523 break;
1524
1525 default:
1526 LOG(FATAL) << "Unexpected type conversion from " << input_type
1527 << " to " << result_type;
1528 };
1529 break;
1530
Roland Levillaindff1f282014-11-05 14:15:05 +00001531 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001532 switch (input_type) {
1533 case Primitive::kPrimByte:
1534 case Primitive::kPrimShort:
1535 case Primitive::kPrimInt:
1536 case Primitive::kPrimChar:
1537 // Processing a Dex `int-to-double' instruction.
1538 locations->SetInAt(0, Location::RequiresRegister());
1539 locations->SetOut(Location::RequiresFpuRegister());
1540 break;
1541
1542 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001543 // Processing a Dex `long-to-double' instruction.
1544 locations->SetInAt(0, Location::RequiresRegister());
1545 locations->SetOut(Location::RequiresFpuRegister());
1546 break;
1547
Roland Levillaincff13742014-11-17 14:32:17 +00001548 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001549 // Processing a Dex `float-to-double' instruction.
1550 locations->SetInAt(0, Location::RequiresFpuRegister());
1551 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001552 break;
1553
1554 default:
1555 LOG(FATAL) << "Unexpected type conversion from " << input_type
1556 << " to " << result_type;
1557 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001558 break;
1559
1560 default:
1561 LOG(FATAL) << "Unexpected type conversion from " << input_type
1562 << " to " << result_type;
1563 }
1564}
1565
1566void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1567 LocationSummary* locations = conversion->GetLocations();
1568 Location out = locations->Out();
1569 Location in = locations->InAt(0);
1570 Primitive::Type result_type = conversion->GetResultType();
1571 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001572 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001573 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001574 case Primitive::kPrimByte:
1575 switch (input_type) {
1576 case Primitive::kPrimShort:
1577 case Primitive::kPrimInt:
1578 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001579 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001580 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001582 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001583 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001584 Address(CpuRegister(RSP), in.GetStackIndex()));
1585 } else {
1586 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001587 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001588 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1589 }
1590 break;
1591
1592 default:
1593 LOG(FATAL) << "Unexpected type conversion from " << input_type
1594 << " to " << result_type;
1595 }
1596 break;
1597
Roland Levillain01a8d712014-11-14 16:27:39 +00001598 case Primitive::kPrimShort:
1599 switch (input_type) {
1600 case Primitive::kPrimByte:
1601 case Primitive::kPrimInt:
1602 case Primitive::kPrimChar:
1603 // Processing a Dex `int-to-short' instruction.
1604 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001606 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001608 Address(CpuRegister(RSP), in.GetStackIndex()));
1609 } else {
1610 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001611 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001612 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1613 }
1614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620 break;
1621
Roland Levillain946e1432014-11-11 17:35:19 +00001622 case Primitive::kPrimInt:
1623 switch (input_type) {
1624 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001625 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001626 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001627 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001628 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001629 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001630 Address(CpuRegister(RSP), in.GetStackIndex()));
1631 } else {
1632 DCHECK(in.IsConstant());
1633 DCHECK(in.GetConstant()->IsLongConstant());
1634 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001635 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001636 }
1637 break;
1638
Roland Levillain3f8f9362014-12-02 17:45:01 +00001639 case Primitive::kPrimFloat: {
1640 // Processing a Dex `float-to-int' instruction.
1641 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1642 CpuRegister output = out.AsRegister<CpuRegister>();
1643 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1644 Label done, nan;
1645
1646 __ movl(output, Immediate(kPrimIntMax));
1647 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001648 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001649 // if input >= temp goto done
1650 __ comiss(input, temp);
1651 __ j(kAboveEqual, &done);
1652 // if input == NaN goto nan
1653 __ j(kUnordered, &nan);
1654 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001655 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001656 __ jmp(&done);
1657 __ Bind(&nan);
1658 // output = 0
1659 __ xorl(output, output);
1660 __ Bind(&done);
1661 break;
1662 }
1663
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001664 case Primitive::kPrimDouble: {
1665 // Processing a Dex `double-to-int' instruction.
1666 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1667 CpuRegister output = out.AsRegister<CpuRegister>();
1668 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1669 Label done, nan;
1670
1671 __ movl(output, Immediate(kPrimIntMax));
1672 // temp = int-to-double(output)
1673 __ cvtsi2sd(temp, output);
1674 // if input >= temp goto done
1675 __ comisd(input, temp);
1676 __ j(kAboveEqual, &done);
1677 // if input == NaN goto nan
1678 __ j(kUnordered, &nan);
1679 // output = double-to-int-truncate(input)
1680 __ cvttsd2si(output, input);
1681 __ jmp(&done);
1682 __ Bind(&nan);
1683 // output = 0
1684 __ xorl(output, output);
1685 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001686 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001687 }
Roland Levillain946e1432014-11-11 17:35:19 +00001688
1689 default:
1690 LOG(FATAL) << "Unexpected type conversion from " << input_type
1691 << " to " << result_type;
1692 }
1693 break;
1694
Roland Levillaindff1f282014-11-05 14:15:05 +00001695 case Primitive::kPrimLong:
1696 switch (input_type) {
1697 DCHECK(out.IsRegister());
1698 case Primitive::kPrimByte:
1699 case Primitive::kPrimShort:
1700 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001701 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001702 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001703 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001704 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001705 break;
1706
Roland Levillain624279f2014-12-04 11:54:28 +00001707 case Primitive::kPrimFloat: {
1708 // Processing a Dex `float-to-long' instruction.
1709 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1710 CpuRegister output = out.AsRegister<CpuRegister>();
1711 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1712 Label done, nan;
1713
1714 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001715 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001716 __ cvtsi2ss(temp, output, true);
1717 // if input >= temp goto done
1718 __ comiss(input, temp);
1719 __ j(kAboveEqual, &done);
1720 // if input == NaN goto nan
1721 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001722 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001723 __ cvttss2si(output, input, true);
1724 __ jmp(&done);
1725 __ Bind(&nan);
1726 // output = 0
1727 __ xorq(output, output);
1728 __ Bind(&done);
1729 break;
1730 }
1731
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001732 case Primitive::kPrimDouble: {
1733 // Processing a Dex `double-to-long' instruction.
1734 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1735 CpuRegister output = out.AsRegister<CpuRegister>();
1736 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1737 Label done, nan;
1738
1739 __ movq(output, Immediate(kPrimLongMax));
1740 // temp = long-to-double(output)
1741 __ cvtsi2sd(temp, output, true);
1742 // if input >= temp goto done
1743 __ comisd(input, temp);
1744 __ j(kAboveEqual, &done);
1745 // if input == NaN goto nan
1746 __ j(kUnordered, &nan);
1747 // output = double-to-long-truncate(input)
1748 __ cvttsd2si(output, input, true);
1749 __ jmp(&done);
1750 __ Bind(&nan);
1751 // output = 0
1752 __ xorq(output, output);
1753 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001755 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001756
1757 default:
1758 LOG(FATAL) << "Unexpected type conversion from " << input_type
1759 << " to " << result_type;
1760 }
1761 break;
1762
Roland Levillain981e4542014-11-14 11:47:14 +00001763 case Primitive::kPrimChar:
1764 switch (input_type) {
1765 case Primitive::kPrimByte:
1766 case Primitive::kPrimShort:
1767 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001768 // Processing a Dex `int-to-char' instruction.
1769 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001770 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001771 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001772 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001773 Address(CpuRegister(RSP), in.GetStackIndex()));
1774 } else {
1775 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001777 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1778 }
1779 break;
1780
1781 default:
1782 LOG(FATAL) << "Unexpected type conversion from " << input_type
1783 << " to " << result_type;
1784 }
1785 break;
1786
Roland Levillaindff1f282014-11-05 14:15:05 +00001787 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001788 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001789 case Primitive::kPrimByte:
1790 case Primitive::kPrimShort:
1791 case Primitive::kPrimInt:
1792 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001793 // Processing a Dex `int-to-float' instruction.
1794 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001795 break;
1796
1797 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001798 // Processing a Dex `long-to-float' instruction.
1799 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1800 break;
1801
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001803 // Processing a Dex `double-to-float' instruction.
1804 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001805 break;
1806
1807 default:
1808 LOG(FATAL) << "Unexpected type conversion from " << input_type
1809 << " to " << result_type;
1810 };
1811 break;
1812
Roland Levillaindff1f282014-11-05 14:15:05 +00001813 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001814 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001815 case Primitive::kPrimByte:
1816 case Primitive::kPrimShort:
1817 case Primitive::kPrimInt:
1818 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001819 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001820 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001821 break;
1822
1823 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001824 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001825 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001826 break;
1827
Roland Levillaincff13742014-11-17 14:32:17 +00001828 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001829 // Processing a Dex `float-to-double' instruction.
1830 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001831 break;
1832
1833 default:
1834 LOG(FATAL) << "Unexpected type conversion from " << input_type
1835 << " to " << result_type;
1836 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001837 break;
1838
1839 default:
1840 LOG(FATAL) << "Unexpected type conversion from " << input_type
1841 << " to " << result_type;
1842 }
1843}
1844
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001845void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001848 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001849 case Primitive::kPrimInt: {
1850 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001851 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001853 break;
1854 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001855
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001856 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001857 locations->SetInAt(0, Location::RequiresRegister());
1858 locations->SetInAt(1, Location::RequiresRegister());
1859 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001860 break;
1861 }
1862
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 case Primitive::kPrimDouble:
1864 case Primitive::kPrimFloat: {
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001866 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001868 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001869 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001870
1871 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001872 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001873 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001874}
1875
1876void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1877 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001878 Location first = locations->InAt(0);
1879 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001880 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001881
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001882 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001883 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001885 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1886 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1887 } else {
1888 __ leal(out.AsRegister<CpuRegister>(), Address(
1889 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1890 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001892 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1893 __ addl(out.AsRegister<CpuRegister>(),
1894 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
1895 } else {
1896 __ leal(out.AsRegister<CpuRegister>(), Address(
1897 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
1898 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001899 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001900 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001901 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001902 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001903 break;
1904 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001905
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001906 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001907 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001908 break;
1909 }
1910
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001911 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001912 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001913 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001914 }
1915
1916 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001917 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001918 break;
1919 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001920
1921 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001922 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001923 }
1924}
1925
1926void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001927 LocationSummary* locations =
1928 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001929 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001930 case Primitive::kPrimInt: {
1931 locations->SetInAt(0, Location::RequiresRegister());
1932 locations->SetInAt(1, Location::Any());
1933 locations->SetOut(Location::SameAsFirstInput());
1934 break;
1935 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001936 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001937 locations->SetInAt(0, Location::RequiresRegister());
1938 locations->SetInAt(1, Location::RequiresRegister());
1939 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001940 break;
1941 }
Calin Juravle11351682014-10-23 15:38:15 +01001942 case Primitive::kPrimFloat:
1943 case Primitive::kPrimDouble: {
1944 locations->SetInAt(0, Location::RequiresFpuRegister());
1945 locations->SetInAt(1, Location::RequiresFpuRegister());
1946 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001947 break;
Calin Juravle11351682014-10-23 15:38:15 +01001948 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001949 default:
Calin Juravle11351682014-10-23 15:38:15 +01001950 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001952}
1953
1954void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1955 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001956 Location first = locations->InAt(0);
1957 Location second = locations->InAt(1);
1958 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001959 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001960 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001961 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001963 } else if (second.IsConstant()) {
1964 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001965 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001966 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001967 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001968 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001969 break;
1970 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001971 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001973 break;
1974 }
1975
Calin Juravle11351682014-10-23 15:38:15 +01001976 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001977 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001978 break;
Calin Juravle11351682014-10-23 15:38:15 +01001979 }
1980
1981 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001982 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001983 break;
1984 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001985
1986 default:
Calin Juravle11351682014-10-23 15:38:15 +01001987 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001988 }
1989}
1990
Calin Juravle34bacdf2014-10-07 20:23:36 +01001991void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1992 LocationSummary* locations =
1993 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1994 switch (mul->GetResultType()) {
1995 case Primitive::kPrimInt: {
1996 locations->SetInAt(0, Location::RequiresRegister());
1997 locations->SetInAt(1, Location::Any());
1998 locations->SetOut(Location::SameAsFirstInput());
1999 break;
2000 }
2001 case Primitive::kPrimLong: {
2002 locations->SetInAt(0, Location::RequiresRegister());
2003 locations->SetInAt(1, Location::RequiresRegister());
2004 locations->SetOut(Location::SameAsFirstInput());
2005 break;
2006 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002007 case Primitive::kPrimFloat:
2008 case Primitive::kPrimDouble: {
2009 locations->SetInAt(0, Location::RequiresFpuRegister());
2010 locations->SetInAt(1, Location::RequiresFpuRegister());
2011 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002012 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002013 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002014
2015 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002016 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002017 }
2018}
2019
2020void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2021 LocationSummary* locations = mul->GetLocations();
2022 Location first = locations->InAt(0);
2023 Location second = locations->InAt(1);
2024 DCHECK(first.Equals(locations->Out()));
2025 switch (mul->GetResultType()) {
2026 case Primitive::kPrimInt: {
2027 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002028 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002029 } else if (second.IsConstant()) {
2030 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002032 } else {
2033 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002034 __ imull(first.AsRegister<CpuRegister>(),
2035 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002036 }
2037 break;
2038 }
2039 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002040 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002041 break;
2042 }
2043
Calin Juravleb5bfa962014-10-21 18:02:24 +01002044 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002046 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002047 }
2048
2049 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002051 break;
2052 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002053
2054 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002055 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002056 }
2057}
2058
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002059void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2060 uint32_t stack_adjustment, bool is_float) {
2061 if (source.IsStackSlot()) {
2062 DCHECK(is_float);
2063 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2064 } else if (source.IsDoubleStackSlot()) {
2065 DCHECK(!is_float);
2066 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2067 } else {
2068 // Write the value to the temporary location on the stack and load to FP stack.
2069 if (is_float) {
2070 Location stack_temp = Location::StackSlot(temp_offset);
2071 codegen_->Move(stack_temp, source);
2072 __ flds(Address(CpuRegister(RSP), temp_offset));
2073 } else {
2074 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2075 codegen_->Move(stack_temp, source);
2076 __ fldl(Address(CpuRegister(RSP), temp_offset));
2077 }
2078 }
2079}
2080
2081void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2082 Primitive::Type type = rem->GetResultType();
2083 bool is_float = type == Primitive::kPrimFloat;
2084 size_t elem_size = Primitive::ComponentSize(type);
2085 LocationSummary* locations = rem->GetLocations();
2086 Location first = locations->InAt(0);
2087 Location second = locations->InAt(1);
2088 Location out = locations->Out();
2089
2090 // Create stack space for 2 elements.
2091 // TODO: enhance register allocator to ask for stack temporaries.
2092 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2093
2094 // Load the values to the FP stack in reverse order, using temporaries if needed.
2095 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2096 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2097
2098 // Loop doing FPREM until we stabilize.
2099 Label retry;
2100 __ Bind(&retry);
2101 __ fprem();
2102
2103 // Move FP status to AX.
2104 __ fstsw();
2105
2106 // And see if the argument reduction is complete. This is signaled by the
2107 // C2 FPU flag bit set to 0.
2108 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2109 __ j(kNotEqual, &retry);
2110
2111 // We have settled on the final value. Retrieve it into an XMM register.
2112 // Store FP top of stack to real stack.
2113 if (is_float) {
2114 __ fsts(Address(CpuRegister(RSP), 0));
2115 } else {
2116 __ fstl(Address(CpuRegister(RSP), 0));
2117 }
2118
2119 // Pop the 2 items from the FP stack.
2120 __ fucompp();
2121
2122 // Load the value from the stack into an XMM register.
2123 DCHECK(out.IsFpuRegister()) << out;
2124 if (is_float) {
2125 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2126 } else {
2127 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2128 }
2129
2130 // And remove the temporary stack space we allocated.
2131 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2132}
2133
Calin Juravlebacfec32014-11-14 15:54:36 +00002134void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2135 DCHECK(instruction->IsDiv() || instruction->IsRem());
2136 Primitive::Type type = instruction->GetResultType();
2137 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2138
2139 bool is_div = instruction->IsDiv();
2140 LocationSummary* locations = instruction->GetLocations();
2141
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2143 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002144
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002146 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2147
2148 SlowPathCodeX86_64* slow_path =
2149 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2150 out_reg.AsRegister(), type, is_div);
2151 codegen_->AddSlowPath(slow_path);
2152
2153 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2154 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2155 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002156 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002157 __ cmpl(second_reg, Immediate(-1));
2158 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002159 // edx:eax <- sign-extended of eax
2160 __ cdq();
2161 // eax = quotient, edx = remainder
2162 __ idivl(second_reg);
2163 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002164 __ cmpq(second_reg, Immediate(-1));
2165 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002166 // rdx:rax <- sign-extended of rax
2167 __ cqo();
2168 // rax = quotient, rdx = remainder
2169 __ idivq(second_reg);
2170 }
2171
2172 __ Bind(slow_path->GetExitLabel());
2173}
2174
Calin Juravle7c4954d2014-10-28 16:57:40 +00002175void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2176 LocationSummary* locations =
2177 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2178 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002179 case Primitive::kPrimInt:
2180 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002181 locations->SetInAt(0, Location::RegisterLocation(RAX));
2182 locations->SetInAt(1, Location::RequiresRegister());
2183 locations->SetOut(Location::SameAsFirstInput());
2184 // Intel uses edx:eax as the dividend.
2185 locations->AddTemp(Location::RegisterLocation(RDX));
2186 break;
2187 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002188
Calin Juravle7c4954d2014-10-28 16:57:40 +00002189 case Primitive::kPrimFloat:
2190 case Primitive::kPrimDouble: {
2191 locations->SetInAt(0, Location::RequiresFpuRegister());
2192 locations->SetInAt(1, Location::RequiresFpuRegister());
2193 locations->SetOut(Location::SameAsFirstInput());
2194 break;
2195 }
2196
2197 default:
2198 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2199 }
2200}
2201
2202void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2203 LocationSummary* locations = div->GetLocations();
2204 Location first = locations->InAt(0);
2205 Location second = locations->InAt(1);
2206 DCHECK(first.Equals(locations->Out()));
2207
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002208 Primitive::Type type = div->GetResultType();
2209 switch (type) {
2210 case Primitive::kPrimInt:
2211 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002212 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002213 break;
2214 }
2215
Calin Juravle7c4954d2014-10-28 16:57:40 +00002216 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002218 break;
2219 }
2220
2221 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002222 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002223 break;
2224 }
2225
2226 default:
2227 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2228 }
2229}
2230
Calin Juravlebacfec32014-11-14 15:54:36 +00002231void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002232 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002233 LocationSummary* locations =
2234 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002235
2236 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002237 case Primitive::kPrimInt:
2238 case Primitive::kPrimLong: {
2239 locations->SetInAt(0, Location::RegisterLocation(RAX));
2240 locations->SetInAt(1, Location::RequiresRegister());
2241 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2242 locations->SetOut(Location::RegisterLocation(RDX));
2243 break;
2244 }
2245
2246 case Primitive::kPrimFloat:
2247 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002248 locations->SetInAt(0, Location::Any());
2249 locations->SetInAt(1, Location::Any());
2250 locations->SetOut(Location::RequiresFpuRegister());
2251 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002252 break;
2253 }
2254
2255 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002256 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002257 }
2258}
2259
2260void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2261 Primitive::Type type = rem->GetResultType();
2262 switch (type) {
2263 case Primitive::kPrimInt:
2264 case Primitive::kPrimLong: {
2265 GenerateDivRemIntegral(rem);
2266 break;
2267 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002268 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002269 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002270 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002271 break;
2272 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002273 default:
2274 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2275 }
2276}
2277
Calin Juravled0d48522014-11-04 16:40:20 +00002278void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2279 LocationSummary* locations =
2280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2281 locations->SetInAt(0, Location::Any());
2282 if (instruction->HasUses()) {
2283 locations->SetOut(Location::SameAsFirstInput());
2284 }
2285}
2286
2287void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2288 SlowPathCodeX86_64* slow_path =
2289 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2290 codegen_->AddSlowPath(slow_path);
2291
2292 LocationSummary* locations = instruction->GetLocations();
2293 Location value = locations->InAt(0);
2294
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002295 switch (instruction->GetType()) {
2296 case Primitive::kPrimInt: {
2297 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002298 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002299 __ j(kEqual, slow_path->GetEntryLabel());
2300 } else if (value.IsStackSlot()) {
2301 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2302 __ j(kEqual, slow_path->GetEntryLabel());
2303 } else {
2304 DCHECK(value.IsConstant()) << value;
2305 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2306 __ jmp(slow_path->GetEntryLabel());
2307 }
2308 }
2309 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002310 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002311 case Primitive::kPrimLong: {
2312 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002314 __ j(kEqual, slow_path->GetEntryLabel());
2315 } else if (value.IsDoubleStackSlot()) {
2316 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2317 __ j(kEqual, slow_path->GetEntryLabel());
2318 } else {
2319 DCHECK(value.IsConstant()) << value;
2320 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2321 __ jmp(slow_path->GetEntryLabel());
2322 }
2323 }
2324 break;
2325 }
2326 default:
2327 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002328 }
Calin Juravled0d48522014-11-04 16:40:20 +00002329}
2330
Calin Juravle9aec02f2014-11-18 23:06:35 +00002331void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2332 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2333
2334 LocationSummary* locations =
2335 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2336
2337 switch (op->GetResultType()) {
2338 case Primitive::kPrimInt:
2339 case Primitive::kPrimLong: {
2340 locations->SetInAt(0, Location::RequiresRegister());
2341 // The shift count needs to be in CL.
2342 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2343 locations->SetOut(Location::SameAsFirstInput());
2344 break;
2345 }
2346 default:
2347 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2348 }
2349}
2350
2351void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2352 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2353
2354 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002356 Location second = locations->InAt(1);
2357
2358 switch (op->GetResultType()) {
2359 case Primitive::kPrimInt: {
2360 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002362 if (op->IsShl()) {
2363 __ shll(first_reg, second_reg);
2364 } else if (op->IsShr()) {
2365 __ sarl(first_reg, second_reg);
2366 } else {
2367 __ shrl(first_reg, second_reg);
2368 }
2369 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002370 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002371 if (op->IsShl()) {
2372 __ shll(first_reg, imm);
2373 } else if (op->IsShr()) {
2374 __ sarl(first_reg, imm);
2375 } else {
2376 __ shrl(first_reg, imm);
2377 }
2378 }
2379 break;
2380 }
2381 case Primitive::kPrimLong: {
2382 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002384 if (op->IsShl()) {
2385 __ shlq(first_reg, second_reg);
2386 } else if (op->IsShr()) {
2387 __ sarq(first_reg, second_reg);
2388 } else {
2389 __ shrq(first_reg, second_reg);
2390 }
2391 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002392 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002393 if (op->IsShl()) {
2394 __ shlq(first_reg, imm);
2395 } else if (op->IsShr()) {
2396 __ sarq(first_reg, imm);
2397 } else {
2398 __ shrq(first_reg, imm);
2399 }
2400 }
2401 break;
2402 }
2403 default:
2404 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2405 }
2406}
2407
2408void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2409 HandleShift(shl);
2410}
2411
2412void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2413 HandleShift(shl);
2414}
2415
2416void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2417 HandleShift(shr);
2418}
2419
2420void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2421 HandleShift(shr);
2422}
2423
2424void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2425 HandleShift(ushr);
2426}
2427
2428void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2429 HandleShift(ushr);
2430}
2431
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002432void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002433 LocationSummary* locations =
2434 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002435 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002436 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2437 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2438 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002439}
2440
2441void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2442 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002443 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002444 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2445
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002446 __ gs()->call(
2447 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002448
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002449 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002450 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002451}
2452
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002453void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2454 LocationSummary* locations =
2455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2456 InvokeRuntimeCallingConvention calling_convention;
2457 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002458 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002459 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002460 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002461}
2462
2463void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2464 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002465 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002466 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2467
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002468 __ gs()->call(
2469 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002470
2471 DCHECK(!codegen_->IsLeafMethod());
2472 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2473}
2474
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002475void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002476 LocationSummary* locations =
2477 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002478 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2479 if (location.IsStackSlot()) {
2480 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2481 } else if (location.IsDoubleStackSlot()) {
2482 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2483 }
2484 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002485}
2486
2487void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2488 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002489 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002490}
2491
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002492void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002493 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002494 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002495 locations->SetInAt(0, Location::RequiresRegister());
2496 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002497}
2498
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002499void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2500 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2502 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002503 Location out = locations->Out();
2504 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002505 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002506 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002507 break;
2508
2509 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002510 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2515 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002516}
2517
2518void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002519 LocationSummary* locations =
2520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2522 locations->SetInAt(i, Location::Any());
2523 }
2524 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002525}
2526
2527void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002528 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002529 LOG(FATAL) << "Unimplemented";
2530}
2531
Calin Juravle52c48962014-12-16 17:02:57 +00002532void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2533 /*
2534 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2535 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2536 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2537 */
2538 switch (kind) {
2539 case MemBarrierKind::kAnyAny: {
2540 __ mfence();
2541 break;
2542 }
2543 case MemBarrierKind::kAnyStore:
2544 case MemBarrierKind::kLoadAny:
2545 case MemBarrierKind::kStoreStore: {
2546 // nop
2547 break;
2548 }
2549 default:
2550 LOG(FATAL) << "Unexpected memory barier " << kind;
2551 }
2552}
2553
2554void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2555 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2556
Nicolas Geoffray39468442014-09-02 15:17:15 +01002557 LocationSummary* locations =
2558 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002559 locations->SetInAt(0, Location::RequiresRegister());
2560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2561}
2562
2563void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2564 const FieldInfo& field_info) {
2565 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2566
2567 LocationSummary* locations = instruction->GetLocations();
2568 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2569 Location out = locations->Out();
2570 bool is_volatile = field_info.IsVolatile();
2571 Primitive::Type field_type = field_info.GetFieldType();
2572 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2573
2574 switch (field_type) {
2575 case Primitive::kPrimBoolean: {
2576 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2577 break;
2578 }
2579
2580 case Primitive::kPrimByte: {
2581 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2582 break;
2583 }
2584
2585 case Primitive::kPrimShort: {
2586 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2587 break;
2588 }
2589
2590 case Primitive::kPrimChar: {
2591 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2592 break;
2593 }
2594
2595 case Primitive::kPrimInt:
2596 case Primitive::kPrimNot: {
2597 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2598 break;
2599 }
2600
2601 case Primitive::kPrimLong: {
2602 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2603 break;
2604 }
2605
2606 case Primitive::kPrimFloat: {
2607 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2608 break;
2609 }
2610
2611 case Primitive::kPrimDouble: {
2612 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2613 break;
2614 }
2615
2616 case Primitive::kPrimVoid:
2617 LOG(FATAL) << "Unreachable type " << field_type;
2618 UNREACHABLE();
2619 }
2620
Calin Juravle77520bc2015-01-12 18:45:46 +00002621 codegen_->MaybeRecordImplicitNullCheck(instruction);
2622
Calin Juravle52c48962014-12-16 17:02:57 +00002623 if (is_volatile) {
2624 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2625 }
2626}
2627
2628void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2629 const FieldInfo& field_info) {
2630 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2631
2632 LocationSummary* locations =
2633 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002634 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002635 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2636
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002637 locations->SetInAt(0, Location::RequiresRegister());
2638 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002639 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002640 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002641 locations->AddTemp(Location::RequiresRegister());
2642 locations->AddTemp(Location::RequiresRegister());
2643 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002644}
2645
Calin Juravle52c48962014-12-16 17:02:57 +00002646void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2647 const FieldInfo& field_info) {
2648 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2649
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002650 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002651 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2652 Location value = locations->InAt(1);
2653 bool is_volatile = field_info.IsVolatile();
2654 Primitive::Type field_type = field_info.GetFieldType();
2655 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2656
2657 if (is_volatile) {
2658 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2659 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002660
2661 switch (field_type) {
2662 case Primitive::kPrimBoolean:
2663 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002664 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002665 break;
2666 }
2667
2668 case Primitive::kPrimShort:
2669 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002670 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671 break;
2672 }
2673
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002674 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002675 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002676 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677 break;
2678 }
2679
2680 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002681 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002682 break;
2683 }
2684
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002685 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002686 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002687 break;
2688 }
2689
2690 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002691 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002692 break;
2693 }
2694
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002695 case Primitive::kPrimVoid:
2696 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002697 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698 }
Calin Juravle52c48962014-12-16 17:02:57 +00002699
Calin Juravle77520bc2015-01-12 18:45:46 +00002700 codegen_->MaybeRecordImplicitNullCheck(instruction);
2701
2702 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2703 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2704 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2705 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2706 }
2707
Calin Juravle52c48962014-12-16 17:02:57 +00002708 if (is_volatile) {
2709 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2710 }
2711}
2712
2713void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2714 HandleFieldSet(instruction, instruction->GetFieldInfo());
2715}
2716
2717void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2718 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002719}
2720
2721void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002722 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002723}
2724
2725void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002726 HandleFieldGet(instruction, instruction->GetFieldInfo());
2727}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002728
Calin Juravle52c48962014-12-16 17:02:57 +00002729void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2730 HandleFieldGet(instruction);
2731}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732
Calin Juravle52c48962014-12-16 17:02:57 +00002733void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2734 HandleFieldGet(instruction, instruction->GetFieldInfo());
2735}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736
Calin Juravle52c48962014-12-16 17:02:57 +00002737void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2738 HandleFieldSet(instruction, instruction->GetFieldInfo());
2739}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740
Calin Juravle52c48962014-12-16 17:02:57 +00002741void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2742 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002743}
2744
2745void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002746 LocationSummary* locations =
2747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002748 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2749 ? Location::RequiresRegister()
2750 : Location::Any();
2751 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002752 if (instruction->HasUses()) {
2753 locations->SetOut(Location::SameAsFirstInput());
2754 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002755}
2756
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002757void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002758 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2759 return;
2760 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002761 LocationSummary* locations = instruction->GetLocations();
2762 Location obj = locations->InAt(0);
2763
2764 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2765 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2766}
2767
2768void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002769 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002770 codegen_->AddSlowPath(slow_path);
2771
2772 LocationSummary* locations = instruction->GetLocations();
2773 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002774
2775 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002776 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002777 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002778 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002779 } else {
2780 DCHECK(obj.IsConstant()) << obj;
2781 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2782 __ jmp(slow_path->GetEntryLabel());
2783 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002784 }
2785 __ j(kEqual, slow_path->GetEntryLabel());
2786}
2787
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002788void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2789 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2790 GenerateImplicitNullCheck(instruction);
2791 } else {
2792 GenerateExplicitNullCheck(instruction);
2793 }
2794}
2795
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002796void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002797 LocationSummary* locations =
2798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002799 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002800 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002801 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002803}
2804
2805void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2806 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002807 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002808 Location index = locations->InAt(1);
2809
2810 switch (instruction->GetType()) {
2811 case Primitive::kPrimBoolean: {
2812 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002814 if (index.IsConstant()) {
2815 __ movzxb(out, Address(obj,
2816 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2817 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002818 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002819 }
2820 break;
2821 }
2822
2823 case Primitive::kPrimByte: {
2824 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 if (index.IsConstant()) {
2827 __ movsxb(out, Address(obj,
2828 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2829 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002831 }
2832 break;
2833 }
2834
2835 case Primitive::kPrimShort: {
2836 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002837 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002838 if (index.IsConstant()) {
2839 __ movsxw(out, Address(obj,
2840 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2841 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002843 }
2844 break;
2845 }
2846
2847 case Primitive::kPrimChar: {
2848 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002849 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002850 if (index.IsConstant()) {
2851 __ movzxw(out, Address(obj,
2852 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2853 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002855 }
2856 break;
2857 }
2858
2859 case Primitive::kPrimInt:
2860 case Primitive::kPrimNot: {
2861 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2862 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002863 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002864 if (index.IsConstant()) {
2865 __ movl(out, Address(obj,
2866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002868 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002869 }
2870 break;
2871 }
2872
2873 case Primitive::kPrimLong: {
2874 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002876 if (index.IsConstant()) {
2877 __ movq(out, Address(obj,
2878 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2879 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002880 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002881 }
2882 break;
2883 }
2884
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002885 case Primitive::kPrimFloat: {
2886 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002887 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002888 if (index.IsConstant()) {
2889 __ movss(out, Address(obj,
2890 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2891 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002892 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002893 }
2894 break;
2895 }
2896
2897 case Primitive::kPrimDouble: {
2898 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002900 if (index.IsConstant()) {
2901 __ movsd(out, Address(obj,
2902 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2903 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002904 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002905 }
2906 break;
2907 }
2908
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002909 case Primitive::kPrimVoid:
2910 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002911 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002912 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002913 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002914}
2915
2916void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002917 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002918
2919 bool needs_write_barrier =
2920 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2921 bool needs_runtime_call = instruction->NeedsTypeCheck();
2922
Nicolas Geoffray39468442014-09-02 15:17:15 +01002923 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002924 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2925 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002926 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002927 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2928 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2929 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002930 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002931 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002932 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002933 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2934 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002935 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002936 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002937 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2938 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002939 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002940 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002941 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002942
2943 if (needs_write_barrier) {
2944 // Temporary registers for the write barrier.
2945 locations->AddTemp(Location::RequiresRegister());
2946 locations->AddTemp(Location::RequiresRegister());
2947 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002948 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002949}
2950
2951void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2952 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002953 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002954 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002955 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002956 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002957 bool needs_runtime_call = locations->WillCall();
2958 bool needs_write_barrier =
2959 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002960
2961 switch (value_type) {
2962 case Primitive::kPrimBoolean:
2963 case Primitive::kPrimByte: {
2964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002965 if (index.IsConstant()) {
2966 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002967 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002969 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002970 __ movb(Address(obj, offset),
2971 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002972 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002973 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002974 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002975 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2976 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002977 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002978 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002979 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2980 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002981 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002983 break;
2984 }
2985
2986 case Primitive::kPrimShort:
2987 case Primitive::kPrimChar: {
2988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002989 if (index.IsConstant()) {
2990 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002991 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002993 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002994 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002995 __ movw(Address(obj, offset),
2996 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002997 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002998 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002999 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003000 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3002 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003003 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003004 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003005 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003006 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3007 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003008 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003009 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003010 break;
3011 }
3012
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003013 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003014 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003015 if (!needs_runtime_call) {
3016 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3017 if (index.IsConstant()) {
3018 size_t offset =
3019 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3020 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003022 } else {
3023 DCHECK(value.IsConstant()) << value;
3024 __ movl(Address(obj, offset),
3025 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3026 }
3027 } else {
3028 DCHECK(index.IsRegister()) << index;
3029 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3031 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003032 } else {
3033 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003035 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3036 }
3037 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003038 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003039 if (needs_write_barrier) {
3040 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003041 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3042 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3043 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003044 }
3045 } else {
3046 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003047 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3048 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003049 DCHECK(!codegen_->IsLeafMethod());
3050 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3051 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003052 break;
3053 }
3054
3055 case Primitive::kPrimLong: {
3056 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003057 if (index.IsConstant()) {
3058 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003059 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003061 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003062 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3064 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003065 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003066 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003067 break;
3068 }
3069
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003070 case Primitive::kPrimFloat: {
3071 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3072 if (index.IsConstant()) {
3073 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3074 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003075 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003076 } else {
3077 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003078 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3079 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003080 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003081 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003082 break;
3083 }
3084
3085 case Primitive::kPrimDouble: {
3086 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3087 if (index.IsConstant()) {
3088 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3089 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003091 } else {
3092 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3094 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003095 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003096 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003097 break;
3098 }
3099
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100 case Primitive::kPrimVoid:
3101 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003102 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003103 }
3104}
3105
3106void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003107 LocationSummary* locations =
3108 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003109 locations->SetInAt(0, Location::RequiresRegister());
3110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003111}
3112
3113void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3114 LocationSummary* locations = instruction->GetLocations();
3115 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003116 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3117 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003118 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003119 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120}
3121
3122void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003123 LocationSummary* locations =
3124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 locations->SetInAt(0, Location::RequiresRegister());
3126 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003127 if (instruction->HasUses()) {
3128 locations->SetOut(Location::SameAsFirstInput());
3129 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130}
3131
3132void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3133 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003134 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003135 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003136 codegen_->AddSlowPath(slow_path);
3137
Roland Levillain271ab9c2014-11-27 15:23:57 +00003138 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3139 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003140
3141 __ cmpl(index, length);
3142 __ j(kAboveEqual, slow_path->GetEntryLabel());
3143}
3144
3145void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3146 CpuRegister card,
3147 CpuRegister object,
3148 CpuRegister value) {
3149 Label is_null;
3150 __ testl(value, value);
3151 __ j(kEqual, &is_null);
3152 __ gs()->movq(card, Address::Absolute(
3153 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3154 __ movq(temp, object);
3155 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3156 __ movb(Address(temp, card, TIMES_1, 0), card);
3157 __ Bind(&is_null);
3158}
3159
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003160void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3161 temp->SetLocations(nullptr);
3162}
3163
3164void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3165 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003166 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003167}
3168
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003169void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003170 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003171 LOG(FATAL) << "Unimplemented";
3172}
3173
3174void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003175 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3176}
3177
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003178void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3179 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3180}
3181
3182void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003183 HBasicBlock* block = instruction->GetBlock();
3184 if (block->GetLoopInformation() != nullptr) {
3185 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3186 // The back edge will generate the suspend check.
3187 return;
3188 }
3189 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3190 // The goto will generate the suspend check.
3191 return;
3192 }
3193 GenerateSuspendCheck(instruction, nullptr);
3194}
3195
3196void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3197 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003198 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003199 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003200 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003201 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003202 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003203 if (successor == nullptr) {
3204 __ j(kNotEqual, slow_path->GetEntryLabel());
3205 __ Bind(slow_path->GetReturnLabel());
3206 } else {
3207 __ j(kEqual, codegen_->GetLabelOf(successor));
3208 __ jmp(slow_path->GetEntryLabel());
3209 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003210}
3211
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003212X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3213 return codegen_->GetAssembler();
3214}
3215
3216void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3217 MoveOperands* move = moves_.Get(index);
3218 Location source = move->GetSource();
3219 Location destination = move->GetDestination();
3220
3221 if (source.IsRegister()) {
3222 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003224 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003225 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003227 } else {
3228 DCHECK(destination.IsDoubleStackSlot());
3229 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003230 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003231 }
3232 } else if (source.IsStackSlot()) {
3233 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003235 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003236 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003237 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003238 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003239 } else {
3240 DCHECK(destination.IsStackSlot());
3241 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3242 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3243 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003244 } else if (source.IsDoubleStackSlot()) {
3245 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003246 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003247 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003248 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003249 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3250 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003251 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003252 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003253 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3254 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3255 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003256 } else if (source.IsConstant()) {
3257 HConstant* constant = source.GetConstant();
3258 if (constant->IsIntConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003259 int32_t value = constant->AsIntConstant()->GetValue();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003260 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003261 if (value == 0) {
3262 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3263 } else {
3264 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3265 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003266 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003267 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003268 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003269 }
3270 } else if (constant->IsLongConstant()) {
3271 int64_t value = constant->AsLongConstant()->GetValue();
3272 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003273 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003274 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003275 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003276 __ movq(CpuRegister(TMP), Immediate(value));
3277 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3278 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003279 } else if (constant->IsFloatConstant()) {
3280 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3281 if (destination.IsFpuRegister()) {
3282 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003283 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003284 } else {
3285 DCHECK(destination.IsStackSlot()) << destination;
3286 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3287 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003288 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003289 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3290 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3291 if (destination.IsFpuRegister()) {
3292 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003294 } else {
3295 DCHECK(destination.IsDoubleStackSlot()) << destination;
3296 __ movq(CpuRegister(TMP), imm);
3297 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3298 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003299 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003300 } else if (source.IsFpuRegister()) {
3301 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003303 } else if (destination.IsStackSlot()) {
3304 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003305 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003306 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003307 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003308 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003309 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003310 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003311 }
3312}
3313
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003314void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003315 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003316 __ movl(Address(CpuRegister(RSP), mem), reg);
3317 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003318}
3319
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003320void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003321 ScratchRegisterScope ensure_scratch(
3322 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3323
3324 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3325 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3326 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3327 Address(CpuRegister(RSP), mem2 + stack_offset));
3328 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3329 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3330 CpuRegister(ensure_scratch.GetRegister()));
3331}
3332
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003333void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3334 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3335 __ movq(Address(CpuRegister(RSP), mem), reg);
3336 __ movq(reg, CpuRegister(TMP));
3337}
3338
3339void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3340 ScratchRegisterScope ensure_scratch(
3341 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3342
3343 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3344 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3345 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3346 Address(CpuRegister(RSP), mem2 + stack_offset));
3347 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3348 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3349 CpuRegister(ensure_scratch.GetRegister()));
3350}
3351
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003352void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3353 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3354 __ movss(Address(CpuRegister(RSP), mem), reg);
3355 __ movd(reg, CpuRegister(TMP));
3356}
3357
3358void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3359 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3360 __ movsd(Address(CpuRegister(RSP), mem), reg);
3361 __ movd(reg, CpuRegister(TMP));
3362}
3363
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003364void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3365 MoveOperands* move = moves_.Get(index);
3366 Location source = move->GetSource();
3367 Location destination = move->GetDestination();
3368
3369 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003370 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003371 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003372 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003373 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003374 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003375 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003376 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3377 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003378 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003379 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003380 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003381 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3382 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003383 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003384 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3385 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3386 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003387 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003388 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003389 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003390 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003391 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003392 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003393 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003394 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003395 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003396 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003397 }
3398}
3399
3400
3401void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3402 __ pushq(CpuRegister(reg));
3403}
3404
3405
3406void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3407 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003408}
3409
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003410void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3411 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3412 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3413 Immediate(mirror::Class::kStatusInitialized));
3414 __ j(kLess, slow_path->GetEntryLabel());
3415 __ Bind(slow_path->GetExitLabel());
3416 // No need for memory fence, thanks to the X86_64 memory model.
3417}
3418
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003419void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003420 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3421 ? LocationSummary::kCallOnSlowPath
3422 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003423 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003424 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003425 locations->SetOut(Location::RequiresRegister());
3426}
3427
3428void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003429 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003430 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003431 DCHECK(!cls->CanCallRuntime());
3432 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003433 codegen_->LoadCurrentMethod(out);
3434 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3435 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003436 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003437 codegen_->LoadCurrentMethod(out);
3438 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3439 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003440 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3441 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3442 codegen_->AddSlowPath(slow_path);
3443 __ testl(out, out);
3444 __ j(kEqual, slow_path->GetEntryLabel());
3445 if (cls->MustGenerateClinitCheck()) {
3446 GenerateClassInitializationCheck(slow_path, out);
3447 } else {
3448 __ Bind(slow_path->GetExitLabel());
3449 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003450 }
3451}
3452
3453void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3454 LocationSummary* locations =
3455 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3456 locations->SetInAt(0, Location::RequiresRegister());
3457 if (check->HasUses()) {
3458 locations->SetOut(Location::SameAsFirstInput());
3459 }
3460}
3461
3462void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003463 // We assume the class to not be null.
3464 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3465 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003466 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003467 GenerateClassInitializationCheck(slow_path,
3468 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003469}
3470
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003471void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3472 LocationSummary* locations =
3473 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3474 locations->SetOut(Location::RequiresRegister());
3475}
3476
3477void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3478 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3479 codegen_->AddSlowPath(slow_path);
3480
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003482 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003483 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3484 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003485 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3486 __ testl(out, out);
3487 __ j(kEqual, slow_path->GetEntryLabel());
3488 __ Bind(slow_path->GetExitLabel());
3489}
3490
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003491void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3492 LocationSummary* locations =
3493 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3494 locations->SetOut(Location::RequiresRegister());
3495}
3496
3497void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3498 Address address = Address::Absolute(
3499 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003500 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003501 __ gs()->movl(address, Immediate(0));
3502}
3503
3504void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3505 LocationSummary* locations =
3506 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3507 InvokeRuntimeCallingConvention calling_convention;
3508 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3509}
3510
3511void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3512 __ gs()->call(
3513 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3514 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3515}
3516
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003517void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003518 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3519 ? LocationSummary::kNoCall
3520 : LocationSummary::kCallOnSlowPath;
3521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3522 locations->SetInAt(0, Location::RequiresRegister());
3523 locations->SetInAt(1, Location::Any());
3524 locations->SetOut(Location::RequiresRegister());
3525}
3526
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003527void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003528 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003529 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003530 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003531 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003532 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3533 Label done, zero;
3534 SlowPathCodeX86_64* slow_path = nullptr;
3535
3536 // Return 0 if `obj` is null.
3537 // TODO: avoid this check if we know obj is not null.
3538 __ testl(obj, obj);
3539 __ j(kEqual, &zero);
3540 // Compare the class of `obj` with `cls`.
3541 __ movl(out, Address(obj, class_offset));
3542 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003543 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003544 } else {
3545 DCHECK(cls.IsStackSlot()) << cls;
3546 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3547 }
3548 if (instruction->IsClassFinal()) {
3549 // Classes must be equal for the instanceof to succeed.
3550 __ j(kNotEqual, &zero);
3551 __ movl(out, Immediate(1));
3552 __ jmp(&done);
3553 } else {
3554 // If the classes are not equal, we go into a slow path.
3555 DCHECK(locations->OnlyCallsOnSlowPath());
3556 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003557 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003558 codegen_->AddSlowPath(slow_path);
3559 __ j(kNotEqual, slow_path->GetEntryLabel());
3560 __ movl(out, Immediate(1));
3561 __ jmp(&done);
3562 }
3563 __ Bind(&zero);
3564 __ movl(out, Immediate(0));
3565 if (slow_path != nullptr) {
3566 __ Bind(slow_path->GetExitLabel());
3567 }
3568 __ Bind(&done);
3569}
3570
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003571void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3573 instruction, LocationSummary::kCallOnSlowPath);
3574 locations->SetInAt(0, Location::RequiresRegister());
3575 locations->SetInAt(1, Location::Any());
3576 locations->AddTemp(Location::RequiresRegister());
3577}
3578
3579void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3580 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003581 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003582 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003583 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003584 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3585 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3586 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3587 codegen_->AddSlowPath(slow_path);
3588
3589 // TODO: avoid this check if we know obj is not null.
3590 __ testl(obj, obj);
3591 __ j(kEqual, slow_path->GetExitLabel());
3592 // Compare the class of `obj` with `cls`.
3593 __ movl(temp, Address(obj, class_offset));
3594 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003596 } else {
3597 DCHECK(cls.IsStackSlot()) << cls;
3598 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3599 }
3600 // Classes must be equal for the checkcast to succeed.
3601 __ j(kNotEqual, slow_path->GetEntryLabel());
3602 __ Bind(slow_path->GetExitLabel());
3603}
3604
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003605void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3606 LocationSummary* locations =
3607 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3608 InvokeRuntimeCallingConvention calling_convention;
3609 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3610}
3611
3612void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3613 __ gs()->call(Address::Absolute(instruction->IsEnter()
3614 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3615 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3616 true));
3617 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3618}
3619
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003620void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3621void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3622void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3623
3624void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3625 LocationSummary* locations =
3626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3627 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3628 || instruction->GetResultType() == Primitive::kPrimLong);
3629 locations->SetInAt(0, Location::RequiresRegister());
3630 if (instruction->GetType() == Primitive::kPrimInt) {
3631 locations->SetInAt(1, Location::Any());
3632 } else {
3633 // Request a register to avoid loading a 64bits constant.
3634 locations->SetInAt(1, Location::RequiresRegister());
3635 }
3636 locations->SetOut(Location::SameAsFirstInput());
3637}
3638
3639void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3640 HandleBitwiseOperation(instruction);
3641}
3642
3643void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3644 HandleBitwiseOperation(instruction);
3645}
3646
3647void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3648 HandleBitwiseOperation(instruction);
3649}
3650
3651void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3652 LocationSummary* locations = instruction->GetLocations();
3653 Location first = locations->InAt(0);
3654 Location second = locations->InAt(1);
3655 DCHECK(first.Equals(locations->Out()));
3656
3657 if (instruction->GetResultType() == Primitive::kPrimInt) {
3658 if (second.IsRegister()) {
3659 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003660 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003661 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003662 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003663 } else {
3664 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003666 }
3667 } else if (second.IsConstant()) {
3668 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3669 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003670 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003671 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003673 } else {
3674 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003675 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003676 }
3677 } else {
3678 Address address(CpuRegister(RSP), second.GetStackIndex());
3679 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003681 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003682 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003683 } else {
3684 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003685 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003686 }
3687 }
3688 } else {
3689 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3690 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003692 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003694 } else {
3695 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003697 }
3698 }
3699}
3700
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003701} // namespace x86_64
3702} // namespace art