blob: b2b427f863d3a558f11ade3d5a6533296429720a [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();
610 int32_t value;
611 if (constant->IsFloatConstant()) {
612 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
613 } else {
614 DCHECK(constant->IsIntConstant());
615 value = constant->AsIntConstant()->GetValue();
616 }
617 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100618 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500619 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000620 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
621 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100622 }
623 } else {
624 DCHECK(destination.IsDoubleStackSlot());
625 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 } else if (source.IsFpuRegister()) {
629 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500631 } else if (source.IsConstant()) {
632 HConstant* constant = source.GetConstant();
633 int64_t value = constant->AsLongConstant()->GetValue();
634 if (constant->IsDoubleConstant()) {
635 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
636 } else {
637 DCHECK(constant->IsLongConstant());
638 value = constant->AsLongConstant()->GetValue();
639 }
640 __ movq(CpuRegister(TMP), Immediate(value));
641 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100642 } else {
643 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000644 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
645 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100646 }
647 }
648}
649
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100650void CodeGeneratorX86_64::Move(HInstruction* instruction,
651 Location location,
652 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000653 LocationSummary* locations = instruction->GetLocations();
654 if (locations != nullptr && locations->Out().Equals(location)) {
655 return;
656 }
657
658 if (locations != nullptr && locations->Out().IsConstant()) {
659 HConstant* const_to_move = locations->Out().GetConstant();
660 if (const_to_move->IsIntConstant()) {
661 Immediate imm(const_to_move->AsIntConstant()->GetValue());
662 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000663 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000664 } else if (location.IsStackSlot()) {
665 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
666 } else {
667 DCHECK(location.IsConstant());
668 DCHECK_EQ(location.GetConstant(), const_to_move);
669 }
670 } else if (const_to_move->IsLongConstant()) {
671 int64_t value = const_to_move->AsLongConstant()->GetValue();
672 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000673 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000674 } else if (location.IsDoubleStackSlot()) {
675 __ movq(CpuRegister(TMP), Immediate(value));
676 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
677 } else {
678 DCHECK(location.IsConstant());
679 DCHECK_EQ(location.GetConstant(), const_to_move);
680 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100681 }
Roland Levillain476df552014-10-09 17:51:36 +0100682 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100683 switch (instruction->GetType()) {
684 case Primitive::kPrimBoolean:
685 case Primitive::kPrimByte:
686 case Primitive::kPrimChar:
687 case Primitive::kPrimShort:
688 case Primitive::kPrimInt:
689 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100690 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100691 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
692 break;
693
694 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000696 Move(location,
697 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100698 break;
699
700 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000703 } else if (instruction->IsTemporary()) {
704 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
705 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100706 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100707 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708 switch (instruction->GetType()) {
709 case Primitive::kPrimBoolean:
710 case Primitive::kPrimByte:
711 case Primitive::kPrimChar:
712 case Primitive::kPrimShort:
713 case Primitive::kPrimInt:
714 case Primitive::kPrimNot:
715 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 case Primitive::kPrimFloat:
717 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000718 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100719 break;
720
721 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100723 }
724 }
725}
726
727void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
728 got->SetLocations(nullptr);
729}
730
731void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
732 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100733 DCHECK(!successor->IsExitBlock());
734
735 HBasicBlock* block = got->GetBlock();
736 HInstruction* previous = got->GetPrevious();
737
738 HLoopInformation* info = block->GetLoopInformation();
739 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
740 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
741 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
742 return;
743 }
744
745 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
746 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
747 }
748 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100749 __ jmp(codegen_->GetLabelOf(successor));
750 }
751}
752
753void LocationsBuilderX86_64::VisitExit(HExit* exit) {
754 exit->SetLocations(nullptr);
755}
756
757void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700758 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100759 if (kIsDebugBuild) {
760 __ Comment("Unreachable");
761 __ int3();
762 }
763}
764
765void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100766 LocationSummary* locations =
767 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100768 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100769 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100770 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100771 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100772}
773
774void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700775 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100776 if (cond->IsIntConstant()) {
777 // Constant condition, statically compared against 1.
778 int32_t cond_value = cond->AsIntConstant()->GetValue();
779 if (cond_value == 1) {
780 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
781 if_instr->IfTrueSuccessor())) {
782 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100783 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100784 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100785 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100786 DCHECK_EQ(cond_value, 0);
787 }
788 } else {
789 bool materialized =
790 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
791 // Moves do not affect the eflags register, so if the condition is
792 // evaluated just before the if, we don't need to evaluate it
793 // again.
794 bool eflags_set = cond->IsCondition()
795 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
796 if (materialized) {
797 if (!eflags_set) {
798 // Materialized condition, compare against 0.
799 Location lhs = if_instr->GetLocations()->InAt(0);
800 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000801 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100802 } else {
803 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
804 Immediate(0));
805 }
806 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
807 } else {
808 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
809 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
810 }
811 } else {
812 Location lhs = cond->GetLocations()->InAt(0);
813 Location rhs = cond->GetLocations()->InAt(1);
814 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000815 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000817 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100818 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
819 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000820 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100821 Address(CpuRegister(RSP), rhs.GetStackIndex()));
822 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100823 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
824 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700825 }
Dave Allison20dfc792014-06-16 20:44:29 -0700826 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100827 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
828 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700829 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100830 }
831}
832
833void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
834 local->SetLocations(nullptr);
835}
836
837void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
838 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
839}
840
841void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
842 local->SetLocations(nullptr);
843}
844
845void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
846 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700847 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848}
849
850void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100851 LocationSummary* locations =
852 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100853 switch (store->InputAt(1)->GetType()) {
854 case Primitive::kPrimBoolean:
855 case Primitive::kPrimByte:
856 case Primitive::kPrimChar:
857 case Primitive::kPrimShort:
858 case Primitive::kPrimInt:
859 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100860 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
862 break;
863
864 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100865 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100866 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
867 break;
868
869 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100870 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100871 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872}
873
874void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700875 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100876}
877
Dave Allison20dfc792014-06-16 20:44:29 -0700878void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100879 LocationSummary* locations =
880 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100881 locations->SetInAt(0, Location::RequiresRegister());
882 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100883 if (comp->NeedsMaterialization()) {
884 locations->SetOut(Location::RequiresRegister());
885 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100886}
887
Dave Allison20dfc792014-06-16 20:44:29 -0700888void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
889 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100890 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000891 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100892 // Clear register: setcc only sets the low byte.
893 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100894 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000895 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
896 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100897 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000898 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100899 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
900 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000901 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100902 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
903 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100904 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700905 }
906}
907
908void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
909 VisitCondition(comp);
910}
911
912void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
913 VisitCondition(comp);
914}
915
916void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
917 VisitCondition(comp);
918}
919
920void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
921 VisitCondition(comp);
922}
923
924void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
925 VisitCondition(comp);
926}
927
928void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
929 VisitCondition(comp);
930}
931
932void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
933 VisitCondition(comp);
934}
935
936void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
937 VisitCondition(comp);
938}
939
940void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
941 VisitCondition(comp);
942}
943
944void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
945 VisitCondition(comp);
946}
947
948void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
949 VisitCondition(comp);
950}
951
952void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
953 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100954}
955
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100956void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100957 LocationSummary* locations =
958 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000959 switch (compare->InputAt(0)->GetType()) {
960 case Primitive::kPrimLong: {
961 locations->SetInAt(0, Location::RequiresRegister());
962 locations->SetInAt(1, Location::RequiresRegister());
963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
964 break;
965 }
966 case Primitive::kPrimFloat:
967 case Primitive::kPrimDouble: {
968 locations->SetInAt(0, Location::RequiresFpuRegister());
969 locations->SetInAt(1, Location::RequiresFpuRegister());
970 locations->SetOut(Location::RequiresRegister());
971 break;
972 }
973 default:
974 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
975 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100976}
977
978void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100979 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000980 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000981 Location left = locations->InAt(0);
982 Location right = locations->InAt(1);
983
984 Label less, greater, done;
985 Primitive::Type type = compare->InputAt(0)->GetType();
986 switch (type) {
987 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000988 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100989 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000990 }
991 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000992 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000993 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
994 break;
995 }
996 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000997 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000998 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
999 break;
1000 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001001 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001002 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001003 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001004 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001005 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001006 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001007
Calin Juravle91debbc2014-11-26 19:01:09 +00001008 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001009 __ movl(out, Immediate(1));
1010 __ jmp(&done);
1011
1012 __ Bind(&less);
1013 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001014
1015 __ Bind(&done);
1016}
1017
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001018void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001019 LocationSummary* locations =
1020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001021 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001022}
1023
1024void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001025 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001026 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001027}
1028
1029void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001030 LocationSummary* locations =
1031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001032 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033}
1034
1035void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001036 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001037 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001038}
1039
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001040void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1041 LocationSummary* locations =
1042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1043 locations->SetOut(Location::ConstantLocation(constant));
1044}
1045
1046void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1047 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001048 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001049}
1050
1051void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1052 LocationSummary* locations =
1053 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1054 locations->SetOut(Location::ConstantLocation(constant));
1055}
1056
1057void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1058 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001059 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001060}
1061
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1063 ret->SetLocations(nullptr);
1064}
1065
1066void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001067 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068 codegen_->GenerateFrameExit();
1069 __ ret();
1070}
1071
1072void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001073 LocationSummary* locations =
1074 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075 switch (ret->InputAt(0)->GetType()) {
1076 case Primitive::kPrimBoolean:
1077 case Primitive::kPrimByte:
1078 case Primitive::kPrimChar:
1079 case Primitive::kPrimShort:
1080 case Primitive::kPrimInt:
1081 case Primitive::kPrimNot:
1082 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001083 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001084 break;
1085
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 case Primitive::kPrimFloat:
1087 case Primitive::kPrimDouble:
1088 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 break;
1091
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095}
1096
1097void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1098 if (kIsDebugBuild) {
1099 switch (ret->InputAt(0)->GetType()) {
1100 case Primitive::kPrimBoolean:
1101 case Primitive::kPrimByte:
1102 case Primitive::kPrimChar:
1103 case Primitive::kPrimShort:
1104 case Primitive::kPrimInt:
1105 case Primitive::kPrimNot:
1106 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108 break;
1109
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 case Primitive::kPrimFloat:
1111 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001112 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 XMM0);
1114 break;
1115
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 }
1119 }
1120 codegen_->GenerateFrameExit();
1121 __ ret();
1122}
1123
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001124Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1125 switch (type) {
1126 case Primitive::kPrimBoolean:
1127 case Primitive::kPrimByte:
1128 case Primitive::kPrimChar:
1129 case Primitive::kPrimShort:
1130 case Primitive::kPrimInt:
1131 case Primitive::kPrimNot: {
1132 uint32_t index = gp_index_++;
1133 stack_index_++;
1134 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001135 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136 } else {
1137 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1138 }
1139 }
1140
1141 case Primitive::kPrimLong: {
1142 uint32_t index = gp_index_;
1143 stack_index_ += 2;
1144 if (index < calling_convention.GetNumberOfRegisters()) {
1145 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001146 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147 } else {
1148 gp_index_ += 2;
1149 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1150 }
1151 }
1152
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001153 case Primitive::kPrimFloat: {
1154 uint32_t index = fp_index_++;
1155 stack_index_++;
1156 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 } else {
1159 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1160 }
1161 }
1162
1163 case Primitive::kPrimDouble: {
1164 uint32_t index = fp_index_++;
1165 stack_index_ += 2;
1166 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else {
1169 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1170 }
1171 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001172
1173 case Primitive::kPrimVoid:
1174 LOG(FATAL) << "Unexpected parameter type " << type;
1175 break;
1176 }
1177 return Location();
1178}
1179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001180void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001181 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1182 if (intrinsic.TryDispatch(invoke)) {
1183 return;
1184 }
1185
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001186 HandleInvoke(invoke);
1187}
1188
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001189static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1190 if (invoke->GetLocations()->Intrinsified()) {
1191 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1192 intrinsic.Dispatch(invoke);
1193 return true;
1194 }
1195 return false;
1196}
1197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001198void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001199 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1200 return;
1201 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001202
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001203 codegen_->GenerateStaticOrDirectCall(
1204 invoke,
1205 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001206}
1207
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001208void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001209 LocationSummary* locations =
1210 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001211 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001212
1213 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001214 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215 HInstruction* input = invoke->InputAt(i);
1216 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1217 }
1218
1219 switch (invoke->GetType()) {
1220 case Primitive::kPrimBoolean:
1221 case Primitive::kPrimByte:
1222 case Primitive::kPrimChar:
1223 case Primitive::kPrimShort:
1224 case Primitive::kPrimInt:
1225 case Primitive::kPrimNot:
1226 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001227 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 break;
1229
1230 case Primitive::kPrimVoid:
1231 break;
1232
1233 case Primitive::kPrimDouble:
1234 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001235 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001236 break;
1237 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001238}
1239
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001240void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001241 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1242 if (intrinsic.TryDispatch(invoke)) {
1243 return;
1244 }
1245
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001246 HandleInvoke(invoke);
1247}
1248
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001249void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001250 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1251 return;
1252 }
1253
Roland Levillain271ab9c2014-11-27 15:23:57 +00001254 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001255 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1256 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1257 LocationSummary* locations = invoke->GetLocations();
1258 Location receiver = locations->InAt(0);
1259 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1260 // temp = object->GetClass();
1261 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1263 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001264 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001265 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001266 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001267 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 // temp = temp->GetMethodAt(method_offset);
1269 __ movl(temp, Address(temp, method_offset));
1270 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001271 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001272 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001274 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001275 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276}
1277
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001278void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1279 HandleInvoke(invoke);
1280 // Add the hidden argument.
1281 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1282}
1283
1284void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1285 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001286 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001287 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1288 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1289 LocationSummary* locations = invoke->GetLocations();
1290 Location receiver = locations->InAt(0);
1291 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1292
1293 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001294 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001295 Immediate(invoke->GetDexMethodIndex()));
1296
1297 // temp = object->GetClass();
1298 if (receiver.IsStackSlot()) {
1299 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1300 __ movl(temp, Address(temp, class_offset));
1301 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001302 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001303 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001304 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001305 // temp = temp->GetImtEntryAt(method_offset);
1306 __ movl(temp, Address(temp, method_offset));
1307 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001308 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001309 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310
1311 DCHECK(!codegen_->IsLeafMethod());
1312 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1313}
1314
Roland Levillain88cb1752014-10-20 16:36:47 +01001315void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1316 LocationSummary* locations =
1317 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1318 switch (neg->GetResultType()) {
1319 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001320 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001321 locations->SetInAt(0, Location::RequiresRegister());
1322 locations->SetOut(Location::SameAsFirstInput());
1323 break;
1324
Roland Levillain88cb1752014-10-20 16:36:47 +01001325 case Primitive::kPrimFloat:
1326 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001327 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001328 locations->SetOut(Location::SameAsFirstInput());
1329 locations->AddTemp(Location::RequiresRegister());
1330 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001331 break;
1332
1333 default:
1334 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1335 }
1336}
1337
1338void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1339 LocationSummary* locations = neg->GetLocations();
1340 Location out = locations->Out();
1341 Location in = locations->InAt(0);
1342 switch (neg->GetResultType()) {
1343 case Primitive::kPrimInt:
1344 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001345 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001346 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001347 break;
1348
1349 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001350 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001352 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001353 break;
1354
Roland Levillain5368c212014-11-27 15:03:41 +00001355 case Primitive::kPrimFloat: {
1356 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1358 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001359 // Implement float negation with an exclusive or with value
1360 // 0x80000000 (mask for bit 31, representing the sign of a
1361 // single-precision floating-point number).
1362 __ movq(constant, Immediate(INT64_C(0x80000000)));
1363 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001364 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001365 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001366 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001367
Roland Levillain5368c212014-11-27 15:03:41 +00001368 case Primitive::kPrimDouble: {
1369 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1371 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001372 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001373 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001374 // a double-precision floating-point number).
1375 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1376 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001378 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001379 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001380
1381 default:
1382 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1383 }
1384}
1385
Roland Levillaindff1f282014-11-05 14:15:05 +00001386void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1387 LocationSummary* locations =
1388 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1389 Primitive::Type result_type = conversion->GetResultType();
1390 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001391 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001392 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001393 case Primitive::kPrimByte:
1394 switch (input_type) {
1395 case Primitive::kPrimShort:
1396 case Primitive::kPrimInt:
1397 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001398 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001399 locations->SetInAt(0, Location::Any());
1400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1401 break;
1402
1403 default:
1404 LOG(FATAL) << "Unexpected type conversion from " << input_type
1405 << " to " << result_type;
1406 }
1407 break;
1408
Roland Levillain01a8d712014-11-14 16:27:39 +00001409 case Primitive::kPrimShort:
1410 switch (input_type) {
1411 case Primitive::kPrimByte:
1412 case Primitive::kPrimInt:
1413 case Primitive::kPrimChar:
1414 // Processing a Dex `int-to-short' instruction.
1415 locations->SetInAt(0, Location::Any());
1416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1417 break;
1418
1419 default:
1420 LOG(FATAL) << "Unexpected type conversion from " << input_type
1421 << " to " << result_type;
1422 }
1423 break;
1424
Roland Levillain946e1432014-11-11 17:35:19 +00001425 case Primitive::kPrimInt:
1426 switch (input_type) {
1427 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001428 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001429 locations->SetInAt(0, Location::Any());
1430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1431 break;
1432
1433 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001434 // Processing a Dex `float-to-int' instruction.
1435 locations->SetInAt(0, Location::RequiresFpuRegister());
1436 locations->SetOut(Location::RequiresRegister());
1437 locations->AddTemp(Location::RequiresFpuRegister());
1438 break;
1439
Roland Levillain946e1432014-11-11 17:35:19 +00001440 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001441 // Processing a Dex `double-to-int' instruction.
1442 locations->SetInAt(0, Location::RequiresFpuRegister());
1443 locations->SetOut(Location::RequiresRegister());
1444 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001445 break;
1446
1447 default:
1448 LOG(FATAL) << "Unexpected type conversion from " << input_type
1449 << " to " << result_type;
1450 }
1451 break;
1452
Roland Levillaindff1f282014-11-05 14:15:05 +00001453 case Primitive::kPrimLong:
1454 switch (input_type) {
1455 case Primitive::kPrimByte:
1456 case Primitive::kPrimShort:
1457 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001458 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001459 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001460 // TODO: We would benefit from a (to-be-implemented)
1461 // Location::RegisterOrStackSlot requirement for this input.
1462 locations->SetInAt(0, Location::RequiresRegister());
1463 locations->SetOut(Location::RequiresRegister());
1464 break;
1465
1466 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001467 // Processing a Dex `float-to-long' instruction.
1468 locations->SetInAt(0, Location::RequiresFpuRegister());
1469 locations->SetOut(Location::RequiresRegister());
1470 locations->AddTemp(Location::RequiresFpuRegister());
1471 break;
1472
Roland Levillaindff1f282014-11-05 14:15:05 +00001473 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001474 // Processing a Dex `double-to-long' instruction.
1475 locations->SetInAt(0, Location::RequiresFpuRegister());
1476 locations->SetOut(Location::RequiresRegister());
1477 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 break;
1479
1480 default:
1481 LOG(FATAL) << "Unexpected type conversion from " << input_type
1482 << " to " << result_type;
1483 }
1484 break;
1485
Roland Levillain981e4542014-11-14 11:47:14 +00001486 case Primitive::kPrimChar:
1487 switch (input_type) {
1488 case Primitive::kPrimByte:
1489 case Primitive::kPrimShort:
1490 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001491 // Processing a Dex `int-to-char' instruction.
1492 locations->SetInAt(0, Location::Any());
1493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1494 break;
1495
1496 default:
1497 LOG(FATAL) << "Unexpected type conversion from " << input_type
1498 << " to " << result_type;
1499 }
1500 break;
1501
Roland Levillaindff1f282014-11-05 14:15:05 +00001502 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001503 switch (input_type) {
1504 case Primitive::kPrimByte:
1505 case Primitive::kPrimShort:
1506 case Primitive::kPrimInt:
1507 case Primitive::kPrimChar:
1508 // Processing a Dex `int-to-float' instruction.
1509 locations->SetInAt(0, Location::RequiresRegister());
1510 locations->SetOut(Location::RequiresFpuRegister());
1511 break;
1512
1513 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001514 // Processing a Dex `long-to-float' instruction.
1515 locations->SetInAt(0, Location::RequiresRegister());
1516 locations->SetOut(Location::RequiresFpuRegister());
1517 break;
1518
Roland Levillaincff13742014-11-17 14:32:17 +00001519 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001520 // Processing a Dex `double-to-float' instruction.
1521 locations->SetInAt(0, Location::RequiresFpuRegister());
1522 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001523 break;
1524
1525 default:
1526 LOG(FATAL) << "Unexpected type conversion from " << input_type
1527 << " to " << result_type;
1528 };
1529 break;
1530
Roland Levillaindff1f282014-11-05 14:15:05 +00001531 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001532 switch (input_type) {
1533 case Primitive::kPrimByte:
1534 case Primitive::kPrimShort:
1535 case Primitive::kPrimInt:
1536 case Primitive::kPrimChar:
1537 // Processing a Dex `int-to-double' instruction.
1538 locations->SetInAt(0, Location::RequiresRegister());
1539 locations->SetOut(Location::RequiresFpuRegister());
1540 break;
1541
1542 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001543 // Processing a Dex `long-to-double' instruction.
1544 locations->SetInAt(0, Location::RequiresRegister());
1545 locations->SetOut(Location::RequiresFpuRegister());
1546 break;
1547
Roland Levillaincff13742014-11-17 14:32:17 +00001548 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001549 // Processing a Dex `float-to-double' instruction.
1550 locations->SetInAt(0, Location::RequiresFpuRegister());
1551 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001552 break;
1553
1554 default:
1555 LOG(FATAL) << "Unexpected type conversion from " << input_type
1556 << " to " << result_type;
1557 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001558 break;
1559
1560 default:
1561 LOG(FATAL) << "Unexpected type conversion from " << input_type
1562 << " to " << result_type;
1563 }
1564}
1565
1566void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1567 LocationSummary* locations = conversion->GetLocations();
1568 Location out = locations->Out();
1569 Location in = locations->InAt(0);
1570 Primitive::Type result_type = conversion->GetResultType();
1571 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001572 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001573 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001574 case Primitive::kPrimByte:
1575 switch (input_type) {
1576 case Primitive::kPrimShort:
1577 case Primitive::kPrimInt:
1578 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001579 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001580 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001582 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001583 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001584 Address(CpuRegister(RSP), in.GetStackIndex()));
1585 } else {
1586 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001587 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001588 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1589 }
1590 break;
1591
1592 default:
1593 LOG(FATAL) << "Unexpected type conversion from " << input_type
1594 << " to " << result_type;
1595 }
1596 break;
1597
Roland Levillain01a8d712014-11-14 16:27:39 +00001598 case Primitive::kPrimShort:
1599 switch (input_type) {
1600 case Primitive::kPrimByte:
1601 case Primitive::kPrimInt:
1602 case Primitive::kPrimChar:
1603 // Processing a Dex `int-to-short' instruction.
1604 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001606 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001608 Address(CpuRegister(RSP), in.GetStackIndex()));
1609 } else {
1610 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001611 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001612 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1613 }
1614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620 break;
1621
Roland Levillain946e1432014-11-11 17:35:19 +00001622 case Primitive::kPrimInt:
1623 switch (input_type) {
1624 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001625 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001626 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001627 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001628 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001629 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001630 Address(CpuRegister(RSP), in.GetStackIndex()));
1631 } else {
1632 DCHECK(in.IsConstant());
1633 DCHECK(in.GetConstant()->IsLongConstant());
1634 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001635 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001636 }
1637 break;
1638
Roland Levillain3f8f9362014-12-02 17:45:01 +00001639 case Primitive::kPrimFloat: {
1640 // Processing a Dex `float-to-int' instruction.
1641 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1642 CpuRegister output = out.AsRegister<CpuRegister>();
1643 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1644 Label done, nan;
1645
1646 __ movl(output, Immediate(kPrimIntMax));
1647 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001648 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001649 // if input >= temp goto done
1650 __ comiss(input, temp);
1651 __ j(kAboveEqual, &done);
1652 // if input == NaN goto nan
1653 __ j(kUnordered, &nan);
1654 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001655 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001656 __ jmp(&done);
1657 __ Bind(&nan);
1658 // output = 0
1659 __ xorl(output, output);
1660 __ Bind(&done);
1661 break;
1662 }
1663
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001664 case Primitive::kPrimDouble: {
1665 // Processing a Dex `double-to-int' instruction.
1666 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1667 CpuRegister output = out.AsRegister<CpuRegister>();
1668 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1669 Label done, nan;
1670
1671 __ movl(output, Immediate(kPrimIntMax));
1672 // temp = int-to-double(output)
1673 __ cvtsi2sd(temp, output);
1674 // if input >= temp goto done
1675 __ comisd(input, temp);
1676 __ j(kAboveEqual, &done);
1677 // if input == NaN goto nan
1678 __ j(kUnordered, &nan);
1679 // output = double-to-int-truncate(input)
1680 __ cvttsd2si(output, input);
1681 __ jmp(&done);
1682 __ Bind(&nan);
1683 // output = 0
1684 __ xorl(output, output);
1685 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001686 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001687 }
Roland Levillain946e1432014-11-11 17:35:19 +00001688
1689 default:
1690 LOG(FATAL) << "Unexpected type conversion from " << input_type
1691 << " to " << result_type;
1692 }
1693 break;
1694
Roland Levillaindff1f282014-11-05 14:15:05 +00001695 case Primitive::kPrimLong:
1696 switch (input_type) {
1697 DCHECK(out.IsRegister());
1698 case Primitive::kPrimByte:
1699 case Primitive::kPrimShort:
1700 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001701 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001702 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001703 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001704 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001705 break;
1706
Roland Levillain624279f2014-12-04 11:54:28 +00001707 case Primitive::kPrimFloat: {
1708 // Processing a Dex `float-to-long' instruction.
1709 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1710 CpuRegister output = out.AsRegister<CpuRegister>();
1711 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1712 Label done, nan;
1713
1714 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001715 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001716 __ cvtsi2ss(temp, output, true);
1717 // if input >= temp goto done
1718 __ comiss(input, temp);
1719 __ j(kAboveEqual, &done);
1720 // if input == NaN goto nan
1721 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001722 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001723 __ cvttss2si(output, input, true);
1724 __ jmp(&done);
1725 __ Bind(&nan);
1726 // output = 0
1727 __ xorq(output, output);
1728 __ Bind(&done);
1729 break;
1730 }
1731
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001732 case Primitive::kPrimDouble: {
1733 // Processing a Dex `double-to-long' instruction.
1734 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1735 CpuRegister output = out.AsRegister<CpuRegister>();
1736 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1737 Label done, nan;
1738
1739 __ movq(output, Immediate(kPrimLongMax));
1740 // temp = long-to-double(output)
1741 __ cvtsi2sd(temp, output, true);
1742 // if input >= temp goto done
1743 __ comisd(input, temp);
1744 __ j(kAboveEqual, &done);
1745 // if input == NaN goto nan
1746 __ j(kUnordered, &nan);
1747 // output = double-to-long-truncate(input)
1748 __ cvttsd2si(output, input, true);
1749 __ jmp(&done);
1750 __ Bind(&nan);
1751 // output = 0
1752 __ xorq(output, output);
1753 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001755 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001756
1757 default:
1758 LOG(FATAL) << "Unexpected type conversion from " << input_type
1759 << " to " << result_type;
1760 }
1761 break;
1762
Roland Levillain981e4542014-11-14 11:47:14 +00001763 case Primitive::kPrimChar:
1764 switch (input_type) {
1765 case Primitive::kPrimByte:
1766 case Primitive::kPrimShort:
1767 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001768 // Processing a Dex `int-to-char' instruction.
1769 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001770 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001771 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001772 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001773 Address(CpuRegister(RSP), in.GetStackIndex()));
1774 } else {
1775 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001777 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1778 }
1779 break;
1780
1781 default:
1782 LOG(FATAL) << "Unexpected type conversion from " << input_type
1783 << " to " << result_type;
1784 }
1785 break;
1786
Roland Levillaindff1f282014-11-05 14:15:05 +00001787 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001788 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001789 case Primitive::kPrimByte:
1790 case Primitive::kPrimShort:
1791 case Primitive::kPrimInt:
1792 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001793 // Processing a Dex `int-to-float' instruction.
1794 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001795 break;
1796
1797 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001798 // Processing a Dex `long-to-float' instruction.
1799 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1800 break;
1801
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001803 // Processing a Dex `double-to-float' instruction.
1804 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001805 break;
1806
1807 default:
1808 LOG(FATAL) << "Unexpected type conversion from " << input_type
1809 << " to " << result_type;
1810 };
1811 break;
1812
Roland Levillaindff1f282014-11-05 14:15:05 +00001813 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001814 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001815 case Primitive::kPrimByte:
1816 case Primitive::kPrimShort:
1817 case Primitive::kPrimInt:
1818 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001819 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001820 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001821 break;
1822
1823 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001824 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001825 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001826 break;
1827
Roland Levillaincff13742014-11-17 14:32:17 +00001828 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001829 // Processing a Dex `float-to-double' instruction.
1830 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001831 break;
1832
1833 default:
1834 LOG(FATAL) << "Unexpected type conversion from " << input_type
1835 << " to " << result_type;
1836 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001837 break;
1838
1839 default:
1840 LOG(FATAL) << "Unexpected type conversion from " << input_type
1841 << " to " << result_type;
1842 }
1843}
1844
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001845void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001848 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001849 case Primitive::kPrimInt: {
1850 locations->SetInAt(0, Location::RequiresRegister());
1851 locations->SetInAt(1, Location::Any());
1852 locations->SetOut(Location::SameAsFirstInput());
1853 break;
1854 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001855
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001856 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001857 locations->SetInAt(0, Location::RequiresRegister());
1858 locations->SetInAt(1, Location::RequiresRegister());
1859 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001860 break;
1861 }
1862
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 case Primitive::kPrimDouble:
1864 case Primitive::kPrimFloat: {
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001866 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001868 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001869 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001870
1871 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001872 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001873 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001874}
1875
1876void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1877 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001878 Location first = locations->InAt(0);
1879 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001881
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001882 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001883 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001885 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001887 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001888 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001889 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001890 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001891 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001892 break;
1893 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001894
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001895 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001896 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001897 break;
1898 }
1899
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001900 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001901 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001902 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001903 }
1904
1905 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001906 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001907 break;
1908 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001909
1910 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001911 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001912 }
1913}
1914
1915void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001918 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001919 case Primitive::kPrimInt: {
1920 locations->SetInAt(0, Location::RequiresRegister());
1921 locations->SetInAt(1, Location::Any());
1922 locations->SetOut(Location::SameAsFirstInput());
1923 break;
1924 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001925 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001926 locations->SetInAt(0, Location::RequiresRegister());
1927 locations->SetInAt(1, Location::RequiresRegister());
1928 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001929 break;
1930 }
Calin Juravle11351682014-10-23 15:38:15 +01001931 case Primitive::kPrimFloat:
1932 case Primitive::kPrimDouble: {
1933 locations->SetInAt(0, Location::RequiresFpuRegister());
1934 locations->SetInAt(1, Location::RequiresFpuRegister());
1935 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001936 break;
Calin Juravle11351682014-10-23 15:38:15 +01001937 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001938 default:
Calin Juravle11351682014-10-23 15:38:15 +01001939 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001940 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001941}
1942
1943void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1944 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001945 Location first = locations->InAt(0);
1946 Location second = locations->InAt(1);
1947 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001948 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001949 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001950 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001951 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001952 } else if (second.IsConstant()) {
1953 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001955 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001956 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001957 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001958 break;
1959 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001960 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001962 break;
1963 }
1964
Calin Juravle11351682014-10-23 15:38:15 +01001965 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001966 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967 break;
Calin Juravle11351682014-10-23 15:38:15 +01001968 }
1969
1970 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001971 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001972 break;
1973 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001974
1975 default:
Calin Juravle11351682014-10-23 15:38:15 +01001976 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977 }
1978}
1979
Calin Juravle34bacdf2014-10-07 20:23:36 +01001980void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1981 LocationSummary* locations =
1982 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1983 switch (mul->GetResultType()) {
1984 case Primitive::kPrimInt: {
1985 locations->SetInAt(0, Location::RequiresRegister());
1986 locations->SetInAt(1, Location::Any());
1987 locations->SetOut(Location::SameAsFirstInput());
1988 break;
1989 }
1990 case Primitive::kPrimLong: {
1991 locations->SetInAt(0, Location::RequiresRegister());
1992 locations->SetInAt(1, Location::RequiresRegister());
1993 locations->SetOut(Location::SameAsFirstInput());
1994 break;
1995 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001996 case Primitive::kPrimFloat:
1997 case Primitive::kPrimDouble: {
1998 locations->SetInAt(0, Location::RequiresFpuRegister());
1999 locations->SetInAt(1, Location::RequiresFpuRegister());
2000 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002001 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002002 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002003
2004 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002005 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002006 }
2007}
2008
2009void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2010 LocationSummary* locations = mul->GetLocations();
2011 Location first = locations->InAt(0);
2012 Location second = locations->InAt(1);
2013 DCHECK(first.Equals(locations->Out()));
2014 switch (mul->GetResultType()) {
2015 case Primitive::kPrimInt: {
2016 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002017 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002018 } else if (second.IsConstant()) {
2019 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002021 } else {
2022 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002023 __ imull(first.AsRegister<CpuRegister>(),
2024 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002025 }
2026 break;
2027 }
2028 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002029 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002030 break;
2031 }
2032
Calin Juravleb5bfa962014-10-21 18:02:24 +01002033 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002034 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002035 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002036 }
2037
2038 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002040 break;
2041 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002042
2043 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002044 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002045 }
2046}
2047
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002048void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2049 uint32_t stack_adjustment, bool is_float) {
2050 if (source.IsStackSlot()) {
2051 DCHECK(is_float);
2052 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2053 } else if (source.IsDoubleStackSlot()) {
2054 DCHECK(!is_float);
2055 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2056 } else {
2057 // Write the value to the temporary location on the stack and load to FP stack.
2058 if (is_float) {
2059 Location stack_temp = Location::StackSlot(temp_offset);
2060 codegen_->Move(stack_temp, source);
2061 __ flds(Address(CpuRegister(RSP), temp_offset));
2062 } else {
2063 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2064 codegen_->Move(stack_temp, source);
2065 __ fldl(Address(CpuRegister(RSP), temp_offset));
2066 }
2067 }
2068}
2069
2070void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2071 Primitive::Type type = rem->GetResultType();
2072 bool is_float = type == Primitive::kPrimFloat;
2073 size_t elem_size = Primitive::ComponentSize(type);
2074 LocationSummary* locations = rem->GetLocations();
2075 Location first = locations->InAt(0);
2076 Location second = locations->InAt(1);
2077 Location out = locations->Out();
2078
2079 // Create stack space for 2 elements.
2080 // TODO: enhance register allocator to ask for stack temporaries.
2081 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2082
2083 // Load the values to the FP stack in reverse order, using temporaries if needed.
2084 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2085 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2086
2087 // Loop doing FPREM until we stabilize.
2088 Label retry;
2089 __ Bind(&retry);
2090 __ fprem();
2091
2092 // Move FP status to AX.
2093 __ fstsw();
2094
2095 // And see if the argument reduction is complete. This is signaled by the
2096 // C2 FPU flag bit set to 0.
2097 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2098 __ j(kNotEqual, &retry);
2099
2100 // We have settled on the final value. Retrieve it into an XMM register.
2101 // Store FP top of stack to real stack.
2102 if (is_float) {
2103 __ fsts(Address(CpuRegister(RSP), 0));
2104 } else {
2105 __ fstl(Address(CpuRegister(RSP), 0));
2106 }
2107
2108 // Pop the 2 items from the FP stack.
2109 __ fucompp();
2110
2111 // Load the value from the stack into an XMM register.
2112 DCHECK(out.IsFpuRegister()) << out;
2113 if (is_float) {
2114 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2115 } else {
2116 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2117 }
2118
2119 // And remove the temporary stack space we allocated.
2120 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2121}
2122
Calin Juravlebacfec32014-11-14 15:54:36 +00002123void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2124 DCHECK(instruction->IsDiv() || instruction->IsRem());
2125 Primitive::Type type = instruction->GetResultType();
2126 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2127
2128 bool is_div = instruction->IsDiv();
2129 LocationSummary* locations = instruction->GetLocations();
2130
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2132 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002133
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002135 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2136
2137 SlowPathCodeX86_64* slow_path =
2138 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2139 out_reg.AsRegister(), type, is_div);
2140 codegen_->AddSlowPath(slow_path);
2141
2142 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2143 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2144 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002145 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002146 __ cmpl(second_reg, Immediate(-1));
2147 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002148 // edx:eax <- sign-extended of eax
2149 __ cdq();
2150 // eax = quotient, edx = remainder
2151 __ idivl(second_reg);
2152 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002153 __ cmpq(second_reg, Immediate(-1));
2154 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002155 // rdx:rax <- sign-extended of rax
2156 __ cqo();
2157 // rax = quotient, rdx = remainder
2158 __ idivq(second_reg);
2159 }
2160
2161 __ Bind(slow_path->GetExitLabel());
2162}
2163
Calin Juravle7c4954d2014-10-28 16:57:40 +00002164void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2165 LocationSummary* locations =
2166 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2167 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002168 case Primitive::kPrimInt:
2169 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002170 locations->SetInAt(0, Location::RegisterLocation(RAX));
2171 locations->SetInAt(1, Location::RequiresRegister());
2172 locations->SetOut(Location::SameAsFirstInput());
2173 // Intel uses edx:eax as the dividend.
2174 locations->AddTemp(Location::RegisterLocation(RDX));
2175 break;
2176 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002177
Calin Juravle7c4954d2014-10-28 16:57:40 +00002178 case Primitive::kPrimFloat:
2179 case Primitive::kPrimDouble: {
2180 locations->SetInAt(0, Location::RequiresFpuRegister());
2181 locations->SetInAt(1, Location::RequiresFpuRegister());
2182 locations->SetOut(Location::SameAsFirstInput());
2183 break;
2184 }
2185
2186 default:
2187 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2188 }
2189}
2190
2191void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2192 LocationSummary* locations = div->GetLocations();
2193 Location first = locations->InAt(0);
2194 Location second = locations->InAt(1);
2195 DCHECK(first.Equals(locations->Out()));
2196
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002197 Primitive::Type type = div->GetResultType();
2198 switch (type) {
2199 case Primitive::kPrimInt:
2200 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002201 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002202 break;
2203 }
2204
Calin Juravle7c4954d2014-10-28 16:57:40 +00002205 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002206 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002207 break;
2208 }
2209
2210 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002212 break;
2213 }
2214
2215 default:
2216 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2217 }
2218}
2219
Calin Juravlebacfec32014-11-14 15:54:36 +00002220void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002221 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002222 LocationSummary* locations =
2223 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002224
2225 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002226 case Primitive::kPrimInt:
2227 case Primitive::kPrimLong: {
2228 locations->SetInAt(0, Location::RegisterLocation(RAX));
2229 locations->SetInAt(1, Location::RequiresRegister());
2230 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2231 locations->SetOut(Location::RegisterLocation(RDX));
2232 break;
2233 }
2234
2235 case Primitive::kPrimFloat:
2236 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002237 locations->SetInAt(0, Location::Any());
2238 locations->SetInAt(1, Location::Any());
2239 locations->SetOut(Location::RequiresFpuRegister());
2240 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002241 break;
2242 }
2243
2244 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002245 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002246 }
2247}
2248
2249void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2250 Primitive::Type type = rem->GetResultType();
2251 switch (type) {
2252 case Primitive::kPrimInt:
2253 case Primitive::kPrimLong: {
2254 GenerateDivRemIntegral(rem);
2255 break;
2256 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002257 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002258 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002259 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002260 break;
2261 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002262 default:
2263 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2264 }
2265}
2266
Calin Juravled0d48522014-11-04 16:40:20 +00002267void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2268 LocationSummary* locations =
2269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2270 locations->SetInAt(0, Location::Any());
2271 if (instruction->HasUses()) {
2272 locations->SetOut(Location::SameAsFirstInput());
2273 }
2274}
2275
2276void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2277 SlowPathCodeX86_64* slow_path =
2278 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2279 codegen_->AddSlowPath(slow_path);
2280
2281 LocationSummary* locations = instruction->GetLocations();
2282 Location value = locations->InAt(0);
2283
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002284 switch (instruction->GetType()) {
2285 case Primitive::kPrimInt: {
2286 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002287 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002288 __ j(kEqual, slow_path->GetEntryLabel());
2289 } else if (value.IsStackSlot()) {
2290 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2291 __ j(kEqual, slow_path->GetEntryLabel());
2292 } else {
2293 DCHECK(value.IsConstant()) << value;
2294 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2295 __ jmp(slow_path->GetEntryLabel());
2296 }
2297 }
2298 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002299 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002300 case Primitive::kPrimLong: {
2301 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002303 __ j(kEqual, slow_path->GetEntryLabel());
2304 } else if (value.IsDoubleStackSlot()) {
2305 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2306 __ j(kEqual, slow_path->GetEntryLabel());
2307 } else {
2308 DCHECK(value.IsConstant()) << value;
2309 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2310 __ jmp(slow_path->GetEntryLabel());
2311 }
2312 }
2313 break;
2314 }
2315 default:
2316 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002317 }
Calin Juravled0d48522014-11-04 16:40:20 +00002318}
2319
Calin Juravle9aec02f2014-11-18 23:06:35 +00002320void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2321 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2322
2323 LocationSummary* locations =
2324 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2325
2326 switch (op->GetResultType()) {
2327 case Primitive::kPrimInt:
2328 case Primitive::kPrimLong: {
2329 locations->SetInAt(0, Location::RequiresRegister());
2330 // The shift count needs to be in CL.
2331 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2332 locations->SetOut(Location::SameAsFirstInput());
2333 break;
2334 }
2335 default:
2336 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2337 }
2338}
2339
2340void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2341 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2342
2343 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002344 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002345 Location second = locations->InAt(1);
2346
2347 switch (op->GetResultType()) {
2348 case Primitive::kPrimInt: {
2349 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002351 if (op->IsShl()) {
2352 __ shll(first_reg, second_reg);
2353 } else if (op->IsShr()) {
2354 __ sarl(first_reg, second_reg);
2355 } else {
2356 __ shrl(first_reg, second_reg);
2357 }
2358 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002359 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002360 if (op->IsShl()) {
2361 __ shll(first_reg, imm);
2362 } else if (op->IsShr()) {
2363 __ sarl(first_reg, imm);
2364 } else {
2365 __ shrl(first_reg, imm);
2366 }
2367 }
2368 break;
2369 }
2370 case Primitive::kPrimLong: {
2371 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002372 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002373 if (op->IsShl()) {
2374 __ shlq(first_reg, second_reg);
2375 } else if (op->IsShr()) {
2376 __ sarq(first_reg, second_reg);
2377 } else {
2378 __ shrq(first_reg, second_reg);
2379 }
2380 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002381 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002382 if (op->IsShl()) {
2383 __ shlq(first_reg, imm);
2384 } else if (op->IsShr()) {
2385 __ sarq(first_reg, imm);
2386 } else {
2387 __ shrq(first_reg, imm);
2388 }
2389 }
2390 break;
2391 }
2392 default:
2393 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2394 }
2395}
2396
2397void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2398 HandleShift(shl);
2399}
2400
2401void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2402 HandleShift(shl);
2403}
2404
2405void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2406 HandleShift(shr);
2407}
2408
2409void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2410 HandleShift(shr);
2411}
2412
2413void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2414 HandleShift(ushr);
2415}
2416
2417void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2418 HandleShift(ushr);
2419}
2420
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002421void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002422 LocationSummary* locations =
2423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002424 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002425 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2426 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2427 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002428}
2429
2430void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2431 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002432 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002433 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2434
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002435 __ gs()->call(
2436 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002437
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002438 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002439 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002440}
2441
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002442void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2443 LocationSummary* locations =
2444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2445 InvokeRuntimeCallingConvention calling_convention;
2446 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002447 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002448 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002449 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002450}
2451
2452void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2453 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002454 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002455 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2456
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002457 __ gs()->call(
2458 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002459
2460 DCHECK(!codegen_->IsLeafMethod());
2461 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2462}
2463
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002464void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002465 LocationSummary* locations =
2466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002467 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2468 if (location.IsStackSlot()) {
2469 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2470 } else if (location.IsDoubleStackSlot()) {
2471 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2472 }
2473 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002474}
2475
2476void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2477 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002478 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002479}
2480
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002481void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002482 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002483 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002484 locations->SetInAt(0, Location::RequiresRegister());
2485 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002486}
2487
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002488void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2489 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2491 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002492 Location out = locations->Out();
2493 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002494 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002496 break;
2497
2498 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002500 break;
2501
2502 default:
2503 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2504 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002505}
2506
2507void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002508 LocationSummary* locations =
2509 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002510 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2511 locations->SetInAt(i, Location::Any());
2512 }
2513 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002514}
2515
2516void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002517 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002518 LOG(FATAL) << "Unimplemented";
2519}
2520
Calin Juravle52c48962014-12-16 17:02:57 +00002521void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2522 /*
2523 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2524 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2525 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2526 */
2527 switch (kind) {
2528 case MemBarrierKind::kAnyAny: {
2529 __ mfence();
2530 break;
2531 }
2532 case MemBarrierKind::kAnyStore:
2533 case MemBarrierKind::kLoadAny:
2534 case MemBarrierKind::kStoreStore: {
2535 // nop
2536 break;
2537 }
2538 default:
2539 LOG(FATAL) << "Unexpected memory barier " << kind;
2540 }
2541}
2542
2543void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2544 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2545
Nicolas Geoffray39468442014-09-02 15:17:15 +01002546 LocationSummary* locations =
2547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002548 locations->SetInAt(0, Location::RequiresRegister());
2549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2550}
2551
2552void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2553 const FieldInfo& field_info) {
2554 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2555
2556 LocationSummary* locations = instruction->GetLocations();
2557 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2558 Location out = locations->Out();
2559 bool is_volatile = field_info.IsVolatile();
2560 Primitive::Type field_type = field_info.GetFieldType();
2561 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2562
2563 switch (field_type) {
2564 case Primitive::kPrimBoolean: {
2565 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2566 break;
2567 }
2568
2569 case Primitive::kPrimByte: {
2570 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2571 break;
2572 }
2573
2574 case Primitive::kPrimShort: {
2575 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2576 break;
2577 }
2578
2579 case Primitive::kPrimChar: {
2580 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2581 break;
2582 }
2583
2584 case Primitive::kPrimInt:
2585 case Primitive::kPrimNot: {
2586 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2587 break;
2588 }
2589
2590 case Primitive::kPrimLong: {
2591 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2592 break;
2593 }
2594
2595 case Primitive::kPrimFloat: {
2596 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2597 break;
2598 }
2599
2600 case Primitive::kPrimDouble: {
2601 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2602 break;
2603 }
2604
2605 case Primitive::kPrimVoid:
2606 LOG(FATAL) << "Unreachable type " << field_type;
2607 UNREACHABLE();
2608 }
2609
Calin Juravle77520bc2015-01-12 18:45:46 +00002610 codegen_->MaybeRecordImplicitNullCheck(instruction);
2611
Calin Juravle52c48962014-12-16 17:02:57 +00002612 if (is_volatile) {
2613 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2614 }
2615}
2616
2617void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2618 const FieldInfo& field_info) {
2619 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2620
2621 LocationSummary* locations =
2622 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002623 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002624 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2625
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002626 locations->SetInAt(0, Location::RequiresRegister());
2627 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002628 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002629 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002630 locations->AddTemp(Location::RequiresRegister());
2631 locations->AddTemp(Location::RequiresRegister());
2632 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633}
2634
Calin Juravle52c48962014-12-16 17:02:57 +00002635void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2636 const FieldInfo& field_info) {
2637 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2638
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002639 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002640 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2641 Location value = locations->InAt(1);
2642 bool is_volatile = field_info.IsVolatile();
2643 Primitive::Type field_type = field_info.GetFieldType();
2644 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2645
2646 if (is_volatile) {
2647 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2648 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002649
2650 switch (field_type) {
2651 case Primitive::kPrimBoolean:
2652 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002653 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002654 break;
2655 }
2656
2657 case Primitive::kPrimShort:
2658 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002659 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002660 break;
2661 }
2662
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002663 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002664 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002665 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002666 break;
2667 }
2668
2669 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002670 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671 break;
2672 }
2673
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002674 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002675 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002676 break;
2677 }
2678
2679 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002680 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002681 break;
2682 }
2683
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 case Primitive::kPrimVoid:
2685 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002686 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002687 }
Calin Juravle52c48962014-12-16 17:02:57 +00002688
Calin Juravle77520bc2015-01-12 18:45:46 +00002689 codegen_->MaybeRecordImplicitNullCheck(instruction);
2690
2691 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2692 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2693 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2694 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2695 }
2696
Calin Juravle52c48962014-12-16 17:02:57 +00002697 if (is_volatile) {
2698 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2699 }
2700}
2701
2702void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2703 HandleFieldSet(instruction, instruction->GetFieldInfo());
2704}
2705
2706void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2707 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002708}
2709
2710void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002711 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002712}
2713
2714void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002715 HandleFieldGet(instruction, instruction->GetFieldInfo());
2716}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002717
Calin Juravle52c48962014-12-16 17:02:57 +00002718void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2719 HandleFieldGet(instruction);
2720}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002721
Calin Juravle52c48962014-12-16 17:02:57 +00002722void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2723 HandleFieldGet(instruction, instruction->GetFieldInfo());
2724}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002725
Calin Juravle52c48962014-12-16 17:02:57 +00002726void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2727 HandleFieldSet(instruction, instruction->GetFieldInfo());
2728}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002729
Calin Juravle52c48962014-12-16 17:02:57 +00002730void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2731 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732}
2733
2734void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002735 LocationSummary* locations =
2736 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002737 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2738 ? Location::RequiresRegister()
2739 : Location::Any();
2740 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002741 if (instruction->HasUses()) {
2742 locations->SetOut(Location::SameAsFirstInput());
2743 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002744}
2745
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002746void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002747 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2748 return;
2749 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002750 LocationSummary* locations = instruction->GetLocations();
2751 Location obj = locations->InAt(0);
2752
2753 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2754 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2755}
2756
2757void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002758 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002759 codegen_->AddSlowPath(slow_path);
2760
2761 LocationSummary* locations = instruction->GetLocations();
2762 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002763
2764 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002766 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002767 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002768 } else {
2769 DCHECK(obj.IsConstant()) << obj;
2770 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2771 __ jmp(slow_path->GetEntryLabel());
2772 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002773 }
2774 __ j(kEqual, slow_path->GetEntryLabel());
2775}
2776
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002777void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2778 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2779 GenerateImplicitNullCheck(instruction);
2780 } else {
2781 GenerateExplicitNullCheck(instruction);
2782 }
2783}
2784
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002785void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002786 LocationSummary* locations =
2787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002788 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002789 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002790 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002792}
2793
2794void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2795 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002797 Location index = locations->InAt(1);
2798
2799 switch (instruction->GetType()) {
2800 case Primitive::kPrimBoolean: {
2801 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002803 if (index.IsConstant()) {
2804 __ movzxb(out, Address(obj,
2805 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2806 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002807 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002808 }
2809 break;
2810 }
2811
2812 case Primitive::kPrimByte: {
2813 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002814 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002815 if (index.IsConstant()) {
2816 __ movsxb(out, Address(obj,
2817 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2818 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002819 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002820 }
2821 break;
2822 }
2823
2824 case Primitive::kPrimShort: {
2825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_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 __ movsxw(out, Address(obj,
2829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2830 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002832 }
2833 break;
2834 }
2835
2836 case Primitive::kPrimChar: {
2837 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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 __ movzxw(out, Address(obj,
2841 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002843 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002844 }
2845 break;
2846 }
2847
2848 case Primitive::kPrimInt:
2849 case Primitive::kPrimNot: {
2850 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2851 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002853 if (index.IsConstant()) {
2854 __ movl(out, Address(obj,
2855 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2856 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002857 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002858 }
2859 break;
2860 }
2861
2862 case Primitive::kPrimLong: {
2863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002865 if (index.IsConstant()) {
2866 __ movq(out, Address(obj,
2867 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2868 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002869 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002870 }
2871 break;
2872 }
2873
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002874 case Primitive::kPrimFloat: {
2875 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002877 if (index.IsConstant()) {
2878 __ movss(out, Address(obj,
2879 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2880 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002881 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002882 }
2883 break;
2884 }
2885
2886 case Primitive::kPrimDouble: {
2887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002889 if (index.IsConstant()) {
2890 __ movsd(out, Address(obj,
2891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2892 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002893 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002894 }
2895 break;
2896 }
2897
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002898 case Primitive::kPrimVoid:
2899 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002900 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002901 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002902 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002903}
2904
2905void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002906 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002907
2908 bool needs_write_barrier =
2909 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2910 bool needs_runtime_call = instruction->NeedsTypeCheck();
2911
Nicolas Geoffray39468442014-09-02 15:17:15 +01002912 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002913 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2914 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002915 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002916 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2917 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2918 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002919 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002920 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002921 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002922 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2923 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002924 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002925 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002926 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2927 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002928 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002929 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002930 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002931
2932 if (needs_write_barrier) {
2933 // Temporary registers for the write barrier.
2934 locations->AddTemp(Location::RequiresRegister());
2935 locations->AddTemp(Location::RequiresRegister());
2936 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002937 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938}
2939
2940void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2941 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002942 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002943 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002944 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002945 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002946 bool needs_runtime_call = locations->WillCall();
2947 bool needs_write_barrier =
2948 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002949
2950 switch (value_type) {
2951 case Primitive::kPrimBoolean:
2952 case Primitive::kPrimByte: {
2953 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002954 if (index.IsConstant()) {
2955 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002956 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002958 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002959 __ movb(Address(obj, offset),
2960 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002961 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002962 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002963 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002964 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2965 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002966 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002967 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002968 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2969 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002970 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002971 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002972 break;
2973 }
2974
2975 case Primitive::kPrimShort:
2976 case Primitive::kPrimChar: {
2977 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 if (index.IsConstant()) {
2979 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002980 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002982 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002983 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002984 __ movw(Address(obj, offset),
2985 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002986 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002988 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002989 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002990 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2991 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002992 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002993 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002994 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002995 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2996 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002997 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002998 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002999 break;
3000 }
3001
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003002 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003003 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003004 if (!needs_runtime_call) {
3005 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3006 if (index.IsConstant()) {
3007 size_t offset =
3008 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3009 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003011 } else {
3012 DCHECK(value.IsConstant()) << value;
3013 __ movl(Address(obj, offset),
3014 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3015 }
3016 } else {
3017 DCHECK(index.IsRegister()) << index;
3018 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003019 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3020 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003021 } else {
3022 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003024 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3025 }
3026 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003028 if (needs_write_barrier) {
3029 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3031 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3032 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003033 }
3034 } else {
3035 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003036 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3037 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003038 DCHECK(!codegen_->IsLeafMethod());
3039 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003041 break;
3042 }
3043
3044 case Primitive::kPrimLong: {
3045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003046 if (index.IsConstant()) {
3047 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003048 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003049 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003051 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3053 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003055 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003056 break;
3057 }
3058
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003059 case Primitive::kPrimFloat: {
3060 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3061 if (index.IsConstant()) {
3062 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3063 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003065 } else {
3066 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003067 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3068 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003069 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003070 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003071 break;
3072 }
3073
3074 case Primitive::kPrimDouble: {
3075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3076 if (index.IsConstant()) {
3077 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3078 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003079 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003080 } else {
3081 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3083 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003084 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003085 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003086 break;
3087 }
3088
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003089 case Primitive::kPrimVoid:
3090 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003091 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003092 }
3093}
3094
3095void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003096 LocationSummary* locations =
3097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003098 locations->SetInAt(0, Location::RequiresRegister());
3099 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100}
3101
3102void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3103 LocationSummary* locations = instruction->GetLocations();
3104 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3106 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003107 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003108 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003109}
3110
3111void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003112 LocationSummary* locations =
3113 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114 locations->SetInAt(0, Location::RequiresRegister());
3115 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003116 if (instruction->HasUses()) {
3117 locations->SetOut(Location::SameAsFirstInput());
3118 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003119}
3120
3121void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3122 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003123 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003124 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 codegen_->AddSlowPath(slow_path);
3126
Roland Levillain271ab9c2014-11-27 15:23:57 +00003127 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3128 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129
3130 __ cmpl(index, length);
3131 __ j(kAboveEqual, slow_path->GetEntryLabel());
3132}
3133
3134void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3135 CpuRegister card,
3136 CpuRegister object,
3137 CpuRegister value) {
3138 Label is_null;
3139 __ testl(value, value);
3140 __ j(kEqual, &is_null);
3141 __ gs()->movq(card, Address::Absolute(
3142 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3143 __ movq(temp, object);
3144 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3145 __ movb(Address(temp, card, TIMES_1, 0), card);
3146 __ Bind(&is_null);
3147}
3148
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003149void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3150 temp->SetLocations(nullptr);
3151}
3152
3153void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3154 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003155 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003156}
3157
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003158void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003159 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003160 LOG(FATAL) << "Unimplemented";
3161}
3162
3163void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003164 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3165}
3166
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003167void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3169}
3170
3171void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003172 HBasicBlock* block = instruction->GetBlock();
3173 if (block->GetLoopInformation() != nullptr) {
3174 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3175 // The back edge will generate the suspend check.
3176 return;
3177 }
3178 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3179 // The goto will generate the suspend check.
3180 return;
3181 }
3182 GenerateSuspendCheck(instruction, nullptr);
3183}
3184
3185void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3186 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003187 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003188 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003189 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003190 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003191 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003192 if (successor == nullptr) {
3193 __ j(kNotEqual, slow_path->GetEntryLabel());
3194 __ Bind(slow_path->GetReturnLabel());
3195 } else {
3196 __ j(kEqual, codegen_->GetLabelOf(successor));
3197 __ jmp(slow_path->GetEntryLabel());
3198 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003199}
3200
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003201X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3202 return codegen_->GetAssembler();
3203}
3204
3205void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3206 MoveOperands* move = moves_.Get(index);
3207 Location source = move->GetSource();
3208 Location destination = move->GetDestination();
3209
3210 if (source.IsRegister()) {
3211 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003212 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003213 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003214 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003215 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003216 } else {
3217 DCHECK(destination.IsDoubleStackSlot());
3218 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003220 }
3221 } else if (source.IsStackSlot()) {
3222 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003224 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003225 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003227 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003228 } else {
3229 DCHECK(destination.IsStackSlot());
3230 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3231 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3232 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003233 } else if (source.IsDoubleStackSlot()) {
3234 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003235 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003236 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003237 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003238 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3239 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003240 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003241 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003242 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3243 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3244 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003245 } else if (source.IsConstant()) {
3246 HConstant* constant = source.GetConstant();
3247 if (constant->IsIntConstant()) {
3248 Immediate imm(constant->AsIntConstant()->GetValue());
3249 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003251 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003252 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003253 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3254 }
3255 } else if (constant->IsLongConstant()) {
3256 int64_t value = constant->AsLongConstant()->GetValue();
3257 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003259 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003260 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003261 __ movq(CpuRegister(TMP), Immediate(value));
3262 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3263 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003264 } else if (constant->IsFloatConstant()) {
3265 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3266 if (destination.IsFpuRegister()) {
3267 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003268 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003269 } else {
3270 DCHECK(destination.IsStackSlot()) << destination;
3271 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3272 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003273 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003274 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3275 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3276 if (destination.IsFpuRegister()) {
3277 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003278 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003279 } else {
3280 DCHECK(destination.IsDoubleStackSlot()) << destination;
3281 __ movq(CpuRegister(TMP), imm);
3282 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3283 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003284 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003285 } else if (source.IsFpuRegister()) {
3286 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003287 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003288 } else if (destination.IsStackSlot()) {
3289 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003290 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003291 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003292 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003293 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003294 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003295 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003296 }
3297}
3298
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003299void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003300 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003301 __ movl(Address(CpuRegister(RSP), mem), reg);
3302 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003303}
3304
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003305void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003306 ScratchRegisterScope ensure_scratch(
3307 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3308
3309 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3310 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3311 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3312 Address(CpuRegister(RSP), mem2 + stack_offset));
3313 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3314 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3315 CpuRegister(ensure_scratch.GetRegister()));
3316}
3317
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003318void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3319 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3320 __ movq(Address(CpuRegister(RSP), mem), reg);
3321 __ movq(reg, CpuRegister(TMP));
3322}
3323
3324void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3325 ScratchRegisterScope ensure_scratch(
3326 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3327
3328 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3329 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3330 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3331 Address(CpuRegister(RSP), mem2 + stack_offset));
3332 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3333 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3334 CpuRegister(ensure_scratch.GetRegister()));
3335}
3336
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003337void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3338 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3339 __ movss(Address(CpuRegister(RSP), mem), reg);
3340 __ movd(reg, CpuRegister(TMP));
3341}
3342
3343void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3344 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3345 __ movsd(Address(CpuRegister(RSP), mem), reg);
3346 __ movd(reg, CpuRegister(TMP));
3347}
3348
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003349void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3350 MoveOperands* move = moves_.Get(index);
3351 Location source = move->GetSource();
3352 Location destination = move->GetDestination();
3353
3354 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003356 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003357 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003358 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003360 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003361 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3362 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003363 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003364 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003365 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003366 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3367 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003368 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003369 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3370 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3371 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003372 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003374 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003375 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003376 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003377 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003378 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003380 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003381 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003382 }
3383}
3384
3385
3386void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3387 __ pushq(CpuRegister(reg));
3388}
3389
3390
3391void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3392 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003393}
3394
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003395void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3396 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3397 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3398 Immediate(mirror::Class::kStatusInitialized));
3399 __ j(kLess, slow_path->GetEntryLabel());
3400 __ Bind(slow_path->GetExitLabel());
3401 // No need for memory fence, thanks to the X86_64 memory model.
3402}
3403
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003404void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003405 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3406 ? LocationSummary::kCallOnSlowPath
3407 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003408 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003409 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003410 locations->SetOut(Location::RequiresRegister());
3411}
3412
3413void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003414 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003415 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003416 DCHECK(!cls->CanCallRuntime());
3417 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003418 codegen_->LoadCurrentMethod(out);
3419 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3420 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003421 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003422 codegen_->LoadCurrentMethod(out);
3423 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3424 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003425 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3426 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3427 codegen_->AddSlowPath(slow_path);
3428 __ testl(out, out);
3429 __ j(kEqual, slow_path->GetEntryLabel());
3430 if (cls->MustGenerateClinitCheck()) {
3431 GenerateClassInitializationCheck(slow_path, out);
3432 } else {
3433 __ Bind(slow_path->GetExitLabel());
3434 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003435 }
3436}
3437
3438void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3439 LocationSummary* locations =
3440 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3441 locations->SetInAt(0, Location::RequiresRegister());
3442 if (check->HasUses()) {
3443 locations->SetOut(Location::SameAsFirstInput());
3444 }
3445}
3446
3447void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003448 // We assume the class to not be null.
3449 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3450 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003451 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003452 GenerateClassInitializationCheck(slow_path,
3453 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003454}
3455
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003456void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3457 LocationSummary* locations =
3458 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3459 locations->SetOut(Location::RequiresRegister());
3460}
3461
3462void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3463 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3464 codegen_->AddSlowPath(slow_path);
3465
Roland Levillain271ab9c2014-11-27 15:23:57 +00003466 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003467 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003468 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3469 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003470 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3471 __ testl(out, out);
3472 __ j(kEqual, slow_path->GetEntryLabel());
3473 __ Bind(slow_path->GetExitLabel());
3474}
3475
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003476void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3477 LocationSummary* locations =
3478 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3479 locations->SetOut(Location::RequiresRegister());
3480}
3481
3482void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3483 Address address = Address::Absolute(
3484 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003486 __ gs()->movl(address, Immediate(0));
3487}
3488
3489void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3490 LocationSummary* locations =
3491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3492 InvokeRuntimeCallingConvention calling_convention;
3493 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3494}
3495
3496void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3497 __ gs()->call(
3498 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3499 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3500}
3501
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003502void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003503 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3504 ? LocationSummary::kNoCall
3505 : LocationSummary::kCallOnSlowPath;
3506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3507 locations->SetInAt(0, Location::RequiresRegister());
3508 locations->SetInAt(1, Location::Any());
3509 locations->SetOut(Location::RequiresRegister());
3510}
3511
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003512void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003513 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003514 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003515 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003516 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003517 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3518 Label done, zero;
3519 SlowPathCodeX86_64* slow_path = nullptr;
3520
3521 // Return 0 if `obj` is null.
3522 // TODO: avoid this check if we know obj is not null.
3523 __ testl(obj, obj);
3524 __ j(kEqual, &zero);
3525 // Compare the class of `obj` with `cls`.
3526 __ movl(out, Address(obj, class_offset));
3527 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003528 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003529 } else {
3530 DCHECK(cls.IsStackSlot()) << cls;
3531 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3532 }
3533 if (instruction->IsClassFinal()) {
3534 // Classes must be equal for the instanceof to succeed.
3535 __ j(kNotEqual, &zero);
3536 __ movl(out, Immediate(1));
3537 __ jmp(&done);
3538 } else {
3539 // If the classes are not equal, we go into a slow path.
3540 DCHECK(locations->OnlyCallsOnSlowPath());
3541 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003542 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003543 codegen_->AddSlowPath(slow_path);
3544 __ j(kNotEqual, slow_path->GetEntryLabel());
3545 __ movl(out, Immediate(1));
3546 __ jmp(&done);
3547 }
3548 __ Bind(&zero);
3549 __ movl(out, Immediate(0));
3550 if (slow_path != nullptr) {
3551 __ Bind(slow_path->GetExitLabel());
3552 }
3553 __ Bind(&done);
3554}
3555
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003556void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3557 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3558 instruction, LocationSummary::kCallOnSlowPath);
3559 locations->SetInAt(0, Location::RequiresRegister());
3560 locations->SetInAt(1, Location::Any());
3561 locations->AddTemp(Location::RequiresRegister());
3562}
3563
3564void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3565 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003567 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003568 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003569 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3570 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3571 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3572 codegen_->AddSlowPath(slow_path);
3573
3574 // TODO: avoid this check if we know obj is not null.
3575 __ testl(obj, obj);
3576 __ j(kEqual, slow_path->GetExitLabel());
3577 // Compare the class of `obj` with `cls`.
3578 __ movl(temp, Address(obj, class_offset));
3579 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003581 } else {
3582 DCHECK(cls.IsStackSlot()) << cls;
3583 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3584 }
3585 // Classes must be equal for the checkcast to succeed.
3586 __ j(kNotEqual, slow_path->GetEntryLabel());
3587 __ Bind(slow_path->GetExitLabel());
3588}
3589
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003590void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3591 LocationSummary* locations =
3592 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3593 InvokeRuntimeCallingConvention calling_convention;
3594 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3595}
3596
3597void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3598 __ gs()->call(Address::Absolute(instruction->IsEnter()
3599 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3600 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3601 true));
3602 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3603}
3604
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003605void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3606void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3607void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3608
3609void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3610 LocationSummary* locations =
3611 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3612 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3613 || instruction->GetResultType() == Primitive::kPrimLong);
3614 locations->SetInAt(0, Location::RequiresRegister());
3615 if (instruction->GetType() == Primitive::kPrimInt) {
3616 locations->SetInAt(1, Location::Any());
3617 } else {
3618 // Request a register to avoid loading a 64bits constant.
3619 locations->SetInAt(1, Location::RequiresRegister());
3620 }
3621 locations->SetOut(Location::SameAsFirstInput());
3622}
3623
3624void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3625 HandleBitwiseOperation(instruction);
3626}
3627
3628void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3629 HandleBitwiseOperation(instruction);
3630}
3631
3632void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3633 HandleBitwiseOperation(instruction);
3634}
3635
3636void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3637 LocationSummary* locations = instruction->GetLocations();
3638 Location first = locations->InAt(0);
3639 Location second = locations->InAt(1);
3640 DCHECK(first.Equals(locations->Out()));
3641
3642 if (instruction->GetResultType() == Primitive::kPrimInt) {
3643 if (second.IsRegister()) {
3644 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003646 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003648 } else {
3649 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003651 }
3652 } else if (second.IsConstant()) {
3653 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3654 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003655 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003656 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003657 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003658 } else {
3659 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003660 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003661 }
3662 } else {
3663 Address address(CpuRegister(RSP), second.GetStackIndex());
3664 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003666 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003667 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003668 } else {
3669 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003670 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003671 }
3672 }
3673 } else {
3674 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3675 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003676 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003677 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003679 } else {
3680 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003681 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003682 }
3683 }
3684}
3685
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003686} // namespace x86_64
3687} // namespace art