blob: dd6861f67b475f3b37f5d45d8f94beda9a613ecc [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
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
47static constexpr size_t kRuntimeParameterFpuRegistersLength =
48 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049
Mark Mendell24f2dfa2015-01-14 19:51:45 -050050static constexpr int kC2ConditionMask = 0x400;
51
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053 public:
54 InvokeRuntimeCallingConvention()
55 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010056 kRuntimeParameterCoreRegistersLength,
57 kRuntimeParameterFpuRegisters,
58 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010059
60 private:
61 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
62};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010063
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
65
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010066class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010068 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069
70 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
71 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010072 __ gs()->call(
73 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
77 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
80};
81
Calin Juravled0d48522014-11-04 16:40:20 +000082class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
83 public:
84 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
85
86 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
87 __ Bind(GetEntryLabel());
88 __ gs()->call(
89 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
90 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
91 }
92
93 private:
94 HDivZeroCheck* const instruction_;
95 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
96};
97
Calin Juravlebacfec32014-11-14 15:54:36 +000098class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000099 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000100 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
101 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000102
103 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
104 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000105 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 if (is_div_) {
107 __ negl(cpu_reg_);
108 } else {
109 __ movl(cpu_reg_, Immediate(0));
110 }
111
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000112 } else {
113 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negq(cpu_reg_);
116 } else {
117 __ movq(cpu_reg_, Immediate(0));
118 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000119 }
Calin Juravled0d48522014-11-04 16:40:20 +0000120 __ jmp(GetExitLabel());
121 }
122
123 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000125 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 const bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100131 public:
132 StackOverflowCheckSlowPathX86_64() {}
133
134 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
135 __ Bind(GetEntryLabel());
136 __ addq(CpuRegister(RSP),
137 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
138 __ gs()->jmp(
139 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
140 }
141
142 private:
143 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
144};
145
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
149 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150
151 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100152 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000153 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100154 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
156 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100157 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 if (successor_ == nullptr) {
159 __ jmp(GetReturnLabel());
160 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100161 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 }
164
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 Label* GetReturnLabel() {
166 DCHECK(successor_ == nullptr);
167 return &return_label_;
168 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169
170 private:
171 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173 Label return_label_;
174
175 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
176};
177
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100178class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100180 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
181 Location index_location,
182 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100183 : instruction_(instruction),
184 index_location_(index_location),
185 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186
187 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000189 // We're moving two locations to locations that could overlap, so we need a parallel
190 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000192 codegen->EmitParallelMoves(
193 index_location_,
194 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
195 length_location_,
196 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 __ gs()->call(Address::Absolute(
198 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100199 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100200 }
201
202 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100203 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100204 const Location index_location_;
205 const Location length_location_;
206
207 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
208};
209
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 LoadClassSlowPathX86_64(HLoadClass* cls,
213 HInstruction* at,
214 uint32_t dex_pc,
215 bool do_clinit)
216 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
217 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
218 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219
220 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
223 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100224
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 codegen->SaveLiveRegisters(locations);
226
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100229 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000230 __ gs()->call(Address::Absolute((do_clinit_
231 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
232 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
233 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000235 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000237 if (out.IsValid()) {
238 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
239 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 }
241
242 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243 __ jmp(GetExitLabel());
244 }
245
246 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 // The class this slow path will load.
248 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100249
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 // The instruction where this slow path is happening.
251 // (Might be the load class or an initialization check).
252 HInstruction* const at_;
253
254 // The dex PC of `at_`.
255 const uint32_t dex_pc_;
256
257 // Whether to initialize the class.
258 const bool do_clinit_;
259
260 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100261};
262
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
264 public:
265 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
266
267 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
268 LocationSummary* locations = instruction_->GetLocations();
269 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
270
271 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
272 __ Bind(GetEntryLabel());
273 codegen->SaveLiveRegisters(locations);
274
275 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
277 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000278 Immediate(instruction_->GetStringIndex()));
279 __ gs()->call(Address::Absolute(
280 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
281 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
282 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
283 codegen->RestoreLiveRegisters(locations);
284 __ jmp(GetExitLabel());
285 }
286
287 private:
288 HLoadString* const instruction_;
289
290 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
291};
292
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
294 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 TypeCheckSlowPathX86_64(HInstruction* instruction,
296 Location class_to_check,
297 Location object_class,
298 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 class_to_check_(class_to_check),
301 object_class_(object_class),
302 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
304 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
305 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000306 DCHECK(instruction_->IsCheckCast()
307 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308
309 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
310 __ Bind(GetEntryLabel());
311 codegen->SaveLiveRegisters(locations);
312
313 // We're moving two locations to locations that could overlap, so we need a parallel
314 // move resolver.
315 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000316 codegen->EmitParallelMoves(
317 class_to_check_,
318 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
319 object_class_,
320 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 if (instruction_->IsInstanceOf()) {
323 __ gs()->call(
324 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
325 } else {
326 DCHECK(instruction_->IsCheckCast());
327 __ gs()->call(
328 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
329 }
330 codegen->RecordPcInfo(instruction_, dex_pc_);
331
332 if (instruction_->IsInstanceOf()) {
333 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
334 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335
336 codegen->RestoreLiveRegisters(locations);
337 __ jmp(GetExitLabel());
338 }
339
340 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000341 HInstruction* const instruction_;
342 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
347};
348
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100349#undef __
350#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
351
Dave Allison20dfc792014-06-16 20:44:29 -0700352inline Condition X86_64Condition(IfCondition cond) {
353 switch (cond) {
354 case kCondEQ: return kEqual;
355 case kCondNE: return kNotEqual;
356 case kCondLT: return kLess;
357 case kCondLE: return kLessEqual;
358 case kCondGT: return kGreater;
359 case kCondGE: return kGreaterEqual;
360 default:
361 LOG(FATAL) << "Unknown if condition";
362 }
363 return kEqual;
364}
365
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800366void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
367 CpuRegister temp) {
368 // All registers are assumed to be correctly set up.
369
370 // TODO: Implement all kinds of calls:
371 // 1) boot -> boot
372 // 2) app -> boot
373 // 3) app -> app
374 //
375 // Currently we implement the app -> app logic, which looks up in the resolve cache.
376
377 // temp = method;
378 LoadCurrentMethod(temp);
379 // temp = temp->dex_cache_resolved_methods_;
380 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
381 // temp = temp[index_in_cache]
382 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
383 // (temp + offset_of_quick_compiled_code)()
384 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
385 kX86_64WordSize).SizeValue()));
386
387 DCHECK(!IsLeafMethod());
388 RecordPcInfo(invoke, invoke->GetDexPc());
389}
390
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100391void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
392 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
393}
394
395void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
396 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
397}
398
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100399size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
400 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
401 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100402}
403
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100404size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
405 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
406 return kX86_64WordSize;
407}
408
409size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
410 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
411 return kX86_64WordSize;
412}
413
414size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
415 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
416 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100417}
418
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000419CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
420 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100421 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100422 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000423 instruction_visitor_(graph, this),
424 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100425
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100426size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
427 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
428}
429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100430InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
431 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100432 : HGraphVisitor(graph),
433 assembler_(codegen->GetAssembler()),
434 codegen_(codegen) {}
435
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100436Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100437 switch (type) {
438 case Primitive::kPrimLong:
439 case Primitive::kPrimByte:
440 case Primitive::kPrimBoolean:
441 case Primitive::kPrimChar:
442 case Primitive::kPrimShort:
443 case Primitive::kPrimInt:
444 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100445 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100446 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447 }
448
449 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100450 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100452 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100453 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454
455 case Primitive::kPrimVoid:
456 LOG(FATAL) << "Unreachable type " << type;
457 }
458
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100459 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100460}
461
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100462void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100463 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100464 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100465
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000466 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000468
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000469 // TODO: We currently don't use Quick's callee saved registers.
470 blocked_core_registers_[RBX] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100471 blocked_core_registers_[RBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000472 blocked_core_registers_[R12] = true;
473 blocked_core_registers_[R13] = true;
474 blocked_core_registers_[R14] = true;
475 blocked_core_registers_[R15] = true;
476
477 blocked_fpu_registers_[XMM12] = true;
478 blocked_fpu_registers_[XMM13] = true;
479 blocked_fpu_registers_[XMM14] = true;
480 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100481}
482
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100483void CodeGeneratorX86_64::GenerateFrameEntry() {
484 // Create a fake register to mimic Quick.
485 static const int kFakeReturnRegister = 16;
486 core_spill_mask_ |= (1 << kFakeReturnRegister);
487
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100488 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700489 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Calin Juravle93edf732015-01-20 20:14:07 +0000490 bool implicitStackOverflowChecks = GetCompilerOptions().GetImplicitStackOverflowChecks();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100491
Calin Juravle93edf732015-01-20 20:14:07 +0000492 if (!skip_overflow_check && implicitStackOverflowChecks) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100493 __ testq(CpuRegister(RAX), Address(
494 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100495 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100496 }
497
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100498 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100499 __ subq(CpuRegister(RSP),
500 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
501
Calin Juravle93edf732015-01-20 20:14:07 +0000502 if (!skip_overflow_check && !implicitStackOverflowChecks) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100503 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100504 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100505
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100506 __ gs()->cmpq(CpuRegister(RSP),
507 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
508 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100509 }
510
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
512}
513
514void CodeGeneratorX86_64::GenerateFrameExit() {
515 __ addq(CpuRegister(RSP),
516 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
517}
518
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100519void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
520 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100521}
522
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100523void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100524 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
525}
526
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100527Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
528 switch (load->GetType()) {
529 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100530 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
532 break;
533
534 case Primitive::kPrimInt:
535 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100538
539 case Primitive::kPrimBoolean:
540 case Primitive::kPrimByte:
541 case Primitive::kPrimChar:
542 case Primitive::kPrimShort:
543 case Primitive::kPrimVoid:
544 LOG(FATAL) << "Unexpected type " << load->GetType();
545 }
546
547 LOG(FATAL) << "Unreachable";
548 return Location();
549}
550
551void CodeGeneratorX86_64::Move(Location destination, Location source) {
552 if (source.Equals(destination)) {
553 return;
554 }
555 if (destination.IsRegister()) {
556 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000557 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000559 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000561 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100562 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 } else {
564 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000565 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100566 Address(CpuRegister(RSP), source.GetStackIndex()));
567 }
568 } else if (destination.IsFpuRegister()) {
569 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000570 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000572 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000574 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 Address(CpuRegister(RSP), source.GetStackIndex()));
576 } else {
577 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000578 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100579 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 }
581 } else if (destination.IsStackSlot()) {
582 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000584 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 } else if (source.IsFpuRegister()) {
586 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500588 } else if (source.IsConstant()) {
589 HConstant* constant = source.GetConstant();
590 int32_t value;
591 if (constant->IsFloatConstant()) {
592 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
593 } else {
594 DCHECK(constant->IsIntConstant());
595 value = constant->AsIntConstant()->GetValue();
596 }
597 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100598 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500599 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000600 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
601 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100602 }
603 } else {
604 DCHECK(destination.IsDoubleStackSlot());
605 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100606 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
609 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000610 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500611 } else if (source.IsConstant()) {
612 HConstant* constant = source.GetConstant();
613 int64_t value = constant->AsLongConstant()->GetValue();
614 if (constant->IsDoubleConstant()) {
615 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
616 } else {
617 DCHECK(constant->IsLongConstant());
618 value = constant->AsLongConstant()->GetValue();
619 }
620 __ movq(CpuRegister(TMP), Immediate(value));
621 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100622 } else {
623 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000624 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
625 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100626 }
627 }
628}
629
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100630void CodeGeneratorX86_64::Move(HInstruction* instruction,
631 Location location,
632 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000633 LocationSummary* locations = instruction->GetLocations();
634 if (locations != nullptr && locations->Out().Equals(location)) {
635 return;
636 }
637
638 if (locations != nullptr && locations->Out().IsConstant()) {
639 HConstant* const_to_move = locations->Out().GetConstant();
640 if (const_to_move->IsIntConstant()) {
641 Immediate imm(const_to_move->AsIntConstant()->GetValue());
642 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000643 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000644 } else if (location.IsStackSlot()) {
645 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
646 } else {
647 DCHECK(location.IsConstant());
648 DCHECK_EQ(location.GetConstant(), const_to_move);
649 }
650 } else if (const_to_move->IsLongConstant()) {
651 int64_t value = const_to_move->AsLongConstant()->GetValue();
652 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000654 } else if (location.IsDoubleStackSlot()) {
655 __ movq(CpuRegister(TMP), Immediate(value));
656 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
657 } else {
658 DCHECK(location.IsConstant());
659 DCHECK_EQ(location.GetConstant(), const_to_move);
660 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100661 }
Roland Levillain476df552014-10-09 17:51:36 +0100662 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663 switch (instruction->GetType()) {
664 case Primitive::kPrimBoolean:
665 case Primitive::kPrimByte:
666 case Primitive::kPrimChar:
667 case Primitive::kPrimShort:
668 case Primitive::kPrimInt:
669 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100671 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
672 break;
673
674 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000676 Move(location,
677 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100678 break;
679
680 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100681 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000683 } else if (instruction->IsTemporary()) {
684 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
685 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100688 switch (instruction->GetType()) {
689 case Primitive::kPrimBoolean:
690 case Primitive::kPrimByte:
691 case Primitive::kPrimChar:
692 case Primitive::kPrimShort:
693 case Primitive::kPrimInt:
694 case Primitive::kPrimNot:
695 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100696 case Primitive::kPrimFloat:
697 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000698 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100699 break;
700
701 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100702 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100703 }
704 }
705}
706
707void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
708 got->SetLocations(nullptr);
709}
710
711void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
712 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100713 DCHECK(!successor->IsExitBlock());
714
715 HBasicBlock* block = got->GetBlock();
716 HInstruction* previous = got->GetPrevious();
717
718 HLoopInformation* info = block->GetLoopInformation();
719 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
720 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
721 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
722 return;
723 }
724
725 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
726 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
727 }
728 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100729 __ jmp(codegen_->GetLabelOf(successor));
730 }
731}
732
733void LocationsBuilderX86_64::VisitExit(HExit* exit) {
734 exit->SetLocations(nullptr);
735}
736
737void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700738 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100739 if (kIsDebugBuild) {
740 __ Comment("Unreachable");
741 __ int3();
742 }
743}
744
745void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100746 LocationSummary* locations =
747 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100748 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100749 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100750 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100751 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752}
753
754void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700755 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100756 if (cond->IsIntConstant()) {
757 // Constant condition, statically compared against 1.
758 int32_t cond_value = cond->AsIntConstant()->GetValue();
759 if (cond_value == 1) {
760 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
761 if_instr->IfTrueSuccessor())) {
762 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100763 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100764 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100765 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100766 DCHECK_EQ(cond_value, 0);
767 }
768 } else {
769 bool materialized =
770 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
771 // Moves do not affect the eflags register, so if the condition is
772 // evaluated just before the if, we don't need to evaluate it
773 // again.
774 bool eflags_set = cond->IsCondition()
775 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
776 if (materialized) {
777 if (!eflags_set) {
778 // Materialized condition, compare against 0.
779 Location lhs = if_instr->GetLocations()->InAt(0);
780 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000781 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100782 } else {
783 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
784 Immediate(0));
785 }
786 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
787 } else {
788 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
789 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
790 }
791 } else {
792 Location lhs = cond->GetLocations()->InAt(0);
793 Location rhs = cond->GetLocations()->InAt(1);
794 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000795 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100798 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
799 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000800 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100801 Address(CpuRegister(RSP), rhs.GetStackIndex()));
802 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100803 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
804 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700805 }
Dave Allison20dfc792014-06-16 20:44:29 -0700806 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
808 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700809 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100810 }
811}
812
813void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
814 local->SetLocations(nullptr);
815}
816
817void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
818 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
819}
820
821void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
822 local->SetLocations(nullptr);
823}
824
825void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
826 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700827 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100828}
829
830void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100831 LocationSummary* locations =
832 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100833 switch (store->InputAt(1)->GetType()) {
834 case Primitive::kPrimBoolean:
835 case Primitive::kPrimByte:
836 case Primitive::kPrimChar:
837 case Primitive::kPrimShort:
838 case Primitive::kPrimInt:
839 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100840 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100841 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
842 break;
843
844 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
847 break;
848
849 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100850 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100852}
853
854void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700855 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100856}
857
Dave Allison20dfc792014-06-16 20:44:29 -0700858void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100859 LocationSummary* locations =
860 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100861 locations->SetInAt(0, Location::RequiresRegister());
862 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100863 if (comp->NeedsMaterialization()) {
864 locations->SetOut(Location::RequiresRegister());
865 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100866}
867
Dave Allison20dfc792014-06-16 20:44:29 -0700868void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
869 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100870 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000871 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100872 // Clear register: setcc only sets the low byte.
873 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100874 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000875 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
876 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100877 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100879 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
880 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000881 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100882 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
883 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100884 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700885 }
886}
887
888void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
889 VisitCondition(comp);
890}
891
892void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
893 VisitCondition(comp);
894}
895
896void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
897 VisitCondition(comp);
898}
899
900void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
901 VisitCondition(comp);
902}
903
904void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
905 VisitCondition(comp);
906}
907
908void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
909 VisitCondition(comp);
910}
911
912void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
913 VisitCondition(comp);
914}
915
916void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
917 VisitCondition(comp);
918}
919
920void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
921 VisitCondition(comp);
922}
923
924void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
925 VisitCondition(comp);
926}
927
928void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
929 VisitCondition(comp);
930}
931
932void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
933 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100934}
935
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100936void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100937 LocationSummary* locations =
938 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000939 switch (compare->InputAt(0)->GetType()) {
940 case Primitive::kPrimLong: {
941 locations->SetInAt(0, Location::RequiresRegister());
942 locations->SetInAt(1, Location::RequiresRegister());
943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
944 break;
945 }
946 case Primitive::kPrimFloat:
947 case Primitive::kPrimDouble: {
948 locations->SetInAt(0, Location::RequiresFpuRegister());
949 locations->SetInAt(1, Location::RequiresFpuRegister());
950 locations->SetOut(Location::RequiresRegister());
951 break;
952 }
953 default:
954 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
955 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100956}
957
958void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100959 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000960 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000961 Location left = locations->InAt(0);
962 Location right = locations->InAt(1);
963
964 Label less, greater, done;
965 Primitive::Type type = compare->InputAt(0)->GetType();
966 switch (type) {
967 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000968 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100969 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000970 }
971 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000972 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000973 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
974 break;
975 }
976 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000977 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000978 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
979 break;
980 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100981 default:
Calin Juravleddb7df22014-11-25 20:56:51 +0000982 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100983 }
Calin Juravleddb7df22014-11-25 20:56:51 +0000984 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +0000985 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +0000986 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +0000987
Calin Juravle91debbc2014-11-26 19:01:09 +0000988 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +0000989 __ movl(out, Immediate(1));
990 __ jmp(&done);
991
992 __ Bind(&less);
993 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100994
995 __ Bind(&done);
996}
997
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100999 LocationSummary* locations =
1000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001001 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002}
1003
1004void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001005 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007}
1008
1009void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001010 LocationSummary* locations =
1011 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001012 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013}
1014
1015void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001016 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001017 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001018}
1019
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001020void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1021 LocationSummary* locations =
1022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1023 locations->SetOut(Location::ConstantLocation(constant));
1024}
1025
1026void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1027 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001029}
1030
1031void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1034 locations->SetOut(Location::ConstantLocation(constant));
1035}
1036
1037void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1038 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001039 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001040}
1041
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1043 ret->SetLocations(nullptr);
1044}
1045
1046void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001047 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001048 codegen_->GenerateFrameExit();
1049 __ ret();
1050}
1051
1052void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001053 LocationSummary* locations =
1054 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001055 switch (ret->InputAt(0)->GetType()) {
1056 case Primitive::kPrimBoolean:
1057 case Primitive::kPrimByte:
1058 case Primitive::kPrimChar:
1059 case Primitive::kPrimShort:
1060 case Primitive::kPrimInt:
1061 case Primitive::kPrimNot:
1062 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001064 break;
1065
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001066 case Primitive::kPrimFloat:
1067 case Primitive::kPrimDouble:
1068 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001070 break;
1071
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001072 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001074 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075}
1076
1077void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1078 if (kIsDebugBuild) {
1079 switch (ret->InputAt(0)->GetType()) {
1080 case Primitive::kPrimBoolean:
1081 case Primitive::kPrimByte:
1082 case Primitive::kPrimChar:
1083 case Primitive::kPrimShort:
1084 case Primitive::kPrimInt:
1085 case Primitive::kPrimNot:
1086 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001087 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088 break;
1089
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 case Primitive::kPrimFloat:
1091 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001092 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 XMM0);
1094 break;
1095
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001096 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098 }
1099 }
1100 codegen_->GenerateFrameExit();
1101 __ ret();
1102}
1103
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1105 switch (type) {
1106 case Primitive::kPrimBoolean:
1107 case Primitive::kPrimByte:
1108 case Primitive::kPrimChar:
1109 case Primitive::kPrimShort:
1110 case Primitive::kPrimInt:
1111 case Primitive::kPrimNot: {
1112 uint32_t index = gp_index_++;
1113 stack_index_++;
1114 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001115 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116 } else {
1117 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1118 }
1119 }
1120
1121 case Primitive::kPrimLong: {
1122 uint32_t index = gp_index_;
1123 stack_index_ += 2;
1124 if (index < calling_convention.GetNumberOfRegisters()) {
1125 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001126 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001127 } else {
1128 gp_index_ += 2;
1129 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1130 }
1131 }
1132
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 case Primitive::kPrimFloat: {
1134 uint32_t index = fp_index_++;
1135 stack_index_++;
1136 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001137 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001138 } else {
1139 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1140 }
1141 }
1142
1143 case Primitive::kPrimDouble: {
1144 uint32_t index = fp_index_++;
1145 stack_index_ += 2;
1146 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 } else {
1149 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1150 }
1151 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152
1153 case Primitive::kPrimVoid:
1154 LOG(FATAL) << "Unexpected parameter type " << type;
1155 break;
1156 }
1157 return Location();
1158}
1159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001160void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001161 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1162 if (intrinsic.TryDispatch(invoke)) {
1163 return;
1164 }
1165
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001166 HandleInvoke(invoke);
1167}
1168
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001169static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1170 if (invoke->GetLocations()->Intrinsified()) {
1171 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1172 intrinsic.Dispatch(invoke);
1173 return true;
1174 }
1175 return false;
1176}
1177
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001178void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001179 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1180 return;
1181 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001182
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001183 codegen_->GenerateStaticOrDirectCall(
1184 invoke,
1185 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001186}
1187
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001188void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001189 LocationSummary* locations =
1190 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001191 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001192
1193 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001194 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195 HInstruction* input = invoke->InputAt(i);
1196 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1197 }
1198
1199 switch (invoke->GetType()) {
1200 case Primitive::kPrimBoolean:
1201 case Primitive::kPrimByte:
1202 case Primitive::kPrimChar:
1203 case Primitive::kPrimShort:
1204 case Primitive::kPrimInt:
1205 case Primitive::kPrimNot:
1206 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001207 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001208 break;
1209
1210 case Primitive::kPrimVoid:
1211 break;
1212
1213 case Primitive::kPrimDouble:
1214 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001215 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216 break;
1217 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218}
1219
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001221 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1222 if (intrinsic.TryDispatch(invoke)) {
1223 return;
1224 }
1225
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001226 HandleInvoke(invoke);
1227}
1228
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001229void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001230 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1231 return;
1232 }
1233
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001235 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1236 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1237 LocationSummary* locations = invoke->GetLocations();
1238 Location receiver = locations->InAt(0);
1239 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1240 // temp = object->GetClass();
1241 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1243 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001244 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001245 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001246 }
1247 // temp = temp->GetMethodAt(method_offset);
1248 __ movl(temp, Address(temp, method_offset));
1249 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001250 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001251 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001252
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001253 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001254 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001255}
1256
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001257void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1258 HandleInvoke(invoke);
1259 // Add the hidden argument.
1260 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1261}
1262
1263void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1264 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001265 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001266 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1267 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1268 LocationSummary* locations = invoke->GetLocations();
1269 Location receiver = locations->InAt(0);
1270 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1271
1272 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001273 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001274 Immediate(invoke->GetDexMethodIndex()));
1275
1276 // temp = object->GetClass();
1277 if (receiver.IsStackSlot()) {
1278 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1279 __ movl(temp, Address(temp, class_offset));
1280 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001281 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001282 }
1283 // temp = temp->GetImtEntryAt(method_offset);
1284 __ movl(temp, Address(temp, method_offset));
1285 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001286 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001287 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001288
1289 DCHECK(!codegen_->IsLeafMethod());
1290 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1291}
1292
Roland Levillain88cb1752014-10-20 16:36:47 +01001293void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1294 LocationSummary* locations =
1295 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1296 switch (neg->GetResultType()) {
1297 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001298 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001299 locations->SetInAt(0, Location::RequiresRegister());
1300 locations->SetOut(Location::SameAsFirstInput());
1301 break;
1302
Roland Levillain88cb1752014-10-20 16:36:47 +01001303 case Primitive::kPrimFloat:
1304 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001305 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001306 locations->SetOut(Location::SameAsFirstInput());
1307 locations->AddTemp(Location::RequiresRegister());
1308 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001309 break;
1310
1311 default:
1312 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1313 }
1314}
1315
1316void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1317 LocationSummary* locations = neg->GetLocations();
1318 Location out = locations->Out();
1319 Location in = locations->InAt(0);
1320 switch (neg->GetResultType()) {
1321 case Primitive::kPrimInt:
1322 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001323 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001324 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001325 break;
1326
1327 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001328 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001329 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001330 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001331 break;
1332
Roland Levillain5368c212014-11-27 15:03:41 +00001333 case Primitive::kPrimFloat: {
1334 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001335 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1336 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001337 // Implement float negation with an exclusive or with value
1338 // 0x80000000 (mask for bit 31, representing the sign of a
1339 // single-precision floating-point number).
1340 __ movq(constant, Immediate(INT64_C(0x80000000)));
1341 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001342 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001343 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001344 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001345
Roland Levillain5368c212014-11-27 15:03:41 +00001346 case Primitive::kPrimDouble: {
1347 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001348 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1349 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001350 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001352 // a double-precision floating-point number).
1353 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1354 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001355 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001357 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001358
1359 default:
1360 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1361 }
1362}
1363
Roland Levillaindff1f282014-11-05 14:15:05 +00001364void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1365 LocationSummary* locations =
1366 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1367 Primitive::Type result_type = conversion->GetResultType();
1368 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001369 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001370 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001371 case Primitive::kPrimByte:
1372 switch (input_type) {
1373 case Primitive::kPrimShort:
1374 case Primitive::kPrimInt:
1375 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001376 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001377 locations->SetInAt(0, Location::Any());
1378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1379 break;
1380
1381 default:
1382 LOG(FATAL) << "Unexpected type conversion from " << input_type
1383 << " to " << result_type;
1384 }
1385 break;
1386
Roland Levillain01a8d712014-11-14 16:27:39 +00001387 case Primitive::kPrimShort:
1388 switch (input_type) {
1389 case Primitive::kPrimByte:
1390 case Primitive::kPrimInt:
1391 case Primitive::kPrimChar:
1392 // Processing a Dex `int-to-short' instruction.
1393 locations->SetInAt(0, Location::Any());
1394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1395 break;
1396
1397 default:
1398 LOG(FATAL) << "Unexpected type conversion from " << input_type
1399 << " to " << result_type;
1400 }
1401 break;
1402
Roland Levillain946e1432014-11-11 17:35:19 +00001403 case Primitive::kPrimInt:
1404 switch (input_type) {
1405 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001406 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001407 locations->SetInAt(0, Location::Any());
1408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1409 break;
1410
1411 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001412 // Processing a Dex `float-to-int' instruction.
1413 locations->SetInAt(0, Location::RequiresFpuRegister());
1414 locations->SetOut(Location::RequiresRegister());
1415 locations->AddTemp(Location::RequiresFpuRegister());
1416 break;
1417
Roland Levillain946e1432014-11-11 17:35:19 +00001418 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001419 // Processing a Dex `double-to-int' instruction.
1420 locations->SetInAt(0, Location::RequiresFpuRegister());
1421 locations->SetOut(Location::RequiresRegister());
1422 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001423 break;
1424
1425 default:
1426 LOG(FATAL) << "Unexpected type conversion from " << input_type
1427 << " to " << result_type;
1428 }
1429 break;
1430
Roland Levillaindff1f282014-11-05 14:15:05 +00001431 case Primitive::kPrimLong:
1432 switch (input_type) {
1433 case Primitive::kPrimByte:
1434 case Primitive::kPrimShort:
1435 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001436 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001437 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001438 // TODO: We would benefit from a (to-be-implemented)
1439 // Location::RegisterOrStackSlot requirement for this input.
1440 locations->SetInAt(0, Location::RequiresRegister());
1441 locations->SetOut(Location::RequiresRegister());
1442 break;
1443
1444 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001445 // Processing a Dex `float-to-long' instruction.
1446 locations->SetInAt(0, Location::RequiresFpuRegister());
1447 locations->SetOut(Location::RequiresRegister());
1448 locations->AddTemp(Location::RequiresFpuRegister());
1449 break;
1450
Roland Levillaindff1f282014-11-05 14:15:05 +00001451 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001452 // Processing a Dex `double-to-long' instruction.
1453 locations->SetInAt(0, Location::RequiresFpuRegister());
1454 locations->SetOut(Location::RequiresRegister());
1455 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001456 break;
1457
1458 default:
1459 LOG(FATAL) << "Unexpected type conversion from " << input_type
1460 << " to " << result_type;
1461 }
1462 break;
1463
Roland Levillain981e4542014-11-14 11:47:14 +00001464 case Primitive::kPrimChar:
1465 switch (input_type) {
1466 case Primitive::kPrimByte:
1467 case Primitive::kPrimShort:
1468 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001469 // Processing a Dex `int-to-char' instruction.
1470 locations->SetInAt(0, Location::Any());
1471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1472 break;
1473
1474 default:
1475 LOG(FATAL) << "Unexpected type conversion from " << input_type
1476 << " to " << result_type;
1477 }
1478 break;
1479
Roland Levillaindff1f282014-11-05 14:15:05 +00001480 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001481 switch (input_type) {
1482 case Primitive::kPrimByte:
1483 case Primitive::kPrimShort:
1484 case Primitive::kPrimInt:
1485 case Primitive::kPrimChar:
1486 // Processing a Dex `int-to-float' instruction.
1487 locations->SetInAt(0, Location::RequiresRegister());
1488 locations->SetOut(Location::RequiresFpuRegister());
1489 break;
1490
1491 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001492 // Processing a Dex `long-to-float' instruction.
1493 locations->SetInAt(0, Location::RequiresRegister());
1494 locations->SetOut(Location::RequiresFpuRegister());
1495 break;
1496
Roland Levillaincff13742014-11-17 14:32:17 +00001497 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001498 // Processing a Dex `double-to-float' instruction.
1499 locations->SetInAt(0, Location::RequiresFpuRegister());
1500 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001501 break;
1502
1503 default:
1504 LOG(FATAL) << "Unexpected type conversion from " << input_type
1505 << " to " << result_type;
1506 };
1507 break;
1508
Roland Levillaindff1f282014-11-05 14:15:05 +00001509 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001510 switch (input_type) {
1511 case Primitive::kPrimByte:
1512 case Primitive::kPrimShort:
1513 case Primitive::kPrimInt:
1514 case Primitive::kPrimChar:
1515 // Processing a Dex `int-to-double' instruction.
1516 locations->SetInAt(0, Location::RequiresRegister());
1517 locations->SetOut(Location::RequiresFpuRegister());
1518 break;
1519
1520 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001521 // Processing a Dex `long-to-double' instruction.
1522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetOut(Location::RequiresFpuRegister());
1524 break;
1525
Roland Levillaincff13742014-11-17 14:32:17 +00001526 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001527 // Processing a Dex `float-to-double' instruction.
1528 locations->SetInAt(0, Location::RequiresFpuRegister());
1529 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001530 break;
1531
1532 default:
1533 LOG(FATAL) << "Unexpected type conversion from " << input_type
1534 << " to " << result_type;
1535 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 }
1542}
1543
1544void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1545 LocationSummary* locations = conversion->GetLocations();
1546 Location out = locations->Out();
1547 Location in = locations->InAt(0);
1548 Primitive::Type result_type = conversion->GetResultType();
1549 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001550 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001551 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001552 case Primitive::kPrimByte:
1553 switch (input_type) {
1554 case Primitive::kPrimShort:
1555 case Primitive::kPrimInt:
1556 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001557 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001558 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001559 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001560 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001561 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001562 Address(CpuRegister(RSP), in.GetStackIndex()));
1563 } else {
1564 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001565 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001566 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1567 }
1568 break;
1569
1570 default:
1571 LOG(FATAL) << "Unexpected type conversion from " << input_type
1572 << " to " << result_type;
1573 }
1574 break;
1575
Roland Levillain01a8d712014-11-14 16:27:39 +00001576 case Primitive::kPrimShort:
1577 switch (input_type) {
1578 case Primitive::kPrimByte:
1579 case Primitive::kPrimInt:
1580 case Primitive::kPrimChar:
1581 // Processing a Dex `int-to-short' instruction.
1582 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001583 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001584 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001585 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001586 Address(CpuRegister(RSP), in.GetStackIndex()));
1587 } else {
1588 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001589 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001590 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1591 }
1592 break;
1593
1594 default:
1595 LOG(FATAL) << "Unexpected type conversion from " << input_type
1596 << " to " << result_type;
1597 }
1598 break;
1599
Roland Levillain946e1432014-11-11 17:35:19 +00001600 case Primitive::kPrimInt:
1601 switch (input_type) {
1602 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001603 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001604 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001606 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001608 Address(CpuRegister(RSP), in.GetStackIndex()));
1609 } else {
1610 DCHECK(in.IsConstant());
1611 DCHECK(in.GetConstant()->IsLongConstant());
1612 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001613 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001614 }
1615 break;
1616
Roland Levillain3f8f9362014-12-02 17:45:01 +00001617 case Primitive::kPrimFloat: {
1618 // Processing a Dex `float-to-int' instruction.
1619 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1620 CpuRegister output = out.AsRegister<CpuRegister>();
1621 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1622 Label done, nan;
1623
1624 __ movl(output, Immediate(kPrimIntMax));
1625 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001626 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001627 // if input >= temp goto done
1628 __ comiss(input, temp);
1629 __ j(kAboveEqual, &done);
1630 // if input == NaN goto nan
1631 __ j(kUnordered, &nan);
1632 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001633 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001634 __ jmp(&done);
1635 __ Bind(&nan);
1636 // output = 0
1637 __ xorl(output, output);
1638 __ Bind(&done);
1639 break;
1640 }
1641
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001642 case Primitive::kPrimDouble: {
1643 // Processing a Dex `double-to-int' instruction.
1644 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1645 CpuRegister output = out.AsRegister<CpuRegister>();
1646 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1647 Label done, nan;
1648
1649 __ movl(output, Immediate(kPrimIntMax));
1650 // temp = int-to-double(output)
1651 __ cvtsi2sd(temp, output);
1652 // if input >= temp goto done
1653 __ comisd(input, temp);
1654 __ j(kAboveEqual, &done);
1655 // if input == NaN goto nan
1656 __ j(kUnordered, &nan);
1657 // output = double-to-int-truncate(input)
1658 __ cvttsd2si(output, input);
1659 __ jmp(&done);
1660 __ Bind(&nan);
1661 // output = 0
1662 __ xorl(output, output);
1663 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001664 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001665 }
Roland Levillain946e1432014-11-11 17:35:19 +00001666
1667 default:
1668 LOG(FATAL) << "Unexpected type conversion from " << input_type
1669 << " to " << result_type;
1670 }
1671 break;
1672
Roland Levillaindff1f282014-11-05 14:15:05 +00001673 case Primitive::kPrimLong:
1674 switch (input_type) {
1675 DCHECK(out.IsRegister());
1676 case Primitive::kPrimByte:
1677 case Primitive::kPrimShort:
1678 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001679 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001680 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001681 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001683 break;
1684
Roland Levillain624279f2014-12-04 11:54:28 +00001685 case Primitive::kPrimFloat: {
1686 // Processing a Dex `float-to-long' instruction.
1687 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1688 CpuRegister output = out.AsRegister<CpuRegister>();
1689 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1690 Label done, nan;
1691
1692 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001693 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001694 __ cvtsi2ss(temp, output, true);
1695 // if input >= temp goto done
1696 __ comiss(input, temp);
1697 __ j(kAboveEqual, &done);
1698 // if input == NaN goto nan
1699 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001700 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001701 __ cvttss2si(output, input, true);
1702 __ jmp(&done);
1703 __ Bind(&nan);
1704 // output = 0
1705 __ xorq(output, output);
1706 __ Bind(&done);
1707 break;
1708 }
1709
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001710 case Primitive::kPrimDouble: {
1711 // Processing a Dex `double-to-long' instruction.
1712 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1713 CpuRegister output = out.AsRegister<CpuRegister>();
1714 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1715 Label done, nan;
1716
1717 __ movq(output, Immediate(kPrimLongMax));
1718 // temp = long-to-double(output)
1719 __ cvtsi2sd(temp, output, true);
1720 // if input >= temp goto done
1721 __ comisd(input, temp);
1722 __ j(kAboveEqual, &done);
1723 // if input == NaN goto nan
1724 __ j(kUnordered, &nan);
1725 // output = double-to-long-truncate(input)
1726 __ cvttsd2si(output, input, true);
1727 __ jmp(&done);
1728 __ Bind(&nan);
1729 // output = 0
1730 __ xorq(output, output);
1731 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001732 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001733 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001734
1735 default:
1736 LOG(FATAL) << "Unexpected type conversion from " << input_type
1737 << " to " << result_type;
1738 }
1739 break;
1740
Roland Levillain981e4542014-11-14 11:47:14 +00001741 case Primitive::kPrimChar:
1742 switch (input_type) {
1743 case Primitive::kPrimByte:
1744 case Primitive::kPrimShort:
1745 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001746 // Processing a Dex `int-to-char' instruction.
1747 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001749 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001751 Address(CpuRegister(RSP), in.GetStackIndex()));
1752 } else {
1753 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001754 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001755 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1756 }
1757 break;
1758
1759 default:
1760 LOG(FATAL) << "Unexpected type conversion from " << input_type
1761 << " to " << result_type;
1762 }
1763 break;
1764
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001766 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001767 case Primitive::kPrimByte:
1768 case Primitive::kPrimShort:
1769 case Primitive::kPrimInt:
1770 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001771 // Processing a Dex `int-to-float' instruction.
1772 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001773 break;
1774
1775 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001776 // Processing a Dex `long-to-float' instruction.
1777 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1778 break;
1779
Roland Levillaincff13742014-11-17 14:32:17 +00001780 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001781 // Processing a Dex `double-to-float' instruction.
1782 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001783 break;
1784
1785 default:
1786 LOG(FATAL) << "Unexpected type conversion from " << input_type
1787 << " to " << result_type;
1788 };
1789 break;
1790
Roland Levillaindff1f282014-11-05 14:15:05 +00001791 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001792 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001793 case Primitive::kPrimByte:
1794 case Primitive::kPrimShort:
1795 case Primitive::kPrimInt:
1796 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001797 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001799 break;
1800
1801 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001802 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001803 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001804 break;
1805
Roland Levillaincff13742014-11-17 14:32:17 +00001806 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001807 // Processing a Dex `float-to-double' instruction.
1808 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001809 break;
1810
1811 default:
1812 LOG(FATAL) << "Unexpected type conversion from " << input_type
1813 << " to " << result_type;
1814 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001815 break;
1816
1817 default:
1818 LOG(FATAL) << "Unexpected type conversion from " << input_type
1819 << " to " << result_type;
1820 }
1821}
1822
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001823void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001824 LocationSummary* locations =
1825 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001826 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001827 case Primitive::kPrimInt: {
1828 locations->SetInAt(0, Location::RequiresRegister());
1829 locations->SetInAt(1, Location::Any());
1830 locations->SetOut(Location::SameAsFirstInput());
1831 break;
1832 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001833
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001834 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001835 locations->SetInAt(0, Location::RequiresRegister());
1836 locations->SetInAt(1, Location::RequiresRegister());
1837 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001838 break;
1839 }
1840
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001841 case Primitive::kPrimDouble:
1842 case Primitive::kPrimFloat: {
1843 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001844 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001845 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001846 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001847 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001848
1849 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001850 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001851 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001852}
1853
1854void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1855 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 Location first = locations->InAt(0);
1857 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001859
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001860 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001861 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001862 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001863 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001864 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001865 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001866 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001869 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001870 break;
1871 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001872
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001873 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001874 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001875 break;
1876 }
1877
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001878 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001879 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001880 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 }
1882
1883 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001884 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 break;
1886 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001887
1888 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001890 }
1891}
1892
1893void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001894 LocationSummary* locations =
1895 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001896 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001897 case Primitive::kPrimInt: {
1898 locations->SetInAt(0, Location::RequiresRegister());
1899 locations->SetInAt(1, Location::Any());
1900 locations->SetOut(Location::SameAsFirstInput());
1901 break;
1902 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001903 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001904 locations->SetInAt(0, Location::RequiresRegister());
1905 locations->SetInAt(1, Location::RequiresRegister());
1906 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001907 break;
1908 }
Calin Juravle11351682014-10-23 15:38:15 +01001909 case Primitive::kPrimFloat:
1910 case Primitive::kPrimDouble: {
1911 locations->SetInAt(0, Location::RequiresFpuRegister());
1912 locations->SetInAt(1, Location::RequiresFpuRegister());
1913 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001914 break;
Calin Juravle11351682014-10-23 15:38:15 +01001915 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001916 default:
Calin Juravle11351682014-10-23 15:38:15 +01001917 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001918 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001919}
1920
1921void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1922 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001923 Location first = locations->InAt(0);
1924 Location second = locations->InAt(1);
1925 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001926 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001927 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001928 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001929 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001930 } else if (second.IsConstant()) {
1931 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001932 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001933 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001935 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001936 break;
1937 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001938 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001940 break;
1941 }
1942
Calin Juravle11351682014-10-23 15:38:15 +01001943 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001944 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001945 break;
Calin Juravle11351682014-10-23 15:38:15 +01001946 }
1947
1948 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001949 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001950 break;
1951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001952
1953 default:
Calin Juravle11351682014-10-23 15:38:15 +01001954 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001955 }
1956}
1957
Calin Juravle34bacdf2014-10-07 20:23:36 +01001958void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1959 LocationSummary* locations =
1960 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1961 switch (mul->GetResultType()) {
1962 case Primitive::kPrimInt: {
1963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetInAt(1, Location::Any());
1965 locations->SetOut(Location::SameAsFirstInput());
1966 break;
1967 }
1968 case Primitive::kPrimLong: {
1969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetInAt(1, Location::RequiresRegister());
1971 locations->SetOut(Location::SameAsFirstInput());
1972 break;
1973 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001974 case Primitive::kPrimFloat:
1975 case Primitive::kPrimDouble: {
1976 locations->SetInAt(0, Location::RequiresFpuRegister());
1977 locations->SetInAt(1, Location::RequiresFpuRegister());
1978 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001979 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001980 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001981
1982 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001983 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001984 }
1985}
1986
1987void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1988 LocationSummary* locations = mul->GetLocations();
1989 Location first = locations->InAt(0);
1990 Location second = locations->InAt(1);
1991 DCHECK(first.Equals(locations->Out()));
1992 switch (mul->GetResultType()) {
1993 case Primitive::kPrimInt: {
1994 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001995 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001996 } else if (second.IsConstant()) {
1997 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001998 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001999 } else {
2000 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002001 __ imull(first.AsRegister<CpuRegister>(),
2002 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002003 }
2004 break;
2005 }
2006 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002008 break;
2009 }
2010
Calin Juravleb5bfa962014-10-21 18:02:24 +01002011 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002013 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002014 }
2015
2016 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002017 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002018 break;
2019 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002020
2021 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002022 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002023 }
2024}
2025
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002026void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2027 uint32_t stack_adjustment, bool is_float) {
2028 if (source.IsStackSlot()) {
2029 DCHECK(is_float);
2030 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2031 } else if (source.IsDoubleStackSlot()) {
2032 DCHECK(!is_float);
2033 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2034 } else {
2035 // Write the value to the temporary location on the stack and load to FP stack.
2036 if (is_float) {
2037 Location stack_temp = Location::StackSlot(temp_offset);
2038 codegen_->Move(stack_temp, source);
2039 __ flds(Address(CpuRegister(RSP), temp_offset));
2040 } else {
2041 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2042 codegen_->Move(stack_temp, source);
2043 __ fldl(Address(CpuRegister(RSP), temp_offset));
2044 }
2045 }
2046}
2047
2048void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2049 Primitive::Type type = rem->GetResultType();
2050 bool is_float = type == Primitive::kPrimFloat;
2051 size_t elem_size = Primitive::ComponentSize(type);
2052 LocationSummary* locations = rem->GetLocations();
2053 Location first = locations->InAt(0);
2054 Location second = locations->InAt(1);
2055 Location out = locations->Out();
2056
2057 // Create stack space for 2 elements.
2058 // TODO: enhance register allocator to ask for stack temporaries.
2059 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2060
2061 // Load the values to the FP stack in reverse order, using temporaries if needed.
2062 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2063 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2064
2065 // Loop doing FPREM until we stabilize.
2066 Label retry;
2067 __ Bind(&retry);
2068 __ fprem();
2069
2070 // Move FP status to AX.
2071 __ fstsw();
2072
2073 // And see if the argument reduction is complete. This is signaled by the
2074 // C2 FPU flag bit set to 0.
2075 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2076 __ j(kNotEqual, &retry);
2077
2078 // We have settled on the final value. Retrieve it into an XMM register.
2079 // Store FP top of stack to real stack.
2080 if (is_float) {
2081 __ fsts(Address(CpuRegister(RSP), 0));
2082 } else {
2083 __ fstl(Address(CpuRegister(RSP), 0));
2084 }
2085
2086 // Pop the 2 items from the FP stack.
2087 __ fucompp();
2088
2089 // Load the value from the stack into an XMM register.
2090 DCHECK(out.IsFpuRegister()) << out;
2091 if (is_float) {
2092 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2093 } else {
2094 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2095 }
2096
2097 // And remove the temporary stack space we allocated.
2098 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2099}
2100
Calin Juravlebacfec32014-11-14 15:54:36 +00002101void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2102 DCHECK(instruction->IsDiv() || instruction->IsRem());
2103 Primitive::Type type = instruction->GetResultType();
2104 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2105
2106 bool is_div = instruction->IsDiv();
2107 LocationSummary* locations = instruction->GetLocations();
2108
Roland Levillain271ab9c2014-11-27 15:23:57 +00002109 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2110 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002111
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002113 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2114
2115 SlowPathCodeX86_64* slow_path =
2116 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2117 out_reg.AsRegister(), type, is_div);
2118 codegen_->AddSlowPath(slow_path);
2119
2120 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2121 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2122 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002123 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002124 __ cmpl(second_reg, Immediate(-1));
2125 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002126 // edx:eax <- sign-extended of eax
2127 __ cdq();
2128 // eax = quotient, edx = remainder
2129 __ idivl(second_reg);
2130 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002131 __ cmpq(second_reg, Immediate(-1));
2132 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002133 // rdx:rax <- sign-extended of rax
2134 __ cqo();
2135 // rax = quotient, rdx = remainder
2136 __ idivq(second_reg);
2137 }
2138
2139 __ Bind(slow_path->GetExitLabel());
2140}
2141
Calin Juravle7c4954d2014-10-28 16:57:40 +00002142void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2143 LocationSummary* locations =
2144 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2145 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002146 case Primitive::kPrimInt:
2147 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002148 locations->SetInAt(0, Location::RegisterLocation(RAX));
2149 locations->SetInAt(1, Location::RequiresRegister());
2150 locations->SetOut(Location::SameAsFirstInput());
2151 // Intel uses edx:eax as the dividend.
2152 locations->AddTemp(Location::RegisterLocation(RDX));
2153 break;
2154 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002155
Calin Juravle7c4954d2014-10-28 16:57:40 +00002156 case Primitive::kPrimFloat:
2157 case Primitive::kPrimDouble: {
2158 locations->SetInAt(0, Location::RequiresFpuRegister());
2159 locations->SetInAt(1, Location::RequiresFpuRegister());
2160 locations->SetOut(Location::SameAsFirstInput());
2161 break;
2162 }
2163
2164 default:
2165 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2166 }
2167}
2168
2169void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2170 LocationSummary* locations = div->GetLocations();
2171 Location first = locations->InAt(0);
2172 Location second = locations->InAt(1);
2173 DCHECK(first.Equals(locations->Out()));
2174
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002175 Primitive::Type type = div->GetResultType();
2176 switch (type) {
2177 case Primitive::kPrimInt:
2178 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002179 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002180 break;
2181 }
2182
Calin Juravle7c4954d2014-10-28 16:57:40 +00002183 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002184 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002185 break;
2186 }
2187
2188 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002189 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002190 break;
2191 }
2192
2193 default:
2194 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2195 }
2196}
2197
Calin Juravlebacfec32014-11-14 15:54:36 +00002198void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002199 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002200 LocationSummary* locations =
2201 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002202
2203 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002204 case Primitive::kPrimInt:
2205 case Primitive::kPrimLong: {
2206 locations->SetInAt(0, Location::RegisterLocation(RAX));
2207 locations->SetInAt(1, Location::RequiresRegister());
2208 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2209 locations->SetOut(Location::RegisterLocation(RDX));
2210 break;
2211 }
2212
2213 case Primitive::kPrimFloat:
2214 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002215 locations->SetInAt(0, Location::Any());
2216 locations->SetInAt(1, Location::Any());
2217 locations->SetOut(Location::RequiresFpuRegister());
2218 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002219 break;
2220 }
2221
2222 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002223 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002224 }
2225}
2226
2227void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2228 Primitive::Type type = rem->GetResultType();
2229 switch (type) {
2230 case Primitive::kPrimInt:
2231 case Primitive::kPrimLong: {
2232 GenerateDivRemIntegral(rem);
2233 break;
2234 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002235 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002236 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002237 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002238 break;
2239 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002240 default:
2241 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2242 }
2243}
2244
Calin Juravled0d48522014-11-04 16:40:20 +00002245void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2248 locations->SetInAt(0, Location::Any());
2249 if (instruction->HasUses()) {
2250 locations->SetOut(Location::SameAsFirstInput());
2251 }
2252}
2253
2254void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2255 SlowPathCodeX86_64* slow_path =
2256 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2257 codegen_->AddSlowPath(slow_path);
2258
2259 LocationSummary* locations = instruction->GetLocations();
2260 Location value = locations->InAt(0);
2261
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002262 switch (instruction->GetType()) {
2263 case Primitive::kPrimInt: {
2264 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002265 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002266 __ j(kEqual, slow_path->GetEntryLabel());
2267 } else if (value.IsStackSlot()) {
2268 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2269 __ j(kEqual, slow_path->GetEntryLabel());
2270 } else {
2271 DCHECK(value.IsConstant()) << value;
2272 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2273 __ jmp(slow_path->GetEntryLabel());
2274 }
2275 }
2276 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002277 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002278 case Primitive::kPrimLong: {
2279 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002280 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002281 __ j(kEqual, slow_path->GetEntryLabel());
2282 } else if (value.IsDoubleStackSlot()) {
2283 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2284 __ j(kEqual, slow_path->GetEntryLabel());
2285 } else {
2286 DCHECK(value.IsConstant()) << value;
2287 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2288 __ jmp(slow_path->GetEntryLabel());
2289 }
2290 }
2291 break;
2292 }
2293 default:
2294 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002295 }
Calin Juravled0d48522014-11-04 16:40:20 +00002296}
2297
Calin Juravle9aec02f2014-11-18 23:06:35 +00002298void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2299 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2300
2301 LocationSummary* locations =
2302 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2303
2304 switch (op->GetResultType()) {
2305 case Primitive::kPrimInt:
2306 case Primitive::kPrimLong: {
2307 locations->SetInAt(0, Location::RequiresRegister());
2308 // The shift count needs to be in CL.
2309 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2310 locations->SetOut(Location::SameAsFirstInput());
2311 break;
2312 }
2313 default:
2314 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2315 }
2316}
2317
2318void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2319 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2320
2321 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002322 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002323 Location second = locations->InAt(1);
2324
2325 switch (op->GetResultType()) {
2326 case Primitive::kPrimInt: {
2327 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002329 if (op->IsShl()) {
2330 __ shll(first_reg, second_reg);
2331 } else if (op->IsShr()) {
2332 __ sarl(first_reg, second_reg);
2333 } else {
2334 __ shrl(first_reg, second_reg);
2335 }
2336 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002337 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002338 if (op->IsShl()) {
2339 __ shll(first_reg, imm);
2340 } else if (op->IsShr()) {
2341 __ sarl(first_reg, imm);
2342 } else {
2343 __ shrl(first_reg, imm);
2344 }
2345 }
2346 break;
2347 }
2348 case Primitive::kPrimLong: {
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 __ shlq(first_reg, second_reg);
2353 } else if (op->IsShr()) {
2354 __ sarq(first_reg, second_reg);
2355 } else {
2356 __ shrq(first_reg, second_reg);
2357 }
2358 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002359 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002360 if (op->IsShl()) {
2361 __ shlq(first_reg, imm);
2362 } else if (op->IsShr()) {
2363 __ sarq(first_reg, imm);
2364 } else {
2365 __ shrq(first_reg, imm);
2366 }
2367 }
2368 break;
2369 }
2370 default:
2371 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2372 }
2373}
2374
2375void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2376 HandleShift(shl);
2377}
2378
2379void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2380 HandleShift(shl);
2381}
2382
2383void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2384 HandleShift(shr);
2385}
2386
2387void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2388 HandleShift(shr);
2389}
2390
2391void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2392 HandleShift(ushr);
2393}
2394
2395void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2396 HandleShift(ushr);
2397}
2398
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002399void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002400 LocationSummary* locations =
2401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002402 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002403 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2404 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2405 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002406}
2407
2408void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2409 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002410 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002411 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2412
2413 __ gs()->call(Address::Absolute(
2414 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2415
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002416 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002417 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002418}
2419
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002420void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2421 LocationSummary* locations =
2422 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2423 InvokeRuntimeCallingConvention calling_convention;
2424 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002425 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002426 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002427 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002428}
2429
2430void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2431 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002432 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002433 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2434
2435 __ gs()->call(Address::Absolute(
2436 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2437
2438 DCHECK(!codegen_->IsLeafMethod());
2439 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2440}
2441
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002442void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002443 LocationSummary* locations =
2444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002445 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2446 if (location.IsStackSlot()) {
2447 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2448 } else if (location.IsDoubleStackSlot()) {
2449 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2450 }
2451 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002452}
2453
2454void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2455 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002456 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002457}
2458
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002459void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002460 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002461 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002462 locations->SetInAt(0, Location::RequiresRegister());
2463 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002464}
2465
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002466void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2467 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2469 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002470 Location out = locations->Out();
2471 switch (not_->InputAt(0)->GetType()) {
2472 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002473 __ xorq(out.AsRegister<CpuRegister>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002474 break;
2475
2476 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002477 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002478 break;
2479
2480 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002481 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002482 break;
2483
2484 default:
2485 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2486 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002487}
2488
2489void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002490 LocationSummary* locations =
2491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002492 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2493 locations->SetInAt(i, Location::Any());
2494 }
2495 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002496}
2497
2498void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002499 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002500 LOG(FATAL) << "Unimplemented";
2501}
2502
Calin Juravle52c48962014-12-16 17:02:57 +00002503void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2504 /*
2505 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2506 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2507 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2508 */
2509 switch (kind) {
2510 case MemBarrierKind::kAnyAny: {
2511 __ mfence();
2512 break;
2513 }
2514 case MemBarrierKind::kAnyStore:
2515 case MemBarrierKind::kLoadAny:
2516 case MemBarrierKind::kStoreStore: {
2517 // nop
2518 break;
2519 }
2520 default:
2521 LOG(FATAL) << "Unexpected memory barier " << kind;
2522 }
2523}
2524
2525void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2526 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2527
Nicolas Geoffray39468442014-09-02 15:17:15 +01002528 LocationSummary* locations =
2529 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002530 locations->SetInAt(0, Location::RequiresRegister());
2531 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2532}
2533
2534void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2535 const FieldInfo& field_info) {
2536 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2537
2538 LocationSummary* locations = instruction->GetLocations();
2539 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2540 Location out = locations->Out();
2541 bool is_volatile = field_info.IsVolatile();
2542 Primitive::Type field_type = field_info.GetFieldType();
2543 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2544
2545 switch (field_type) {
2546 case Primitive::kPrimBoolean: {
2547 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2548 break;
2549 }
2550
2551 case Primitive::kPrimByte: {
2552 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2553 break;
2554 }
2555
2556 case Primitive::kPrimShort: {
2557 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2558 break;
2559 }
2560
2561 case Primitive::kPrimChar: {
2562 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2563 break;
2564 }
2565
2566 case Primitive::kPrimInt:
2567 case Primitive::kPrimNot: {
2568 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2569 break;
2570 }
2571
2572 case Primitive::kPrimLong: {
2573 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2574 break;
2575 }
2576
2577 case Primitive::kPrimFloat: {
2578 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2579 break;
2580 }
2581
2582 case Primitive::kPrimDouble: {
2583 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2584 break;
2585 }
2586
2587 case Primitive::kPrimVoid:
2588 LOG(FATAL) << "Unreachable type " << field_type;
2589 UNREACHABLE();
2590 }
2591
2592 if (is_volatile) {
2593 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2594 }
2595}
2596
2597void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2598 const FieldInfo& field_info) {
2599 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2600
2601 LocationSummary* locations =
2602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002603 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002604 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2605
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002608 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002609 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002610 locations->AddTemp(Location::RequiresRegister());
2611 locations->AddTemp(Location::RequiresRegister());
2612 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002613}
2614
Calin Juravle52c48962014-12-16 17:02:57 +00002615void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2616 const FieldInfo& field_info) {
2617 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2618
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002619 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002620 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2621 Location value = locations->InAt(1);
2622 bool is_volatile = field_info.IsVolatile();
2623 Primitive::Type field_type = field_info.GetFieldType();
2624 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2625
2626 if (is_volatile) {
2627 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2628 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002629
2630 switch (field_type) {
2631 case Primitive::kPrimBoolean:
2632 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002633 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002634 break;
2635 }
2636
2637 case Primitive::kPrimShort:
2638 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002639 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002640 break;
2641 }
2642
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002643 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002644 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002645 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
2646 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2648 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00002649 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002650 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002651 break;
2652 }
2653
2654 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002655 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002656 break;
2657 }
2658
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002659 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002660 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002661 break;
2662 }
2663
2664 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002665 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002666 break;
2667 }
2668
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002669 case Primitive::kPrimVoid:
2670 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002671 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002672 }
Calin Juravle52c48962014-12-16 17:02:57 +00002673
2674 if (is_volatile) {
2675 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2676 }
2677}
2678
2679void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2680 HandleFieldSet(instruction, instruction->GetFieldInfo());
2681}
2682
2683void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2684 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002685}
2686
2687void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002688 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002689}
2690
2691void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002692 HandleFieldGet(instruction, instruction->GetFieldInfo());
2693}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002694
Calin Juravle52c48962014-12-16 17:02:57 +00002695void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2696 HandleFieldGet(instruction);
2697}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698
Calin Juravle52c48962014-12-16 17:02:57 +00002699void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2700 HandleFieldGet(instruction, instruction->GetFieldInfo());
2701}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702
Calin Juravle52c48962014-12-16 17:02:57 +00002703void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2704 HandleFieldSet(instruction, instruction->GetFieldInfo());
2705}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002706
Calin Juravle52c48962014-12-16 17:02:57 +00002707void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2708 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709}
2710
2711void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002712 LocationSummary* locations =
2713 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002714 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2715 ? Location::RequiresRegister()
2716 : Location::Any();
2717 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002718 if (instruction->HasUses()) {
2719 locations->SetOut(Location::SameAsFirstInput());
2720 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002721}
2722
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002723void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
2724 LocationSummary* locations = instruction->GetLocations();
2725 Location obj = locations->InAt(0);
2726
2727 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2728 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2729}
2730
2731void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002732 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002733 codegen_->AddSlowPath(slow_path);
2734
2735 LocationSummary* locations = instruction->GetLocations();
2736 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002737
2738 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002739 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002740 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002742 } else {
2743 DCHECK(obj.IsConstant()) << obj;
2744 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2745 __ jmp(slow_path->GetEntryLabel());
2746 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002747 }
2748 __ j(kEqual, slow_path->GetEntryLabel());
2749}
2750
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002751void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2752 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2753 GenerateImplicitNullCheck(instruction);
2754 } else {
2755 GenerateExplicitNullCheck(instruction);
2756 }
2757}
2758
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002759void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002762 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002763 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002764 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002766}
2767
2768void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2769 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002771 Location index = locations->InAt(1);
2772
2773 switch (instruction->GetType()) {
2774 case Primitive::kPrimBoolean: {
2775 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002777 if (index.IsConstant()) {
2778 __ movzxb(out, Address(obj,
2779 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2780 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002782 }
2783 break;
2784 }
2785
2786 case Primitive::kPrimByte: {
2787 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002788 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002789 if (index.IsConstant()) {
2790 __ movsxb(out, Address(obj,
2791 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2792 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002794 }
2795 break;
2796 }
2797
2798 case Primitive::kPrimShort: {
2799 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002800 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002801 if (index.IsConstant()) {
2802 __ movsxw(out, Address(obj,
2803 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2804 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002805 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002806 }
2807 break;
2808 }
2809
2810 case Primitive::kPrimChar: {
2811 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002812 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002813 if (index.IsConstant()) {
2814 __ movzxw(out, Address(obj,
2815 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2816 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002817 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002818 }
2819 break;
2820 }
2821
2822 case Primitive::kPrimInt:
2823 case Primitive::kPrimNot: {
2824 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_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 __ movl(out, Address(obj,
2829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2830 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002832 }
2833 break;
2834 }
2835
2836 case Primitive::kPrimLong: {
2837 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_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 __ movq(out, Address(obj,
2841 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002843 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002844 }
2845 break;
2846 }
2847
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002848 case Primitive::kPrimFloat: {
2849 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002850 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002851 if (index.IsConstant()) {
2852 __ movss(out, Address(obj,
2853 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2854 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002856 }
2857 break;
2858 }
2859
2860 case Primitive::kPrimDouble: {
2861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002862 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002863 if (index.IsConstant()) {
2864 __ movsd(out, Address(obj,
2865 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2866 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002867 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002868 }
2869 break;
2870 }
2871
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002872 case Primitive::kPrimVoid:
2873 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002874 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002875 }
2876}
2877
2878void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002879 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002880
2881 bool needs_write_barrier =
2882 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2883 bool needs_runtime_call = instruction->NeedsTypeCheck();
2884
Nicolas Geoffray39468442014-09-02 15:17:15 +01002885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002886 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2887 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002888 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002889 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2890 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2891 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002892 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002893 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002894 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002895 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2896 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002897 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002898 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002899 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2900 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002901 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002902 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002903 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002904
2905 if (needs_write_barrier) {
2906 // Temporary registers for the write barrier.
2907 locations->AddTemp(Location::RequiresRegister());
2908 locations->AddTemp(Location::RequiresRegister());
2909 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002910 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002911}
2912
2913void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2914 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002915 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002916 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002917 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002918 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002919 bool needs_runtime_call = locations->WillCall();
2920 bool needs_write_barrier =
2921 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002922
2923 switch (value_type) {
2924 case Primitive::kPrimBoolean:
2925 case Primitive::kPrimByte: {
2926 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927 if (index.IsConstant()) {
2928 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002929 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002930 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002931 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002932 __ movb(Address(obj, offset),
2933 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002934 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002935 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002936 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002937 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2938 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002939 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002940 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002941 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2942 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002943 }
2944 break;
2945 }
2946
2947 case Primitive::kPrimShort:
2948 case Primitive::kPrimChar: {
2949 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002950 if (index.IsConstant()) {
2951 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002952 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002953 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002954 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002955 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002956 __ movw(Address(obj, offset),
2957 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002958 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002959 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002960 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002961 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002962 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2963 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002964 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002965 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002966 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002967 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2968 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002969 }
2970 break;
2971 }
2972
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002973 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002974 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002975 if (!needs_runtime_call) {
2976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2977 if (index.IsConstant()) {
2978 size_t offset =
2979 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2980 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002982 } else {
2983 DCHECK(value.IsConstant()) << value;
2984 __ movl(Address(obj, offset),
2985 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2986 }
2987 } else {
2988 DCHECK(index.IsRegister()) << index;
2989 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002990 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2991 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002992 } else {
2993 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002994 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002995 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2996 }
2997 }
2998
2999 if (needs_write_barrier) {
3000 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3002 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3003 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003004 }
3005 } else {
3006 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003007 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3008 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003009 DCHECK(!codegen_->IsLeafMethod());
3010 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3011 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003012 break;
3013 }
3014
3015 case Primitive::kPrimLong: {
3016 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003017 if (index.IsConstant()) {
3018 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003019 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003021 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003022 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3024 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003025 }
3026 break;
3027 }
3028
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003029 case Primitive::kPrimFloat: {
3030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3031 if (index.IsConstant()) {
3032 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3033 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003035 } else {
3036 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3038 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003039 }
3040 break;
3041 }
3042
3043 case Primitive::kPrimDouble: {
3044 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3045 if (index.IsConstant()) {
3046 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3047 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003048 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003049 } else {
3050 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3052 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003053 }
3054 break;
3055 }
3056
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003057 case Primitive::kPrimVoid:
3058 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003059 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003060 }
3061}
3062
3063void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003064 LocationSummary* locations =
3065 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003066 locations->SetInAt(0, Location::RequiresRegister());
3067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003068}
3069
3070void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3071 LocationSummary* locations = instruction->GetLocations();
3072 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003073 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3074 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003075 __ movl(out, Address(obj, offset));
3076}
3077
3078void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003079 LocationSummary* locations =
3080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003081 locations->SetInAt(0, Location::RequiresRegister());
3082 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003083 if (instruction->HasUses()) {
3084 locations->SetOut(Location::SameAsFirstInput());
3085 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003086}
3087
3088void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3089 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003090 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003091 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003092 codegen_->AddSlowPath(slow_path);
3093
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3095 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003096
3097 __ cmpl(index, length);
3098 __ j(kAboveEqual, slow_path->GetEntryLabel());
3099}
3100
3101void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3102 CpuRegister card,
3103 CpuRegister object,
3104 CpuRegister value) {
3105 Label is_null;
3106 __ testl(value, value);
3107 __ j(kEqual, &is_null);
3108 __ gs()->movq(card, Address::Absolute(
3109 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3110 __ movq(temp, object);
3111 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3112 __ movb(Address(temp, card, TIMES_1, 0), card);
3113 __ Bind(&is_null);
3114}
3115
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003116void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3117 temp->SetLocations(nullptr);
3118}
3119
3120void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3121 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003122 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003123}
3124
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003125void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003126 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003127 LOG(FATAL) << "Unimplemented";
3128}
3129
3130void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003131 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3132}
3133
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003134void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3136}
3137
3138void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003139 HBasicBlock* block = instruction->GetBlock();
3140 if (block->GetLoopInformation() != nullptr) {
3141 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3142 // The back edge will generate the suspend check.
3143 return;
3144 }
3145 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3146 // The goto will generate the suspend check.
3147 return;
3148 }
3149 GenerateSuspendCheck(instruction, nullptr);
3150}
3151
3152void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3153 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003154 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003155 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003156 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003157 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003158 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003159 if (successor == nullptr) {
3160 __ j(kNotEqual, slow_path->GetEntryLabel());
3161 __ Bind(slow_path->GetReturnLabel());
3162 } else {
3163 __ j(kEqual, codegen_->GetLabelOf(successor));
3164 __ jmp(slow_path->GetEntryLabel());
3165 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003166}
3167
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003168X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3169 return codegen_->GetAssembler();
3170}
3171
3172void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3173 MoveOperands* move = moves_.Get(index);
3174 Location source = move->GetSource();
3175 Location destination = move->GetDestination();
3176
3177 if (source.IsRegister()) {
3178 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003179 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003180 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003181 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003183 } else {
3184 DCHECK(destination.IsDoubleStackSlot());
3185 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003187 }
3188 } else if (source.IsStackSlot()) {
3189 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003190 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003191 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003192 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003194 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003195 } else {
3196 DCHECK(destination.IsStackSlot());
3197 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3198 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3199 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003200 } else if (source.IsDoubleStackSlot()) {
3201 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003203 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003204 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003205 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3206 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003207 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003208 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003209 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3210 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3211 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003212 } else if (source.IsConstant()) {
3213 HConstant* constant = source.GetConstant();
3214 if (constant->IsIntConstant()) {
3215 Immediate imm(constant->AsIntConstant()->GetValue());
3216 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003218 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003219 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003220 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3221 }
3222 } else if (constant->IsLongConstant()) {
3223 int64_t value = constant->AsLongConstant()->GetValue();
3224 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003226 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003227 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003228 __ movq(CpuRegister(TMP), Immediate(value));
3229 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3230 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003231 } else if (constant->IsFloatConstant()) {
3232 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3233 if (destination.IsFpuRegister()) {
3234 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003235 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003236 } else {
3237 DCHECK(destination.IsStackSlot()) << destination;
3238 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3239 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003240 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003241 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3242 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3243 if (destination.IsFpuRegister()) {
3244 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003246 } else {
3247 DCHECK(destination.IsDoubleStackSlot()) << destination;
3248 __ movq(CpuRegister(TMP), imm);
3249 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3250 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003251 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003252 } else if (source.IsFpuRegister()) {
3253 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003254 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003255 } else if (destination.IsStackSlot()) {
3256 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003257 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003258 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003259 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003260 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003261 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003262 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003263 }
3264}
3265
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003266void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003267 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003268 __ movl(Address(CpuRegister(RSP), mem), reg);
3269 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003270}
3271
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003272void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003273 ScratchRegisterScope ensure_scratch(
3274 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3275
3276 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3277 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3278 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3279 Address(CpuRegister(RSP), mem2 + stack_offset));
3280 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3281 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3282 CpuRegister(ensure_scratch.GetRegister()));
3283}
3284
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003285void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3286 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3287 __ movq(Address(CpuRegister(RSP), mem), reg);
3288 __ movq(reg, CpuRegister(TMP));
3289}
3290
3291void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3292 ScratchRegisterScope ensure_scratch(
3293 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3294
3295 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3296 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3297 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3298 Address(CpuRegister(RSP), mem2 + stack_offset));
3299 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3300 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3301 CpuRegister(ensure_scratch.GetRegister()));
3302}
3303
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003304void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3305 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3306 __ movss(Address(CpuRegister(RSP), mem), reg);
3307 __ movd(reg, CpuRegister(TMP));
3308}
3309
3310void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3311 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3312 __ movsd(Address(CpuRegister(RSP), mem), reg);
3313 __ movd(reg, CpuRegister(TMP));
3314}
3315
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003316void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3317 MoveOperands* move = moves_.Get(index);
3318 Location source = move->GetSource();
3319 Location destination = move->GetDestination();
3320
3321 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003322 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003323 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003325 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003327 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003328 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3329 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003330 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003331 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003332 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003333 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3334 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003335 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3337 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3338 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003339 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003340 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003341 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003342 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003343 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003345 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003346 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003347 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003348 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003349 }
3350}
3351
3352
3353void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3354 __ pushq(CpuRegister(reg));
3355}
3356
3357
3358void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3359 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003360}
3361
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003362void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3363 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3364 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3365 Immediate(mirror::Class::kStatusInitialized));
3366 __ j(kLess, slow_path->GetEntryLabel());
3367 __ Bind(slow_path->GetExitLabel());
3368 // No need for memory fence, thanks to the X86_64 memory model.
3369}
3370
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003371void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003372 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3373 ? LocationSummary::kCallOnSlowPath
3374 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003375 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003376 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003377 locations->SetOut(Location::RequiresRegister());
3378}
3379
3380void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003381 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003382 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003383 DCHECK(!cls->CanCallRuntime());
3384 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003385 codegen_->LoadCurrentMethod(out);
3386 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3387 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003388 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003389 codegen_->LoadCurrentMethod(out);
3390 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3391 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003392 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3393 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3394 codegen_->AddSlowPath(slow_path);
3395 __ testl(out, out);
3396 __ j(kEqual, slow_path->GetEntryLabel());
3397 if (cls->MustGenerateClinitCheck()) {
3398 GenerateClassInitializationCheck(slow_path, out);
3399 } else {
3400 __ Bind(slow_path->GetExitLabel());
3401 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003402 }
3403}
3404
3405void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3406 LocationSummary* locations =
3407 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3408 locations->SetInAt(0, Location::RequiresRegister());
3409 if (check->HasUses()) {
3410 locations->SetOut(Location::SameAsFirstInput());
3411 }
3412}
3413
3414void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003415 // We assume the class to not be null.
3416 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3417 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003418 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003419 GenerateClassInitializationCheck(slow_path,
3420 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003421}
3422
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003423void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3424 LocationSummary* locations =
3425 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3426 locations->SetOut(Location::RequiresRegister());
3427}
3428
3429void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3430 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3431 codegen_->AddSlowPath(slow_path);
3432
Roland Levillain271ab9c2014-11-27 15:23:57 +00003433 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003434 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003435 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3436 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003437 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3438 __ testl(out, out);
3439 __ j(kEqual, slow_path->GetEntryLabel());
3440 __ Bind(slow_path->GetExitLabel());
3441}
3442
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003443void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3444 LocationSummary* locations =
3445 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3446 locations->SetOut(Location::RequiresRegister());
3447}
3448
3449void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3450 Address address = Address::Absolute(
3451 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003452 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003453 __ gs()->movl(address, Immediate(0));
3454}
3455
3456void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3457 LocationSummary* locations =
3458 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3459 InvokeRuntimeCallingConvention calling_convention;
3460 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3461}
3462
3463void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3464 __ gs()->call(
3465 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3466 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3467}
3468
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003469void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003470 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3471 ? LocationSummary::kNoCall
3472 : LocationSummary::kCallOnSlowPath;
3473 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3474 locations->SetInAt(0, Location::RequiresRegister());
3475 locations->SetInAt(1, Location::Any());
3476 locations->SetOut(Location::RequiresRegister());
3477}
3478
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003479void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003480 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003482 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003483 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003484 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3485 Label done, zero;
3486 SlowPathCodeX86_64* slow_path = nullptr;
3487
3488 // Return 0 if `obj` is null.
3489 // TODO: avoid this check if we know obj is not null.
3490 __ testl(obj, obj);
3491 __ j(kEqual, &zero);
3492 // Compare the class of `obj` with `cls`.
3493 __ movl(out, Address(obj, class_offset));
3494 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003495 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003496 } else {
3497 DCHECK(cls.IsStackSlot()) << cls;
3498 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3499 }
3500 if (instruction->IsClassFinal()) {
3501 // Classes must be equal for the instanceof to succeed.
3502 __ j(kNotEqual, &zero);
3503 __ movl(out, Immediate(1));
3504 __ jmp(&done);
3505 } else {
3506 // If the classes are not equal, we go into a slow path.
3507 DCHECK(locations->OnlyCallsOnSlowPath());
3508 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003509 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003510 codegen_->AddSlowPath(slow_path);
3511 __ j(kNotEqual, slow_path->GetEntryLabel());
3512 __ movl(out, Immediate(1));
3513 __ jmp(&done);
3514 }
3515 __ Bind(&zero);
3516 __ movl(out, Immediate(0));
3517 if (slow_path != nullptr) {
3518 __ Bind(slow_path->GetExitLabel());
3519 }
3520 __ Bind(&done);
3521}
3522
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003523void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3525 instruction, LocationSummary::kCallOnSlowPath);
3526 locations->SetInAt(0, Location::RequiresRegister());
3527 locations->SetInAt(1, Location::Any());
3528 locations->AddTemp(Location::RequiresRegister());
3529}
3530
3531void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3532 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003533 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003534 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003535 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003536 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3537 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3538 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3539 codegen_->AddSlowPath(slow_path);
3540
3541 // TODO: avoid this check if we know obj is not null.
3542 __ testl(obj, obj);
3543 __ j(kEqual, slow_path->GetExitLabel());
3544 // Compare the class of `obj` with `cls`.
3545 __ movl(temp, Address(obj, class_offset));
3546 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003547 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003548 } else {
3549 DCHECK(cls.IsStackSlot()) << cls;
3550 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3551 }
3552 // Classes must be equal for the checkcast to succeed.
3553 __ j(kNotEqual, slow_path->GetEntryLabel());
3554 __ Bind(slow_path->GetExitLabel());
3555}
3556
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003557void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3558 LocationSummary* locations =
3559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3560 InvokeRuntimeCallingConvention calling_convention;
3561 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3562}
3563
3564void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3565 __ gs()->call(Address::Absolute(instruction->IsEnter()
3566 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3567 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3568 true));
3569 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3570}
3571
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003572void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3573void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3574void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3575
3576void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3577 LocationSummary* locations =
3578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3579 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3580 || instruction->GetResultType() == Primitive::kPrimLong);
3581 locations->SetInAt(0, Location::RequiresRegister());
3582 if (instruction->GetType() == Primitive::kPrimInt) {
3583 locations->SetInAt(1, Location::Any());
3584 } else {
3585 // Request a register to avoid loading a 64bits constant.
3586 locations->SetInAt(1, Location::RequiresRegister());
3587 }
3588 locations->SetOut(Location::SameAsFirstInput());
3589}
3590
3591void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3592 HandleBitwiseOperation(instruction);
3593}
3594
3595void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3596 HandleBitwiseOperation(instruction);
3597}
3598
3599void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3600 HandleBitwiseOperation(instruction);
3601}
3602
3603void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3604 LocationSummary* locations = instruction->GetLocations();
3605 Location first = locations->InAt(0);
3606 Location second = locations->InAt(1);
3607 DCHECK(first.Equals(locations->Out()));
3608
3609 if (instruction->GetResultType() == Primitive::kPrimInt) {
3610 if (second.IsRegister()) {
3611 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003613 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003615 } else {
3616 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003617 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003618 }
3619 } else if (second.IsConstant()) {
3620 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3621 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003622 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003623 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003625 } else {
3626 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003628 }
3629 } else {
3630 Address address(CpuRegister(RSP), second.GetStackIndex());
3631 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003633 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003635 } else {
3636 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003638 }
3639 }
3640 } else {
3641 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3642 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003644 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003646 } else {
3647 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003649 }
3650 }
3651}
3652
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003653} // namespace x86_64
3654} // namespace art