blob: 41a19e11f0e2418132fe5f37b84d491f852060f8 [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 Geoffrayc0572a42015-02-06 14:35:25 +0000490 if (HasEmptyFrame()) {
491 return;
492 }
493
Nicolas Geoffray98893962015-01-21 12:32:32 +0000494 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000495 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000496 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000497 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000498 }
499 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100500
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
502 uint32_t xmm_spill_location = GetFpuSpillStart();
503 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100504
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000505 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
506 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
507 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
508 XmmRegister(kFpuCalleeSaves[i]));
509 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100510 }
511
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
513}
514
515void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000516 if (HasEmptyFrame()) {
517 return;
518 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000519 uint32_t xmm_spill_location = GetFpuSpillStart();
520 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
521 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
522 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
523 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
524 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
525 }
526 }
527
528 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000529
530 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000532 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000534 }
535 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536}
537
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100538void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
539 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100540}
541
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100542void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000543 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
545}
546
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
548 switch (load->GetType()) {
549 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
552 break;
553
554 case Primitive::kPrimInt:
555 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100556 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558
559 case Primitive::kPrimBoolean:
560 case Primitive::kPrimByte:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimShort:
563 case Primitive::kPrimVoid:
564 LOG(FATAL) << "Unexpected type " << load->GetType();
565 }
566
567 LOG(FATAL) << "Unreachable";
568 return Location();
569}
570
571void CodeGeneratorX86_64::Move(Location destination, Location source) {
572 if (source.Equals(destination)) {
573 return;
574 }
575 if (destination.IsRegister()) {
576 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 } else {
584 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 Address(CpuRegister(RSP), source.GetStackIndex()));
587 }
588 } else if (destination.IsFpuRegister()) {
589 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000592 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000594 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100595 Address(CpuRegister(RSP), source.GetStackIndex()));
596 } else {
597 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000598 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 }
601 } else if (destination.IsStackSlot()) {
602 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000604 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 } else if (source.IsFpuRegister()) {
606 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500608 } else if (source.IsConstant()) {
609 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000610 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500611 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100612 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500613 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000614 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
615 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100616 }
617 } else {
618 DCHECK(destination.IsDoubleStackSlot());
619 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (source.IsFpuRegister()) {
623 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500625 } else if (source.IsConstant()) {
626 HConstant* constant = source.GetConstant();
627 int64_t value = constant->AsLongConstant()->GetValue();
628 if (constant->IsDoubleConstant()) {
629 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
630 } else {
631 DCHECK(constant->IsLongConstant());
632 value = constant->AsLongConstant()->GetValue();
633 }
634 __ movq(CpuRegister(TMP), Immediate(value));
635 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636 } else {
637 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000638 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
639 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100640 }
641 }
642}
643
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100644void CodeGeneratorX86_64::Move(HInstruction* instruction,
645 Location location,
646 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000647 LocationSummary* locations = instruction->GetLocations();
648 if (locations != nullptr && locations->Out().Equals(location)) {
649 return;
650 }
651
652 if (locations != nullptr && locations->Out().IsConstant()) {
653 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000654 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
655 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000656 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000657 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000658 } else if (location.IsStackSlot()) {
659 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
660 } else {
661 DCHECK(location.IsConstant());
662 DCHECK_EQ(location.GetConstant(), const_to_move);
663 }
664 } else if (const_to_move->IsLongConstant()) {
665 int64_t value = const_to_move->AsLongConstant()->GetValue();
666 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000667 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000668 } else if (location.IsDoubleStackSlot()) {
669 __ movq(CpuRegister(TMP), Immediate(value));
670 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
671 } else {
672 DCHECK(location.IsConstant());
673 DCHECK_EQ(location.GetConstant(), const_to_move);
674 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675 }
Roland Levillain476df552014-10-09 17:51:36 +0100676 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100677 switch (instruction->GetType()) {
678 case Primitive::kPrimBoolean:
679 case Primitive::kPrimByte:
680 case Primitive::kPrimChar:
681 case Primitive::kPrimShort:
682 case Primitive::kPrimInt:
683 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100684 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
686 break;
687
688 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100689 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000690 Move(location,
691 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100692 break;
693
694 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100696 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000697 } else if (instruction->IsTemporary()) {
698 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
699 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100700 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100701 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702 switch (instruction->GetType()) {
703 case Primitive::kPrimBoolean:
704 case Primitive::kPrimByte:
705 case Primitive::kPrimChar:
706 case Primitive::kPrimShort:
707 case Primitive::kPrimInt:
708 case Primitive::kPrimNot:
709 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 case Primitive::kPrimFloat:
711 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000712 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100713 break;
714
715 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 }
718 }
719}
720
721void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
722 got->SetLocations(nullptr);
723}
724
725void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
726 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100727 DCHECK(!successor->IsExitBlock());
728
729 HBasicBlock* block = got->GetBlock();
730 HInstruction* previous = got->GetPrevious();
731
732 HLoopInformation* info = block->GetLoopInformation();
733 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
734 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
735 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
736 return;
737 }
738
739 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
740 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
741 }
742 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743 __ jmp(codegen_->GetLabelOf(successor));
744 }
745}
746
747void LocationsBuilderX86_64::VisitExit(HExit* exit) {
748 exit->SetLocations(nullptr);
749}
750
751void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700752 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100753 if (kIsDebugBuild) {
754 __ Comment("Unreachable");
755 __ int3();
756 }
757}
758
759void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100760 LocationSummary* locations =
761 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100762 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100763 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100764 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100765 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100766}
767
768void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700769 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100770 if (cond->IsIntConstant()) {
771 // Constant condition, statically compared against 1.
772 int32_t cond_value = cond->AsIntConstant()->GetValue();
773 if (cond_value == 1) {
774 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
775 if_instr->IfTrueSuccessor())) {
776 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100777 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100778 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100780 DCHECK_EQ(cond_value, 0);
781 }
782 } else {
783 bool materialized =
784 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
785 // Moves do not affect the eflags register, so if the condition is
786 // evaluated just before the if, we don't need to evaluate it
787 // again.
788 bool eflags_set = cond->IsCondition()
789 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
790 if (materialized) {
791 if (!eflags_set) {
792 // Materialized condition, compare against 0.
793 Location lhs = if_instr->GetLocations()->InAt(0);
794 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000795 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 } else {
797 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
798 Immediate(0));
799 }
800 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
801 } else {
802 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
803 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
804 }
805 } else {
806 Location lhs = cond->GetLocations()->InAt(0);
807 Location rhs = cond->GetLocations()->InAt(1);
808 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000809 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100810 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000811 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000812 if (constant == 0) {
813 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
814 } else {
815 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
816 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000818 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 Address(CpuRegister(RSP), rhs.GetStackIndex()));
820 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100821 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
822 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700823 }
Dave Allison20dfc792014-06-16 20:44:29 -0700824 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
826 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700827 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100828 }
829}
830
831void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
832 local->SetLocations(nullptr);
833}
834
835void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
836 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
837}
838
839void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
840 local->SetLocations(nullptr);
841}
842
843void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
844 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700845 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846}
847
848void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100849 LocationSummary* locations =
850 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851 switch (store->InputAt(1)->GetType()) {
852 case Primitive::kPrimBoolean:
853 case Primitive::kPrimByte:
854 case Primitive::kPrimChar:
855 case Primitive::kPrimShort:
856 case Primitive::kPrimInt:
857 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
860 break;
861
862 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100864 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
865 break;
866
867 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100870}
871
872void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700873 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100874}
875
Dave Allison20dfc792014-06-16 20:44:29 -0700876void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100877 LocationSummary* locations =
878 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100879 locations->SetInAt(0, Location::RequiresRegister());
880 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100881 if (comp->NeedsMaterialization()) {
882 locations->SetOut(Location::RequiresRegister());
883 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100884}
885
Dave Allison20dfc792014-06-16 20:44:29 -0700886void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
887 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100888 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000889 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100890 // Clear register: setcc only sets the low byte.
891 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000892 Location lhs = locations->InAt(0);
893 Location rhs = locations->InAt(1);
894 if (rhs.IsRegister()) {
895 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
896 } else if (rhs.IsConstant()) {
897 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
898 if (constant == 0) {
899 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
900 } else {
901 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
902 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100903 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000904 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100905 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100906 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700907 }
908}
909
910void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
911 VisitCondition(comp);
912}
913
914void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
915 VisitCondition(comp);
916}
917
918void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
919 VisitCondition(comp);
920}
921
922void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
923 VisitCondition(comp);
924}
925
926void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
927 VisitCondition(comp);
928}
929
930void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
931 VisitCondition(comp);
932}
933
934void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
935 VisitCondition(comp);
936}
937
938void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
939 VisitCondition(comp);
940}
941
942void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
943 VisitCondition(comp);
944}
945
946void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
947 VisitCondition(comp);
948}
949
950void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
951 VisitCondition(comp);
952}
953
954void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
955 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100956}
957
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100958void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100959 LocationSummary* locations =
960 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000961 switch (compare->InputAt(0)->GetType()) {
962 case Primitive::kPrimLong: {
963 locations->SetInAt(0, Location::RequiresRegister());
964 locations->SetInAt(1, Location::RequiresRegister());
965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
966 break;
967 }
968 case Primitive::kPrimFloat:
969 case Primitive::kPrimDouble: {
970 locations->SetInAt(0, Location::RequiresFpuRegister());
971 locations->SetInAt(1, Location::RequiresFpuRegister());
972 locations->SetOut(Location::RequiresRegister());
973 break;
974 }
975 default:
976 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
977 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100978}
979
980void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100981 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000982 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000983 Location left = locations->InAt(0);
984 Location right = locations->InAt(1);
985
986 Label less, greater, done;
987 Primitive::Type type = compare->InputAt(0)->GetType();
988 switch (type) {
989 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000990 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100991 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000992 }
993 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000994 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000995 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
996 break;
997 }
998 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000999 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001000 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1001 break;
1002 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001003 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001004 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001005 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001006 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001007 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001008 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001009
Calin Juravle91debbc2014-11-26 19:01:09 +00001010 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001011 __ movl(out, Immediate(1));
1012 __ jmp(&done);
1013
1014 __ Bind(&less);
1015 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001016
1017 __ Bind(&done);
1018}
1019
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001020void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001021 LocationSummary* locations =
1022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001024}
1025
1026void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001027 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029}
1030
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001031void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1034 locations->SetOut(Location::ConstantLocation(constant));
1035}
1036
1037void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1038 // Will be generated at use site.
1039 UNUSED(constant);
1040}
1041
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001043 LocationSummary* locations =
1044 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001045 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001046}
1047
1048void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001049 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001050 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001051}
1052
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001053void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1054 LocationSummary* locations =
1055 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1056 locations->SetOut(Location::ConstantLocation(constant));
1057}
1058
1059void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1060 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001061 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001062}
1063
1064void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1065 LocationSummary* locations =
1066 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1067 locations->SetOut(Location::ConstantLocation(constant));
1068}
1069
1070void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1071 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001072 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001073}
1074
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1076 ret->SetLocations(nullptr);
1077}
1078
1079void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001080 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081 codegen_->GenerateFrameExit();
1082 __ ret();
1083}
1084
1085void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001086 LocationSummary* locations =
1087 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088 switch (ret->InputAt(0)->GetType()) {
1089 case Primitive::kPrimBoolean:
1090 case Primitive::kPrimByte:
1091 case Primitive::kPrimChar:
1092 case Primitive::kPrimShort:
1093 case Primitive::kPrimInt:
1094 case Primitive::kPrimNot:
1095 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097 break;
1098
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 case Primitive::kPrimFloat:
1100 case Primitive::kPrimDouble:
1101 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 break;
1104
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001107 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108}
1109
1110void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1111 if (kIsDebugBuild) {
1112 switch (ret->InputAt(0)->GetType()) {
1113 case Primitive::kPrimBoolean:
1114 case Primitive::kPrimByte:
1115 case Primitive::kPrimChar:
1116 case Primitive::kPrimShort:
1117 case Primitive::kPrimInt:
1118 case Primitive::kPrimNot:
1119 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001120 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001121 break;
1122
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001123 case Primitive::kPrimFloat:
1124 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001125 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001126 XMM0);
1127 break;
1128
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001130 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131 }
1132 }
1133 codegen_->GenerateFrameExit();
1134 __ ret();
1135}
1136
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1138 switch (type) {
1139 case Primitive::kPrimBoolean:
1140 case Primitive::kPrimByte:
1141 case Primitive::kPrimChar:
1142 case Primitive::kPrimShort:
1143 case Primitive::kPrimInt:
1144 case Primitive::kPrimNot: {
1145 uint32_t index = gp_index_++;
1146 stack_index_++;
1147 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149 } else {
1150 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1151 }
1152 }
1153
1154 case Primitive::kPrimLong: {
1155 uint32_t index = gp_index_;
1156 stack_index_ += 2;
1157 if (index < calling_convention.GetNumberOfRegisters()) {
1158 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160 } else {
1161 gp_index_ += 2;
1162 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1163 }
1164 }
1165
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 case Primitive::kPrimFloat: {
1167 uint32_t index = fp_index_++;
1168 stack_index_++;
1169 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 } else {
1172 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1173 }
1174 }
1175
1176 case Primitive::kPrimDouble: {
1177 uint32_t index = fp_index_++;
1178 stack_index_ += 2;
1179 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001181 } else {
1182 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1183 }
1184 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185
1186 case Primitive::kPrimVoid:
1187 LOG(FATAL) << "Unexpected parameter type " << type;
1188 break;
1189 }
1190 return Location();
1191}
1192
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001193void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001194 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1195 if (intrinsic.TryDispatch(invoke)) {
1196 return;
1197 }
1198
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001199 HandleInvoke(invoke);
1200}
1201
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001202static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1203 if (invoke->GetLocations()->Intrinsified()) {
1204 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1205 intrinsic.Dispatch(invoke);
1206 return true;
1207 }
1208 return false;
1209}
1210
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001211void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001212 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1213 return;
1214 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001215
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001216 codegen_->GenerateStaticOrDirectCall(
1217 invoke,
1218 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001219}
1220
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001221void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001222 LocationSummary* locations =
1223 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001224 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001225
1226 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001227 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 HInstruction* input = invoke->InputAt(i);
1229 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1230 }
1231
1232 switch (invoke->GetType()) {
1233 case Primitive::kPrimBoolean:
1234 case Primitive::kPrimByte:
1235 case Primitive::kPrimChar:
1236 case Primitive::kPrimShort:
1237 case Primitive::kPrimInt:
1238 case Primitive::kPrimNot:
1239 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001240 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 break;
1242
1243 case Primitive::kPrimVoid:
1244 break;
1245
1246 case Primitive::kPrimDouble:
1247 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001248 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 break;
1250 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251}
1252
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001253void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001254 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1255 if (intrinsic.TryDispatch(invoke)) {
1256 return;
1257 }
1258
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001259 HandleInvoke(invoke);
1260}
1261
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001262void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001263 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1264 return;
1265 }
1266
Roland Levillain271ab9c2014-11-27 15:23:57 +00001267 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1269 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1270 LocationSummary* locations = invoke->GetLocations();
1271 Location receiver = locations->InAt(0);
1272 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1273 // temp = object->GetClass();
1274 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001275 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1276 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001280 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001281 // temp = temp->GetMethodAt(method_offset);
1282 __ movl(temp, Address(temp, method_offset));
1283 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001284 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001285 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001286
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001287 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001288 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001289}
1290
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001291void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1292 HandleInvoke(invoke);
1293 // Add the hidden argument.
1294 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1295}
1296
1297void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1298 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001299 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001300 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1301 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1302 LocationSummary* locations = invoke->GetLocations();
1303 Location receiver = locations->InAt(0);
1304 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1305
1306 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001308 Immediate(invoke->GetDexMethodIndex()));
1309
1310 // temp = object->GetClass();
1311 if (receiver.IsStackSlot()) {
1312 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1313 __ movl(temp, Address(temp, class_offset));
1314 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001316 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001317 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001318 // temp = temp->GetImtEntryAt(method_offset);
1319 __ movl(temp, Address(temp, method_offset));
1320 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001321 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001322 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001323
1324 DCHECK(!codegen_->IsLeafMethod());
1325 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1326}
1327
Roland Levillain88cb1752014-10-20 16:36:47 +01001328void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1329 LocationSummary* locations =
1330 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1331 switch (neg->GetResultType()) {
1332 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001333 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001334 locations->SetInAt(0, Location::RequiresRegister());
1335 locations->SetOut(Location::SameAsFirstInput());
1336 break;
1337
Roland Levillain88cb1752014-10-20 16:36:47 +01001338 case Primitive::kPrimFloat:
1339 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001340 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001341 locations->SetOut(Location::SameAsFirstInput());
1342 locations->AddTemp(Location::RequiresRegister());
1343 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 break;
1345
1346 default:
1347 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1348 }
1349}
1350
1351void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1352 LocationSummary* locations = neg->GetLocations();
1353 Location out = locations->Out();
1354 Location in = locations->InAt(0);
1355 switch (neg->GetResultType()) {
1356 case Primitive::kPrimInt:
1357 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001358 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001359 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001360 break;
1361
1362 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001363 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001364 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001366 break;
1367
Roland Levillain5368c212014-11-27 15:03:41 +00001368 case Primitive::kPrimFloat: {
1369 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1371 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001372 // Implement float negation with an exclusive or with value
1373 // 0x80000000 (mask for bit 31, representing the sign of a
1374 // single-precision floating-point number).
1375 __ movq(constant, Immediate(INT64_C(0x80000000)));
1376 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001378 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001379 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380
Roland Levillain5368c212014-11-27 15:03:41 +00001381 case Primitive::kPrimDouble: {
1382 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001383 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1384 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001385 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001386 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001387 // a double-precision floating-point number).
1388 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1389 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001391 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001392 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001393
1394 default:
1395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1396 }
1397}
1398
Roland Levillaindff1f282014-11-05 14:15:05 +00001399void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1400 LocationSummary* locations =
1401 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1402 Primitive::Type result_type = conversion->GetResultType();
1403 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001404 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001405 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001406 case Primitive::kPrimByte:
1407 switch (input_type) {
1408 case Primitive::kPrimShort:
1409 case Primitive::kPrimInt:
1410 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001411 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001412 locations->SetInAt(0, Location::Any());
1413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1414 break;
1415
1416 default:
1417 LOG(FATAL) << "Unexpected type conversion from " << input_type
1418 << " to " << result_type;
1419 }
1420 break;
1421
Roland Levillain01a8d712014-11-14 16:27:39 +00001422 case Primitive::kPrimShort:
1423 switch (input_type) {
1424 case Primitive::kPrimByte:
1425 case Primitive::kPrimInt:
1426 case Primitive::kPrimChar:
1427 // Processing a Dex `int-to-short' instruction.
1428 locations->SetInAt(0, Location::Any());
1429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1430 break;
1431
1432 default:
1433 LOG(FATAL) << "Unexpected type conversion from " << input_type
1434 << " to " << result_type;
1435 }
1436 break;
1437
Roland Levillain946e1432014-11-11 17:35:19 +00001438 case Primitive::kPrimInt:
1439 switch (input_type) {
1440 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001441 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001442 locations->SetInAt(0, Location::Any());
1443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1444 break;
1445
1446 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001447 // Processing a Dex `float-to-int' instruction.
1448 locations->SetInAt(0, Location::RequiresFpuRegister());
1449 locations->SetOut(Location::RequiresRegister());
1450 locations->AddTemp(Location::RequiresFpuRegister());
1451 break;
1452
Roland Levillain946e1432014-11-11 17:35:19 +00001453 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001454 // Processing a Dex `double-to-int' instruction.
1455 locations->SetInAt(0, Location::RequiresFpuRegister());
1456 locations->SetOut(Location::RequiresRegister());
1457 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001458 break;
1459
1460 default:
1461 LOG(FATAL) << "Unexpected type conversion from " << input_type
1462 << " to " << result_type;
1463 }
1464 break;
1465
Roland Levillaindff1f282014-11-05 14:15:05 +00001466 case Primitive::kPrimLong:
1467 switch (input_type) {
1468 case Primitive::kPrimByte:
1469 case Primitive::kPrimShort:
1470 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001471 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001472 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001473 // TODO: We would benefit from a (to-be-implemented)
1474 // Location::RegisterOrStackSlot requirement for this input.
1475 locations->SetInAt(0, Location::RequiresRegister());
1476 locations->SetOut(Location::RequiresRegister());
1477 break;
1478
1479 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001480 // Processing a Dex `float-to-long' instruction.
1481 locations->SetInAt(0, Location::RequiresFpuRegister());
1482 locations->SetOut(Location::RequiresRegister());
1483 locations->AddTemp(Location::RequiresFpuRegister());
1484 break;
1485
Roland Levillaindff1f282014-11-05 14:15:05 +00001486 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001487 // Processing a Dex `double-to-long' instruction.
1488 locations->SetInAt(0, Location::RequiresFpuRegister());
1489 locations->SetOut(Location::RequiresRegister());
1490 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001491 break;
1492
1493 default:
1494 LOG(FATAL) << "Unexpected type conversion from " << input_type
1495 << " to " << result_type;
1496 }
1497 break;
1498
Roland Levillain981e4542014-11-14 11:47:14 +00001499 case Primitive::kPrimChar:
1500 switch (input_type) {
1501 case Primitive::kPrimByte:
1502 case Primitive::kPrimShort:
1503 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001504 // Processing a Dex `int-to-char' instruction.
1505 locations->SetInAt(0, Location::Any());
1506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1507 break;
1508
1509 default:
1510 LOG(FATAL) << "Unexpected type conversion from " << input_type
1511 << " to " << result_type;
1512 }
1513 break;
1514
Roland Levillaindff1f282014-11-05 14:15:05 +00001515 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001516 switch (input_type) {
1517 case Primitive::kPrimByte:
1518 case Primitive::kPrimShort:
1519 case Primitive::kPrimInt:
1520 case Primitive::kPrimChar:
1521 // Processing a Dex `int-to-float' instruction.
1522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetOut(Location::RequiresFpuRegister());
1524 break;
1525
1526 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001527 // Processing a Dex `long-to-float' instruction.
1528 locations->SetInAt(0, Location::RequiresRegister());
1529 locations->SetOut(Location::RequiresFpuRegister());
1530 break;
1531
Roland Levillaincff13742014-11-17 14:32:17 +00001532 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001533 // Processing a Dex `double-to-float' instruction.
1534 locations->SetInAt(0, Location::RequiresFpuRegister());
1535 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 };
1542 break;
1543
Roland Levillaindff1f282014-11-05 14:15:05 +00001544 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001545 switch (input_type) {
1546 case Primitive::kPrimByte:
1547 case Primitive::kPrimShort:
1548 case Primitive::kPrimInt:
1549 case Primitive::kPrimChar:
1550 // Processing a Dex `int-to-double' instruction.
1551 locations->SetInAt(0, Location::RequiresRegister());
1552 locations->SetOut(Location::RequiresFpuRegister());
1553 break;
1554
1555 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001556 // Processing a Dex `long-to-double' instruction.
1557 locations->SetInAt(0, Location::RequiresRegister());
1558 locations->SetOut(Location::RequiresFpuRegister());
1559 break;
1560
Roland Levillaincff13742014-11-17 14:32:17 +00001561 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001562 // Processing a Dex `float-to-double' instruction.
1563 locations->SetInAt(0, Location::RequiresFpuRegister());
1564 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001565 break;
1566
1567 default:
1568 LOG(FATAL) << "Unexpected type conversion from " << input_type
1569 << " to " << result_type;
1570 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001571 break;
1572
1573 default:
1574 LOG(FATAL) << "Unexpected type conversion from " << input_type
1575 << " to " << result_type;
1576 }
1577}
1578
1579void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1580 LocationSummary* locations = conversion->GetLocations();
1581 Location out = locations->Out();
1582 Location in = locations->InAt(0);
1583 Primitive::Type result_type = conversion->GetResultType();
1584 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001585 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001586 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001587 case Primitive::kPrimByte:
1588 switch (input_type) {
1589 case Primitive::kPrimShort:
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001592 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001593 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001594 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001595 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001596 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001597 Address(CpuRegister(RSP), in.GetStackIndex()));
1598 } else {
1599 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001600 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001601 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1602 }
1603 break;
1604
1605 default:
1606 LOG(FATAL) << "Unexpected type conversion from " << input_type
1607 << " to " << result_type;
1608 }
1609 break;
1610
Roland Levillain01a8d712014-11-14 16:27:39 +00001611 case Primitive::kPrimShort:
1612 switch (input_type) {
1613 case Primitive::kPrimByte:
1614 case Primitive::kPrimInt:
1615 case Primitive::kPrimChar:
1616 // Processing a Dex `int-to-short' instruction.
1617 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001618 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001619 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001620 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001621 Address(CpuRegister(RSP), in.GetStackIndex()));
1622 } else {
1623 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001624 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001625 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1626 }
1627 break;
1628
1629 default:
1630 LOG(FATAL) << "Unexpected type conversion from " << input_type
1631 << " to " << result_type;
1632 }
1633 break;
1634
Roland Levillain946e1432014-11-11 17:35:19 +00001635 case Primitive::kPrimInt:
1636 switch (input_type) {
1637 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001638 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001639 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001640 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001641 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001642 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001643 Address(CpuRegister(RSP), in.GetStackIndex()));
1644 } else {
1645 DCHECK(in.IsConstant());
1646 DCHECK(in.GetConstant()->IsLongConstant());
1647 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001648 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001649 }
1650 break;
1651
Roland Levillain3f8f9362014-12-02 17:45:01 +00001652 case Primitive::kPrimFloat: {
1653 // Processing a Dex `float-to-int' instruction.
1654 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1655 CpuRegister output = out.AsRegister<CpuRegister>();
1656 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1657 Label done, nan;
1658
1659 __ movl(output, Immediate(kPrimIntMax));
1660 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001661 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001662 // if input >= temp goto done
1663 __ comiss(input, temp);
1664 __ j(kAboveEqual, &done);
1665 // if input == NaN goto nan
1666 __ j(kUnordered, &nan);
1667 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001668 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001669 __ jmp(&done);
1670 __ Bind(&nan);
1671 // output = 0
1672 __ xorl(output, output);
1673 __ Bind(&done);
1674 break;
1675 }
1676
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001677 case Primitive::kPrimDouble: {
1678 // Processing a Dex `double-to-int' instruction.
1679 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1680 CpuRegister output = out.AsRegister<CpuRegister>();
1681 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1682 Label done, nan;
1683
1684 __ movl(output, Immediate(kPrimIntMax));
1685 // temp = int-to-double(output)
1686 __ cvtsi2sd(temp, output);
1687 // if input >= temp goto done
1688 __ comisd(input, temp);
1689 __ j(kAboveEqual, &done);
1690 // if input == NaN goto nan
1691 __ j(kUnordered, &nan);
1692 // output = double-to-int-truncate(input)
1693 __ cvttsd2si(output, input);
1694 __ jmp(&done);
1695 __ Bind(&nan);
1696 // output = 0
1697 __ xorl(output, output);
1698 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001699 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001700 }
Roland Levillain946e1432014-11-11 17:35:19 +00001701
1702 default:
1703 LOG(FATAL) << "Unexpected type conversion from " << input_type
1704 << " to " << result_type;
1705 }
1706 break;
1707
Roland Levillaindff1f282014-11-05 14:15:05 +00001708 case Primitive::kPrimLong:
1709 switch (input_type) {
1710 DCHECK(out.IsRegister());
1711 case Primitive::kPrimByte:
1712 case Primitive::kPrimShort:
1713 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001714 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001715 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001716 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001717 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001718 break;
1719
Roland Levillain624279f2014-12-04 11:54:28 +00001720 case Primitive::kPrimFloat: {
1721 // Processing a Dex `float-to-long' instruction.
1722 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1723 CpuRegister output = out.AsRegister<CpuRegister>();
1724 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1725 Label done, nan;
1726
1727 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001728 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001729 __ cvtsi2ss(temp, output, true);
1730 // if input >= temp goto done
1731 __ comiss(input, temp);
1732 __ j(kAboveEqual, &done);
1733 // if input == NaN goto nan
1734 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001735 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001736 __ cvttss2si(output, input, true);
1737 __ jmp(&done);
1738 __ Bind(&nan);
1739 // output = 0
1740 __ xorq(output, output);
1741 __ Bind(&done);
1742 break;
1743 }
1744
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001745 case Primitive::kPrimDouble: {
1746 // Processing a Dex `double-to-long' instruction.
1747 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1748 CpuRegister output = out.AsRegister<CpuRegister>();
1749 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1750 Label done, nan;
1751
1752 __ movq(output, Immediate(kPrimLongMax));
1753 // temp = long-to-double(output)
1754 __ cvtsi2sd(temp, output, true);
1755 // if input >= temp goto done
1756 __ comisd(input, temp);
1757 __ j(kAboveEqual, &done);
1758 // if input == NaN goto nan
1759 __ j(kUnordered, &nan);
1760 // output = double-to-long-truncate(input)
1761 __ cvttsd2si(output, input, true);
1762 __ jmp(&done);
1763 __ Bind(&nan);
1764 // output = 0
1765 __ xorq(output, output);
1766 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001767 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001768 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001769
1770 default:
1771 LOG(FATAL) << "Unexpected type conversion from " << input_type
1772 << " to " << result_type;
1773 }
1774 break;
1775
Roland Levillain981e4542014-11-14 11:47:14 +00001776 case Primitive::kPrimChar:
1777 switch (input_type) {
1778 case Primitive::kPrimByte:
1779 case Primitive::kPrimShort:
1780 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001781 // Processing a Dex `int-to-char' instruction.
1782 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001783 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001784 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001785 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001786 Address(CpuRegister(RSP), in.GetStackIndex()));
1787 } else {
1788 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001789 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001790 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1791 }
1792 break;
1793
1794 default:
1795 LOG(FATAL) << "Unexpected type conversion from " << input_type
1796 << " to " << result_type;
1797 }
1798 break;
1799
Roland Levillaindff1f282014-11-05 14:15:05 +00001800 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001801 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimByte:
1803 case Primitive::kPrimShort:
1804 case Primitive::kPrimInt:
1805 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001806 // Processing a Dex `int-to-float' instruction.
1807 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001808 break;
1809
1810 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001811 // Processing a Dex `long-to-float' instruction.
1812 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1813 break;
1814
Roland Levillaincff13742014-11-17 14:32:17 +00001815 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001816 // Processing a Dex `double-to-float' instruction.
1817 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001818 break;
1819
1820 default:
1821 LOG(FATAL) << "Unexpected type conversion from " << input_type
1822 << " to " << result_type;
1823 };
1824 break;
1825
Roland Levillaindff1f282014-11-05 14:15:05 +00001826 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001827 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001828 case Primitive::kPrimByte:
1829 case Primitive::kPrimShort:
1830 case Primitive::kPrimInt:
1831 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001832 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001833 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001834 break;
1835
1836 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001837 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001838 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001839 break;
1840
Roland Levillaincff13742014-11-17 14:32:17 +00001841 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001842 // Processing a Dex `float-to-double' instruction.
1843 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001844 break;
1845
1846 default:
1847 LOG(FATAL) << "Unexpected type conversion from " << input_type
1848 << " to " << result_type;
1849 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001850 break;
1851
1852 default:
1853 LOG(FATAL) << "Unexpected type conversion from " << input_type
1854 << " to " << result_type;
1855 }
1856}
1857
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001858void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001861 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001862 case Primitive::kPrimInt: {
1863 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001864 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001866 break;
1867 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001868
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001870 locations->SetInAt(0, Location::RequiresRegister());
1871 locations->SetInAt(1, Location::RequiresRegister());
1872 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001873 break;
1874 }
1875
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 case Primitive::kPrimDouble:
1877 case Primitive::kPrimFloat: {
1878 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001879 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001881 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001883
1884 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001886 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001887}
1888
1889void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1890 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 Location first = locations->InAt(0);
1892 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001893 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001894
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001895 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001896 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001897 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001898 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1899 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1900 } else {
1901 __ leal(out.AsRegister<CpuRegister>(), Address(
1902 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1903 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001904 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001905 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1906 __ addl(out.AsRegister<CpuRegister>(),
1907 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
1908 } else {
1909 __ leal(out.AsRegister<CpuRegister>(), Address(
1910 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
1911 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001912 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001913 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001914 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001915 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001916 break;
1917 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001918
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001919 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001920 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001921 break;
1922 }
1923
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001924 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001925 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001926 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 }
1928
1929 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001930 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001931 break;
1932 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001933
1934 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001936 }
1937}
1938
1939void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001940 LocationSummary* locations =
1941 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001942 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001943 case Primitive::kPrimInt: {
1944 locations->SetInAt(0, Location::RequiresRegister());
1945 locations->SetInAt(1, Location::Any());
1946 locations->SetOut(Location::SameAsFirstInput());
1947 break;
1948 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001949 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001950 locations->SetInAt(0, Location::RequiresRegister());
1951 locations->SetInAt(1, Location::RequiresRegister());
1952 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001953 break;
1954 }
Calin Juravle11351682014-10-23 15:38:15 +01001955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble: {
1957 locations->SetInAt(0, Location::RequiresFpuRegister());
1958 locations->SetInAt(1, Location::RequiresFpuRegister());
1959 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001960 break;
Calin Juravle11351682014-10-23 15:38:15 +01001961 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001962 default:
Calin Juravle11351682014-10-23 15:38:15 +01001963 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001964 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001965}
1966
1967void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1968 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001969 Location first = locations->InAt(0);
1970 Location second = locations->InAt(1);
1971 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001972 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001973 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001974 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001975 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001976 } else if (second.IsConstant()) {
1977 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001978 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001979 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001980 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001981 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001982 break;
1983 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001984 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001985 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001986 break;
1987 }
1988
Calin Juravle11351682014-10-23 15:38:15 +01001989 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001991 break;
Calin Juravle11351682014-10-23 15:38:15 +01001992 }
1993
1994 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001995 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001996 break;
1997 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001998
1999 default:
Calin Juravle11351682014-10-23 15:38:15 +01002000 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002001 }
2002}
2003
Calin Juravle34bacdf2014-10-07 20:23:36 +01002004void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2005 LocationSummary* locations =
2006 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2007 switch (mul->GetResultType()) {
2008 case Primitive::kPrimInt: {
2009 locations->SetInAt(0, Location::RequiresRegister());
2010 locations->SetInAt(1, Location::Any());
2011 locations->SetOut(Location::SameAsFirstInput());
2012 break;
2013 }
2014 case Primitive::kPrimLong: {
2015 locations->SetInAt(0, Location::RequiresRegister());
2016 locations->SetInAt(1, Location::RequiresRegister());
2017 locations->SetOut(Location::SameAsFirstInput());
2018 break;
2019 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002020 case Primitive::kPrimFloat:
2021 case Primitive::kPrimDouble: {
2022 locations->SetInAt(0, Location::RequiresFpuRegister());
2023 locations->SetInAt(1, Location::RequiresFpuRegister());
2024 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002025 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002026 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002027
2028 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002029 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002030 }
2031}
2032
2033void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2034 LocationSummary* locations = mul->GetLocations();
2035 Location first = locations->InAt(0);
2036 Location second = locations->InAt(1);
2037 DCHECK(first.Equals(locations->Out()));
2038 switch (mul->GetResultType()) {
2039 case Primitive::kPrimInt: {
2040 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002042 } else if (second.IsConstant()) {
2043 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002045 } else {
2046 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002047 __ imull(first.AsRegister<CpuRegister>(),
2048 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002049 }
2050 break;
2051 }
2052 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002053 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002054 break;
2055 }
2056
Calin Juravleb5bfa962014-10-21 18:02:24 +01002057 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002059 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002060 }
2061
2062 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002064 break;
2065 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002066
2067 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002068 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002069 }
2070}
2071
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002072void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2073 uint32_t stack_adjustment, bool is_float) {
2074 if (source.IsStackSlot()) {
2075 DCHECK(is_float);
2076 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2077 } else if (source.IsDoubleStackSlot()) {
2078 DCHECK(!is_float);
2079 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2080 } else {
2081 // Write the value to the temporary location on the stack and load to FP stack.
2082 if (is_float) {
2083 Location stack_temp = Location::StackSlot(temp_offset);
2084 codegen_->Move(stack_temp, source);
2085 __ flds(Address(CpuRegister(RSP), temp_offset));
2086 } else {
2087 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2088 codegen_->Move(stack_temp, source);
2089 __ fldl(Address(CpuRegister(RSP), temp_offset));
2090 }
2091 }
2092}
2093
2094void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2095 Primitive::Type type = rem->GetResultType();
2096 bool is_float = type == Primitive::kPrimFloat;
2097 size_t elem_size = Primitive::ComponentSize(type);
2098 LocationSummary* locations = rem->GetLocations();
2099 Location first = locations->InAt(0);
2100 Location second = locations->InAt(1);
2101 Location out = locations->Out();
2102
2103 // Create stack space for 2 elements.
2104 // TODO: enhance register allocator to ask for stack temporaries.
2105 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2106
2107 // Load the values to the FP stack in reverse order, using temporaries if needed.
2108 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2109 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2110
2111 // Loop doing FPREM until we stabilize.
2112 Label retry;
2113 __ Bind(&retry);
2114 __ fprem();
2115
2116 // Move FP status to AX.
2117 __ fstsw();
2118
2119 // And see if the argument reduction is complete. This is signaled by the
2120 // C2 FPU flag bit set to 0.
2121 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2122 __ j(kNotEqual, &retry);
2123
2124 // We have settled on the final value. Retrieve it into an XMM register.
2125 // Store FP top of stack to real stack.
2126 if (is_float) {
2127 __ fsts(Address(CpuRegister(RSP), 0));
2128 } else {
2129 __ fstl(Address(CpuRegister(RSP), 0));
2130 }
2131
2132 // Pop the 2 items from the FP stack.
2133 __ fucompp();
2134
2135 // Load the value from the stack into an XMM register.
2136 DCHECK(out.IsFpuRegister()) << out;
2137 if (is_float) {
2138 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2139 } else {
2140 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2141 }
2142
2143 // And remove the temporary stack space we allocated.
2144 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2145}
2146
Calin Juravlebacfec32014-11-14 15:54:36 +00002147void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2148 DCHECK(instruction->IsDiv() || instruction->IsRem());
2149 Primitive::Type type = instruction->GetResultType();
2150 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2151
2152 bool is_div = instruction->IsDiv();
2153 LocationSummary* locations = instruction->GetLocations();
2154
Roland Levillain271ab9c2014-11-27 15:23:57 +00002155 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2156 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002157
Roland Levillain271ab9c2014-11-27 15:23:57 +00002158 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002159 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2160
2161 SlowPathCodeX86_64* slow_path =
2162 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2163 out_reg.AsRegister(), type, is_div);
2164 codegen_->AddSlowPath(slow_path);
2165
2166 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2167 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2168 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002169 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002170 __ cmpl(second_reg, Immediate(-1));
2171 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002172 // edx:eax <- sign-extended of eax
2173 __ cdq();
2174 // eax = quotient, edx = remainder
2175 __ idivl(second_reg);
2176 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002177 __ cmpq(second_reg, Immediate(-1));
2178 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002179 // rdx:rax <- sign-extended of rax
2180 __ cqo();
2181 // rax = quotient, rdx = remainder
2182 __ idivq(second_reg);
2183 }
2184
2185 __ Bind(slow_path->GetExitLabel());
2186}
2187
Calin Juravle7c4954d2014-10-28 16:57:40 +00002188void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2189 LocationSummary* locations =
2190 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2191 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002192 case Primitive::kPrimInt:
2193 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002194 locations->SetInAt(0, Location::RegisterLocation(RAX));
2195 locations->SetInAt(1, Location::RequiresRegister());
2196 locations->SetOut(Location::SameAsFirstInput());
2197 // Intel uses edx:eax as the dividend.
2198 locations->AddTemp(Location::RegisterLocation(RDX));
2199 break;
2200 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002201
Calin Juravle7c4954d2014-10-28 16:57:40 +00002202 case Primitive::kPrimFloat:
2203 case Primitive::kPrimDouble: {
2204 locations->SetInAt(0, Location::RequiresFpuRegister());
2205 locations->SetInAt(1, Location::RequiresFpuRegister());
2206 locations->SetOut(Location::SameAsFirstInput());
2207 break;
2208 }
2209
2210 default:
2211 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2212 }
2213}
2214
2215void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2216 LocationSummary* locations = div->GetLocations();
2217 Location first = locations->InAt(0);
2218 Location second = locations->InAt(1);
2219 DCHECK(first.Equals(locations->Out()));
2220
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002221 Primitive::Type type = div->GetResultType();
2222 switch (type) {
2223 case Primitive::kPrimInt:
2224 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002225 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002226 break;
2227 }
2228
Calin Juravle7c4954d2014-10-28 16:57:40 +00002229 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002231 break;
2232 }
2233
2234 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002235 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002236 break;
2237 }
2238
2239 default:
2240 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2241 }
2242}
2243
Calin Juravlebacfec32014-11-14 15:54:36 +00002244void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002245 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002248
2249 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002250 case Primitive::kPrimInt:
2251 case Primitive::kPrimLong: {
2252 locations->SetInAt(0, Location::RegisterLocation(RAX));
2253 locations->SetInAt(1, Location::RequiresRegister());
2254 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2255 locations->SetOut(Location::RegisterLocation(RDX));
2256 break;
2257 }
2258
2259 case Primitive::kPrimFloat:
2260 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002261 locations->SetInAt(0, Location::Any());
2262 locations->SetInAt(1, Location::Any());
2263 locations->SetOut(Location::RequiresFpuRegister());
2264 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002265 break;
2266 }
2267
2268 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002269 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002270 }
2271}
2272
2273void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2274 Primitive::Type type = rem->GetResultType();
2275 switch (type) {
2276 case Primitive::kPrimInt:
2277 case Primitive::kPrimLong: {
2278 GenerateDivRemIntegral(rem);
2279 break;
2280 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002281 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002282 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002283 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002284 break;
2285 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002286 default:
2287 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2288 }
2289}
2290
Calin Juravled0d48522014-11-04 16:40:20 +00002291void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2292 LocationSummary* locations =
2293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2294 locations->SetInAt(0, Location::Any());
2295 if (instruction->HasUses()) {
2296 locations->SetOut(Location::SameAsFirstInput());
2297 }
2298}
2299
2300void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2301 SlowPathCodeX86_64* slow_path =
2302 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2303 codegen_->AddSlowPath(slow_path);
2304
2305 LocationSummary* locations = instruction->GetLocations();
2306 Location value = locations->InAt(0);
2307
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002308 switch (instruction->GetType()) {
2309 case Primitive::kPrimInt: {
2310 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002311 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002312 __ j(kEqual, slow_path->GetEntryLabel());
2313 } else if (value.IsStackSlot()) {
2314 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2315 __ j(kEqual, slow_path->GetEntryLabel());
2316 } else {
2317 DCHECK(value.IsConstant()) << value;
2318 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2319 __ jmp(slow_path->GetEntryLabel());
2320 }
2321 }
2322 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002323 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002324 case Primitive::kPrimLong: {
2325 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002327 __ j(kEqual, slow_path->GetEntryLabel());
2328 } else if (value.IsDoubleStackSlot()) {
2329 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2330 __ j(kEqual, slow_path->GetEntryLabel());
2331 } else {
2332 DCHECK(value.IsConstant()) << value;
2333 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2334 __ jmp(slow_path->GetEntryLabel());
2335 }
2336 }
2337 break;
2338 }
2339 default:
2340 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002341 }
Calin Juravled0d48522014-11-04 16:40:20 +00002342}
2343
Calin Juravle9aec02f2014-11-18 23:06:35 +00002344void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2345 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2346
2347 LocationSummary* locations =
2348 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2349
2350 switch (op->GetResultType()) {
2351 case Primitive::kPrimInt:
2352 case Primitive::kPrimLong: {
2353 locations->SetInAt(0, Location::RequiresRegister());
2354 // The shift count needs to be in CL.
2355 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2356 locations->SetOut(Location::SameAsFirstInput());
2357 break;
2358 }
2359 default:
2360 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2361 }
2362}
2363
2364void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2365 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2366
2367 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002369 Location second = locations->InAt(1);
2370
2371 switch (op->GetResultType()) {
2372 case Primitive::kPrimInt: {
2373 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002374 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002375 if (op->IsShl()) {
2376 __ shll(first_reg, second_reg);
2377 } else if (op->IsShr()) {
2378 __ sarl(first_reg, second_reg);
2379 } else {
2380 __ shrl(first_reg, second_reg);
2381 }
2382 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002383 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002384 if (op->IsShl()) {
2385 __ shll(first_reg, imm);
2386 } else if (op->IsShr()) {
2387 __ sarl(first_reg, imm);
2388 } else {
2389 __ shrl(first_reg, imm);
2390 }
2391 }
2392 break;
2393 }
2394 case Primitive::kPrimLong: {
2395 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002396 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002397 if (op->IsShl()) {
2398 __ shlq(first_reg, second_reg);
2399 } else if (op->IsShr()) {
2400 __ sarq(first_reg, second_reg);
2401 } else {
2402 __ shrq(first_reg, second_reg);
2403 }
2404 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002405 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002406 if (op->IsShl()) {
2407 __ shlq(first_reg, imm);
2408 } else if (op->IsShr()) {
2409 __ sarq(first_reg, imm);
2410 } else {
2411 __ shrq(first_reg, imm);
2412 }
2413 }
2414 break;
2415 }
2416 default:
2417 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2418 }
2419}
2420
2421void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2422 HandleShift(shl);
2423}
2424
2425void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2426 HandleShift(shl);
2427}
2428
2429void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2430 HandleShift(shr);
2431}
2432
2433void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2434 HandleShift(shr);
2435}
2436
2437void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2438 HandleShift(ushr);
2439}
2440
2441void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2442 HandleShift(ushr);
2443}
2444
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002445void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002446 LocationSummary* locations =
2447 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002448 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002449 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2450 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2451 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002452}
2453
2454void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2455 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002456 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002457 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2458
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002459 __ gs()->call(
2460 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002461
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002462 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002463 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002464}
2465
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002466void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2467 LocationSummary* locations =
2468 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2469 InvokeRuntimeCallingConvention calling_convention;
2470 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002471 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002472 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002473 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002474}
2475
2476void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2477 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002478 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002479 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2480
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002481 __ gs()->call(
2482 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002483
2484 DCHECK(!codegen_->IsLeafMethod());
2485 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2486}
2487
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002488void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002489 LocationSummary* locations =
2490 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002491 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2492 if (location.IsStackSlot()) {
2493 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2494 } else if (location.IsDoubleStackSlot()) {
2495 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2496 }
2497 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002498}
2499
2500void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2501 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002502 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002503}
2504
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002505void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002506 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002507 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002508 locations->SetInAt(0, Location::RequiresRegister());
2509 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002510}
2511
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002512void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2513 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2515 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002516 Location out = locations->Out();
2517 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002518 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002519 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002520 break;
2521
2522 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002524 break;
2525
2526 default:
2527 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2528 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002529}
2530
2531void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002532 LocationSummary* locations =
2533 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002534 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2535 locations->SetInAt(i, Location::Any());
2536 }
2537 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002538}
2539
2540void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002541 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002542 LOG(FATAL) << "Unimplemented";
2543}
2544
Calin Juravle52c48962014-12-16 17:02:57 +00002545void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2546 /*
2547 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2548 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2549 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2550 */
2551 switch (kind) {
2552 case MemBarrierKind::kAnyAny: {
2553 __ mfence();
2554 break;
2555 }
2556 case MemBarrierKind::kAnyStore:
2557 case MemBarrierKind::kLoadAny:
2558 case MemBarrierKind::kStoreStore: {
2559 // nop
2560 break;
2561 }
2562 default:
2563 LOG(FATAL) << "Unexpected memory barier " << kind;
2564 }
2565}
2566
2567void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2568 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2569
Nicolas Geoffray39468442014-09-02 15:17:15 +01002570 LocationSummary* locations =
2571 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002572 locations->SetInAt(0, Location::RequiresRegister());
2573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2574}
2575
2576void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2577 const FieldInfo& field_info) {
2578 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2579
2580 LocationSummary* locations = instruction->GetLocations();
2581 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2582 Location out = locations->Out();
2583 bool is_volatile = field_info.IsVolatile();
2584 Primitive::Type field_type = field_info.GetFieldType();
2585 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2586
2587 switch (field_type) {
2588 case Primitive::kPrimBoolean: {
2589 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2590 break;
2591 }
2592
2593 case Primitive::kPrimByte: {
2594 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2595 break;
2596 }
2597
2598 case Primitive::kPrimShort: {
2599 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2600 break;
2601 }
2602
2603 case Primitive::kPrimChar: {
2604 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2605 break;
2606 }
2607
2608 case Primitive::kPrimInt:
2609 case Primitive::kPrimNot: {
2610 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2611 break;
2612 }
2613
2614 case Primitive::kPrimLong: {
2615 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2616 break;
2617 }
2618
2619 case Primitive::kPrimFloat: {
2620 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2621 break;
2622 }
2623
2624 case Primitive::kPrimDouble: {
2625 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2626 break;
2627 }
2628
2629 case Primitive::kPrimVoid:
2630 LOG(FATAL) << "Unreachable type " << field_type;
2631 UNREACHABLE();
2632 }
2633
Calin Juravle77520bc2015-01-12 18:45:46 +00002634 codegen_->MaybeRecordImplicitNullCheck(instruction);
2635
Calin Juravle52c48962014-12-16 17:02:57 +00002636 if (is_volatile) {
2637 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2638 }
2639}
2640
2641void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2642 const FieldInfo& field_info) {
2643 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2644
2645 LocationSummary* locations =
2646 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002647 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002648 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2649
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002650 locations->SetInAt(0, Location::RequiresRegister());
2651 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002652 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002653 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002654 locations->AddTemp(Location::RequiresRegister());
2655 locations->AddTemp(Location::RequiresRegister());
2656 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002657}
2658
Calin Juravle52c48962014-12-16 17:02:57 +00002659void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2660 const FieldInfo& field_info) {
2661 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2662
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002663 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002664 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2665 Location value = locations->InAt(1);
2666 bool is_volatile = field_info.IsVolatile();
2667 Primitive::Type field_type = field_info.GetFieldType();
2668 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2669
2670 if (is_volatile) {
2671 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2672 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002673
2674 switch (field_type) {
2675 case Primitive::kPrimBoolean:
2676 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002677 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002678 break;
2679 }
2680
2681 case Primitive::kPrimShort:
2682 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002683 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 break;
2685 }
2686
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002687 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002688 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002689 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002690 break;
2691 }
2692
2693 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002694 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002695 break;
2696 }
2697
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002698 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002699 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002700 break;
2701 }
2702
2703 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002704 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002705 break;
2706 }
2707
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002708 case Primitive::kPrimVoid:
2709 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002710 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002711 }
Calin Juravle52c48962014-12-16 17:02:57 +00002712
Calin Juravle77520bc2015-01-12 18:45:46 +00002713 codegen_->MaybeRecordImplicitNullCheck(instruction);
2714
2715 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2716 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2717 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2718 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2719 }
2720
Calin Juravle52c48962014-12-16 17:02:57 +00002721 if (is_volatile) {
2722 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2723 }
2724}
2725
2726void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2727 HandleFieldSet(instruction, instruction->GetFieldInfo());
2728}
2729
2730void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2731 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732}
2733
2734void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002735 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736}
2737
2738void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002739 HandleFieldGet(instruction, instruction->GetFieldInfo());
2740}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741
Calin Juravle52c48962014-12-16 17:02:57 +00002742void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2743 HandleFieldGet(instruction);
2744}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002745
Calin Juravle52c48962014-12-16 17:02:57 +00002746void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2747 HandleFieldGet(instruction, instruction->GetFieldInfo());
2748}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002749
Calin Juravle52c48962014-12-16 17:02:57 +00002750void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2751 HandleFieldSet(instruction, instruction->GetFieldInfo());
2752}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002753
Calin Juravle52c48962014-12-16 17:02:57 +00002754void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2755 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756}
2757
2758void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002761 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2762 ? Location::RequiresRegister()
2763 : Location::Any();
2764 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002765 if (instruction->HasUses()) {
2766 locations->SetOut(Location::SameAsFirstInput());
2767 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002768}
2769
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002770void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002771 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2772 return;
2773 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002774 LocationSummary* locations = instruction->GetLocations();
2775 Location obj = locations->InAt(0);
2776
2777 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2778 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2779}
2780
2781void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002782 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783 codegen_->AddSlowPath(slow_path);
2784
2785 LocationSummary* locations = instruction->GetLocations();
2786 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002787
2788 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002789 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002790 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002791 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002792 } else {
2793 DCHECK(obj.IsConstant()) << obj;
2794 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2795 __ jmp(slow_path->GetEntryLabel());
2796 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797 }
2798 __ j(kEqual, slow_path->GetEntryLabel());
2799}
2800
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002801void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2802 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2803 GenerateImplicitNullCheck(instruction);
2804 } else {
2805 GenerateExplicitNullCheck(instruction);
2806 }
2807}
2808
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002809void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002810 LocationSummary* locations =
2811 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002812 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002813 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002814 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002816}
2817
2818void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2819 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002820 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002821 Location index = locations->InAt(1);
2822
2823 switch (instruction->GetType()) {
2824 case Primitive::kPrimBoolean: {
2825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002826 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002827 if (index.IsConstant()) {
2828 __ movzxb(out, Address(obj,
2829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2830 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002832 }
2833 break;
2834 }
2835
2836 case Primitive::kPrimByte: {
2837 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002838 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002839 if (index.IsConstant()) {
2840 __ movsxb(out, Address(obj,
2841 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002843 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002844 }
2845 break;
2846 }
2847
2848 case Primitive::kPrimShort: {
2849 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002850 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002851 if (index.IsConstant()) {
2852 __ movsxw(out, Address(obj,
2853 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2854 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002856 }
2857 break;
2858 }
2859
2860 case Primitive::kPrimChar: {
2861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002862 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002863 if (index.IsConstant()) {
2864 __ movzxw(out, Address(obj,
2865 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2866 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002867 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002868 }
2869 break;
2870 }
2871
2872 case Primitive::kPrimInt:
2873 case Primitive::kPrimNot: {
2874 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2875 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002877 if (index.IsConstant()) {
2878 __ movl(out, Address(obj,
2879 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2880 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002881 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002882 }
2883 break;
2884 }
2885
2886 case Primitive::kPrimLong: {
2887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002889 if (index.IsConstant()) {
2890 __ movq(out, Address(obj,
2891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2892 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002893 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002894 }
2895 break;
2896 }
2897
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002898 case Primitive::kPrimFloat: {
2899 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002900 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901 if (index.IsConstant()) {
2902 __ movss(out, Address(obj,
2903 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2904 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002906 }
2907 break;
2908 }
2909
2910 case Primitive::kPrimDouble: {
2911 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002912 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002913 if (index.IsConstant()) {
2914 __ movsd(out, Address(obj,
2915 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2916 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002917 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002918 }
2919 break;
2920 }
2921
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002922 case Primitive::kPrimVoid:
2923 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002924 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002925 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002926 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927}
2928
2929void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002930 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002931
2932 bool needs_write_barrier =
2933 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2934 bool needs_runtime_call = instruction->NeedsTypeCheck();
2935
Nicolas Geoffray39468442014-09-02 15:17:15 +01002936 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002937 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2938 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002939 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002940 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2941 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2942 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002943 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002944 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002945 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002946 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2947 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002948 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002949 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002950 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2951 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002952 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002953 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002954 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002955
2956 if (needs_write_barrier) {
2957 // Temporary registers for the write barrier.
2958 locations->AddTemp(Location::RequiresRegister());
2959 locations->AddTemp(Location::RequiresRegister());
2960 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002961 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002962}
2963
2964void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2965 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002966 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002967 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002968 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002969 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002970 bool needs_runtime_call = locations->WillCall();
2971 bool needs_write_barrier =
2972 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002973
2974 switch (value_type) {
2975 case Primitive::kPrimBoolean:
2976 case Primitive::kPrimByte: {
2977 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 if (index.IsConstant()) {
2979 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002980 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002982 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002983 __ movb(Address(obj, offset),
2984 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002985 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002986 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002987 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2989 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002990 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002991 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002992 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2993 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002994 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002995 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002996 break;
2997 }
2998
2999 case Primitive::kPrimShort:
3000 case Primitive::kPrimChar: {
3001 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003002 if (index.IsConstant()) {
3003 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003004 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003005 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003006 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003007 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003008 __ movw(Address(obj, offset),
3009 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003010 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003011 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003012 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003013 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003014 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3015 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003016 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003017 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003018 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003019 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3020 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003021 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003022 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003023 break;
3024 }
3025
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003026 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003028 if (!needs_runtime_call) {
3029 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3030 if (index.IsConstant()) {
3031 size_t offset =
3032 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3033 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003035 } else {
3036 DCHECK(value.IsConstant()) << value;
3037 __ movl(Address(obj, offset),
3038 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3039 }
3040 } else {
3041 DCHECK(index.IsRegister()) << index;
3042 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003043 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3044 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003045 } else {
3046 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003048 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3049 }
3050 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003051 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003052 if (needs_write_barrier) {
3053 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003054 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3055 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3056 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003057 }
3058 } else {
3059 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003060 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3061 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003062 DCHECK(!codegen_->IsLeafMethod());
3063 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3064 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003065 break;
3066 }
3067
3068 case Primitive::kPrimLong: {
3069 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 if (index.IsConstant()) {
3071 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003072 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003073 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003074 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003075 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003076 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3077 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003079 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003080 break;
3081 }
3082
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003083 case Primitive::kPrimFloat: {
3084 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3085 if (index.IsConstant()) {
3086 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3087 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003088 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003089 } else {
3090 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003091 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3092 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003093 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003094 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003095 break;
3096 }
3097
3098 case Primitive::kPrimDouble: {
3099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3100 if (index.IsConstant()) {
3101 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3102 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003103 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003104 } else {
3105 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3107 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003108 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003109 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003110 break;
3111 }
3112
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 case Primitive::kPrimVoid:
3114 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003115 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003116 }
3117}
3118
3119void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003120 LocationSummary* locations =
3121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003122 locations->SetInAt(0, Location::RequiresRegister());
3123 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003124}
3125
3126void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3127 LocationSummary* locations = instruction->GetLocations();
3128 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003129 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3130 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003131 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003132 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133}
3134
3135void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003136 LocationSummary* locations =
3137 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003138 locations->SetInAt(0, Location::RequiresRegister());
3139 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003140 if (instruction->HasUses()) {
3141 locations->SetOut(Location::SameAsFirstInput());
3142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003143}
3144
3145void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3146 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003147 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003148 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003149 codegen_->AddSlowPath(slow_path);
3150
Roland Levillain271ab9c2014-11-27 15:23:57 +00003151 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3152 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003153
3154 __ cmpl(index, length);
3155 __ j(kAboveEqual, slow_path->GetEntryLabel());
3156}
3157
3158void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3159 CpuRegister card,
3160 CpuRegister object,
3161 CpuRegister value) {
3162 Label is_null;
3163 __ testl(value, value);
3164 __ j(kEqual, &is_null);
3165 __ gs()->movq(card, Address::Absolute(
3166 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3167 __ movq(temp, object);
3168 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3169 __ movb(Address(temp, card, TIMES_1, 0), card);
3170 __ Bind(&is_null);
3171}
3172
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003173void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3174 temp->SetLocations(nullptr);
3175}
3176
3177void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3178 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003179 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003180}
3181
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003182void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003183 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003184 LOG(FATAL) << "Unimplemented";
3185}
3186
3187void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003188 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3189}
3190
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003191void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3192 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3193}
3194
3195void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003196 HBasicBlock* block = instruction->GetBlock();
3197 if (block->GetLoopInformation() != nullptr) {
3198 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3199 // The back edge will generate the suspend check.
3200 return;
3201 }
3202 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3203 // The goto will generate the suspend check.
3204 return;
3205 }
3206 GenerateSuspendCheck(instruction, nullptr);
3207}
3208
3209void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3210 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003211 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003212 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003213 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003214 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003215 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003216 if (successor == nullptr) {
3217 __ j(kNotEqual, slow_path->GetEntryLabel());
3218 __ Bind(slow_path->GetReturnLabel());
3219 } else {
3220 __ j(kEqual, codegen_->GetLabelOf(successor));
3221 __ jmp(slow_path->GetEntryLabel());
3222 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003223}
3224
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003225X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3226 return codegen_->GetAssembler();
3227}
3228
3229void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3230 MoveOperands* move = moves_.Get(index);
3231 Location source = move->GetSource();
3232 Location destination = move->GetDestination();
3233
3234 if (source.IsRegister()) {
3235 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003237 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003238 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003240 } else {
3241 DCHECK(destination.IsDoubleStackSlot());
3242 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003243 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003244 }
3245 } else if (source.IsStackSlot()) {
3246 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003248 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003249 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003251 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003252 } else {
3253 DCHECK(destination.IsStackSlot());
3254 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3255 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3256 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003257 } else if (source.IsDoubleStackSlot()) {
3258 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003260 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003261 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003262 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3263 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003264 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003265 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003266 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3267 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3268 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003269 } else if (source.IsConstant()) {
3270 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003271 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3272 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003273 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003274 if (value == 0) {
3275 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3276 } else {
3277 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3278 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003279 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003280 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003281 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003282 }
3283 } else if (constant->IsLongConstant()) {
3284 int64_t value = constant->AsLongConstant()->GetValue();
3285 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003286 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003287 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003288 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003289 __ movq(CpuRegister(TMP), Immediate(value));
3290 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3291 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003292 } else if (constant->IsFloatConstant()) {
3293 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3294 if (destination.IsFpuRegister()) {
3295 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003296 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003297 } else {
3298 DCHECK(destination.IsStackSlot()) << destination;
3299 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3300 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003301 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003302 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3303 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3304 if (destination.IsFpuRegister()) {
3305 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003306 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003307 } else {
3308 DCHECK(destination.IsDoubleStackSlot()) << destination;
3309 __ movq(CpuRegister(TMP), imm);
3310 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3311 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003312 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003313 } else if (source.IsFpuRegister()) {
3314 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003315 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003316 } else if (destination.IsStackSlot()) {
3317 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003318 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003319 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003320 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003321 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003322 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003323 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003324 }
3325}
3326
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003327void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003328 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003329 __ movl(Address(CpuRegister(RSP), mem), reg);
3330 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003331}
3332
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003333void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003334 ScratchRegisterScope ensure_scratch(
3335 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3336
3337 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3338 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3339 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3340 Address(CpuRegister(RSP), mem2 + stack_offset));
3341 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3342 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3343 CpuRegister(ensure_scratch.GetRegister()));
3344}
3345
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003346void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3347 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3348 __ movq(Address(CpuRegister(RSP), mem), reg);
3349 __ movq(reg, CpuRegister(TMP));
3350}
3351
3352void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3353 ScratchRegisterScope ensure_scratch(
3354 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3355
3356 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3357 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3358 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3359 Address(CpuRegister(RSP), mem2 + stack_offset));
3360 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3361 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3362 CpuRegister(ensure_scratch.GetRegister()));
3363}
3364
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003365void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3366 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3367 __ movss(Address(CpuRegister(RSP), mem), reg);
3368 __ movd(reg, CpuRegister(TMP));
3369}
3370
3371void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3372 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3373 __ movsd(Address(CpuRegister(RSP), mem), reg);
3374 __ movd(reg, CpuRegister(TMP));
3375}
3376
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003377void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3378 MoveOperands* move = moves_.Get(index);
3379 Location source = move->GetSource();
3380 Location destination = move->GetDestination();
3381
3382 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003383 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003384 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003385 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003386 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003387 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003388 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003389 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3390 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003392 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003393 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003394 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3395 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003396 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3398 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3399 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003400 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003401 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003402 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003403 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003404 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003405 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003406 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003407 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003408 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003409 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003410 }
3411}
3412
3413
3414void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3415 __ pushq(CpuRegister(reg));
3416}
3417
3418
3419void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3420 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003421}
3422
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003423void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3424 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3425 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3426 Immediate(mirror::Class::kStatusInitialized));
3427 __ j(kLess, slow_path->GetEntryLabel());
3428 __ Bind(slow_path->GetExitLabel());
3429 // No need for memory fence, thanks to the X86_64 memory model.
3430}
3431
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003432void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003433 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3434 ? LocationSummary::kCallOnSlowPath
3435 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003436 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003437 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003438 locations->SetOut(Location::RequiresRegister());
3439}
3440
3441void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003442 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003443 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003444 DCHECK(!cls->CanCallRuntime());
3445 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003446 codegen_->LoadCurrentMethod(out);
3447 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3448 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003449 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003450 codegen_->LoadCurrentMethod(out);
3451 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3452 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003453 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3454 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3455 codegen_->AddSlowPath(slow_path);
3456 __ testl(out, out);
3457 __ j(kEqual, slow_path->GetEntryLabel());
3458 if (cls->MustGenerateClinitCheck()) {
3459 GenerateClassInitializationCheck(slow_path, out);
3460 } else {
3461 __ Bind(slow_path->GetExitLabel());
3462 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003463 }
3464}
3465
3466void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3467 LocationSummary* locations =
3468 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3469 locations->SetInAt(0, Location::RequiresRegister());
3470 if (check->HasUses()) {
3471 locations->SetOut(Location::SameAsFirstInput());
3472 }
3473}
3474
3475void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003476 // We assume the class to not be null.
3477 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3478 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003479 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003480 GenerateClassInitializationCheck(slow_path,
3481 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003482}
3483
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003484void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3485 LocationSummary* locations =
3486 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3487 locations->SetOut(Location::RequiresRegister());
3488}
3489
3490void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3491 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3492 codegen_->AddSlowPath(slow_path);
3493
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003495 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003496 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3497 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003498 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3499 __ testl(out, out);
3500 __ j(kEqual, slow_path->GetEntryLabel());
3501 __ Bind(slow_path->GetExitLabel());
3502}
3503
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003504void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3505 LocationSummary* locations =
3506 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3507 locations->SetOut(Location::RequiresRegister());
3508}
3509
3510void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3511 Address address = Address::Absolute(
3512 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003513 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003514 __ gs()->movl(address, Immediate(0));
3515}
3516
3517void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3518 LocationSummary* locations =
3519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3520 InvokeRuntimeCallingConvention calling_convention;
3521 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3522}
3523
3524void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3525 __ gs()->call(
3526 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3527 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3528}
3529
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003530void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003531 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3532 ? LocationSummary::kNoCall
3533 : LocationSummary::kCallOnSlowPath;
3534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3535 locations->SetInAt(0, Location::RequiresRegister());
3536 locations->SetInAt(1, Location::Any());
3537 locations->SetOut(Location::RequiresRegister());
3538}
3539
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003540void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003541 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003542 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003543 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003544 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003545 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3546 Label done, zero;
3547 SlowPathCodeX86_64* slow_path = nullptr;
3548
3549 // Return 0 if `obj` is null.
3550 // TODO: avoid this check if we know obj is not null.
3551 __ testl(obj, obj);
3552 __ j(kEqual, &zero);
3553 // Compare the class of `obj` with `cls`.
3554 __ movl(out, Address(obj, class_offset));
3555 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003556 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003557 } else {
3558 DCHECK(cls.IsStackSlot()) << cls;
3559 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3560 }
3561 if (instruction->IsClassFinal()) {
3562 // Classes must be equal for the instanceof to succeed.
3563 __ j(kNotEqual, &zero);
3564 __ movl(out, Immediate(1));
3565 __ jmp(&done);
3566 } else {
3567 // If the classes are not equal, we go into a slow path.
3568 DCHECK(locations->OnlyCallsOnSlowPath());
3569 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003570 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003571 codegen_->AddSlowPath(slow_path);
3572 __ j(kNotEqual, slow_path->GetEntryLabel());
3573 __ movl(out, Immediate(1));
3574 __ jmp(&done);
3575 }
3576 __ Bind(&zero);
3577 __ movl(out, Immediate(0));
3578 if (slow_path != nullptr) {
3579 __ Bind(slow_path->GetExitLabel());
3580 }
3581 __ Bind(&done);
3582}
3583
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003584void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3586 instruction, LocationSummary::kCallOnSlowPath);
3587 locations->SetInAt(0, Location::RequiresRegister());
3588 locations->SetInAt(1, Location::Any());
3589 locations->AddTemp(Location::RequiresRegister());
3590}
3591
3592void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3593 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003594 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003595 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003596 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003597 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3598 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3599 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3600 codegen_->AddSlowPath(slow_path);
3601
3602 // TODO: avoid this check if we know obj is not null.
3603 __ testl(obj, obj);
3604 __ j(kEqual, slow_path->GetExitLabel());
3605 // Compare the class of `obj` with `cls`.
3606 __ movl(temp, Address(obj, class_offset));
3607 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003608 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003609 } else {
3610 DCHECK(cls.IsStackSlot()) << cls;
3611 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3612 }
3613 // Classes must be equal for the checkcast to succeed.
3614 __ j(kNotEqual, slow_path->GetEntryLabel());
3615 __ Bind(slow_path->GetExitLabel());
3616}
3617
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003618void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3619 LocationSummary* locations =
3620 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3621 InvokeRuntimeCallingConvention calling_convention;
3622 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3623}
3624
3625void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3626 __ gs()->call(Address::Absolute(instruction->IsEnter()
3627 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3628 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3629 true));
3630 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3631}
3632
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003633void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3634void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3635void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3636
3637void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3638 LocationSummary* locations =
3639 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3640 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3641 || instruction->GetResultType() == Primitive::kPrimLong);
3642 locations->SetInAt(0, Location::RequiresRegister());
3643 if (instruction->GetType() == Primitive::kPrimInt) {
3644 locations->SetInAt(1, Location::Any());
3645 } else {
3646 // Request a register to avoid loading a 64bits constant.
3647 locations->SetInAt(1, Location::RequiresRegister());
3648 }
3649 locations->SetOut(Location::SameAsFirstInput());
3650}
3651
3652void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3653 HandleBitwiseOperation(instruction);
3654}
3655
3656void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3657 HandleBitwiseOperation(instruction);
3658}
3659
3660void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3661 HandleBitwiseOperation(instruction);
3662}
3663
3664void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3665 LocationSummary* locations = instruction->GetLocations();
3666 Location first = locations->InAt(0);
3667 Location second = locations->InAt(1);
3668 DCHECK(first.Equals(locations->Out()));
3669
3670 if (instruction->GetResultType() == Primitive::kPrimInt) {
3671 if (second.IsRegister()) {
3672 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003673 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003674 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003675 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003676 } else {
3677 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003679 }
3680 } else if (second.IsConstant()) {
3681 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3682 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003684 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003685 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003686 } else {
3687 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003688 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003689 }
3690 } else {
3691 Address address(CpuRegister(RSP), second.GetStackIndex());
3692 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003694 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003696 } else {
3697 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003698 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003699 }
3700 }
3701 } else {
3702 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3703 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003705 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003706 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003707 } else {
3708 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003710 }
3711 }
3712}
3713
Calin Juravleb1498f62015-02-16 13:13:29 +00003714void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
3715 // Nothing to do, this should be removed during prepare for register allocator.
3716 UNUSED(instruction);
3717 LOG(FATAL) << "Unreachable";
3718}
3719
3720void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
3721 // Nothing to do, this should be removed during prepare for register allocator.
3722 UNUSED(instruction);
3723 LOG(FATAL) << "Unreachable";
3724}
3725
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003726} // namespace x86_64
3727} // namespace art