blob: 350392fbf4b30d6858f358086c0cbae48411eec8 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "intrinsics.h"
22#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070023#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010025#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "mirror/object_reference.h"
27#include "thread.h"
28#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/x86_64/assembler_x86_64.h"
31#include "utils/x86_64/managed_register_x86_64.h"
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace art {
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace x86_64 {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
47static constexpr size_t kRuntimeParameterFpuRegistersLength =
48 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049
Mark Mendell24f2dfa2015-01-14 19:51:45 -050050static constexpr int kC2ConditionMask = 0x400;
51
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053 public:
54 InvokeRuntimeCallingConvention()
55 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010056 kRuntimeParameterCoreRegistersLength,
57 kRuntimeParameterFpuRegisters,
58 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010059
60 private:
61 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
62};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010063
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
65
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010066class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010068 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069
70 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
71 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010072 __ gs()->call(
73 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
77 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
80};
81
Calin Juravled0d48522014-11-04 16:40:20 +000082class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
83 public:
84 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
85
86 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
87 __ Bind(GetEntryLabel());
88 __ gs()->call(
89 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
90 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
91 }
92
93 private:
94 HDivZeroCheck* const instruction_;
95 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
96};
97
Calin Juravlebacfec32014-11-14 15:54:36 +000098class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000099 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000100 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
101 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000102
103 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
104 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000105 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 if (is_div_) {
107 __ negl(cpu_reg_);
108 } else {
109 __ movl(cpu_reg_, Immediate(0));
110 }
111
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000112 } else {
113 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negq(cpu_reg_);
116 } else {
117 __ movq(cpu_reg_, Immediate(0));
118 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000119 }
Calin Juravled0d48522014-11-04 16:40:20 +0000120 __ jmp(GetExitLabel());
121 }
122
123 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000125 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 const bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100131 public:
132 StackOverflowCheckSlowPathX86_64() {}
133
134 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
135 __ Bind(GetEntryLabel());
136 __ addq(CpuRegister(RSP),
137 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
138 __ gs()->jmp(
139 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
140 }
141
142 private:
143 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
144};
145
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
149 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150
151 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100152 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000153 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100154 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
156 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100157 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 if (successor_ == nullptr) {
159 __ jmp(GetReturnLabel());
160 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100161 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 }
164
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 Label* GetReturnLabel() {
166 DCHECK(successor_ == nullptr);
167 return &return_label_;
168 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169
170 private:
171 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173 Label return_label_;
174
175 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
176};
177
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100178class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100180 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
181 Location index_location,
182 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100183 : instruction_(instruction),
184 index_location_(index_location),
185 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186
187 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000189 // We're moving two locations to locations that could overlap, so we need a parallel
190 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000192 codegen->EmitParallelMoves(
193 index_location_,
194 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
195 length_location_,
196 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 __ gs()->call(Address::Absolute(
198 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100199 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100200 }
201
202 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100203 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100204 const Location index_location_;
205 const Location length_location_;
206
207 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
208};
209
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 LoadClassSlowPathX86_64(HLoadClass* cls,
213 HInstruction* at,
214 uint32_t dex_pc,
215 bool do_clinit)
216 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
217 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
218 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219
220 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
223 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100224
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 codegen->SaveLiveRegisters(locations);
226
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100229 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000230 __ gs()->call(Address::Absolute((do_clinit_
231 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
232 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
233 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000235 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000237 if (out.IsValid()) {
238 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
239 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 }
241
242 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243 __ jmp(GetExitLabel());
244 }
245
246 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 // The class this slow path will load.
248 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100249
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 // The instruction where this slow path is happening.
251 // (Might be the load class or an initialization check).
252 HInstruction* const at_;
253
254 // The dex PC of `at_`.
255 const uint32_t dex_pc_;
256
257 // Whether to initialize the class.
258 const bool do_clinit_;
259
260 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100261};
262
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
264 public:
265 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
266
267 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
268 LocationSummary* locations = instruction_->GetLocations();
269 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
270
271 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
272 __ Bind(GetEntryLabel());
273 codegen->SaveLiveRegisters(locations);
274
275 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
277 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000278 Immediate(instruction_->GetStringIndex()));
279 __ gs()->call(Address::Absolute(
280 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
281 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
282 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
283 codegen->RestoreLiveRegisters(locations);
284 __ jmp(GetExitLabel());
285 }
286
287 private:
288 HLoadString* const instruction_;
289
290 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
291};
292
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
294 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 TypeCheckSlowPathX86_64(HInstruction* instruction,
296 Location class_to_check,
297 Location object_class,
298 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 class_to_check_(class_to_check),
301 object_class_(object_class),
302 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
304 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
305 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000306 DCHECK(instruction_->IsCheckCast()
307 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308
309 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
310 __ Bind(GetEntryLabel());
311 codegen->SaveLiveRegisters(locations);
312
313 // We're moving two locations to locations that could overlap, so we need a parallel
314 // move resolver.
315 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000316 codegen->EmitParallelMoves(
317 class_to_check_,
318 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
319 object_class_,
320 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 if (instruction_->IsInstanceOf()) {
323 __ gs()->call(
324 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
325 } else {
326 DCHECK(instruction_->IsCheckCast());
327 __ gs()->call(
328 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
329 }
330 codegen->RecordPcInfo(instruction_, dex_pc_);
331
332 if (instruction_->IsInstanceOf()) {
333 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
334 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335
336 codegen->RestoreLiveRegisters(locations);
337 __ jmp(GetExitLabel());
338 }
339
340 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000341 HInstruction* const instruction_;
342 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
347};
348
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100349#undef __
350#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
351
Dave Allison20dfc792014-06-16 20:44:29 -0700352inline Condition X86_64Condition(IfCondition cond) {
353 switch (cond) {
354 case kCondEQ: return kEqual;
355 case kCondNE: return kNotEqual;
356 case kCondLT: return kLess;
357 case kCondLE: return kLessEqual;
358 case kCondGT: return kGreater;
359 case kCondGE: return kGreaterEqual;
360 default:
361 LOG(FATAL) << "Unknown if condition";
362 }
363 return kEqual;
364}
365
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800366void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
367 CpuRegister temp) {
368 // All registers are assumed to be correctly set up.
369
370 // TODO: Implement all kinds of calls:
371 // 1) boot -> boot
372 // 2) app -> boot
373 // 3) app -> app
374 //
375 // Currently we implement the app -> app logic, which looks up in the resolve cache.
376
377 // temp = method;
378 LoadCurrentMethod(temp);
379 // temp = temp->dex_cache_resolved_methods_;
380 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
381 // temp = temp[index_in_cache]
382 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
383 // (temp + offset_of_quick_compiled_code)()
384 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
385 kX86_64WordSize).SizeValue()));
386
387 DCHECK(!IsLeafMethod());
388 RecordPcInfo(invoke, invoke->GetDexPc());
389}
390
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100391void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
392 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
393}
394
395void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
396 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
397}
398
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100399size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
400 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
401 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100402}
403
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100404size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
405 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
406 return kX86_64WordSize;
407}
408
409size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
410 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
411 return kX86_64WordSize;
412}
413
414size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
415 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
416 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100417}
418
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000419CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
420 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100421 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100422 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000423 instruction_visitor_(graph, this),
424 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100425
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100426size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
427 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
428}
429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100430InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
431 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100432 : HGraphVisitor(graph),
433 assembler_(codegen->GetAssembler()),
434 codegen_(codegen) {}
435
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100436Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100437 switch (type) {
438 case Primitive::kPrimLong:
439 case Primitive::kPrimByte:
440 case Primitive::kPrimBoolean:
441 case Primitive::kPrimChar:
442 case Primitive::kPrimShort:
443 case Primitive::kPrimInt:
444 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100445 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100446 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447 }
448
449 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100450 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100452 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100453 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454
455 case Primitive::kPrimVoid:
456 LOG(FATAL) << "Unreachable type " << type;
457 }
458
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100459 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100460}
461
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100462void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100463 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100464 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100465
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000466 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000468
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000469 // TODO: We currently don't use Quick's callee saved registers.
470 blocked_core_registers_[RBX] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100471 blocked_core_registers_[RBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000472 blocked_core_registers_[R12] = true;
473 blocked_core_registers_[R13] = true;
474 blocked_core_registers_[R14] = true;
475 blocked_core_registers_[R15] = true;
476
477 blocked_fpu_registers_[XMM12] = true;
478 blocked_fpu_registers_[XMM13] = true;
479 blocked_fpu_registers_[XMM14] = true;
480 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100481}
482
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100483void CodeGeneratorX86_64::GenerateFrameEntry() {
484 // Create a fake register to mimic Quick.
485 static const int kFakeReturnRegister = 16;
486 core_spill_mask_ |= (1 << kFakeReturnRegister);
487
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100488 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700489 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Calin Juravle93edf732015-01-20 20:14:07 +0000490 bool implicitStackOverflowChecks = GetCompilerOptions().GetImplicitStackOverflowChecks();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100491
Calin Juravle93edf732015-01-20 20:14:07 +0000492 if (!skip_overflow_check && implicitStackOverflowChecks) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100493 __ testq(CpuRegister(RAX), Address(
494 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100495 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100496 }
497
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100498 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100499 __ subq(CpuRegister(RSP),
500 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
501
Calin Juravle93edf732015-01-20 20:14:07 +0000502 if (!skip_overflow_check && !implicitStackOverflowChecks) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100503 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100504 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100505
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100506 __ gs()->cmpq(CpuRegister(RSP),
507 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
508 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100509 }
510
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
512}
513
514void CodeGeneratorX86_64::GenerateFrameExit() {
515 __ addq(CpuRegister(RSP),
516 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
517}
518
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100519void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
520 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100521}
522
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100523void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100524 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
525}
526
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100527Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
528 switch (load->GetType()) {
529 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100530 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
532 break;
533
534 case Primitive::kPrimInt:
535 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100538
539 case Primitive::kPrimBoolean:
540 case Primitive::kPrimByte:
541 case Primitive::kPrimChar:
542 case Primitive::kPrimShort:
543 case Primitive::kPrimVoid:
544 LOG(FATAL) << "Unexpected type " << load->GetType();
545 }
546
547 LOG(FATAL) << "Unreachable";
548 return Location();
549}
550
551void CodeGeneratorX86_64::Move(Location destination, Location source) {
552 if (source.Equals(destination)) {
553 return;
554 }
555 if (destination.IsRegister()) {
556 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000557 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000559 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000561 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100562 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 } else {
564 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000565 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100566 Address(CpuRegister(RSP), source.GetStackIndex()));
567 }
568 } else if (destination.IsFpuRegister()) {
569 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000570 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000572 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000574 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 Address(CpuRegister(RSP), source.GetStackIndex()));
576 } else {
577 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000578 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100579 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 }
581 } else if (destination.IsStackSlot()) {
582 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000584 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 } else if (source.IsFpuRegister()) {
586 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500588 } else if (source.IsConstant()) {
589 HConstant* constant = source.GetConstant();
590 int32_t value;
591 if (constant->IsFloatConstant()) {
592 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
593 } else {
594 DCHECK(constant->IsIntConstant());
595 value = constant->AsIntConstant()->GetValue();
596 }
597 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100598 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500599 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000600 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
601 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100602 }
603 } else {
604 DCHECK(destination.IsDoubleStackSlot());
605 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100606 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
609 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000610 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500611 } else if (source.IsConstant()) {
612 HConstant* constant = source.GetConstant();
613 int64_t value = constant->AsLongConstant()->GetValue();
614 if (constant->IsDoubleConstant()) {
615 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
616 } else {
617 DCHECK(constant->IsLongConstant());
618 value = constant->AsLongConstant()->GetValue();
619 }
620 __ movq(CpuRegister(TMP), Immediate(value));
621 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100622 } else {
623 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000624 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
625 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100626 }
627 }
628}
629
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100630void CodeGeneratorX86_64::Move(HInstruction* instruction,
631 Location location,
632 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000633 LocationSummary* locations = instruction->GetLocations();
634 if (locations != nullptr && locations->Out().Equals(location)) {
635 return;
636 }
637
638 if (locations != nullptr && locations->Out().IsConstant()) {
639 HConstant* const_to_move = locations->Out().GetConstant();
640 if (const_to_move->IsIntConstant()) {
641 Immediate imm(const_to_move->AsIntConstant()->GetValue());
642 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000643 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000644 } else if (location.IsStackSlot()) {
645 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
646 } else {
647 DCHECK(location.IsConstant());
648 DCHECK_EQ(location.GetConstant(), const_to_move);
649 }
650 } else if (const_to_move->IsLongConstant()) {
651 int64_t value = const_to_move->AsLongConstant()->GetValue();
652 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000654 } else if (location.IsDoubleStackSlot()) {
655 __ movq(CpuRegister(TMP), Immediate(value));
656 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
657 } else {
658 DCHECK(location.IsConstant());
659 DCHECK_EQ(location.GetConstant(), const_to_move);
660 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100661 }
Roland Levillain476df552014-10-09 17:51:36 +0100662 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663 switch (instruction->GetType()) {
664 case Primitive::kPrimBoolean:
665 case Primitive::kPrimByte:
666 case Primitive::kPrimChar:
667 case Primitive::kPrimShort:
668 case Primitive::kPrimInt:
669 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100671 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
672 break;
673
674 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000676 Move(location,
677 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100678 break;
679
680 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100681 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000683 } else if (instruction->IsTemporary()) {
684 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
685 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100688 switch (instruction->GetType()) {
689 case Primitive::kPrimBoolean:
690 case Primitive::kPrimByte:
691 case Primitive::kPrimChar:
692 case Primitive::kPrimShort:
693 case Primitive::kPrimInt:
694 case Primitive::kPrimNot:
695 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100696 case Primitive::kPrimFloat:
697 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000698 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100699 break;
700
701 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100702 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100703 }
704 }
705}
706
707void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
708 got->SetLocations(nullptr);
709}
710
711void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
712 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100713 DCHECK(!successor->IsExitBlock());
714
715 HBasicBlock* block = got->GetBlock();
716 HInstruction* previous = got->GetPrevious();
717
718 HLoopInformation* info = block->GetLoopInformation();
719 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
720 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
721 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
722 return;
723 }
724
725 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
726 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
727 }
728 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100729 __ jmp(codegen_->GetLabelOf(successor));
730 }
731}
732
733void LocationsBuilderX86_64::VisitExit(HExit* exit) {
734 exit->SetLocations(nullptr);
735}
736
737void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700738 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100739 if (kIsDebugBuild) {
740 __ Comment("Unreachable");
741 __ int3();
742 }
743}
744
745void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100746 LocationSummary* locations =
747 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100748 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100749 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100750 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100751 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752}
753
754void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700755 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100756 if (cond->IsIntConstant()) {
757 // Constant condition, statically compared against 1.
758 int32_t cond_value = cond->AsIntConstant()->GetValue();
759 if (cond_value == 1) {
760 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
761 if_instr->IfTrueSuccessor())) {
762 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100763 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100764 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100765 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100766 DCHECK_EQ(cond_value, 0);
767 }
768 } else {
769 bool materialized =
770 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
771 // Moves do not affect the eflags register, so if the condition is
772 // evaluated just before the if, we don't need to evaluate it
773 // again.
774 bool eflags_set = cond->IsCondition()
775 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
776 if (materialized) {
777 if (!eflags_set) {
778 // Materialized condition, compare against 0.
779 Location lhs = if_instr->GetLocations()->InAt(0);
780 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000781 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100782 } else {
783 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
784 Immediate(0));
785 }
786 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
787 } else {
788 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
789 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
790 }
791 } else {
792 Location lhs = cond->GetLocations()->InAt(0);
793 Location rhs = cond->GetLocations()->InAt(1);
794 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000795 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100798 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
799 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000800 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100801 Address(CpuRegister(RSP), rhs.GetStackIndex()));
802 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100803 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
804 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700805 }
Dave Allison20dfc792014-06-16 20:44:29 -0700806 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
808 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700809 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100810 }
811}
812
813void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
814 local->SetLocations(nullptr);
815}
816
817void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
818 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
819}
820
821void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
822 local->SetLocations(nullptr);
823}
824
825void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
826 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700827 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100828}
829
830void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100831 LocationSummary* locations =
832 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100833 switch (store->InputAt(1)->GetType()) {
834 case Primitive::kPrimBoolean:
835 case Primitive::kPrimByte:
836 case Primitive::kPrimChar:
837 case Primitive::kPrimShort:
838 case Primitive::kPrimInt:
839 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100840 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100841 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
842 break;
843
844 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
847 break;
848
849 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100850 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100852}
853
854void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700855 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100856}
857
Dave Allison20dfc792014-06-16 20:44:29 -0700858void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100859 LocationSummary* locations =
860 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100861 locations->SetInAt(0, Location::RequiresRegister());
862 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100863 if (comp->NeedsMaterialization()) {
864 locations->SetOut(Location::RequiresRegister());
865 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100866}
867
Dave Allison20dfc792014-06-16 20:44:29 -0700868void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
869 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100870 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000871 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100872 // Clear register: setcc only sets the low byte.
873 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100874 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000875 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
876 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100877 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100879 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
880 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000881 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100882 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
883 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100884 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700885 }
886}
887
888void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
889 VisitCondition(comp);
890}
891
892void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
893 VisitCondition(comp);
894}
895
896void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
897 VisitCondition(comp);
898}
899
900void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
901 VisitCondition(comp);
902}
903
904void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
905 VisitCondition(comp);
906}
907
908void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
909 VisitCondition(comp);
910}
911
912void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
913 VisitCondition(comp);
914}
915
916void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
917 VisitCondition(comp);
918}
919
920void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
921 VisitCondition(comp);
922}
923
924void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
925 VisitCondition(comp);
926}
927
928void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
929 VisitCondition(comp);
930}
931
932void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
933 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100934}
935
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100936void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100937 LocationSummary* locations =
938 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000939 switch (compare->InputAt(0)->GetType()) {
940 case Primitive::kPrimLong: {
941 locations->SetInAt(0, Location::RequiresRegister());
942 locations->SetInAt(1, Location::RequiresRegister());
943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
944 break;
945 }
946 case Primitive::kPrimFloat:
947 case Primitive::kPrimDouble: {
948 locations->SetInAt(0, Location::RequiresFpuRegister());
949 locations->SetInAt(1, Location::RequiresFpuRegister());
950 locations->SetOut(Location::RequiresRegister());
951 break;
952 }
953 default:
954 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
955 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100956}
957
958void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100959 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000960 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000961 Location left = locations->InAt(0);
962 Location right = locations->InAt(1);
963
964 Label less, greater, done;
965 Primitive::Type type = compare->InputAt(0)->GetType();
966 switch (type) {
967 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000968 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100969 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000970 }
971 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000972 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000973 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
974 break;
975 }
976 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000977 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000978 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
979 break;
980 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100981 default:
Calin Juravleddb7df22014-11-25 20:56:51 +0000982 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100983 }
Calin Juravleddb7df22014-11-25 20:56:51 +0000984 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +0000985 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +0000986 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +0000987
Calin Juravle91debbc2014-11-26 19:01:09 +0000988 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +0000989 __ movl(out, Immediate(1));
990 __ jmp(&done);
991
992 __ Bind(&less);
993 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100994
995 __ Bind(&done);
996}
997
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100999 LocationSummary* locations =
1000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001001 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002}
1003
1004void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001005 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007}
1008
1009void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001010 LocationSummary* locations =
1011 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001012 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013}
1014
1015void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001016 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001017 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001018}
1019
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001020void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1021 LocationSummary* locations =
1022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1023 locations->SetOut(Location::ConstantLocation(constant));
1024}
1025
1026void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1027 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001029}
1030
1031void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1034 locations->SetOut(Location::ConstantLocation(constant));
1035}
1036
1037void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1038 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001039 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001040}
1041
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1043 ret->SetLocations(nullptr);
1044}
1045
1046void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001047 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001048 codegen_->GenerateFrameExit();
1049 __ ret();
1050}
1051
1052void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001053 LocationSummary* locations =
1054 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001055 switch (ret->InputAt(0)->GetType()) {
1056 case Primitive::kPrimBoolean:
1057 case Primitive::kPrimByte:
1058 case Primitive::kPrimChar:
1059 case Primitive::kPrimShort:
1060 case Primitive::kPrimInt:
1061 case Primitive::kPrimNot:
1062 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001064 break;
1065
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001066 case Primitive::kPrimFloat:
1067 case Primitive::kPrimDouble:
1068 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001070 break;
1071
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001072 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001074 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075}
1076
1077void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1078 if (kIsDebugBuild) {
1079 switch (ret->InputAt(0)->GetType()) {
1080 case Primitive::kPrimBoolean:
1081 case Primitive::kPrimByte:
1082 case Primitive::kPrimChar:
1083 case Primitive::kPrimShort:
1084 case Primitive::kPrimInt:
1085 case Primitive::kPrimNot:
1086 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001087 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088 break;
1089
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 case Primitive::kPrimFloat:
1091 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001092 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 XMM0);
1094 break;
1095
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001096 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098 }
1099 }
1100 codegen_->GenerateFrameExit();
1101 __ ret();
1102}
1103
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1105 switch (type) {
1106 case Primitive::kPrimBoolean:
1107 case Primitive::kPrimByte:
1108 case Primitive::kPrimChar:
1109 case Primitive::kPrimShort:
1110 case Primitive::kPrimInt:
1111 case Primitive::kPrimNot: {
1112 uint32_t index = gp_index_++;
1113 stack_index_++;
1114 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001115 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116 } else {
1117 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1118 }
1119 }
1120
1121 case Primitive::kPrimLong: {
1122 uint32_t index = gp_index_;
1123 stack_index_ += 2;
1124 if (index < calling_convention.GetNumberOfRegisters()) {
1125 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001126 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001127 } else {
1128 gp_index_ += 2;
1129 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1130 }
1131 }
1132
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 case Primitive::kPrimFloat: {
1134 uint32_t index = fp_index_++;
1135 stack_index_++;
1136 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001137 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001138 } else {
1139 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1140 }
1141 }
1142
1143 case Primitive::kPrimDouble: {
1144 uint32_t index = fp_index_++;
1145 stack_index_ += 2;
1146 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 } else {
1149 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1150 }
1151 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152
1153 case Primitive::kPrimVoid:
1154 LOG(FATAL) << "Unexpected parameter type " << type;
1155 break;
1156 }
1157 return Location();
1158}
1159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001160void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001161 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1162 if (intrinsic.TryDispatch(invoke)) {
1163 return;
1164 }
1165
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001166 HandleInvoke(invoke);
1167}
1168
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001169static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1170 if (invoke->GetLocations()->Intrinsified()) {
1171 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1172 intrinsic.Dispatch(invoke);
1173 return true;
1174 }
1175 return false;
1176}
1177
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001178void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001179 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1180 return;
1181 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001182
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001183 codegen_->GenerateStaticOrDirectCall(
1184 invoke,
1185 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001186}
1187
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001188void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001189 LocationSummary* locations =
1190 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001191 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001192
1193 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001194 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195 HInstruction* input = invoke->InputAt(i);
1196 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1197 }
1198
1199 switch (invoke->GetType()) {
1200 case Primitive::kPrimBoolean:
1201 case Primitive::kPrimByte:
1202 case Primitive::kPrimChar:
1203 case Primitive::kPrimShort:
1204 case Primitive::kPrimInt:
1205 case Primitive::kPrimNot:
1206 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001207 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001208 break;
1209
1210 case Primitive::kPrimVoid:
1211 break;
1212
1213 case Primitive::kPrimDouble:
1214 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001215 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216 break;
1217 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218}
1219
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001221 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1222 if (intrinsic.TryDispatch(invoke)) {
1223 return;
1224 }
1225
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001226 HandleInvoke(invoke);
1227}
1228
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001229void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001230 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1231 return;
1232 }
1233
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001235 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1236 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1237 LocationSummary* locations = invoke->GetLocations();
1238 Location receiver = locations->InAt(0);
1239 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1240 // temp = object->GetClass();
1241 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1243 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001244 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001245 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001246 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001247 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001248 // temp = temp->GetMethodAt(method_offset);
1249 __ movl(temp, Address(temp, method_offset));
1250 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001251 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001252 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001254 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001255 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001256}
1257
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001258void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1259 HandleInvoke(invoke);
1260 // Add the hidden argument.
1261 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1262}
1263
1264void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1265 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001266 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001267 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1268 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1269 LocationSummary* locations = invoke->GetLocations();
1270 Location receiver = locations->InAt(0);
1271 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1272
1273 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001274 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001275 Immediate(invoke->GetDexMethodIndex()));
1276
1277 // temp = object->GetClass();
1278 if (receiver.IsStackSlot()) {
1279 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1280 __ movl(temp, Address(temp, class_offset));
1281 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001282 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001283 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001284 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001285 // temp = temp->GetImtEntryAt(method_offset);
1286 __ movl(temp, Address(temp, method_offset));
1287 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001288 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001289 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001290
1291 DCHECK(!codegen_->IsLeafMethod());
1292 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1293}
1294
Roland Levillain88cb1752014-10-20 16:36:47 +01001295void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1296 LocationSummary* locations =
1297 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1298 switch (neg->GetResultType()) {
1299 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001300 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001301 locations->SetInAt(0, Location::RequiresRegister());
1302 locations->SetOut(Location::SameAsFirstInput());
1303 break;
1304
Roland Levillain88cb1752014-10-20 16:36:47 +01001305 case Primitive::kPrimFloat:
1306 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001307 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001308 locations->SetOut(Location::SameAsFirstInput());
1309 locations->AddTemp(Location::RequiresRegister());
1310 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001311 break;
1312
1313 default:
1314 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1315 }
1316}
1317
1318void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1319 LocationSummary* locations = neg->GetLocations();
1320 Location out = locations->Out();
1321 Location in = locations->InAt(0);
1322 switch (neg->GetResultType()) {
1323 case Primitive::kPrimInt:
1324 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001325 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001327 break;
1328
1329 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001330 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001331 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001332 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001333 break;
1334
Roland Levillain5368c212014-11-27 15:03:41 +00001335 case Primitive::kPrimFloat: {
1336 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001337 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1338 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001339 // Implement float negation with an exclusive or with value
1340 // 0x80000000 (mask for bit 31, representing the sign of a
1341 // single-precision floating-point number).
1342 __ movq(constant, Immediate(INT64_C(0x80000000)));
1343 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001344 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001345 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001346 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001347
Roland Levillain5368c212014-11-27 15:03:41 +00001348 case Primitive::kPrimDouble: {
1349 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001350 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1351 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001352 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001353 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001354 // a double-precision floating-point number).
1355 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1356 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001358 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001359 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001360
1361 default:
1362 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1363 }
1364}
1365
Roland Levillaindff1f282014-11-05 14:15:05 +00001366void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1367 LocationSummary* locations =
1368 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1369 Primitive::Type result_type = conversion->GetResultType();
1370 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001371 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001372 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001373 case Primitive::kPrimByte:
1374 switch (input_type) {
1375 case Primitive::kPrimShort:
1376 case Primitive::kPrimInt:
1377 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001378 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001379 locations->SetInAt(0, Location::Any());
1380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1381 break;
1382
1383 default:
1384 LOG(FATAL) << "Unexpected type conversion from " << input_type
1385 << " to " << result_type;
1386 }
1387 break;
1388
Roland Levillain01a8d712014-11-14 16:27:39 +00001389 case Primitive::kPrimShort:
1390 switch (input_type) {
1391 case Primitive::kPrimByte:
1392 case Primitive::kPrimInt:
1393 case Primitive::kPrimChar:
1394 // Processing a Dex `int-to-short' instruction.
1395 locations->SetInAt(0, Location::Any());
1396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1397 break;
1398
1399 default:
1400 LOG(FATAL) << "Unexpected type conversion from " << input_type
1401 << " to " << result_type;
1402 }
1403 break;
1404
Roland Levillain946e1432014-11-11 17:35:19 +00001405 case Primitive::kPrimInt:
1406 switch (input_type) {
1407 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001408 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001409 locations->SetInAt(0, Location::Any());
1410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1411 break;
1412
1413 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001414 // Processing a Dex `float-to-int' instruction.
1415 locations->SetInAt(0, Location::RequiresFpuRegister());
1416 locations->SetOut(Location::RequiresRegister());
1417 locations->AddTemp(Location::RequiresFpuRegister());
1418 break;
1419
Roland Levillain946e1432014-11-11 17:35:19 +00001420 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001421 // Processing a Dex `double-to-int' instruction.
1422 locations->SetInAt(0, Location::RequiresFpuRegister());
1423 locations->SetOut(Location::RequiresRegister());
1424 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001425 break;
1426
1427 default:
1428 LOG(FATAL) << "Unexpected type conversion from " << input_type
1429 << " to " << result_type;
1430 }
1431 break;
1432
Roland Levillaindff1f282014-11-05 14:15:05 +00001433 case Primitive::kPrimLong:
1434 switch (input_type) {
1435 case Primitive::kPrimByte:
1436 case Primitive::kPrimShort:
1437 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001438 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001439 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001440 // TODO: We would benefit from a (to-be-implemented)
1441 // Location::RegisterOrStackSlot requirement for this input.
1442 locations->SetInAt(0, Location::RequiresRegister());
1443 locations->SetOut(Location::RequiresRegister());
1444 break;
1445
1446 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001447 // Processing a Dex `float-to-long' instruction.
1448 locations->SetInAt(0, Location::RequiresFpuRegister());
1449 locations->SetOut(Location::RequiresRegister());
1450 locations->AddTemp(Location::RequiresFpuRegister());
1451 break;
1452
Roland Levillaindff1f282014-11-05 14:15:05 +00001453 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001454 // Processing a Dex `double-to-long' instruction.
1455 locations->SetInAt(0, Location::RequiresFpuRegister());
1456 locations->SetOut(Location::RequiresRegister());
1457 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001458 break;
1459
1460 default:
1461 LOG(FATAL) << "Unexpected type conversion from " << input_type
1462 << " to " << result_type;
1463 }
1464 break;
1465
Roland Levillain981e4542014-11-14 11:47:14 +00001466 case Primitive::kPrimChar:
1467 switch (input_type) {
1468 case Primitive::kPrimByte:
1469 case Primitive::kPrimShort:
1470 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001471 // Processing a Dex `int-to-char' instruction.
1472 locations->SetInAt(0, Location::Any());
1473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1474 break;
1475
1476 default:
1477 LOG(FATAL) << "Unexpected type conversion from " << input_type
1478 << " to " << result_type;
1479 }
1480 break;
1481
Roland Levillaindff1f282014-11-05 14:15:05 +00001482 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001483 switch (input_type) {
1484 case Primitive::kPrimByte:
1485 case Primitive::kPrimShort:
1486 case Primitive::kPrimInt:
1487 case Primitive::kPrimChar:
1488 // Processing a Dex `int-to-float' instruction.
1489 locations->SetInAt(0, Location::RequiresRegister());
1490 locations->SetOut(Location::RequiresFpuRegister());
1491 break;
1492
1493 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001494 // Processing a Dex `long-to-float' instruction.
1495 locations->SetInAt(0, Location::RequiresRegister());
1496 locations->SetOut(Location::RequiresFpuRegister());
1497 break;
1498
Roland Levillaincff13742014-11-17 14:32:17 +00001499 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001500 // Processing a Dex `double-to-float' instruction.
1501 locations->SetInAt(0, Location::RequiresFpuRegister());
1502 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001503 break;
1504
1505 default:
1506 LOG(FATAL) << "Unexpected type conversion from " << input_type
1507 << " to " << result_type;
1508 };
1509 break;
1510
Roland Levillaindff1f282014-11-05 14:15:05 +00001511 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001512 switch (input_type) {
1513 case Primitive::kPrimByte:
1514 case Primitive::kPrimShort:
1515 case Primitive::kPrimInt:
1516 case Primitive::kPrimChar:
1517 // Processing a Dex `int-to-double' instruction.
1518 locations->SetInAt(0, Location::RequiresRegister());
1519 locations->SetOut(Location::RequiresFpuRegister());
1520 break;
1521
1522 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001523 // Processing a Dex `long-to-double' instruction.
1524 locations->SetInAt(0, Location::RequiresRegister());
1525 locations->SetOut(Location::RequiresFpuRegister());
1526 break;
1527
Roland Levillaincff13742014-11-17 14:32:17 +00001528 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001529 // Processing a Dex `float-to-double' instruction.
1530 locations->SetInAt(0, Location::RequiresFpuRegister());
1531 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001532 break;
1533
1534 default:
1535 LOG(FATAL) << "Unexpected type conversion from " << input_type
1536 << " to " << result_type;
1537 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001538 break;
1539
1540 default:
1541 LOG(FATAL) << "Unexpected type conversion from " << input_type
1542 << " to " << result_type;
1543 }
1544}
1545
1546void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1547 LocationSummary* locations = conversion->GetLocations();
1548 Location out = locations->Out();
1549 Location in = locations->InAt(0);
1550 Primitive::Type result_type = conversion->GetResultType();
1551 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001552 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001553 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001554 case Primitive::kPrimByte:
1555 switch (input_type) {
1556 case Primitive::kPrimShort:
1557 case Primitive::kPrimInt:
1558 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001559 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001560 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001561 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001562 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001563 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +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 Levillain51d3fc42014-11-13 14:11:42 +00001568 Immediate(static_cast<int8_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 Levillain01a8d712014-11-14 16:27:39 +00001578 case Primitive::kPrimShort:
1579 switch (input_type) {
1580 case Primitive::kPrimByte:
1581 case Primitive::kPrimInt:
1582 case Primitive::kPrimChar:
1583 // Processing a Dex `int-to-short' instruction.
1584 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001585 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001586 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001587 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001588 Address(CpuRegister(RSP), in.GetStackIndex()));
1589 } else {
1590 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001591 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001592 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1593 }
1594 break;
1595
1596 default:
1597 LOG(FATAL) << "Unexpected type conversion from " << input_type
1598 << " to " << result_type;
1599 }
1600 break;
1601
Roland Levillain946e1432014-11-11 17:35:19 +00001602 case Primitive::kPrimInt:
1603 switch (input_type) {
1604 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001605 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001606 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001608 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001609 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001610 Address(CpuRegister(RSP), in.GetStackIndex()));
1611 } else {
1612 DCHECK(in.IsConstant());
1613 DCHECK(in.GetConstant()->IsLongConstant());
1614 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001615 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001616 }
1617 break;
1618
Roland Levillain3f8f9362014-12-02 17:45:01 +00001619 case Primitive::kPrimFloat: {
1620 // Processing a Dex `float-to-int' instruction.
1621 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1622 CpuRegister output = out.AsRegister<CpuRegister>();
1623 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1624 Label done, nan;
1625
1626 __ movl(output, Immediate(kPrimIntMax));
1627 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001628 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001629 // if input >= temp goto done
1630 __ comiss(input, temp);
1631 __ j(kAboveEqual, &done);
1632 // if input == NaN goto nan
1633 __ j(kUnordered, &nan);
1634 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001635 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001636 __ jmp(&done);
1637 __ Bind(&nan);
1638 // output = 0
1639 __ xorl(output, output);
1640 __ Bind(&done);
1641 break;
1642 }
1643
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001644 case Primitive::kPrimDouble: {
1645 // Processing a Dex `double-to-int' instruction.
1646 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1647 CpuRegister output = out.AsRegister<CpuRegister>();
1648 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1649 Label done, nan;
1650
1651 __ movl(output, Immediate(kPrimIntMax));
1652 // temp = int-to-double(output)
1653 __ cvtsi2sd(temp, output);
1654 // if input >= temp goto done
1655 __ comisd(input, temp);
1656 __ j(kAboveEqual, &done);
1657 // if input == NaN goto nan
1658 __ j(kUnordered, &nan);
1659 // output = double-to-int-truncate(input)
1660 __ cvttsd2si(output, input);
1661 __ jmp(&done);
1662 __ Bind(&nan);
1663 // output = 0
1664 __ xorl(output, output);
1665 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001666 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001667 }
Roland Levillain946e1432014-11-11 17:35:19 +00001668
1669 default:
1670 LOG(FATAL) << "Unexpected type conversion from " << input_type
1671 << " to " << result_type;
1672 }
1673 break;
1674
Roland Levillaindff1f282014-11-05 14:15:05 +00001675 case Primitive::kPrimLong:
1676 switch (input_type) {
1677 DCHECK(out.IsRegister());
1678 case Primitive::kPrimByte:
1679 case Primitive::kPrimShort:
1680 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001681 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001682 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001683 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001685 break;
1686
Roland Levillain624279f2014-12-04 11:54:28 +00001687 case Primitive::kPrimFloat: {
1688 // Processing a Dex `float-to-long' instruction.
1689 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1690 CpuRegister output = out.AsRegister<CpuRegister>();
1691 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1692 Label done, nan;
1693
1694 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001695 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001696 __ cvtsi2ss(temp, output, true);
1697 // if input >= temp goto done
1698 __ comiss(input, temp);
1699 __ j(kAboveEqual, &done);
1700 // if input == NaN goto nan
1701 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001702 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001703 __ cvttss2si(output, input, true);
1704 __ jmp(&done);
1705 __ Bind(&nan);
1706 // output = 0
1707 __ xorq(output, output);
1708 __ Bind(&done);
1709 break;
1710 }
1711
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001712 case Primitive::kPrimDouble: {
1713 // Processing a Dex `double-to-long' instruction.
1714 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1715 CpuRegister output = out.AsRegister<CpuRegister>();
1716 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1717 Label done, nan;
1718
1719 __ movq(output, Immediate(kPrimLongMax));
1720 // temp = long-to-double(output)
1721 __ cvtsi2sd(temp, output, true);
1722 // if input >= temp goto done
1723 __ comisd(input, temp);
1724 __ j(kAboveEqual, &done);
1725 // if input == NaN goto nan
1726 __ j(kUnordered, &nan);
1727 // output = double-to-long-truncate(input)
1728 __ cvttsd2si(output, input, true);
1729 __ jmp(&done);
1730 __ Bind(&nan);
1731 // output = 0
1732 __ xorq(output, output);
1733 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001734 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001735 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001736
1737 default:
1738 LOG(FATAL) << "Unexpected type conversion from " << input_type
1739 << " to " << result_type;
1740 }
1741 break;
1742
Roland Levillain981e4542014-11-14 11:47:14 +00001743 case Primitive::kPrimChar:
1744 switch (input_type) {
1745 case Primitive::kPrimByte:
1746 case Primitive::kPrimShort:
1747 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001748 // Processing a Dex `int-to-char' instruction.
1749 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001751 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001752 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001753 Address(CpuRegister(RSP), in.GetStackIndex()));
1754 } else {
1755 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001757 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1758 }
1759 break;
1760
1761 default:
1762 LOG(FATAL) << "Unexpected type conversion from " << input_type
1763 << " to " << result_type;
1764 }
1765 break;
1766
Roland Levillaindff1f282014-11-05 14:15:05 +00001767 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001768 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001769 case Primitive::kPrimByte:
1770 case Primitive::kPrimShort:
1771 case Primitive::kPrimInt:
1772 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001773 // Processing a Dex `int-to-float' instruction.
1774 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001775 break;
1776
1777 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001778 // Processing a Dex `long-to-float' instruction.
1779 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1780 break;
1781
Roland Levillaincff13742014-11-17 14:32:17 +00001782 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001783 // Processing a Dex `double-to-float' instruction.
1784 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001785 break;
1786
1787 default:
1788 LOG(FATAL) << "Unexpected type conversion from " << input_type
1789 << " to " << result_type;
1790 };
1791 break;
1792
Roland Levillaindff1f282014-11-05 14:15:05 +00001793 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001794 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001795 case Primitive::kPrimByte:
1796 case Primitive::kPrimShort:
1797 case Primitive::kPrimInt:
1798 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001799 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001801 break;
1802
1803 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001804 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001805 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001806 break;
1807
Roland Levillaincff13742014-11-17 14:32:17 +00001808 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001809 // Processing a Dex `float-to-double' instruction.
1810 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unexpected type conversion from " << input_type
1815 << " to " << result_type;
1816 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001817 break;
1818
1819 default:
1820 LOG(FATAL) << "Unexpected type conversion from " << input_type
1821 << " to " << result_type;
1822 }
1823}
1824
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001825void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001826 LocationSummary* locations =
1827 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001828 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001829 case Primitive::kPrimInt: {
1830 locations->SetInAt(0, Location::RequiresRegister());
1831 locations->SetInAt(1, Location::Any());
1832 locations->SetOut(Location::SameAsFirstInput());
1833 break;
1834 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001835
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001836 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001837 locations->SetInAt(0, Location::RequiresRegister());
1838 locations->SetInAt(1, Location::RequiresRegister());
1839 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001840 break;
1841 }
1842
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001843 case Primitive::kPrimDouble:
1844 case Primitive::kPrimFloat: {
1845 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001846 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001847 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001848 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001849 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001850
1851 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001853 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001854}
1855
1856void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1857 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 Location first = locations->InAt(0);
1859 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001860 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001861
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001862 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001863 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001864 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001865 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001866 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001867 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001870 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001872 break;
1873 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001874
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001875 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001877 break;
1878 }
1879
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001881 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001882 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001883 }
1884
1885 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001886 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887 break;
1888 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001889
1890 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001892 }
1893}
1894
1895void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001898 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001899 case Primitive::kPrimInt: {
1900 locations->SetInAt(0, Location::RequiresRegister());
1901 locations->SetInAt(1, Location::Any());
1902 locations->SetOut(Location::SameAsFirstInput());
1903 break;
1904 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001905 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001906 locations->SetInAt(0, Location::RequiresRegister());
1907 locations->SetInAt(1, Location::RequiresRegister());
1908 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001909 break;
1910 }
Calin Juravle11351682014-10-23 15:38:15 +01001911 case Primitive::kPrimFloat:
1912 case Primitive::kPrimDouble: {
1913 locations->SetInAt(0, Location::RequiresFpuRegister());
1914 locations->SetInAt(1, Location::RequiresFpuRegister());
1915 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001916 break;
Calin Juravle11351682014-10-23 15:38:15 +01001917 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001918 default:
Calin Juravle11351682014-10-23 15:38:15 +01001919 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001920 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001921}
1922
1923void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1924 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001925 Location first = locations->InAt(0);
1926 Location second = locations->InAt(1);
1927 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001928 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001929 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001930 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001931 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001932 } else if (second.IsConstant()) {
1933 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001935 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001937 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001938 break;
1939 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001940 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001941 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001942 break;
1943 }
1944
Calin Juravle11351682014-10-23 15:38:15 +01001945 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001946 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001947 break;
Calin Juravle11351682014-10-23 15:38:15 +01001948 }
1949
1950 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001951 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001952 break;
1953 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954
1955 default:
Calin Juravle11351682014-10-23 15:38:15 +01001956 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001957 }
1958}
1959
Calin Juravle34bacdf2014-10-07 20:23:36 +01001960void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1961 LocationSummary* locations =
1962 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1963 switch (mul->GetResultType()) {
1964 case Primitive::kPrimInt: {
1965 locations->SetInAt(0, Location::RequiresRegister());
1966 locations->SetInAt(1, Location::Any());
1967 locations->SetOut(Location::SameAsFirstInput());
1968 break;
1969 }
1970 case Primitive::kPrimLong: {
1971 locations->SetInAt(0, Location::RequiresRegister());
1972 locations->SetInAt(1, Location::RequiresRegister());
1973 locations->SetOut(Location::SameAsFirstInput());
1974 break;
1975 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001976 case Primitive::kPrimFloat:
1977 case Primitive::kPrimDouble: {
1978 locations->SetInAt(0, Location::RequiresFpuRegister());
1979 locations->SetInAt(1, Location::RequiresFpuRegister());
1980 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001981 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001982 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001983
1984 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001985 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001986 }
1987}
1988
1989void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1990 LocationSummary* locations = mul->GetLocations();
1991 Location first = locations->InAt(0);
1992 Location second = locations->InAt(1);
1993 DCHECK(first.Equals(locations->Out()));
1994 switch (mul->GetResultType()) {
1995 case Primitive::kPrimInt: {
1996 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001997 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001998 } else if (second.IsConstant()) {
1999 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002001 } else {
2002 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002003 __ imull(first.AsRegister<CpuRegister>(),
2004 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002005 }
2006 break;
2007 }
2008 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002009 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002010 break;
2011 }
2012
Calin Juravleb5bfa962014-10-21 18:02:24 +01002013 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002014 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002015 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002016 }
2017
2018 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002019 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002020 break;
2021 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002022
2023 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002024 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002025 }
2026}
2027
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002028void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2029 uint32_t stack_adjustment, bool is_float) {
2030 if (source.IsStackSlot()) {
2031 DCHECK(is_float);
2032 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2033 } else if (source.IsDoubleStackSlot()) {
2034 DCHECK(!is_float);
2035 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2036 } else {
2037 // Write the value to the temporary location on the stack and load to FP stack.
2038 if (is_float) {
2039 Location stack_temp = Location::StackSlot(temp_offset);
2040 codegen_->Move(stack_temp, source);
2041 __ flds(Address(CpuRegister(RSP), temp_offset));
2042 } else {
2043 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2044 codegen_->Move(stack_temp, source);
2045 __ fldl(Address(CpuRegister(RSP), temp_offset));
2046 }
2047 }
2048}
2049
2050void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2051 Primitive::Type type = rem->GetResultType();
2052 bool is_float = type == Primitive::kPrimFloat;
2053 size_t elem_size = Primitive::ComponentSize(type);
2054 LocationSummary* locations = rem->GetLocations();
2055 Location first = locations->InAt(0);
2056 Location second = locations->InAt(1);
2057 Location out = locations->Out();
2058
2059 // Create stack space for 2 elements.
2060 // TODO: enhance register allocator to ask for stack temporaries.
2061 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2062
2063 // Load the values to the FP stack in reverse order, using temporaries if needed.
2064 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2065 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2066
2067 // Loop doing FPREM until we stabilize.
2068 Label retry;
2069 __ Bind(&retry);
2070 __ fprem();
2071
2072 // Move FP status to AX.
2073 __ fstsw();
2074
2075 // And see if the argument reduction is complete. This is signaled by the
2076 // C2 FPU flag bit set to 0.
2077 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2078 __ j(kNotEqual, &retry);
2079
2080 // We have settled on the final value. Retrieve it into an XMM register.
2081 // Store FP top of stack to real stack.
2082 if (is_float) {
2083 __ fsts(Address(CpuRegister(RSP), 0));
2084 } else {
2085 __ fstl(Address(CpuRegister(RSP), 0));
2086 }
2087
2088 // Pop the 2 items from the FP stack.
2089 __ fucompp();
2090
2091 // Load the value from the stack into an XMM register.
2092 DCHECK(out.IsFpuRegister()) << out;
2093 if (is_float) {
2094 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2095 } else {
2096 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2097 }
2098
2099 // And remove the temporary stack space we allocated.
2100 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2101}
2102
Calin Juravlebacfec32014-11-14 15:54:36 +00002103void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2104 DCHECK(instruction->IsDiv() || instruction->IsRem());
2105 Primitive::Type type = instruction->GetResultType();
2106 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2107
2108 bool is_div = instruction->IsDiv();
2109 LocationSummary* locations = instruction->GetLocations();
2110
Roland Levillain271ab9c2014-11-27 15:23:57 +00002111 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2112 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002113
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002115 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2116
2117 SlowPathCodeX86_64* slow_path =
2118 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2119 out_reg.AsRegister(), type, is_div);
2120 codegen_->AddSlowPath(slow_path);
2121
2122 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2123 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2124 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002125 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002126 __ cmpl(second_reg, Immediate(-1));
2127 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002128 // edx:eax <- sign-extended of eax
2129 __ cdq();
2130 // eax = quotient, edx = remainder
2131 __ idivl(second_reg);
2132 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002133 __ cmpq(second_reg, Immediate(-1));
2134 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002135 // rdx:rax <- sign-extended of rax
2136 __ cqo();
2137 // rax = quotient, rdx = remainder
2138 __ idivq(second_reg);
2139 }
2140
2141 __ Bind(slow_path->GetExitLabel());
2142}
2143
Calin Juravle7c4954d2014-10-28 16:57:40 +00002144void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2145 LocationSummary* locations =
2146 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2147 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002148 case Primitive::kPrimInt:
2149 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002150 locations->SetInAt(0, Location::RegisterLocation(RAX));
2151 locations->SetInAt(1, Location::RequiresRegister());
2152 locations->SetOut(Location::SameAsFirstInput());
2153 // Intel uses edx:eax as the dividend.
2154 locations->AddTemp(Location::RegisterLocation(RDX));
2155 break;
2156 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002157
Calin Juravle7c4954d2014-10-28 16:57:40 +00002158 case Primitive::kPrimFloat:
2159 case Primitive::kPrimDouble: {
2160 locations->SetInAt(0, Location::RequiresFpuRegister());
2161 locations->SetInAt(1, Location::RequiresFpuRegister());
2162 locations->SetOut(Location::SameAsFirstInput());
2163 break;
2164 }
2165
2166 default:
2167 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2168 }
2169}
2170
2171void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2172 LocationSummary* locations = div->GetLocations();
2173 Location first = locations->InAt(0);
2174 Location second = locations->InAt(1);
2175 DCHECK(first.Equals(locations->Out()));
2176
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002177 Primitive::Type type = div->GetResultType();
2178 switch (type) {
2179 case Primitive::kPrimInt:
2180 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002181 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002182 break;
2183 }
2184
Calin Juravle7c4954d2014-10-28 16:57:40 +00002185 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002186 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002187 break;
2188 }
2189
2190 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002191 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002192 break;
2193 }
2194
2195 default:
2196 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2197 }
2198}
2199
Calin Juravlebacfec32014-11-14 15:54:36 +00002200void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002201 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002202 LocationSummary* locations =
2203 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002204
2205 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002206 case Primitive::kPrimInt:
2207 case Primitive::kPrimLong: {
2208 locations->SetInAt(0, Location::RegisterLocation(RAX));
2209 locations->SetInAt(1, Location::RequiresRegister());
2210 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2211 locations->SetOut(Location::RegisterLocation(RDX));
2212 break;
2213 }
2214
2215 case Primitive::kPrimFloat:
2216 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002217 locations->SetInAt(0, Location::Any());
2218 locations->SetInAt(1, Location::Any());
2219 locations->SetOut(Location::RequiresFpuRegister());
2220 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002221 break;
2222 }
2223
2224 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002225 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002226 }
2227}
2228
2229void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2230 Primitive::Type type = rem->GetResultType();
2231 switch (type) {
2232 case Primitive::kPrimInt:
2233 case Primitive::kPrimLong: {
2234 GenerateDivRemIntegral(rem);
2235 break;
2236 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002237 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002238 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002239 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002240 break;
2241 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002242 default:
2243 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2244 }
2245}
2246
Calin Juravled0d48522014-11-04 16:40:20 +00002247void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2248 LocationSummary* locations =
2249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2250 locations->SetInAt(0, Location::Any());
2251 if (instruction->HasUses()) {
2252 locations->SetOut(Location::SameAsFirstInput());
2253 }
2254}
2255
2256void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2257 SlowPathCodeX86_64* slow_path =
2258 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2259 codegen_->AddSlowPath(slow_path);
2260
2261 LocationSummary* locations = instruction->GetLocations();
2262 Location value = locations->InAt(0);
2263
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002264 switch (instruction->GetType()) {
2265 case Primitive::kPrimInt: {
2266 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002267 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002268 __ j(kEqual, slow_path->GetEntryLabel());
2269 } else if (value.IsStackSlot()) {
2270 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2271 __ j(kEqual, slow_path->GetEntryLabel());
2272 } else {
2273 DCHECK(value.IsConstant()) << value;
2274 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2275 __ jmp(slow_path->GetEntryLabel());
2276 }
2277 }
2278 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002279 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002280 case Primitive::kPrimLong: {
2281 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002282 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002283 __ j(kEqual, slow_path->GetEntryLabel());
2284 } else if (value.IsDoubleStackSlot()) {
2285 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2286 __ j(kEqual, slow_path->GetEntryLabel());
2287 } else {
2288 DCHECK(value.IsConstant()) << value;
2289 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2290 __ jmp(slow_path->GetEntryLabel());
2291 }
2292 }
2293 break;
2294 }
2295 default:
2296 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002297 }
Calin Juravled0d48522014-11-04 16:40:20 +00002298}
2299
Calin Juravle9aec02f2014-11-18 23:06:35 +00002300void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2301 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2302
2303 LocationSummary* locations =
2304 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2305
2306 switch (op->GetResultType()) {
2307 case Primitive::kPrimInt:
2308 case Primitive::kPrimLong: {
2309 locations->SetInAt(0, Location::RequiresRegister());
2310 // The shift count needs to be in CL.
2311 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2312 locations->SetOut(Location::SameAsFirstInput());
2313 break;
2314 }
2315 default:
2316 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2317 }
2318}
2319
2320void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2321 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2322
2323 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002324 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002325 Location second = locations->InAt(1);
2326
2327 switch (op->GetResultType()) {
2328 case Primitive::kPrimInt: {
2329 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002330 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002331 if (op->IsShl()) {
2332 __ shll(first_reg, second_reg);
2333 } else if (op->IsShr()) {
2334 __ sarl(first_reg, second_reg);
2335 } else {
2336 __ shrl(first_reg, second_reg);
2337 }
2338 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002339 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002340 if (op->IsShl()) {
2341 __ shll(first_reg, imm);
2342 } else if (op->IsShr()) {
2343 __ sarl(first_reg, imm);
2344 } else {
2345 __ shrl(first_reg, imm);
2346 }
2347 }
2348 break;
2349 }
2350 case Primitive::kPrimLong: {
2351 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002352 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002353 if (op->IsShl()) {
2354 __ shlq(first_reg, second_reg);
2355 } else if (op->IsShr()) {
2356 __ sarq(first_reg, second_reg);
2357 } else {
2358 __ shrq(first_reg, second_reg);
2359 }
2360 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002361 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002362 if (op->IsShl()) {
2363 __ shlq(first_reg, imm);
2364 } else if (op->IsShr()) {
2365 __ sarq(first_reg, imm);
2366 } else {
2367 __ shrq(first_reg, imm);
2368 }
2369 }
2370 break;
2371 }
2372 default:
2373 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2374 }
2375}
2376
2377void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2378 HandleShift(shl);
2379}
2380
2381void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2382 HandleShift(shl);
2383}
2384
2385void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2386 HandleShift(shr);
2387}
2388
2389void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2390 HandleShift(shr);
2391}
2392
2393void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2394 HandleShift(ushr);
2395}
2396
2397void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2398 HandleShift(ushr);
2399}
2400
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002401void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002402 LocationSummary* locations =
2403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002404 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002405 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2406 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2407 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002408}
2409
2410void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2411 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002412 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002413 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2414
2415 __ gs()->call(Address::Absolute(
2416 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2417
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002418 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002419 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002420}
2421
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002422void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2423 LocationSummary* locations =
2424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2425 InvokeRuntimeCallingConvention calling_convention;
2426 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002427 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002428 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002429 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002430}
2431
2432void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2433 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002434 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002435 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2436
2437 __ gs()->call(Address::Absolute(
2438 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2439
2440 DCHECK(!codegen_->IsLeafMethod());
2441 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2442}
2443
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002444void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002445 LocationSummary* locations =
2446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002447 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2448 if (location.IsStackSlot()) {
2449 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2450 } else if (location.IsDoubleStackSlot()) {
2451 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2452 }
2453 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002454}
2455
2456void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2457 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002458 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002459}
2460
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002461void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002462 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002463 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002464 locations->SetInAt(0, Location::RequiresRegister());
2465 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002466}
2467
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002468void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2469 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002470 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2471 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002472 Location out = locations->Out();
2473 switch (not_->InputAt(0)->GetType()) {
2474 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 __ xorq(out.AsRegister<CpuRegister>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002476 break;
2477
2478 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002479 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002480 break;
2481
2482 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002483 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002484 break;
2485
2486 default:
2487 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2488 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002489}
2490
2491void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002492 LocationSummary* locations =
2493 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002494 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2495 locations->SetInAt(i, Location::Any());
2496 }
2497 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002498}
2499
2500void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002501 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002502 LOG(FATAL) << "Unimplemented";
2503}
2504
Calin Juravle52c48962014-12-16 17:02:57 +00002505void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2506 /*
2507 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2508 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2509 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2510 */
2511 switch (kind) {
2512 case MemBarrierKind::kAnyAny: {
2513 __ mfence();
2514 break;
2515 }
2516 case MemBarrierKind::kAnyStore:
2517 case MemBarrierKind::kLoadAny:
2518 case MemBarrierKind::kStoreStore: {
2519 // nop
2520 break;
2521 }
2522 default:
2523 LOG(FATAL) << "Unexpected memory barier " << kind;
2524 }
2525}
2526
2527void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2528 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2529
Nicolas Geoffray39468442014-09-02 15:17:15 +01002530 LocationSummary* locations =
2531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002532 locations->SetInAt(0, Location::RequiresRegister());
2533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2534}
2535
2536void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2537 const FieldInfo& field_info) {
2538 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2539
2540 LocationSummary* locations = instruction->GetLocations();
2541 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2542 Location out = locations->Out();
2543 bool is_volatile = field_info.IsVolatile();
2544 Primitive::Type field_type = field_info.GetFieldType();
2545 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2546
2547 switch (field_type) {
2548 case Primitive::kPrimBoolean: {
2549 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2550 break;
2551 }
2552
2553 case Primitive::kPrimByte: {
2554 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2555 break;
2556 }
2557
2558 case Primitive::kPrimShort: {
2559 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2560 break;
2561 }
2562
2563 case Primitive::kPrimChar: {
2564 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2565 break;
2566 }
2567
2568 case Primitive::kPrimInt:
2569 case Primitive::kPrimNot: {
2570 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2571 break;
2572 }
2573
2574 case Primitive::kPrimLong: {
2575 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2576 break;
2577 }
2578
2579 case Primitive::kPrimFloat: {
2580 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2581 break;
2582 }
2583
2584 case Primitive::kPrimDouble: {
2585 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2586 break;
2587 }
2588
2589 case Primitive::kPrimVoid:
2590 LOG(FATAL) << "Unreachable type " << field_type;
2591 UNREACHABLE();
2592 }
2593
Calin Juravle77520bc2015-01-12 18:45:46 +00002594 codegen_->MaybeRecordImplicitNullCheck(instruction);
2595
Calin Juravle52c48962014-12-16 17:02:57 +00002596 if (is_volatile) {
2597 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2598 }
2599}
2600
2601void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2602 const FieldInfo& field_info) {
2603 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2604
2605 LocationSummary* locations =
2606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002607 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002608 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2609
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002610 locations->SetInAt(0, Location::RequiresRegister());
2611 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002612 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002613 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002614 locations->AddTemp(Location::RequiresRegister());
2615 locations->AddTemp(Location::RequiresRegister());
2616 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002617}
2618
Calin Juravle52c48962014-12-16 17:02:57 +00002619void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2620 const FieldInfo& field_info) {
2621 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2622
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002623 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002624 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2625 Location value = locations->InAt(1);
2626 bool is_volatile = field_info.IsVolatile();
2627 Primitive::Type field_type = field_info.GetFieldType();
2628 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2629
2630 if (is_volatile) {
2631 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2632 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633
2634 switch (field_type) {
2635 case Primitive::kPrimBoolean:
2636 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002637 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002638 break;
2639 }
2640
2641 case Primitive::kPrimShort:
2642 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002643 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002644 break;
2645 }
2646
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002647 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002648 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002649 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002650 break;
2651 }
2652
2653 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002654 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002655 break;
2656 }
2657
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002658 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002659 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002660 break;
2661 }
2662
2663 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002664 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002665 break;
2666 }
2667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002668 case Primitive::kPrimVoid:
2669 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002670 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671 }
Calin Juravle52c48962014-12-16 17:02:57 +00002672
Calin Juravle77520bc2015-01-12 18:45:46 +00002673 codegen_->MaybeRecordImplicitNullCheck(instruction);
2674
2675 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2676 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2677 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2678 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2679 }
2680
Calin Juravle52c48962014-12-16 17:02:57 +00002681 if (is_volatile) {
2682 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2683 }
2684}
2685
2686void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2687 HandleFieldSet(instruction, instruction->GetFieldInfo());
2688}
2689
2690void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2691 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002692}
2693
2694void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002695 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002696}
2697
2698void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002699 HandleFieldGet(instruction, instruction->GetFieldInfo());
2700}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002701
Calin Juravle52c48962014-12-16 17:02:57 +00002702void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2703 HandleFieldGet(instruction);
2704}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002705
Calin Juravle52c48962014-12-16 17:02:57 +00002706void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2707 HandleFieldGet(instruction, instruction->GetFieldInfo());
2708}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709
Calin Juravle52c48962014-12-16 17:02:57 +00002710void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2711 HandleFieldSet(instruction, instruction->GetFieldInfo());
2712}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002713
Calin Juravle52c48962014-12-16 17:02:57 +00002714void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2715 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002716}
2717
2718void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002719 LocationSummary* locations =
2720 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002721 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2722 ? Location::RequiresRegister()
2723 : Location::Any();
2724 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002725 if (instruction->HasUses()) {
2726 locations->SetOut(Location::SameAsFirstInput());
2727 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002728}
2729
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002730void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002731 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2732 return;
2733 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002734 LocationSummary* locations = instruction->GetLocations();
2735 Location obj = locations->InAt(0);
2736
2737 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2738 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2739}
2740
2741void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002742 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002743 codegen_->AddSlowPath(slow_path);
2744
2745 LocationSummary* locations = instruction->GetLocations();
2746 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002747
2748 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002749 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002750 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002751 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002752 } else {
2753 DCHECK(obj.IsConstant()) << obj;
2754 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2755 __ jmp(slow_path->GetEntryLabel());
2756 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002757 }
2758 __ j(kEqual, slow_path->GetEntryLabel());
2759}
2760
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002761void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2762 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2763 GenerateImplicitNullCheck(instruction);
2764 } else {
2765 GenerateExplicitNullCheck(instruction);
2766 }
2767}
2768
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002769void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002770 LocationSummary* locations =
2771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002772 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002773 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002774 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2775 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002776}
2777
2778void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2779 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002780 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002781 Location index = locations->InAt(1);
2782
2783 switch (instruction->GetType()) {
2784 case Primitive::kPrimBoolean: {
2785 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002786 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002787 if (index.IsConstant()) {
2788 __ movzxb(out, Address(obj,
2789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2790 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002791 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002792 }
2793 break;
2794 }
2795
2796 case Primitive::kPrimByte: {
2797 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002798 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002799 if (index.IsConstant()) {
2800 __ movsxb(out, Address(obj,
2801 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2802 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002803 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002804 }
2805 break;
2806 }
2807
2808 case Primitive::kPrimShort: {
2809 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002810 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002811 if (index.IsConstant()) {
2812 __ movsxw(out, Address(obj,
2813 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2814 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002815 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002816 }
2817 break;
2818 }
2819
2820 case Primitive::kPrimChar: {
2821 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002822 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002823 if (index.IsConstant()) {
2824 __ movzxw(out, Address(obj,
2825 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2826 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002827 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002828 }
2829 break;
2830 }
2831
2832 case Primitive::kPrimInt:
2833 case Primitive::kPrimNot: {
2834 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2835 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002836 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002837 if (index.IsConstant()) {
2838 __ movl(out, Address(obj,
2839 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2840 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002841 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002842 }
2843 break;
2844 }
2845
2846 case Primitive::kPrimLong: {
2847 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002849 if (index.IsConstant()) {
2850 __ movq(out, Address(obj,
2851 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2852 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002854 }
2855 break;
2856 }
2857
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002858 case Primitive::kPrimFloat: {
2859 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002860 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002861 if (index.IsConstant()) {
2862 __ movss(out, Address(obj,
2863 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2864 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002865 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002866 }
2867 break;
2868 }
2869
2870 case Primitive::kPrimDouble: {
2871 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002872 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002873 if (index.IsConstant()) {
2874 __ movsd(out, Address(obj,
2875 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2876 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878 }
2879 break;
2880 }
2881
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002882 case Primitive::kPrimVoid:
2883 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002884 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002885 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002886 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002887}
2888
2889void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002890 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002891
2892 bool needs_write_barrier =
2893 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2894 bool needs_runtime_call = instruction->NeedsTypeCheck();
2895
Nicolas Geoffray39468442014-09-02 15:17:15 +01002896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002897 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2898 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002899 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002900 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2901 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2902 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002903 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002904 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002905 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002906 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2907 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002908 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002909 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2911 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002912 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002913 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002914 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002915
2916 if (needs_write_barrier) {
2917 // Temporary registers for the write barrier.
2918 locations->AddTemp(Location::RequiresRegister());
2919 locations->AddTemp(Location::RequiresRegister());
2920 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002921 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002922}
2923
2924void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2925 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002928 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002929 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002930 bool needs_runtime_call = locations->WillCall();
2931 bool needs_write_barrier =
2932 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002933
2934 switch (value_type) {
2935 case Primitive::kPrimBoolean:
2936 case Primitive::kPrimByte: {
2937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938 if (index.IsConstant()) {
2939 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002940 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002942 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002943 __ movb(Address(obj, offset),
2944 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002945 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002946 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002947 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002948 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2949 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002950 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002951 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002952 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2953 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002954 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002955 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002956 break;
2957 }
2958
2959 case Primitive::kPrimShort:
2960 case Primitive::kPrimChar: {
2961 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002962 if (index.IsConstant()) {
2963 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002964 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002966 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002967 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002968 __ movw(Address(obj, offset),
2969 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002971 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002972 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002973 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2975 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002976 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002977 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002978 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002979 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2980 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002981 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002983 break;
2984 }
2985
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002986 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002988 if (!needs_runtime_call) {
2989 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2990 if (index.IsConstant()) {
2991 size_t offset =
2992 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2993 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002994 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002995 } else {
2996 DCHECK(value.IsConstant()) << value;
2997 __ movl(Address(obj, offset),
2998 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2999 }
3000 } else {
3001 DCHECK(index.IsRegister()) << index;
3002 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003003 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3004 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003005 } else {
3006 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003007 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003008 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3009 }
3010 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003011 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003012 if (needs_write_barrier) {
3013 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003014 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3015 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3016 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003017 }
3018 } else {
3019 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003020 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3021 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003022 DCHECK(!codegen_->IsLeafMethod());
3023 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3024 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003025 break;
3026 }
3027
3028 case Primitive::kPrimLong: {
3029 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003030 if (index.IsConstant()) {
3031 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003032 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003034 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003035 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3037 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003039 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003040 break;
3041 }
3042
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003043 case Primitive::kPrimFloat: {
3044 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3045 if (index.IsConstant()) {
3046 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3047 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003048 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003049 } else {
3050 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3052 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003053 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003054 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003055 break;
3056 }
3057
3058 case Primitive::kPrimDouble: {
3059 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3060 if (index.IsConstant()) {
3061 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3062 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003064 } else {
3065 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3067 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003068 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003069 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003070 break;
3071 }
3072
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 case Primitive::kPrimVoid:
3074 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003075 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003076 }
3077}
3078
3079void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003080 LocationSummary* locations =
3081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003082 locations->SetInAt(0, Location::RequiresRegister());
3083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003084}
3085
3086void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3087 LocationSummary* locations = instruction->GetLocations();
3088 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3090 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003091 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003092 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003093}
3094
3095void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003096 LocationSummary* locations =
3097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003098 locations->SetInAt(0, Location::RequiresRegister());
3099 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003100 if (instruction->HasUses()) {
3101 locations->SetOut(Location::SameAsFirstInput());
3102 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003103}
3104
3105void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3106 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003107 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003108 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003109 codegen_->AddSlowPath(slow_path);
3110
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3112 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113
3114 __ cmpl(index, length);
3115 __ j(kAboveEqual, slow_path->GetEntryLabel());
3116}
3117
3118void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3119 CpuRegister card,
3120 CpuRegister object,
3121 CpuRegister value) {
3122 Label is_null;
3123 __ testl(value, value);
3124 __ j(kEqual, &is_null);
3125 __ gs()->movq(card, Address::Absolute(
3126 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3127 __ movq(temp, object);
3128 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3129 __ movb(Address(temp, card, TIMES_1, 0), card);
3130 __ Bind(&is_null);
3131}
3132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003133void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3134 temp->SetLocations(nullptr);
3135}
3136
3137void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3138 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003139 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003140}
3141
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003142void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003143 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003144 LOG(FATAL) << "Unimplemented";
3145}
3146
3147void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003148 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3149}
3150
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003151void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3153}
3154
3155void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003156 HBasicBlock* block = instruction->GetBlock();
3157 if (block->GetLoopInformation() != nullptr) {
3158 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3159 // The back edge will generate the suspend check.
3160 return;
3161 }
3162 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3163 // The goto will generate the suspend check.
3164 return;
3165 }
3166 GenerateSuspendCheck(instruction, nullptr);
3167}
3168
3169void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3170 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003171 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003172 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003173 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003174 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003175 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003176 if (successor == nullptr) {
3177 __ j(kNotEqual, slow_path->GetEntryLabel());
3178 __ Bind(slow_path->GetReturnLabel());
3179 } else {
3180 __ j(kEqual, codegen_->GetLabelOf(successor));
3181 __ jmp(slow_path->GetEntryLabel());
3182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003183}
3184
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003185X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3186 return codegen_->GetAssembler();
3187}
3188
3189void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3190 MoveOperands* move = moves_.Get(index);
3191 Location source = move->GetSource();
3192 Location destination = move->GetDestination();
3193
3194 if (source.IsRegister()) {
3195 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003196 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003197 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003198 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003199 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003200 } else {
3201 DCHECK(destination.IsDoubleStackSlot());
3202 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003203 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003204 }
3205 } else if (source.IsStackSlot()) {
3206 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003208 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003209 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003211 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003212 } else {
3213 DCHECK(destination.IsStackSlot());
3214 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3215 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3216 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003217 } else if (source.IsDoubleStackSlot()) {
3218 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003220 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003221 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003222 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3223 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003224 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003225 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003226 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3227 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3228 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003229 } else if (source.IsConstant()) {
3230 HConstant* constant = source.GetConstant();
3231 if (constant->IsIntConstant()) {
3232 Immediate imm(constant->AsIntConstant()->GetValue());
3233 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003235 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003236 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003237 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3238 }
3239 } else if (constant->IsLongConstant()) {
3240 int64_t value = constant->AsLongConstant()->GetValue();
3241 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003243 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003244 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003245 __ movq(CpuRegister(TMP), Immediate(value));
3246 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3247 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003248 } else if (constant->IsFloatConstant()) {
3249 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3250 if (destination.IsFpuRegister()) {
3251 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003253 } else {
3254 DCHECK(destination.IsStackSlot()) << destination;
3255 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3256 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003257 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003258 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3259 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3260 if (destination.IsFpuRegister()) {
3261 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003262 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003263 } else {
3264 DCHECK(destination.IsDoubleStackSlot()) << destination;
3265 __ movq(CpuRegister(TMP), imm);
3266 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3267 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003268 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003269 } else if (source.IsFpuRegister()) {
3270 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003271 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003272 } else if (destination.IsStackSlot()) {
3273 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003274 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003275 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003276 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003277 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003278 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003279 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003280 }
3281}
3282
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003283void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003284 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003285 __ movl(Address(CpuRegister(RSP), mem), reg);
3286 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003287}
3288
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003289void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003290 ScratchRegisterScope ensure_scratch(
3291 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3292
3293 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3294 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3295 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3296 Address(CpuRegister(RSP), mem2 + stack_offset));
3297 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3298 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3299 CpuRegister(ensure_scratch.GetRegister()));
3300}
3301
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003302void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3303 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3304 __ movq(Address(CpuRegister(RSP), mem), reg);
3305 __ movq(reg, CpuRegister(TMP));
3306}
3307
3308void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3309 ScratchRegisterScope ensure_scratch(
3310 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3311
3312 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3313 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3314 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3315 Address(CpuRegister(RSP), mem2 + stack_offset));
3316 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3317 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3318 CpuRegister(ensure_scratch.GetRegister()));
3319}
3320
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003321void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3322 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3323 __ movss(Address(CpuRegister(RSP), mem), reg);
3324 __ movd(reg, CpuRegister(TMP));
3325}
3326
3327void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3328 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3329 __ movsd(Address(CpuRegister(RSP), mem), reg);
3330 __ movd(reg, CpuRegister(TMP));
3331}
3332
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003333void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3334 MoveOperands* move = moves_.Get(index);
3335 Location source = move->GetSource();
3336 Location destination = move->GetDestination();
3337
3338 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003339 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003340 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003341 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003342 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003344 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003345 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3346 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003348 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003350 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3351 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003352 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003353 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3354 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3355 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003356 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003357 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003358 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003360 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003361 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003362 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003363 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003364 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003365 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003366 }
3367}
3368
3369
3370void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3371 __ pushq(CpuRegister(reg));
3372}
3373
3374
3375void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3376 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003377}
3378
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003379void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3380 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3381 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3382 Immediate(mirror::Class::kStatusInitialized));
3383 __ j(kLess, slow_path->GetEntryLabel());
3384 __ Bind(slow_path->GetExitLabel());
3385 // No need for memory fence, thanks to the X86_64 memory model.
3386}
3387
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003388void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003389 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3390 ? LocationSummary::kCallOnSlowPath
3391 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003392 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003393 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003394 locations->SetOut(Location::RequiresRegister());
3395}
3396
3397void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003398 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003399 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003400 DCHECK(!cls->CanCallRuntime());
3401 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003402 codegen_->LoadCurrentMethod(out);
3403 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3404 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003405 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003406 codegen_->LoadCurrentMethod(out);
3407 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3408 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003409 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3410 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3411 codegen_->AddSlowPath(slow_path);
3412 __ testl(out, out);
3413 __ j(kEqual, slow_path->GetEntryLabel());
3414 if (cls->MustGenerateClinitCheck()) {
3415 GenerateClassInitializationCheck(slow_path, out);
3416 } else {
3417 __ Bind(slow_path->GetExitLabel());
3418 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003419 }
3420}
3421
3422void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3423 LocationSummary* locations =
3424 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3425 locations->SetInAt(0, Location::RequiresRegister());
3426 if (check->HasUses()) {
3427 locations->SetOut(Location::SameAsFirstInput());
3428 }
3429}
3430
3431void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003432 // We assume the class to not be null.
3433 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3434 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003435 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003436 GenerateClassInitializationCheck(slow_path,
3437 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003438}
3439
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003440void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3441 LocationSummary* locations =
3442 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3443 locations->SetOut(Location::RequiresRegister());
3444}
3445
3446void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3447 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3448 codegen_->AddSlowPath(slow_path);
3449
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003451 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003452 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3453 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003454 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3455 __ testl(out, out);
3456 __ j(kEqual, slow_path->GetEntryLabel());
3457 __ Bind(slow_path->GetExitLabel());
3458}
3459
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003460void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3461 LocationSummary* locations =
3462 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3463 locations->SetOut(Location::RequiresRegister());
3464}
3465
3466void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3467 Address address = Address::Absolute(
3468 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003470 __ gs()->movl(address, Immediate(0));
3471}
3472
3473void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3474 LocationSummary* locations =
3475 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3476 InvokeRuntimeCallingConvention calling_convention;
3477 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3478}
3479
3480void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3481 __ gs()->call(
3482 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3483 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3484}
3485
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003486void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003487 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3488 ? LocationSummary::kNoCall
3489 : LocationSummary::kCallOnSlowPath;
3490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3491 locations->SetInAt(0, Location::RequiresRegister());
3492 locations->SetInAt(1, Location::Any());
3493 locations->SetOut(Location::RequiresRegister());
3494}
3495
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003496void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003497 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003498 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003499 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003500 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003501 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3502 Label done, zero;
3503 SlowPathCodeX86_64* slow_path = nullptr;
3504
3505 // Return 0 if `obj` is null.
3506 // TODO: avoid this check if we know obj is not null.
3507 __ testl(obj, obj);
3508 __ j(kEqual, &zero);
3509 // Compare the class of `obj` with `cls`.
3510 __ movl(out, Address(obj, class_offset));
3511 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003512 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003513 } else {
3514 DCHECK(cls.IsStackSlot()) << cls;
3515 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3516 }
3517 if (instruction->IsClassFinal()) {
3518 // Classes must be equal for the instanceof to succeed.
3519 __ j(kNotEqual, &zero);
3520 __ movl(out, Immediate(1));
3521 __ jmp(&done);
3522 } else {
3523 // If the classes are not equal, we go into a slow path.
3524 DCHECK(locations->OnlyCallsOnSlowPath());
3525 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003526 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003527 codegen_->AddSlowPath(slow_path);
3528 __ j(kNotEqual, slow_path->GetEntryLabel());
3529 __ movl(out, Immediate(1));
3530 __ jmp(&done);
3531 }
3532 __ Bind(&zero);
3533 __ movl(out, Immediate(0));
3534 if (slow_path != nullptr) {
3535 __ Bind(slow_path->GetExitLabel());
3536 }
3537 __ Bind(&done);
3538}
3539
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003540void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3541 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3542 instruction, LocationSummary::kCallOnSlowPath);
3543 locations->SetInAt(0, Location::RequiresRegister());
3544 locations->SetInAt(1, Location::Any());
3545 locations->AddTemp(Location::RequiresRegister());
3546}
3547
3548void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3549 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003551 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003553 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3554 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3555 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3556 codegen_->AddSlowPath(slow_path);
3557
3558 // TODO: avoid this check if we know obj is not null.
3559 __ testl(obj, obj);
3560 __ j(kEqual, slow_path->GetExitLabel());
3561 // Compare the class of `obj` with `cls`.
3562 __ movl(temp, Address(obj, class_offset));
3563 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003564 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003565 } else {
3566 DCHECK(cls.IsStackSlot()) << cls;
3567 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3568 }
3569 // Classes must be equal for the checkcast to succeed.
3570 __ j(kNotEqual, slow_path->GetEntryLabel());
3571 __ Bind(slow_path->GetExitLabel());
3572}
3573
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003574void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3575 LocationSummary* locations =
3576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3577 InvokeRuntimeCallingConvention calling_convention;
3578 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3579}
3580
3581void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3582 __ gs()->call(Address::Absolute(instruction->IsEnter()
3583 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3584 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3585 true));
3586 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3587}
3588
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003589void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3590void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3591void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3592
3593void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3594 LocationSummary* locations =
3595 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3596 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3597 || instruction->GetResultType() == Primitive::kPrimLong);
3598 locations->SetInAt(0, Location::RequiresRegister());
3599 if (instruction->GetType() == Primitive::kPrimInt) {
3600 locations->SetInAt(1, Location::Any());
3601 } else {
3602 // Request a register to avoid loading a 64bits constant.
3603 locations->SetInAt(1, Location::RequiresRegister());
3604 }
3605 locations->SetOut(Location::SameAsFirstInput());
3606}
3607
3608void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3609 HandleBitwiseOperation(instruction);
3610}
3611
3612void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3613 HandleBitwiseOperation(instruction);
3614}
3615
3616void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3617 HandleBitwiseOperation(instruction);
3618}
3619
3620void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3621 LocationSummary* locations = instruction->GetLocations();
3622 Location first = locations->InAt(0);
3623 Location second = locations->InAt(1);
3624 DCHECK(first.Equals(locations->Out()));
3625
3626 if (instruction->GetResultType() == Primitive::kPrimInt) {
3627 if (second.IsRegister()) {
3628 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003629 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003630 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003631 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003632 } else {
3633 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003635 }
3636 } else if (second.IsConstant()) {
3637 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3638 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003639 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003640 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003641 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003642 } else {
3643 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003645 }
3646 } else {
3647 Address address(CpuRegister(RSP), second.GetStackIndex());
3648 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003650 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003652 } else {
3653 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003655 }
3656 }
3657 } else {
3658 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3659 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003660 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003661 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003662 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003663 } else {
3664 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003666 }
3667 }
3668}
3669
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003670} // namespace x86_64
3671} // namespace art