blob: 723573afa01206aa1957cfdf7daecb0c7f5cb8ca [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 Geoffrayf6e206c2014-08-07 20:25:41 +010037static constexpr bool kExplicitStackOverflowCheck = false;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038
39// Some x86_64 instructions require a register to be available as temp.
40static constexpr Register TMP = R11;
41
42static constexpr int kNumberOfPushedRegistersAtEntry = 1;
43static constexpr int kCurrentMethodStackOffset = 0;
44
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
46static constexpr size_t kRuntimeParameterCoreRegistersLength =
47 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000048static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
49static constexpr size_t kRuntimeParameterFpuRegistersLength =
50 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
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);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100490
491 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
492 __ testq(CpuRegister(RAX), Address(
493 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100494 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100495 }
496
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100497 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100498 __ subq(CpuRegister(RSP),
499 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
500
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100501 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100502 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100503 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100504
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100505 __ gs()->cmpq(CpuRegister(RSP),
506 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
507 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100508 }
509
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100510 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
511}
512
513void CodeGeneratorX86_64::GenerateFrameExit() {
514 __ addq(CpuRegister(RSP),
515 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
516}
517
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100518void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
519 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100520}
521
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100522void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100523 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
524}
525
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100526Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
527 switch (load->GetType()) {
528 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100529 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100530 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
531 break;
532
533 case Primitive::kPrimInt:
534 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100535 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100536 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100537
538 case Primitive::kPrimBoolean:
539 case Primitive::kPrimByte:
540 case Primitive::kPrimChar:
541 case Primitive::kPrimShort:
542 case Primitive::kPrimVoid:
543 LOG(FATAL) << "Unexpected type " << load->GetType();
544 }
545
546 LOG(FATAL) << "Unreachable";
547 return Location();
548}
549
550void CodeGeneratorX86_64::Move(Location destination, Location source) {
551 if (source.Equals(destination)) {
552 return;
553 }
554 if (destination.IsRegister()) {
555 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000556 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000558 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100559 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000560 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100561 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100562 } else {
563 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000564 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100565 Address(CpuRegister(RSP), source.GetStackIndex()));
566 }
567 } else if (destination.IsFpuRegister()) {
568 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000569 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100570 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000571 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100572 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000573 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 Address(CpuRegister(RSP), source.GetStackIndex()));
575 } else {
576 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579 }
580 } else if (destination.IsStackSlot()) {
581 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 } else if (source.IsFpuRegister()) {
585 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100587 } else {
588 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000589 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
590 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 }
592 } else {
593 DCHECK(destination.IsDoubleStackSlot());
594 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100595 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000596 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100597 } else if (source.IsFpuRegister()) {
598 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000599 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 } else {
601 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000602 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
603 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100604 }
605 }
606}
607
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100608void CodeGeneratorX86_64::Move(HInstruction* instruction,
609 Location location,
610 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000611 LocationSummary* locations = instruction->GetLocations();
612 if (locations != nullptr && locations->Out().Equals(location)) {
613 return;
614 }
615
616 if (locations != nullptr && locations->Out().IsConstant()) {
617 HConstant* const_to_move = locations->Out().GetConstant();
618 if (const_to_move->IsIntConstant()) {
619 Immediate imm(const_to_move->AsIntConstant()->GetValue());
620 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000622 } else if (location.IsStackSlot()) {
623 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
624 } else {
625 DCHECK(location.IsConstant());
626 DCHECK_EQ(location.GetConstant(), const_to_move);
627 }
628 } else if (const_to_move->IsLongConstant()) {
629 int64_t value = const_to_move->AsLongConstant()->GetValue();
630 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000632 } else if (location.IsDoubleStackSlot()) {
633 __ movq(CpuRegister(TMP), Immediate(value));
634 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
635 } else {
636 DCHECK(location.IsConstant());
637 DCHECK_EQ(location.GetConstant(), const_to_move);
638 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100639 }
Roland Levillain476df552014-10-09 17:51:36 +0100640 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641 switch (instruction->GetType()) {
642 case Primitive::kPrimBoolean:
643 case Primitive::kPrimByte:
644 case Primitive::kPrimChar:
645 case Primitive::kPrimShort:
646 case Primitive::kPrimInt:
647 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100648 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100649 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
650 break;
651
652 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100653 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000654 Move(location,
655 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100656 break;
657
658 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100659 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100660 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000661 } else if (instruction->IsTemporary()) {
662 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
663 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100664 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100665 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100666 switch (instruction->GetType()) {
667 case Primitive::kPrimBoolean:
668 case Primitive::kPrimByte:
669 case Primitive::kPrimChar:
670 case Primitive::kPrimShort:
671 case Primitive::kPrimInt:
672 case Primitive::kPrimNot:
673 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 case Primitive::kPrimFloat:
675 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000676 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100677 break;
678
679 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100680 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100681 }
682 }
683}
684
685void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
686 got->SetLocations(nullptr);
687}
688
689void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
690 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100691 DCHECK(!successor->IsExitBlock());
692
693 HBasicBlock* block = got->GetBlock();
694 HInstruction* previous = got->GetPrevious();
695
696 HLoopInformation* info = block->GetLoopInformation();
697 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
698 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
699 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
700 return;
701 }
702
703 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
704 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
705 }
706 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100707 __ jmp(codegen_->GetLabelOf(successor));
708 }
709}
710
711void LocationsBuilderX86_64::VisitExit(HExit* exit) {
712 exit->SetLocations(nullptr);
713}
714
715void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700716 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 if (kIsDebugBuild) {
718 __ Comment("Unreachable");
719 __ int3();
720 }
721}
722
723void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100724 LocationSummary* locations =
725 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100726 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100727 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100728 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100729 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730}
731
732void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700733 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100734 if (cond->IsIntConstant()) {
735 // Constant condition, statically compared against 1.
736 int32_t cond_value = cond->AsIntConstant()->GetValue();
737 if (cond_value == 1) {
738 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
739 if_instr->IfTrueSuccessor())) {
740 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100741 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100742 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100743 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100744 DCHECK_EQ(cond_value, 0);
745 }
746 } else {
747 bool materialized =
748 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
749 // Moves do not affect the eflags register, so if the condition is
750 // evaluated just before the if, we don't need to evaluate it
751 // again.
752 bool eflags_set = cond->IsCondition()
753 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
754 if (materialized) {
755 if (!eflags_set) {
756 // Materialized condition, compare against 0.
757 Location lhs = if_instr->GetLocations()->InAt(0);
758 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000759 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100760 } else {
761 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
762 Immediate(0));
763 }
764 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
765 } else {
766 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
767 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
768 }
769 } else {
770 Location lhs = cond->GetLocations()->InAt(0);
771 Location rhs = cond->GetLocations()->InAt(1);
772 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000773 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100774 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000775 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100776 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
777 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000778 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100779 Address(CpuRegister(RSP), rhs.GetStackIndex()));
780 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100781 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
782 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700783 }
Dave Allison20dfc792014-06-16 20:44:29 -0700784 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100785 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
786 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700787 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100788 }
789}
790
791void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
792 local->SetLocations(nullptr);
793}
794
795void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
796 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
797}
798
799void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
800 local->SetLocations(nullptr);
801}
802
803void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
804 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700805 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100806}
807
808void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100809 LocationSummary* locations =
810 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100811 switch (store->InputAt(1)->GetType()) {
812 case Primitive::kPrimBoolean:
813 case Primitive::kPrimByte:
814 case Primitive::kPrimChar:
815 case Primitive::kPrimShort:
816 case Primitive::kPrimInt:
817 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100818 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100819 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
820 break;
821
822 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100823 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100824 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
825 break;
826
827 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100828 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100829 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100830}
831
832void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700833 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100834}
835
Dave Allison20dfc792014-06-16 20:44:29 -0700836void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100837 LocationSummary* locations =
838 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100839 locations->SetInAt(0, Location::RequiresRegister());
840 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100841 if (comp->NeedsMaterialization()) {
842 locations->SetOut(Location::RequiresRegister());
843 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100844}
845
Dave Allison20dfc792014-06-16 20:44:29 -0700846void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
847 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100848 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000849 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100850 // Clear register: setcc only sets the low byte.
851 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100852 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000853 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
854 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100855 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000856 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100857 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
858 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000859 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100860 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
861 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100862 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700863 }
864}
865
866void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
867 VisitCondition(comp);
868}
869
870void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
871 VisitCondition(comp);
872}
873
874void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
875 VisitCondition(comp);
876}
877
878void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
879 VisitCondition(comp);
880}
881
882void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
883 VisitCondition(comp);
884}
885
886void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
887 VisitCondition(comp);
888}
889
890void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
891 VisitCondition(comp);
892}
893
894void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
895 VisitCondition(comp);
896}
897
898void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
899 VisitCondition(comp);
900}
901
902void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
903 VisitCondition(comp);
904}
905
906void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
907 VisitCondition(comp);
908}
909
910void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
911 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100912}
913
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100914void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100915 LocationSummary* locations =
916 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000917 switch (compare->InputAt(0)->GetType()) {
918 case Primitive::kPrimLong: {
919 locations->SetInAt(0, Location::RequiresRegister());
920 locations->SetInAt(1, Location::RequiresRegister());
921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
922 break;
923 }
924 case Primitive::kPrimFloat:
925 case Primitive::kPrimDouble: {
926 locations->SetInAt(0, Location::RequiresFpuRegister());
927 locations->SetInAt(1, Location::RequiresFpuRegister());
928 locations->SetOut(Location::RequiresRegister());
929 break;
930 }
931 default:
932 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
933 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100934}
935
936void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100937 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000938 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000939 Location left = locations->InAt(0);
940 Location right = locations->InAt(1);
941
942 Label less, greater, done;
943 Primitive::Type type = compare->InputAt(0)->GetType();
944 switch (type) {
945 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000946 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100947 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000948 }
949 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000950 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000951 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
952 break;
953 }
954 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000955 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000956 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
957 break;
958 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100959 default:
Calin Juravleddb7df22014-11-25 20:56:51 +0000960 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100961 }
Calin Juravleddb7df22014-11-25 20:56:51 +0000962 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +0000963 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +0000964 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +0000965
Calin Juravle91debbc2014-11-26 19:01:09 +0000966 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +0000967 __ movl(out, Immediate(1));
968 __ jmp(&done);
969
970 __ Bind(&less);
971 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100972
973 __ Bind(&done);
974}
975
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100976void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100977 LocationSummary* locations =
978 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100979 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100980}
981
982void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100983 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700984 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100985}
986
987void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100988 LocationSummary* locations =
989 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100990 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100991}
992
993void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100994 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700995 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100996}
997
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100998void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
999 LocationSummary* locations =
1000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1001 locations->SetOut(Location::ConstantLocation(constant));
1002}
1003
1004void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1005 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001007}
1008
1009void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1010 LocationSummary* locations =
1011 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1012 locations->SetOut(Location::ConstantLocation(constant));
1013}
1014
1015void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1016 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001017 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001018}
1019
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001020void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1021 ret->SetLocations(nullptr);
1022}
1023
1024void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001025 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001026 codegen_->GenerateFrameExit();
1027 __ ret();
1028}
1029
1030void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001031 LocationSummary* locations =
1032 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033 switch (ret->InputAt(0)->GetType()) {
1034 case Primitive::kPrimBoolean:
1035 case Primitive::kPrimByte:
1036 case Primitive::kPrimChar:
1037 case Primitive::kPrimShort:
1038 case Primitive::kPrimInt:
1039 case Primitive::kPrimNot:
1040 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001041 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042 break;
1043
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 case Primitive::kPrimFloat:
1045 case Primitive::kPrimDouble:
1046 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 break;
1049
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001050 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001053}
1054
1055void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1056 if (kIsDebugBuild) {
1057 switch (ret->InputAt(0)->GetType()) {
1058 case Primitive::kPrimBoolean:
1059 case Primitive::kPrimByte:
1060 case Primitive::kPrimChar:
1061 case Primitive::kPrimShort:
1062 case Primitive::kPrimInt:
1063 case Primitive::kPrimNot:
1064 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001065 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001066 break;
1067
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 case Primitive::kPrimFloat:
1069 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001070 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001071 XMM0);
1072 break;
1073
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001074 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001076 }
1077 }
1078 codegen_->GenerateFrameExit();
1079 __ ret();
1080}
1081
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001082Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1083 switch (type) {
1084 case Primitive::kPrimBoolean:
1085 case Primitive::kPrimByte:
1086 case Primitive::kPrimChar:
1087 case Primitive::kPrimShort:
1088 case Primitive::kPrimInt:
1089 case Primitive::kPrimNot: {
1090 uint32_t index = gp_index_++;
1091 stack_index_++;
1092 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001093 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094 } else {
1095 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1096 }
1097 }
1098
1099 case Primitive::kPrimLong: {
1100 uint32_t index = gp_index_;
1101 stack_index_ += 2;
1102 if (index < calling_convention.GetNumberOfRegisters()) {
1103 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001104 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105 } else {
1106 gp_index_ += 2;
1107 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1108 }
1109 }
1110
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001111 case Primitive::kPrimFloat: {
1112 uint32_t index = fp_index_++;
1113 stack_index_++;
1114 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001115 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 } else {
1117 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1118 }
1119 }
1120
1121 case Primitive::kPrimDouble: {
1122 uint32_t index = fp_index_++;
1123 stack_index_ += 2;
1124 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001125 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001126 } else {
1127 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1128 }
1129 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130
1131 case Primitive::kPrimVoid:
1132 LOG(FATAL) << "Unexpected parameter type " << type;
1133 break;
1134 }
1135 return Location();
1136}
1137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001138void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001139 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1140 if (intrinsic.TryDispatch(invoke)) {
1141 return;
1142 }
1143
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001144 HandleInvoke(invoke);
1145}
1146
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001147static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1148 if (invoke->GetLocations()->Intrinsified()) {
1149 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1150 intrinsic.Dispatch(invoke);
1151 return true;
1152 }
1153 return false;
1154}
1155
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001156void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001157 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1158 return;
1159 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001160
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001161 codegen_->GenerateStaticOrDirectCall(
1162 invoke,
1163 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001164}
1165
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001166void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001167 LocationSummary* locations =
1168 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001169 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001170
1171 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001172 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001173 HInstruction* input = invoke->InputAt(i);
1174 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1175 }
1176
1177 switch (invoke->GetType()) {
1178 case Primitive::kPrimBoolean:
1179 case Primitive::kPrimByte:
1180 case Primitive::kPrimChar:
1181 case Primitive::kPrimShort:
1182 case Primitive::kPrimInt:
1183 case Primitive::kPrimNot:
1184 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001185 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001186 break;
1187
1188 case Primitive::kPrimVoid:
1189 break;
1190
1191 case Primitive::kPrimDouble:
1192 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001193 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001194 break;
1195 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196}
1197
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001198void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001199 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1200 if (intrinsic.TryDispatch(invoke)) {
1201 return;
1202 }
1203
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001204 HandleInvoke(invoke);
1205}
1206
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001207void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001208 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1209 return;
1210 }
1211
Roland Levillain271ab9c2014-11-27 15:23:57 +00001212 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001213 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1214 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1215 LocationSummary* locations = invoke->GetLocations();
1216 Location receiver = locations->InAt(0);
1217 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1218 // temp = object->GetClass();
1219 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1221 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001222 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001224 }
1225 // temp = temp->GetMethodAt(method_offset);
1226 __ movl(temp, Address(temp, method_offset));
1227 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001228 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001229 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001231 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001232 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001233}
1234
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001235void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1236 HandleInvoke(invoke);
1237 // Add the hidden argument.
1238 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1239}
1240
1241void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1242 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001244 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1245 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1246 LocationSummary* locations = invoke->GetLocations();
1247 Location receiver = locations->InAt(0);
1248 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1249
1250 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001252 Immediate(invoke->GetDexMethodIndex()));
1253
1254 // temp = object->GetClass();
1255 if (receiver.IsStackSlot()) {
1256 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1257 __ movl(temp, Address(temp, class_offset));
1258 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001259 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001260 }
1261 // temp = temp->GetImtEntryAt(method_offset);
1262 __ movl(temp, Address(temp, method_offset));
1263 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001264 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001265 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001266
1267 DCHECK(!codegen_->IsLeafMethod());
1268 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1269}
1270
Roland Levillain88cb1752014-10-20 16:36:47 +01001271void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1272 LocationSummary* locations =
1273 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1274 switch (neg->GetResultType()) {
1275 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001276 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001277 locations->SetInAt(0, Location::RequiresRegister());
1278 locations->SetOut(Location::SameAsFirstInput());
1279 break;
1280
Roland Levillain88cb1752014-10-20 16:36:47 +01001281 case Primitive::kPrimFloat:
1282 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001283 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001284 locations->SetOut(Location::SameAsFirstInput());
1285 locations->AddTemp(Location::RequiresRegister());
1286 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001287 break;
1288
1289 default:
1290 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1291 }
1292}
1293
1294void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1295 LocationSummary* locations = neg->GetLocations();
1296 Location out = locations->Out();
1297 Location in = locations->InAt(0);
1298 switch (neg->GetResultType()) {
1299 case Primitive::kPrimInt:
1300 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001302 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001303 break;
1304
1305 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001306 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001307 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001308 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001309 break;
1310
Roland Levillain5368c212014-11-27 15:03:41 +00001311 case Primitive::kPrimFloat: {
1312 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001313 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1314 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001315 // Implement float negation with an exclusive or with value
1316 // 0x80000000 (mask for bit 31, representing the sign of a
1317 // single-precision floating-point number).
1318 __ movq(constant, Immediate(INT64_C(0x80000000)));
1319 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001320 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001321 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001322 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001323
Roland Levillain5368c212014-11-27 15:03:41 +00001324 case Primitive::kPrimDouble: {
1325 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1327 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001328 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001329 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001330 // a double-precision floating-point number).
1331 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1332 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001333 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001334 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001335 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001336
1337 default:
1338 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1339 }
1340}
1341
Roland Levillaindff1f282014-11-05 14:15:05 +00001342void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1343 LocationSummary* locations =
1344 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1345 Primitive::Type result_type = conversion->GetResultType();
1346 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001347 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001348 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001349 case Primitive::kPrimByte:
1350 switch (input_type) {
1351 case Primitive::kPrimShort:
1352 case Primitive::kPrimInt:
1353 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001354 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001355 locations->SetInAt(0, Location::Any());
1356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1357 break;
1358
1359 default:
1360 LOG(FATAL) << "Unexpected type conversion from " << input_type
1361 << " to " << result_type;
1362 }
1363 break;
1364
Roland Levillain01a8d712014-11-14 16:27:39 +00001365 case Primitive::kPrimShort:
1366 switch (input_type) {
1367 case Primitive::kPrimByte:
1368 case Primitive::kPrimInt:
1369 case Primitive::kPrimChar:
1370 // Processing a Dex `int-to-short' instruction.
1371 locations->SetInAt(0, Location::Any());
1372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1373 break;
1374
1375 default:
1376 LOG(FATAL) << "Unexpected type conversion from " << input_type
1377 << " to " << result_type;
1378 }
1379 break;
1380
Roland Levillain946e1432014-11-11 17:35:19 +00001381 case Primitive::kPrimInt:
1382 switch (input_type) {
1383 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001384 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001385 locations->SetInAt(0, Location::Any());
1386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1387 break;
1388
1389 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001390 // Processing a Dex `float-to-int' instruction.
1391 locations->SetInAt(0, Location::RequiresFpuRegister());
1392 locations->SetOut(Location::RequiresRegister());
1393 locations->AddTemp(Location::RequiresFpuRegister());
1394 break;
1395
Roland Levillain946e1432014-11-11 17:35:19 +00001396 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001397 // Processing a Dex `double-to-int' instruction.
1398 locations->SetInAt(0, Location::RequiresFpuRegister());
1399 locations->SetOut(Location::RequiresRegister());
1400 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001401 break;
1402
1403 default:
1404 LOG(FATAL) << "Unexpected type conversion from " << input_type
1405 << " to " << result_type;
1406 }
1407 break;
1408
Roland Levillaindff1f282014-11-05 14:15:05 +00001409 case Primitive::kPrimLong:
1410 switch (input_type) {
1411 case Primitive::kPrimByte:
1412 case Primitive::kPrimShort:
1413 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001414 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001415 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001416 // TODO: We would benefit from a (to-be-implemented)
1417 // Location::RegisterOrStackSlot requirement for this input.
1418 locations->SetInAt(0, Location::RequiresRegister());
1419 locations->SetOut(Location::RequiresRegister());
1420 break;
1421
1422 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001423 // Processing a Dex `float-to-long' instruction.
1424 locations->SetInAt(0, Location::RequiresFpuRegister());
1425 locations->SetOut(Location::RequiresRegister());
1426 locations->AddTemp(Location::RequiresFpuRegister());
1427 break;
1428
Roland Levillaindff1f282014-11-05 14:15:05 +00001429 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001430 // Processing a Dex `double-to-long' instruction.
1431 locations->SetInAt(0, Location::RequiresFpuRegister());
1432 locations->SetOut(Location::RequiresRegister());
1433 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001434 break;
1435
1436 default:
1437 LOG(FATAL) << "Unexpected type conversion from " << input_type
1438 << " to " << result_type;
1439 }
1440 break;
1441
Roland Levillain981e4542014-11-14 11:47:14 +00001442 case Primitive::kPrimChar:
1443 switch (input_type) {
1444 case Primitive::kPrimByte:
1445 case Primitive::kPrimShort:
1446 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001447 // Processing a Dex `int-to-char' instruction.
1448 locations->SetInAt(0, Location::Any());
1449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1450 break;
1451
1452 default:
1453 LOG(FATAL) << "Unexpected type conversion from " << input_type
1454 << " to " << result_type;
1455 }
1456 break;
1457
Roland Levillaindff1f282014-11-05 14:15:05 +00001458 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001459 switch (input_type) {
1460 case Primitive::kPrimByte:
1461 case Primitive::kPrimShort:
1462 case Primitive::kPrimInt:
1463 case Primitive::kPrimChar:
1464 // Processing a Dex `int-to-float' instruction.
1465 locations->SetInAt(0, Location::RequiresRegister());
1466 locations->SetOut(Location::RequiresFpuRegister());
1467 break;
1468
1469 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001470 // Processing a Dex `long-to-float' instruction.
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 locations->SetOut(Location::RequiresFpuRegister());
1473 break;
1474
Roland Levillaincff13742014-11-17 14:32:17 +00001475 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001476 // Processing a Dex `double-to-float' instruction.
1477 locations->SetInAt(0, Location::RequiresFpuRegister());
1478 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001479 break;
1480
1481 default:
1482 LOG(FATAL) << "Unexpected type conversion from " << input_type
1483 << " to " << result_type;
1484 };
1485 break;
1486
Roland Levillaindff1f282014-11-05 14:15:05 +00001487 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001488 switch (input_type) {
1489 case Primitive::kPrimByte:
1490 case Primitive::kPrimShort:
1491 case Primitive::kPrimInt:
1492 case Primitive::kPrimChar:
1493 // Processing a Dex `int-to-double' instruction.
1494 locations->SetInAt(0, Location::RequiresRegister());
1495 locations->SetOut(Location::RequiresFpuRegister());
1496 break;
1497
1498 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001499 // Processing a Dex `long-to-double' instruction.
1500 locations->SetInAt(0, Location::RequiresRegister());
1501 locations->SetOut(Location::RequiresFpuRegister());
1502 break;
1503
Roland Levillaincff13742014-11-17 14:32:17 +00001504 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001505 // Processing a Dex `float-to-double' instruction.
1506 locations->SetInAt(0, Location::RequiresFpuRegister());
1507 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001508 break;
1509
1510 default:
1511 LOG(FATAL) << "Unexpected type conversion from " << input_type
1512 << " to " << result_type;
1513 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001514 break;
1515
1516 default:
1517 LOG(FATAL) << "Unexpected type conversion from " << input_type
1518 << " to " << result_type;
1519 }
1520}
1521
1522void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1523 LocationSummary* locations = conversion->GetLocations();
1524 Location out = locations->Out();
1525 Location in = locations->InAt(0);
1526 Primitive::Type result_type = conversion->GetResultType();
1527 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001528 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001529 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001530 case Primitive::kPrimByte:
1531 switch (input_type) {
1532 case Primitive::kPrimShort:
1533 case Primitive::kPrimInt:
1534 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001535 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001536 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001537 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001538 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001539 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001540 Address(CpuRegister(RSP), in.GetStackIndex()));
1541 } else {
1542 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001543 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001544 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1545 }
1546 break;
1547
1548 default:
1549 LOG(FATAL) << "Unexpected type conversion from " << input_type
1550 << " to " << result_type;
1551 }
1552 break;
1553
Roland Levillain01a8d712014-11-14 16:27:39 +00001554 case Primitive::kPrimShort:
1555 switch (input_type) {
1556 case Primitive::kPrimByte:
1557 case Primitive::kPrimInt:
1558 case Primitive::kPrimChar:
1559 // Processing a Dex `int-to-short' instruction.
1560 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001561 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001562 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001563 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001564 Address(CpuRegister(RSP), in.GetStackIndex()));
1565 } else {
1566 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001567 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001568 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1569 }
1570 break;
1571
1572 default:
1573 LOG(FATAL) << "Unexpected type conversion from " << input_type
1574 << " to " << result_type;
1575 }
1576 break;
1577
Roland Levillain946e1432014-11-11 17:35:19 +00001578 case Primitive::kPrimInt:
1579 switch (input_type) {
1580 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001581 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001582 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001583 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001584 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001585 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001586 Address(CpuRegister(RSP), in.GetStackIndex()));
1587 } else {
1588 DCHECK(in.IsConstant());
1589 DCHECK(in.GetConstant()->IsLongConstant());
1590 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001591 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001592 }
1593 break;
1594
Roland Levillain3f8f9362014-12-02 17:45:01 +00001595 case Primitive::kPrimFloat: {
1596 // Processing a Dex `float-to-int' instruction.
1597 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1598 CpuRegister output = out.AsRegister<CpuRegister>();
1599 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1600 Label done, nan;
1601
1602 __ movl(output, Immediate(kPrimIntMax));
1603 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001604 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001605 // if input >= temp goto done
1606 __ comiss(input, temp);
1607 __ j(kAboveEqual, &done);
1608 // if input == NaN goto nan
1609 __ j(kUnordered, &nan);
1610 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001611 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001612 __ jmp(&done);
1613 __ Bind(&nan);
1614 // output = 0
1615 __ xorl(output, output);
1616 __ Bind(&done);
1617 break;
1618 }
1619
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001620 case Primitive::kPrimDouble: {
1621 // Processing a Dex `double-to-int' instruction.
1622 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1623 CpuRegister output = out.AsRegister<CpuRegister>();
1624 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1625 Label done, nan;
1626
1627 __ movl(output, Immediate(kPrimIntMax));
1628 // temp = int-to-double(output)
1629 __ cvtsi2sd(temp, output);
1630 // if input >= temp goto done
1631 __ comisd(input, temp);
1632 __ j(kAboveEqual, &done);
1633 // if input == NaN goto nan
1634 __ j(kUnordered, &nan);
1635 // output = double-to-int-truncate(input)
1636 __ cvttsd2si(output, input);
1637 __ jmp(&done);
1638 __ Bind(&nan);
1639 // output = 0
1640 __ xorl(output, output);
1641 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001642 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001643 }
Roland Levillain946e1432014-11-11 17:35:19 +00001644
1645 default:
1646 LOG(FATAL) << "Unexpected type conversion from " << input_type
1647 << " to " << result_type;
1648 }
1649 break;
1650
Roland Levillaindff1f282014-11-05 14:15:05 +00001651 case Primitive::kPrimLong:
1652 switch (input_type) {
1653 DCHECK(out.IsRegister());
1654 case Primitive::kPrimByte:
1655 case Primitive::kPrimShort:
1656 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001657 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001658 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001659 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001660 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001661 break;
1662
Roland Levillain624279f2014-12-04 11:54:28 +00001663 case Primitive::kPrimFloat: {
1664 // Processing a Dex `float-to-long' instruction.
1665 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1666 CpuRegister output = out.AsRegister<CpuRegister>();
1667 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1668 Label done, nan;
1669
1670 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001671 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001672 __ cvtsi2ss(temp, output, true);
1673 // if input >= temp goto done
1674 __ comiss(input, temp);
1675 __ j(kAboveEqual, &done);
1676 // if input == NaN goto nan
1677 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001678 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001679 __ cvttss2si(output, input, true);
1680 __ jmp(&done);
1681 __ Bind(&nan);
1682 // output = 0
1683 __ xorq(output, output);
1684 __ Bind(&done);
1685 break;
1686 }
1687
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001688 case Primitive::kPrimDouble: {
1689 // Processing a Dex `double-to-long' instruction.
1690 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1691 CpuRegister output = out.AsRegister<CpuRegister>();
1692 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1693 Label done, nan;
1694
1695 __ movq(output, Immediate(kPrimLongMax));
1696 // temp = long-to-double(output)
1697 __ cvtsi2sd(temp, output, true);
1698 // if input >= temp goto done
1699 __ comisd(input, temp);
1700 __ j(kAboveEqual, &done);
1701 // if input == NaN goto nan
1702 __ j(kUnordered, &nan);
1703 // output = double-to-long-truncate(input)
1704 __ cvttsd2si(output, input, true);
1705 __ jmp(&done);
1706 __ Bind(&nan);
1707 // output = 0
1708 __ xorq(output, output);
1709 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001710 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001711 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001712
1713 default:
1714 LOG(FATAL) << "Unexpected type conversion from " << input_type
1715 << " to " << result_type;
1716 }
1717 break;
1718
Roland Levillain981e4542014-11-14 11:47:14 +00001719 case Primitive::kPrimChar:
1720 switch (input_type) {
1721 case Primitive::kPrimByte:
1722 case Primitive::kPrimShort:
1723 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001724 // Processing a Dex `int-to-char' instruction.
1725 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001726 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001727 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001729 Address(CpuRegister(RSP), in.GetStackIndex()));
1730 } else {
1731 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001732 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001733 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1734 }
1735 break;
1736
1737 default:
1738 LOG(FATAL) << "Unexpected type conversion from " << input_type
1739 << " to " << result_type;
1740 }
1741 break;
1742
Roland Levillaindff1f282014-11-05 14:15:05 +00001743 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001744 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001745 case Primitive::kPrimByte:
1746 case Primitive::kPrimShort:
1747 case Primitive::kPrimInt:
1748 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001749 // Processing a Dex `int-to-float' instruction.
1750 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001751 break;
1752
1753 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001754 // Processing a Dex `long-to-float' instruction.
1755 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1756 break;
1757
Roland Levillaincff13742014-11-17 14:32:17 +00001758 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001759 // Processing a Dex `double-to-float' instruction.
1760 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001761 break;
1762
1763 default:
1764 LOG(FATAL) << "Unexpected type conversion from " << input_type
1765 << " to " << result_type;
1766 };
1767 break;
1768
Roland Levillaindff1f282014-11-05 14:15:05 +00001769 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001770 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001771 case Primitive::kPrimByte:
1772 case Primitive::kPrimShort:
1773 case Primitive::kPrimInt:
1774 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001775 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001777 break;
1778
1779 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001780 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001781 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001782 break;
1783
Roland Levillaincff13742014-11-17 14:32:17 +00001784 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001785 // Processing a Dex `float-to-double' instruction.
1786 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001787 break;
1788
1789 default:
1790 LOG(FATAL) << "Unexpected type conversion from " << input_type
1791 << " to " << result_type;
1792 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001793 break;
1794
1795 default:
1796 LOG(FATAL) << "Unexpected type conversion from " << input_type
1797 << " to " << result_type;
1798 }
1799}
1800
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001801void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001802 LocationSummary* locations =
1803 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001804 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001805 case Primitive::kPrimInt: {
1806 locations->SetInAt(0, Location::RequiresRegister());
1807 locations->SetInAt(1, Location::Any());
1808 locations->SetOut(Location::SameAsFirstInput());
1809 break;
1810 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001811
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001812 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001813 locations->SetInAt(0, Location::RequiresRegister());
1814 locations->SetInAt(1, Location::RequiresRegister());
1815 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001816 break;
1817 }
1818
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001819 case Primitive::kPrimDouble:
1820 case Primitive::kPrimFloat: {
1821 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001822 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001823 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001824 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001825 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001826
1827 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001828 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001829 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001830}
1831
1832void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1833 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001834 Location first = locations->InAt(0);
1835 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001836 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001837
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001838 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001839 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001840 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001841 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001842 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001843 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001844 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001845 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001846 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001847 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001848 break;
1849 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001850
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001851 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001852 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001853 break;
1854 }
1855
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001857 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001858 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001859 }
1860
1861 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001862 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 break;
1864 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001865
1866 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001868 }
1869}
1870
1871void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001872 LocationSummary* locations =
1873 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001874 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001875 case Primitive::kPrimInt: {
1876 locations->SetInAt(0, Location::RequiresRegister());
1877 locations->SetInAt(1, Location::Any());
1878 locations->SetOut(Location::SameAsFirstInput());
1879 break;
1880 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001881 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::RequiresRegister());
1884 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001885 break;
1886 }
Calin Juravle11351682014-10-23 15:38:15 +01001887 case Primitive::kPrimFloat:
1888 case Primitive::kPrimDouble: {
1889 locations->SetInAt(0, Location::RequiresFpuRegister());
1890 locations->SetInAt(1, Location::RequiresFpuRegister());
1891 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001892 break;
Calin Juravle11351682014-10-23 15:38:15 +01001893 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001894 default:
Calin Juravle11351682014-10-23 15:38:15 +01001895 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001896 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001897}
1898
1899void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1900 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001901 Location first = locations->InAt(0);
1902 Location second = locations->InAt(1);
1903 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001904 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001905 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001906 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001907 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001908 } else if (second.IsConstant()) {
1909 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001910 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001911 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001912 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001913 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001914 break;
1915 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001916 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001917 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001918 break;
1919 }
1920
Calin Juravle11351682014-10-23 15:38:15 +01001921 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001923 break;
Calin Juravle11351682014-10-23 15:38:15 +01001924 }
1925
1926 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001927 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001928 break;
1929 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001930
1931 default:
Calin Juravle11351682014-10-23 15:38:15 +01001932 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001933 }
1934}
1935
Calin Juravle34bacdf2014-10-07 20:23:36 +01001936void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1937 LocationSummary* locations =
1938 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1939 switch (mul->GetResultType()) {
1940 case Primitive::kPrimInt: {
1941 locations->SetInAt(0, Location::RequiresRegister());
1942 locations->SetInAt(1, Location::Any());
1943 locations->SetOut(Location::SameAsFirstInput());
1944 break;
1945 }
1946 case Primitive::kPrimLong: {
1947 locations->SetInAt(0, Location::RequiresRegister());
1948 locations->SetInAt(1, Location::RequiresRegister());
1949 locations->SetOut(Location::SameAsFirstInput());
1950 break;
1951 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001952 case Primitive::kPrimFloat:
1953 case Primitive::kPrimDouble: {
1954 locations->SetInAt(0, Location::RequiresFpuRegister());
1955 locations->SetInAt(1, Location::RequiresFpuRegister());
1956 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001957 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001958 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001959
1960 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001961 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001962 }
1963}
1964
1965void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1966 LocationSummary* locations = mul->GetLocations();
1967 Location first = locations->InAt(0);
1968 Location second = locations->InAt(1);
1969 DCHECK(first.Equals(locations->Out()));
1970 switch (mul->GetResultType()) {
1971 case Primitive::kPrimInt: {
1972 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001973 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001974 } else if (second.IsConstant()) {
1975 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001976 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001977 } else {
1978 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00001979 __ imull(first.AsRegister<CpuRegister>(),
1980 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001981 }
1982 break;
1983 }
1984 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001985 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001986 break;
1987 }
1988
Calin Juravleb5bfa962014-10-21 18:02:24 +01001989 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001991 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001992 }
1993
1994 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001995 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01001996 break;
1997 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001998
1999 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002000 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002001 }
2002}
2003
Calin Juravlebacfec32014-11-14 15:54:36 +00002004void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2005 DCHECK(instruction->IsDiv() || instruction->IsRem());
2006 Primitive::Type type = instruction->GetResultType();
2007 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2008
2009 bool is_div = instruction->IsDiv();
2010 LocationSummary* locations = instruction->GetLocations();
2011
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2013 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002014
Roland Levillain271ab9c2014-11-27 15:23:57 +00002015 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002016 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2017
2018 SlowPathCodeX86_64* slow_path =
2019 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2020 out_reg.AsRegister(), type, is_div);
2021 codegen_->AddSlowPath(slow_path);
2022
2023 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2024 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2025 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002026 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002027 __ cmpl(second_reg, Immediate(-1));
2028 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002029 // edx:eax <- sign-extended of eax
2030 __ cdq();
2031 // eax = quotient, edx = remainder
2032 __ idivl(second_reg);
2033 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002034 __ cmpq(second_reg, Immediate(-1));
2035 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002036 // rdx:rax <- sign-extended of rax
2037 __ cqo();
2038 // rax = quotient, rdx = remainder
2039 __ idivq(second_reg);
2040 }
2041
2042 __ Bind(slow_path->GetExitLabel());
2043}
2044
Calin Juravle7c4954d2014-10-28 16:57:40 +00002045void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2046 LocationSummary* locations =
2047 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2048 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002049 case Primitive::kPrimInt:
2050 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002051 locations->SetInAt(0, Location::RegisterLocation(RAX));
2052 locations->SetInAt(1, Location::RequiresRegister());
2053 locations->SetOut(Location::SameAsFirstInput());
2054 // Intel uses edx:eax as the dividend.
2055 locations->AddTemp(Location::RegisterLocation(RDX));
2056 break;
2057 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002058
Calin Juravle7c4954d2014-10-28 16:57:40 +00002059 case Primitive::kPrimFloat:
2060 case Primitive::kPrimDouble: {
2061 locations->SetInAt(0, Location::RequiresFpuRegister());
2062 locations->SetInAt(1, Location::RequiresFpuRegister());
2063 locations->SetOut(Location::SameAsFirstInput());
2064 break;
2065 }
2066
2067 default:
2068 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2069 }
2070}
2071
2072void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2073 LocationSummary* locations = div->GetLocations();
2074 Location first = locations->InAt(0);
2075 Location second = locations->InAt(1);
2076 DCHECK(first.Equals(locations->Out()));
2077
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002078 Primitive::Type type = div->GetResultType();
2079 switch (type) {
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002082 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002083 break;
2084 }
2085
Calin Juravle7c4954d2014-10-28 16:57:40 +00002086 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002088 break;
2089 }
2090
2091 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002093 break;
2094 }
2095
2096 default:
2097 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2098 }
2099}
2100
Calin Juravlebacfec32014-11-14 15:54:36 +00002101void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002102 Primitive::Type type = rem->GetResultType();
2103 LocationSummary::CallKind call_kind =
2104 (type == Primitive::kPrimInt) || (type == Primitive::kPrimLong)
2105 ? LocationSummary::kNoCall
2106 : LocationSummary::kCall;
2107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2108
2109 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002110 case Primitive::kPrimInt:
2111 case Primitive::kPrimLong: {
2112 locations->SetInAt(0, Location::RegisterLocation(RAX));
2113 locations->SetInAt(1, Location::RequiresRegister());
2114 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2115 locations->SetOut(Location::RegisterLocation(RDX));
2116 break;
2117 }
2118
2119 case Primitive::kPrimFloat:
2120 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002121 InvokeRuntimeCallingConvention calling_convention;
2122 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2123 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2124 // The runtime helper puts the result in XMM0.
2125 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Calin Juravlebacfec32014-11-14 15:54:36 +00002126 break;
2127 }
2128
2129 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002130 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002131 }
2132}
2133
2134void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2135 Primitive::Type type = rem->GetResultType();
2136 switch (type) {
2137 case Primitive::kPrimInt:
2138 case Primitive::kPrimLong: {
2139 GenerateDivRemIntegral(rem);
2140 break;
2141 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002142 case Primitive::kPrimFloat: {
2143 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pFmodf), true));
2144 codegen_->RecordPcInfo(rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002145 break;
2146 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002147 case Primitive::kPrimDouble: {
2148 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pFmod), true));
2149 codegen_->RecordPcInfo(rem, rem->GetDexPc());
2150 break;
2151 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002152 default:
2153 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2154 }
2155}
2156
Calin Juravled0d48522014-11-04 16:40:20 +00002157void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2158 LocationSummary* locations =
2159 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2160 locations->SetInAt(0, Location::Any());
2161 if (instruction->HasUses()) {
2162 locations->SetOut(Location::SameAsFirstInput());
2163 }
2164}
2165
2166void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2167 SlowPathCodeX86_64* slow_path =
2168 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2169 codegen_->AddSlowPath(slow_path);
2170
2171 LocationSummary* locations = instruction->GetLocations();
2172 Location value = locations->InAt(0);
2173
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002174 switch (instruction->GetType()) {
2175 case Primitive::kPrimInt: {
2176 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002177 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002178 __ j(kEqual, slow_path->GetEntryLabel());
2179 } else if (value.IsStackSlot()) {
2180 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2181 __ j(kEqual, slow_path->GetEntryLabel());
2182 } else {
2183 DCHECK(value.IsConstant()) << value;
2184 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2185 __ jmp(slow_path->GetEntryLabel());
2186 }
2187 }
2188 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002189 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002190 case Primitive::kPrimLong: {
2191 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002192 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002193 __ j(kEqual, slow_path->GetEntryLabel());
2194 } else if (value.IsDoubleStackSlot()) {
2195 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2196 __ j(kEqual, slow_path->GetEntryLabel());
2197 } else {
2198 DCHECK(value.IsConstant()) << value;
2199 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2200 __ jmp(slow_path->GetEntryLabel());
2201 }
2202 }
2203 break;
2204 }
2205 default:
2206 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002207 }
Calin Juravled0d48522014-11-04 16:40:20 +00002208}
2209
Calin Juravle9aec02f2014-11-18 23:06:35 +00002210void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2211 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2212
2213 LocationSummary* locations =
2214 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2215
2216 switch (op->GetResultType()) {
2217 case Primitive::kPrimInt:
2218 case Primitive::kPrimLong: {
2219 locations->SetInAt(0, Location::RequiresRegister());
2220 // The shift count needs to be in CL.
2221 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2222 locations->SetOut(Location::SameAsFirstInput());
2223 break;
2224 }
2225 default:
2226 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2227 }
2228}
2229
2230void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2231 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2232
2233 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002235 Location second = locations->InAt(1);
2236
2237 switch (op->GetResultType()) {
2238 case Primitive::kPrimInt: {
2239 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002241 if (op->IsShl()) {
2242 __ shll(first_reg, second_reg);
2243 } else if (op->IsShr()) {
2244 __ sarl(first_reg, second_reg);
2245 } else {
2246 __ shrl(first_reg, second_reg);
2247 }
2248 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002249 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002250 if (op->IsShl()) {
2251 __ shll(first_reg, imm);
2252 } else if (op->IsShr()) {
2253 __ sarl(first_reg, imm);
2254 } else {
2255 __ shrl(first_reg, imm);
2256 }
2257 }
2258 break;
2259 }
2260 case Primitive::kPrimLong: {
2261 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002262 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002263 if (op->IsShl()) {
2264 __ shlq(first_reg, second_reg);
2265 } else if (op->IsShr()) {
2266 __ sarq(first_reg, second_reg);
2267 } else {
2268 __ shrq(first_reg, second_reg);
2269 }
2270 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002271 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002272 if (op->IsShl()) {
2273 __ shlq(first_reg, imm);
2274 } else if (op->IsShr()) {
2275 __ sarq(first_reg, imm);
2276 } else {
2277 __ shrq(first_reg, imm);
2278 }
2279 }
2280 break;
2281 }
2282 default:
2283 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2284 }
2285}
2286
2287void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2288 HandleShift(shl);
2289}
2290
2291void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2292 HandleShift(shl);
2293}
2294
2295void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2296 HandleShift(shr);
2297}
2298
2299void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2300 HandleShift(shr);
2301}
2302
2303void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2304 HandleShift(ushr);
2305}
2306
2307void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2308 HandleShift(ushr);
2309}
2310
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002311void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002312 LocationSummary* locations =
2313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002314 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002315 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2316 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2317 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002318}
2319
2320void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2321 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002322 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002323 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2324
2325 __ gs()->call(Address::Absolute(
2326 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2327
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002328 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002329 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002330}
2331
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002332void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2333 LocationSummary* locations =
2334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2335 InvokeRuntimeCallingConvention calling_convention;
2336 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002337 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002338 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002339 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002340}
2341
2342void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2343 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002344 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002345 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2346
2347 __ gs()->call(Address::Absolute(
2348 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2349
2350 DCHECK(!codegen_->IsLeafMethod());
2351 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2352}
2353
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002354void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002355 LocationSummary* locations =
2356 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002357 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2358 if (location.IsStackSlot()) {
2359 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2360 } else if (location.IsDoubleStackSlot()) {
2361 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2362 }
2363 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002364}
2365
2366void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2367 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002368 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002369}
2370
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002371void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002372 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002373 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002374 locations->SetInAt(0, Location::RequiresRegister());
2375 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002376}
2377
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002378void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2379 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002380 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2381 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002382 Location out = locations->Out();
2383 switch (not_->InputAt(0)->GetType()) {
2384 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002385 __ xorq(out.AsRegister<CpuRegister>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002386 break;
2387
2388 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002389 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002390 break;
2391
2392 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002393 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002394 break;
2395
2396 default:
2397 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2398 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002399}
2400
2401void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002402 LocationSummary* locations =
2403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002404 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2405 locations->SetInAt(i, Location::Any());
2406 }
2407 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002408}
2409
2410void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002411 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002412 LOG(FATAL) << "Unimplemented";
2413}
2414
Calin Juravle52c48962014-12-16 17:02:57 +00002415void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2416 /*
2417 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2418 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2419 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2420 */
2421 switch (kind) {
2422 case MemBarrierKind::kAnyAny: {
2423 __ mfence();
2424 break;
2425 }
2426 case MemBarrierKind::kAnyStore:
2427 case MemBarrierKind::kLoadAny:
2428 case MemBarrierKind::kStoreStore: {
2429 // nop
2430 break;
2431 }
2432 default:
2433 LOG(FATAL) << "Unexpected memory barier " << kind;
2434 }
2435}
2436
2437void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2438 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2439
Nicolas Geoffray39468442014-09-02 15:17:15 +01002440 LocationSummary* locations =
2441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002442 locations->SetInAt(0, Location::RequiresRegister());
2443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2444}
2445
2446void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2447 const FieldInfo& field_info) {
2448 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2449
2450 LocationSummary* locations = instruction->GetLocations();
2451 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2452 Location out = locations->Out();
2453 bool is_volatile = field_info.IsVolatile();
2454 Primitive::Type field_type = field_info.GetFieldType();
2455 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2456
2457 switch (field_type) {
2458 case Primitive::kPrimBoolean: {
2459 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2460 break;
2461 }
2462
2463 case Primitive::kPrimByte: {
2464 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2465 break;
2466 }
2467
2468 case Primitive::kPrimShort: {
2469 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2470 break;
2471 }
2472
2473 case Primitive::kPrimChar: {
2474 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2475 break;
2476 }
2477
2478 case Primitive::kPrimInt:
2479 case Primitive::kPrimNot: {
2480 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2481 break;
2482 }
2483
2484 case Primitive::kPrimLong: {
2485 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2486 break;
2487 }
2488
2489 case Primitive::kPrimFloat: {
2490 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2491 break;
2492 }
2493
2494 case Primitive::kPrimDouble: {
2495 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2496 break;
2497 }
2498
2499 case Primitive::kPrimVoid:
2500 LOG(FATAL) << "Unreachable type " << field_type;
2501 UNREACHABLE();
2502 }
2503
2504 if (is_volatile) {
2505 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2506 }
2507}
2508
2509void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2510 const FieldInfo& field_info) {
2511 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2512
2513 LocationSummary* locations =
2514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002515 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002516 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2517
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002518 locations->SetInAt(0, Location::RequiresRegister());
2519 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002520 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002521 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002522 locations->AddTemp(Location::RequiresRegister());
2523 locations->AddTemp(Location::RequiresRegister());
2524 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002525}
2526
Calin Juravle52c48962014-12-16 17:02:57 +00002527void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2528 const FieldInfo& field_info) {
2529 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2530
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002531 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002532 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2533 Location value = locations->InAt(1);
2534 bool is_volatile = field_info.IsVolatile();
2535 Primitive::Type field_type = field_info.GetFieldType();
2536 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2537
2538 if (is_volatile) {
2539 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2540 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002541
2542 switch (field_type) {
2543 case Primitive::kPrimBoolean:
2544 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002545 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002546 break;
2547 }
2548
2549 case Primitive::kPrimShort:
2550 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002551 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002552 break;
2553 }
2554
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002555 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002556 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002557 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
2558 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002559 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2560 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00002561 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002562 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002563 break;
2564 }
2565
2566 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002567 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002568 break;
2569 }
2570
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002571 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002572 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002573 break;
2574 }
2575
2576 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002577 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002578 break;
2579 }
2580
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002581 case Primitive::kPrimVoid:
2582 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002583 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002584 }
Calin Juravle52c48962014-12-16 17:02:57 +00002585
2586 if (is_volatile) {
2587 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2588 }
2589}
2590
2591void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2592 HandleFieldSet(instruction, instruction->GetFieldInfo());
2593}
2594
2595void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2596 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002597}
2598
2599void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002600 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002601}
2602
2603void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002604 HandleFieldGet(instruction, instruction->GetFieldInfo());
2605}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002606
Calin Juravle52c48962014-12-16 17:02:57 +00002607void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2608 HandleFieldGet(instruction);
2609}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002610
Calin Juravle52c48962014-12-16 17:02:57 +00002611void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2612 HandleFieldGet(instruction, instruction->GetFieldInfo());
2613}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002614
Calin Juravle52c48962014-12-16 17:02:57 +00002615void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2616 HandleFieldSet(instruction, instruction->GetFieldInfo());
2617}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002618
Calin Juravle52c48962014-12-16 17:02:57 +00002619void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2620 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002621}
2622
2623void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002624 LocationSummary* locations =
2625 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002626 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2627 ? Location::RequiresRegister()
2628 : Location::Any();
2629 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002630 if (instruction->HasUses()) {
2631 locations->SetOut(Location::SameAsFirstInput());
2632 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633}
2634
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002635void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
2636 LocationSummary* locations = instruction->GetLocations();
2637 Location obj = locations->InAt(0);
2638
2639 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2640 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2641}
2642
2643void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002644 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002645 codegen_->AddSlowPath(slow_path);
2646
2647 LocationSummary* locations = instruction->GetLocations();
2648 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002649
2650 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002651 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002652 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002653 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002654 } else {
2655 DCHECK(obj.IsConstant()) << obj;
2656 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2657 __ jmp(slow_path->GetEntryLabel());
2658 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002659 }
2660 __ j(kEqual, slow_path->GetEntryLabel());
2661}
2662
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002663void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2664 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2665 GenerateImplicitNullCheck(instruction);
2666 } else {
2667 GenerateExplicitNullCheck(instruction);
2668 }
2669}
2670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002671void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002672 LocationSummary* locations =
2673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002674 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002675 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002676 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002678}
2679
2680void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2681 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002682 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002683 Location index = locations->InAt(1);
2684
2685 switch (instruction->GetType()) {
2686 case Primitive::kPrimBoolean: {
2687 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002689 if (index.IsConstant()) {
2690 __ movzxb(out, Address(obj,
2691 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2692 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002693 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002694 }
2695 break;
2696 }
2697
2698 case Primitive::kPrimByte: {
2699 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002700 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002701 if (index.IsConstant()) {
2702 __ movsxb(out, Address(obj,
2703 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2704 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002706 }
2707 break;
2708 }
2709
2710 case Primitive::kPrimShort: {
2711 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002712 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002713 if (index.IsConstant()) {
2714 __ movsxw(out, Address(obj,
2715 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2716 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002718 }
2719 break;
2720 }
2721
2722 case Primitive::kPrimChar: {
2723 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002724 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002725 if (index.IsConstant()) {
2726 __ movzxw(out, Address(obj,
2727 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2728 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002730 }
2731 break;
2732 }
2733
2734 case Primitive::kPrimInt:
2735 case Primitive::kPrimNot: {
2736 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2737 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002738 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002739 if (index.IsConstant()) {
2740 __ movl(out, Address(obj,
2741 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2742 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002743 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002744 }
2745 break;
2746 }
2747
2748 case Primitive::kPrimLong: {
2749 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002750 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002751 if (index.IsConstant()) {
2752 __ movq(out, Address(obj,
2753 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2754 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002756 }
2757 break;
2758 }
2759
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002760 case Primitive::kPrimFloat: {
2761 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002762 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002763 if (index.IsConstant()) {
2764 __ movss(out, Address(obj,
2765 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2766 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002768 }
2769 break;
2770 }
2771
2772 case Primitive::kPrimDouble: {
2773 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002775 if (index.IsConstant()) {
2776 __ movsd(out, Address(obj,
2777 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2778 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002779 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002780 }
2781 break;
2782 }
2783
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002784 case Primitive::kPrimVoid:
2785 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002786 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002787 }
2788}
2789
2790void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002791 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002792
2793 bool needs_write_barrier =
2794 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2795 bool needs_runtime_call = instruction->NeedsTypeCheck();
2796
Nicolas Geoffray39468442014-09-02 15:17:15 +01002797 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002798 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2799 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002800 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002801 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2802 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2803 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002804 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002805 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002806 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002807 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2808 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002809 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002810 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002811 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2812 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002813 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002814 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002815 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002816
2817 if (needs_write_barrier) {
2818 // Temporary registers for the write barrier.
2819 locations->AddTemp(Location::RequiresRegister());
2820 locations->AddTemp(Location::RequiresRegister());
2821 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002822 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002823}
2824
2825void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2826 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002827 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002828 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002829 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002830 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002831 bool needs_runtime_call = locations->WillCall();
2832 bool needs_write_barrier =
2833 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002834
2835 switch (value_type) {
2836 case Primitive::kPrimBoolean:
2837 case Primitive::kPrimByte: {
2838 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002839 if (index.IsConstant()) {
2840 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002841 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002843 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002844 __ movb(Address(obj, offset),
2845 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002846 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002847 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002848 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002849 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2850 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002851 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002853 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2854 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002855 }
2856 break;
2857 }
2858
2859 case Primitive::kPrimShort:
2860 case Primitive::kPrimChar: {
2861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002862 if (index.IsConstant()) {
2863 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002864 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002865 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002866 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002867 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002868 __ movw(Address(obj, offset),
2869 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002870 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002871 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002872 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002873 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002874 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2875 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002876 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002877 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002878 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002879 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2880 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002881 }
2882 break;
2883 }
2884
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002885 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002886 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002887 if (!needs_runtime_call) {
2888 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2889 if (index.IsConstant()) {
2890 size_t offset =
2891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2892 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002893 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002894 } else {
2895 DCHECK(value.IsConstant()) << value;
2896 __ movl(Address(obj, offset),
2897 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2898 }
2899 } else {
2900 DCHECK(index.IsRegister()) << index;
2901 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002902 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2903 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002904 } else {
2905 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002906 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002907 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2908 }
2909 }
2910
2911 if (needs_write_barrier) {
2912 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002913 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2914 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2915 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002916 }
2917 } else {
2918 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00002919 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
2920 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002921 DCHECK(!codegen_->IsLeafMethod());
2922 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2923 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002924 break;
2925 }
2926
2927 case Primitive::kPrimLong: {
2928 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002929 if (index.IsConstant()) {
2930 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002931 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002932 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002933 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002934 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002935 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2936 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002937 }
2938 break;
2939 }
2940
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002941 case Primitive::kPrimFloat: {
2942 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2943 if (index.IsConstant()) {
2944 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2945 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002946 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002947 } else {
2948 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002949 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2950 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002951 }
2952 break;
2953 }
2954
2955 case Primitive::kPrimDouble: {
2956 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2957 if (index.IsConstant()) {
2958 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2959 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002961 } else {
2962 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002963 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2964 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002965 }
2966 break;
2967 }
2968
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002969 case Primitive::kPrimVoid:
2970 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002971 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002972 }
2973}
2974
2975void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002976 LocationSummary* locations =
2977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002978 locations->SetInAt(0, Location::RequiresRegister());
2979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002980}
2981
2982void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2983 LocationSummary* locations = instruction->GetLocations();
2984 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002985 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2986 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 __ movl(out, Address(obj, offset));
2988}
2989
2990void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002991 LocationSummary* locations =
2992 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002993 locations->SetInAt(0, Location::RequiresRegister());
2994 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002995 if (instruction->HasUses()) {
2996 locations->SetOut(Location::SameAsFirstInput());
2997 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002998}
2999
3000void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3001 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003002 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003003 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003004 codegen_->AddSlowPath(slow_path);
3005
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3007 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003008
3009 __ cmpl(index, length);
3010 __ j(kAboveEqual, slow_path->GetEntryLabel());
3011}
3012
3013void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3014 CpuRegister card,
3015 CpuRegister object,
3016 CpuRegister value) {
3017 Label is_null;
3018 __ testl(value, value);
3019 __ j(kEqual, &is_null);
3020 __ gs()->movq(card, Address::Absolute(
3021 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3022 __ movq(temp, object);
3023 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3024 __ movb(Address(temp, card, TIMES_1, 0), card);
3025 __ Bind(&is_null);
3026}
3027
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003028void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3029 temp->SetLocations(nullptr);
3030}
3031
3032void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3033 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003034 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003035}
3036
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003037void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003038 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 LOG(FATAL) << "Unimplemented";
3040}
3041
3042void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003043 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3044}
3045
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003046void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3047 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3048}
3049
3050void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003051 HBasicBlock* block = instruction->GetBlock();
3052 if (block->GetLoopInformation() != nullptr) {
3053 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3054 // The back edge will generate the suspend check.
3055 return;
3056 }
3057 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3058 // The goto will generate the suspend check.
3059 return;
3060 }
3061 GenerateSuspendCheck(instruction, nullptr);
3062}
3063
3064void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3065 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003066 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003067 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003068 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003069 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003070 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003071 if (successor == nullptr) {
3072 __ j(kNotEqual, slow_path->GetEntryLabel());
3073 __ Bind(slow_path->GetReturnLabel());
3074 } else {
3075 __ j(kEqual, codegen_->GetLabelOf(successor));
3076 __ jmp(slow_path->GetEntryLabel());
3077 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003078}
3079
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003080X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3081 return codegen_->GetAssembler();
3082}
3083
3084void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3085 MoveOperands* move = moves_.Get(index);
3086 Location source = move->GetSource();
3087 Location destination = move->GetDestination();
3088
3089 if (source.IsRegister()) {
3090 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003091 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003092 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003093 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003095 } else {
3096 DCHECK(destination.IsDoubleStackSlot());
3097 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003098 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003099 }
3100 } else if (source.IsStackSlot()) {
3101 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003103 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003104 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003106 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003107 } else {
3108 DCHECK(destination.IsStackSlot());
3109 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3110 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3111 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003112 } else if (source.IsDoubleStackSlot()) {
3113 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003115 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003116 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003117 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3118 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003119 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003120 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003121 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3122 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3123 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003124 } else if (source.IsConstant()) {
3125 HConstant* constant = source.GetConstant();
3126 if (constant->IsIntConstant()) {
3127 Immediate imm(constant->AsIntConstant()->GetValue());
3128 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003129 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003130 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003131 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003132 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3133 }
3134 } else if (constant->IsLongConstant()) {
3135 int64_t value = constant->AsLongConstant()->GetValue();
3136 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003138 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003139 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003140 __ movq(CpuRegister(TMP), Immediate(value));
3141 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3142 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143 } else if (constant->IsFloatConstant()) {
3144 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3145 if (destination.IsFpuRegister()) {
3146 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003147 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148 } else {
3149 DCHECK(destination.IsStackSlot()) << destination;
3150 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3151 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003152 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003153 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3154 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3155 if (destination.IsFpuRegister()) {
3156 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003158 } else {
3159 DCHECK(destination.IsDoubleStackSlot()) << destination;
3160 __ movq(CpuRegister(TMP), imm);
3161 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3162 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003163 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003164 } else if (source.IsFpuRegister()) {
3165 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003166 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003167 } else if (destination.IsStackSlot()) {
3168 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003169 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003170 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003171 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003172 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003173 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003174 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003175 }
3176}
3177
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003178void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003179 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003180 __ movl(Address(CpuRegister(RSP), mem), reg);
3181 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003182}
3183
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003184void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003185 ScratchRegisterScope ensure_scratch(
3186 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3187
3188 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3189 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3190 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3191 Address(CpuRegister(RSP), mem2 + stack_offset));
3192 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3193 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3194 CpuRegister(ensure_scratch.GetRegister()));
3195}
3196
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003197void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3198 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3199 __ movq(Address(CpuRegister(RSP), mem), reg);
3200 __ movq(reg, CpuRegister(TMP));
3201}
3202
3203void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3204 ScratchRegisterScope ensure_scratch(
3205 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3206
3207 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3208 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3209 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3210 Address(CpuRegister(RSP), mem2 + stack_offset));
3211 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3212 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3213 CpuRegister(ensure_scratch.GetRegister()));
3214}
3215
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003216void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3217 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3218 __ movss(Address(CpuRegister(RSP), mem), reg);
3219 __ movd(reg, CpuRegister(TMP));
3220}
3221
3222void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3223 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3224 __ movsd(Address(CpuRegister(RSP), mem), reg);
3225 __ movd(reg, CpuRegister(TMP));
3226}
3227
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003228void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3229 MoveOperands* move = moves_.Get(index);
3230 Location source = move->GetSource();
3231 Location destination = move->GetDestination();
3232
3233 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003235 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003237 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003238 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003239 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003240 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3241 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003243 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003245 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3246 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003247 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3249 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3250 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003251 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003253 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003254 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003255 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003256 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003257 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003259 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003260 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003261 }
3262}
3263
3264
3265void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3266 __ pushq(CpuRegister(reg));
3267}
3268
3269
3270void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3271 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003272}
3273
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003274void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3275 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3276 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3277 Immediate(mirror::Class::kStatusInitialized));
3278 __ j(kLess, slow_path->GetEntryLabel());
3279 __ Bind(slow_path->GetExitLabel());
3280 // No need for memory fence, thanks to the X86_64 memory model.
3281}
3282
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003283void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003284 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3285 ? LocationSummary::kCallOnSlowPath
3286 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003287 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003288 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003289 locations->SetOut(Location::RequiresRegister());
3290}
3291
3292void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003294 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003295 DCHECK(!cls->CanCallRuntime());
3296 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003297 codegen_->LoadCurrentMethod(out);
3298 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3299 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003300 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003301 codegen_->LoadCurrentMethod(out);
3302 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3303 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003304 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3305 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3306 codegen_->AddSlowPath(slow_path);
3307 __ testl(out, out);
3308 __ j(kEqual, slow_path->GetEntryLabel());
3309 if (cls->MustGenerateClinitCheck()) {
3310 GenerateClassInitializationCheck(slow_path, out);
3311 } else {
3312 __ Bind(slow_path->GetExitLabel());
3313 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003314 }
3315}
3316
3317void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3318 LocationSummary* locations =
3319 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3320 locations->SetInAt(0, Location::RequiresRegister());
3321 if (check->HasUses()) {
3322 locations->SetOut(Location::SameAsFirstInput());
3323 }
3324}
3325
3326void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003327 // We assume the class to not be null.
3328 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3329 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003330 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003331 GenerateClassInitializationCheck(slow_path,
3332 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003333}
3334
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003335void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3336 LocationSummary* locations =
3337 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3338 locations->SetOut(Location::RequiresRegister());
3339}
3340
3341void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3342 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3343 codegen_->AddSlowPath(slow_path);
3344
Roland Levillain271ab9c2014-11-27 15:23:57 +00003345 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003346 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003347 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3348 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003349 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3350 __ testl(out, out);
3351 __ j(kEqual, slow_path->GetEntryLabel());
3352 __ Bind(slow_path->GetExitLabel());
3353}
3354
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003355void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3356 LocationSummary* locations =
3357 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3358 locations->SetOut(Location::RequiresRegister());
3359}
3360
3361void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3362 Address address = Address::Absolute(
3363 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003364 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003365 __ gs()->movl(address, Immediate(0));
3366}
3367
3368void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3369 LocationSummary* locations =
3370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3371 InvokeRuntimeCallingConvention calling_convention;
3372 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3373}
3374
3375void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3376 __ gs()->call(
3377 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3378 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3379}
3380
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003381void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003382 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3383 ? LocationSummary::kNoCall
3384 : LocationSummary::kCallOnSlowPath;
3385 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3386 locations->SetInAt(0, Location::RequiresRegister());
3387 locations->SetInAt(1, Location::Any());
3388 locations->SetOut(Location::RequiresRegister());
3389}
3390
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003391void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003392 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003393 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003394 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003395 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003396 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3397 Label done, zero;
3398 SlowPathCodeX86_64* slow_path = nullptr;
3399
3400 // Return 0 if `obj` is null.
3401 // TODO: avoid this check if we know obj is not null.
3402 __ testl(obj, obj);
3403 __ j(kEqual, &zero);
3404 // Compare the class of `obj` with `cls`.
3405 __ movl(out, Address(obj, class_offset));
3406 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003407 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003408 } else {
3409 DCHECK(cls.IsStackSlot()) << cls;
3410 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3411 }
3412 if (instruction->IsClassFinal()) {
3413 // Classes must be equal for the instanceof to succeed.
3414 __ j(kNotEqual, &zero);
3415 __ movl(out, Immediate(1));
3416 __ jmp(&done);
3417 } else {
3418 // If the classes are not equal, we go into a slow path.
3419 DCHECK(locations->OnlyCallsOnSlowPath());
3420 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003421 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003422 codegen_->AddSlowPath(slow_path);
3423 __ j(kNotEqual, slow_path->GetEntryLabel());
3424 __ movl(out, Immediate(1));
3425 __ jmp(&done);
3426 }
3427 __ Bind(&zero);
3428 __ movl(out, Immediate(0));
3429 if (slow_path != nullptr) {
3430 __ Bind(slow_path->GetExitLabel());
3431 }
3432 __ Bind(&done);
3433}
3434
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003435void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3437 instruction, LocationSummary::kCallOnSlowPath);
3438 locations->SetInAt(0, Location::RequiresRegister());
3439 locations->SetInAt(1, Location::Any());
3440 locations->AddTemp(Location::RequiresRegister());
3441}
3442
3443void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3444 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003446 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003447 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003448 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3449 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3450 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3451 codegen_->AddSlowPath(slow_path);
3452
3453 // TODO: avoid this check if we know obj is not null.
3454 __ testl(obj, obj);
3455 __ j(kEqual, slow_path->GetExitLabel());
3456 // Compare the class of `obj` with `cls`.
3457 __ movl(temp, Address(obj, class_offset));
3458 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003459 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003460 } else {
3461 DCHECK(cls.IsStackSlot()) << cls;
3462 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3463 }
3464 // Classes must be equal for the checkcast to succeed.
3465 __ j(kNotEqual, slow_path->GetEntryLabel());
3466 __ Bind(slow_path->GetExitLabel());
3467}
3468
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003469void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3470 LocationSummary* locations =
3471 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3472 InvokeRuntimeCallingConvention calling_convention;
3473 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3474}
3475
3476void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3477 __ gs()->call(Address::Absolute(instruction->IsEnter()
3478 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3479 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3480 true));
3481 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3482}
3483
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003484void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3485void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3486void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3487
3488void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3489 LocationSummary* locations =
3490 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3491 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3492 || instruction->GetResultType() == Primitive::kPrimLong);
3493 locations->SetInAt(0, Location::RequiresRegister());
3494 if (instruction->GetType() == Primitive::kPrimInt) {
3495 locations->SetInAt(1, Location::Any());
3496 } else {
3497 // Request a register to avoid loading a 64bits constant.
3498 locations->SetInAt(1, Location::RequiresRegister());
3499 }
3500 locations->SetOut(Location::SameAsFirstInput());
3501}
3502
3503void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3504 HandleBitwiseOperation(instruction);
3505}
3506
3507void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3508 HandleBitwiseOperation(instruction);
3509}
3510
3511void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3512 HandleBitwiseOperation(instruction);
3513}
3514
3515void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3516 LocationSummary* locations = instruction->GetLocations();
3517 Location first = locations->InAt(0);
3518 Location second = locations->InAt(1);
3519 DCHECK(first.Equals(locations->Out()));
3520
3521 if (instruction->GetResultType() == Primitive::kPrimInt) {
3522 if (second.IsRegister()) {
3523 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003524 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003525 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003526 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003527 } else {
3528 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003529 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003530 }
3531 } else if (second.IsConstant()) {
3532 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3533 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003535 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003537 } else {
3538 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003539 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003540 }
3541 } else {
3542 Address address(CpuRegister(RSP), second.GetStackIndex());
3543 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003544 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003545 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003546 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003547 } else {
3548 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003550 }
3551 }
3552 } else {
3553 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3554 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003555 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003556 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003557 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003558 } else {
3559 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003560 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003561 }
3562 }
3563}
3564
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003565} // namespace x86_64
3566} // namespace art