blob: 6780cbcddd0c89beecbb23e143ea69dbb0c05e38 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "intrinsics.h"
22#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070023#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010025#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "mirror/object_reference.h"
27#include "thread.h"
28#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/x86_64/assembler_x86_64.h"
31#include "utils/x86_64/managed_register_x86_64.h"
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace art {
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace x86_64 {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr int kCurrentMethodStackOffset = 0;
41
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
43static constexpr size_t kRuntimeParameterCoreRegistersLength =
44 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000045static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
46static constexpr size_t kRuntimeParameterFpuRegistersLength =
47 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000048static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050
Mark Mendell24f2dfa2015-01-14 19:51:45 -050051static constexpr int kC2ConditionMask = 0x400;
52
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054 public:
55 InvokeRuntimeCallingConvention()
56 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057 kRuntimeParameterCoreRegistersLength,
58 kRuntimeParameterFpuRegisters,
59 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010060
61 private:
62 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
63};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
66
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010067class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
Alexandre Rames2ed20af2015-03-06 13:55:35 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010073 __ gs()->call(
74 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 }
77
78 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
81};
82
Calin Juravled0d48522014-11-04 16:40:20 +000083class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
84 public:
85 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
86
Alexandre Rames2ed20af2015-03-06 13:55:35 +000087 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ Bind(GetEntryLabel());
89 __ gs()->call(
90 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
91 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
92 }
93
94 private:
95 HDivZeroCheck* const instruction_;
96 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
97};
98
Calin Juravlebacfec32014-11-14 15:54:36 +000099class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000101 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
102 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000106 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000107 if (is_div_) {
108 __ negl(cpu_reg_);
109 } else {
110 __ movl(cpu_reg_, Immediate(0));
111 }
112
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000113 } else {
114 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 if (is_div_) {
116 __ negq(cpu_reg_);
117 } else {
118 __ movq(cpu_reg_, Immediate(0));
119 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000120 }
Calin Juravled0d48522014-11-04 16:40:20 +0000121 __ jmp(GetExitLabel());
122 }
123
124 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 const bool is_div_;
128 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000129};
130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
134 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000136 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100139 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
141 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100142 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100143 if (successor_ == nullptr) {
144 __ jmp(GetReturnLabel());
145 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 }
149
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150 Label* GetReturnLabel() {
151 DCHECK(successor_ == nullptr);
152 return &return_label_;
153 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154
155 private:
156 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 Label return_label_;
159
160 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
161};
162
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100165 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
166 Location index_location,
167 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100168 : instruction_(instruction),
169 index_location_(index_location),
170 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 codegen->EmitParallelMoves(
178 index_location_,
179 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
180 length_location_,
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 __ gs()->call(Address::Absolute(
183 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 }
186
187 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100188 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 const Location index_location_;
190 const Location length_location_;
191
192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
193};
194
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 LoadClassSlowPathX86_64(HLoadClass* cls,
198 HInstruction* at,
199 uint32_t dex_pc,
200 bool do_clinit)
201 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
202 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000205 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
208 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 codegen->SaveLiveRegisters(locations);
211
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ gs()->call(Address::Absolute((do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
218 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000220 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
226
227 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 __ jmp(GetExitLabel());
229 }
230
231 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 // The class this slow path will load.
233 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100246};
247
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000248class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
249 public:
250 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
251
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000252 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 LocationSummary* locations = instruction_->GetLocations();
254 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
255
256 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
257 __ Bind(GetEntryLabel());
258 codegen->SaveLiveRegisters(locations);
259
260 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800261 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
262 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263 Immediate(instruction_->GetStringIndex()));
264 __ gs()->call(Address::Absolute(
265 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
266 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
267 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
268 codegen->RestoreLiveRegisters(locations);
269 __ jmp(GetExitLabel());
270 }
271
272 private:
273 HLoadString* const instruction_;
274
275 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
276};
277
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
279 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 TypeCheckSlowPathX86_64(HInstruction* instruction,
281 Location class_to_check,
282 Location object_class,
283 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 class_to_check_(class_to_check),
286 object_class_(object_class),
287 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 DCHECK(instruction_->IsCheckCast()
292 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
294 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
295 __ Bind(GetEntryLabel());
296 codegen->SaveLiveRegisters(locations);
297
298 // We're moving two locations to locations that could overlap, so we need a parallel
299 // move resolver.
300 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000301 codegen->EmitParallelMoves(
302 class_to_check_,
303 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
304 object_class_,
305 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 if (instruction_->IsInstanceOf()) {
308 __ gs()->call(
309 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
310 } else {
311 DCHECK(instruction_->IsCheckCast());
312 __ gs()->call(
313 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
314 }
315 codegen->RecordPcInfo(instruction_, dex_pc_);
316
317 if (instruction_->IsInstanceOf()) {
318 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
319 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 codegen->RestoreLiveRegisters(locations);
322 __ jmp(GetExitLabel());
323 }
324
325 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 HInstruction* const instruction_;
327 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
331 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
332};
333
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100334#undef __
335#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
336
Dave Allison20dfc792014-06-16 20:44:29 -0700337inline Condition X86_64Condition(IfCondition cond) {
338 switch (cond) {
339 case kCondEQ: return kEqual;
340 case kCondNE: return kNotEqual;
341 case kCondLT: return kLess;
342 case kCondLE: return kLessEqual;
343 case kCondGT: return kGreater;
344 case kCondGE: return kGreaterEqual;
345 default:
346 LOG(FATAL) << "Unknown if condition";
347 }
348 return kEqual;
349}
350
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800351void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
352 CpuRegister temp) {
353 // All registers are assumed to be correctly set up.
354
355 // TODO: Implement all kinds of calls:
356 // 1) boot -> boot
357 // 2) app -> boot
358 // 3) app -> app
359 //
360 // Currently we implement the app -> app logic, which looks up in the resolve cache.
361
362 // temp = method;
363 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000364 if (!invoke->IsRecursive()) {
365 // temp = temp->dex_cache_resolved_methods_;
366 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
367 // temp = temp[index_in_cache]
368 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
369 // (temp + offset_of_quick_compiled_code)()
370 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
371 kX86_64WordSize).SizeValue()));
372 } else {
373 __ call(&frame_entry_label_);
374 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800375
376 DCHECK(!IsLeafMethod());
377 RecordPcInfo(invoke, invoke->GetDexPc());
378}
379
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100380void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
381 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
382}
383
384void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
385 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
386}
387
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100388size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
389 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
390 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100391}
392
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100393size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
394 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
395 return kX86_64WordSize;
396}
397
398size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
399 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
400 return kX86_64WordSize;
401}
402
403size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
404 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
405 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100406}
407
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000408static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000409// Use a fake return address register to mimic Quick.
410static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000411CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000412 : CodeGenerator(graph,
413 kNumberOfCpuRegisters,
414 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000415 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000416 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
417 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000418 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000419 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
420 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000421 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100422 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000424 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000425 move_resolver_(graph->GetArena(), this) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000426 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
427}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100428
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100429InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
430 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100431 : HGraphVisitor(graph),
432 assembler_(codegen->GetAssembler()),
433 codegen_(codegen) {}
434
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436 switch (type) {
437 case Primitive::kPrimLong:
438 case Primitive::kPrimByte:
439 case Primitive::kPrimBoolean:
440 case Primitive::kPrimChar:
441 case Primitive::kPrimShort:
442 case Primitive::kPrimInt:
443 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100444 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100445 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100446 }
447
448 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100449 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100451 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100452 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453
454 case Primitive::kPrimVoid:
455 LOG(FATAL) << "Unreachable type " << type;
456 }
457
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100458 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459}
460
Nicolas Geoffray98893962015-01-21 12:32:32 +0000461void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100462 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100463 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100464
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000465 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100466 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000467
Nicolas Geoffray98893962015-01-21 12:32:32 +0000468 if (is_baseline) {
469 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
470 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
471 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000472 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
473 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
474 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000475 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100476}
477
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478void CodeGeneratorX86_64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000479 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100480 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700481 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000482 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100483
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000484 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100485 __ testq(CpuRegister(RAX), Address(
486 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100487 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100488 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000489
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000490 if (HasEmptyFrame()) {
491 return;
492 }
493
Nicolas Geoffray98893962015-01-21 12:32:32 +0000494 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000495 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000496 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000497 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000498 }
499 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100500
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
502 uint32_t xmm_spill_location = GetFpuSpillStart();
503 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100504
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000505 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
506 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
507 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
508 XmmRegister(kFpuCalleeSaves[i]));
509 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100510 }
511
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
513}
514
515void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000516 if (HasEmptyFrame()) {
517 return;
518 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000519 uint32_t xmm_spill_location = GetFpuSpillStart();
520 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
521 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
522 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
523 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
524 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
525 }
526 }
527
528 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000529
530 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000532 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000534 }
535 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536}
537
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100538void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
539 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100540}
541
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100542void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000543 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
545}
546
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
548 switch (load->GetType()) {
549 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
552 break;
553
554 case Primitive::kPrimInt:
555 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100556 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558
559 case Primitive::kPrimBoolean:
560 case Primitive::kPrimByte:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimShort:
563 case Primitive::kPrimVoid:
564 LOG(FATAL) << "Unexpected type " << load->GetType();
565 }
566
567 LOG(FATAL) << "Unreachable";
568 return Location();
569}
570
571void CodeGeneratorX86_64::Move(Location destination, Location source) {
572 if (source.Equals(destination)) {
573 return;
574 }
575 if (destination.IsRegister()) {
576 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 } else {
584 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 Address(CpuRegister(RSP), source.GetStackIndex()));
587 }
588 } else if (destination.IsFpuRegister()) {
589 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000592 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000594 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100595 Address(CpuRegister(RSP), source.GetStackIndex()));
596 } else {
597 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000598 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 }
601 } else if (destination.IsStackSlot()) {
602 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000604 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 } else if (source.IsFpuRegister()) {
606 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500608 } else if (source.IsConstant()) {
609 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000610 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500611 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100612 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500613 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000614 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
615 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100616 }
617 } else {
618 DCHECK(destination.IsDoubleStackSlot());
619 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (source.IsFpuRegister()) {
623 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500625 } else if (source.IsConstant()) {
626 HConstant* constant = source.GetConstant();
627 int64_t value = constant->AsLongConstant()->GetValue();
628 if (constant->IsDoubleConstant()) {
629 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
630 } else {
631 DCHECK(constant->IsLongConstant());
632 value = constant->AsLongConstant()->GetValue();
633 }
634 __ movq(CpuRegister(TMP), Immediate(value));
635 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636 } else {
637 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000638 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
639 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100640 }
641 }
642}
643
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100644void CodeGeneratorX86_64::Move(HInstruction* instruction,
645 Location location,
646 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000647 LocationSummary* locations = instruction->GetLocations();
648 if (locations != nullptr && locations->Out().Equals(location)) {
649 return;
650 }
651
652 if (locations != nullptr && locations->Out().IsConstant()) {
653 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000654 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
655 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000656 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000657 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000658 } else if (location.IsStackSlot()) {
659 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
660 } else {
661 DCHECK(location.IsConstant());
662 DCHECK_EQ(location.GetConstant(), const_to_move);
663 }
664 } else if (const_to_move->IsLongConstant()) {
665 int64_t value = const_to_move->AsLongConstant()->GetValue();
666 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000667 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000668 } else if (location.IsDoubleStackSlot()) {
669 __ movq(CpuRegister(TMP), Immediate(value));
670 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
671 } else {
672 DCHECK(location.IsConstant());
673 DCHECK_EQ(location.GetConstant(), const_to_move);
674 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675 }
Roland Levillain476df552014-10-09 17:51:36 +0100676 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100677 switch (instruction->GetType()) {
678 case Primitive::kPrimBoolean:
679 case Primitive::kPrimByte:
680 case Primitive::kPrimChar:
681 case Primitive::kPrimShort:
682 case Primitive::kPrimInt:
683 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100684 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
686 break;
687
688 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100689 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000690 Move(location,
691 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100692 break;
693
694 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100696 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000697 } else if (instruction->IsTemporary()) {
698 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
699 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100700 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100701 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702 switch (instruction->GetType()) {
703 case Primitive::kPrimBoolean:
704 case Primitive::kPrimByte:
705 case Primitive::kPrimChar:
706 case Primitive::kPrimShort:
707 case Primitive::kPrimInt:
708 case Primitive::kPrimNot:
709 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 case Primitive::kPrimFloat:
711 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000712 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100713 break;
714
715 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 }
718 }
719}
720
721void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
722 got->SetLocations(nullptr);
723}
724
725void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
726 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100727 DCHECK(!successor->IsExitBlock());
728
729 HBasicBlock* block = got->GetBlock();
730 HInstruction* previous = got->GetPrevious();
731
732 HLoopInformation* info = block->GetLoopInformation();
733 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
734 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
735 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
736 return;
737 }
738
739 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
740 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
741 }
742 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743 __ jmp(codegen_->GetLabelOf(successor));
744 }
745}
746
747void LocationsBuilderX86_64::VisitExit(HExit* exit) {
748 exit->SetLocations(nullptr);
749}
750
751void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700752 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100753 if (kIsDebugBuild) {
754 __ Comment("Unreachable");
755 __ int3();
756 }
757}
758
759void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100760 LocationSummary* locations =
761 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100762 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100763 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100764 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100765 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100766}
767
768void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700769 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100770 if (cond->IsIntConstant()) {
771 // Constant condition, statically compared against 1.
772 int32_t cond_value = cond->AsIntConstant()->GetValue();
773 if (cond_value == 1) {
774 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
775 if_instr->IfTrueSuccessor())) {
776 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100777 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100778 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100780 DCHECK_EQ(cond_value, 0);
781 }
782 } else {
783 bool materialized =
784 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
785 // Moves do not affect the eflags register, so if the condition is
786 // evaluated just before the if, we don't need to evaluate it
787 // again.
788 bool eflags_set = cond->IsCondition()
789 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
790 if (materialized) {
791 if (!eflags_set) {
792 // Materialized condition, compare against 0.
793 Location lhs = if_instr->GetLocations()->InAt(0);
794 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000795 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 } else {
797 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
798 Immediate(0));
799 }
800 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
801 } else {
802 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
803 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
804 }
805 } else {
806 Location lhs = cond->GetLocations()->InAt(0);
807 Location rhs = cond->GetLocations()->InAt(1);
808 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000809 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100810 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000811 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000812 if (constant == 0) {
813 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
814 } else {
815 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
816 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000818 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 Address(CpuRegister(RSP), rhs.GetStackIndex()));
820 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100821 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
822 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700823 }
Dave Allison20dfc792014-06-16 20:44:29 -0700824 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
826 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700827 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100828 }
829}
830
831void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
832 local->SetLocations(nullptr);
833}
834
835void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
836 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
837}
838
839void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
840 local->SetLocations(nullptr);
841}
842
843void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
844 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700845 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846}
847
848void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100849 LocationSummary* locations =
850 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851 switch (store->InputAt(1)->GetType()) {
852 case Primitive::kPrimBoolean:
853 case Primitive::kPrimByte:
854 case Primitive::kPrimChar:
855 case Primitive::kPrimShort:
856 case Primitive::kPrimInt:
857 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
860 break;
861
862 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100864 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
865 break;
866
867 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100870}
871
872void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700873 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100874}
875
Dave Allison20dfc792014-06-16 20:44:29 -0700876void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100877 LocationSummary* locations =
878 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100879 locations->SetInAt(0, Location::RequiresRegister());
880 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100881 if (comp->NeedsMaterialization()) {
882 locations->SetOut(Location::RequiresRegister());
883 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100884}
885
Dave Allison20dfc792014-06-16 20:44:29 -0700886void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
887 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100888 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000889 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100890 // Clear register: setcc only sets the low byte.
891 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000892 Location lhs = locations->InAt(0);
893 Location rhs = locations->InAt(1);
894 if (rhs.IsRegister()) {
895 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
896 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800897 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000898 if (constant == 0) {
899 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
900 } else {
901 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
902 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100903 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000904 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100905 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100906 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700907 }
908}
909
910void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
911 VisitCondition(comp);
912}
913
914void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
915 VisitCondition(comp);
916}
917
918void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
919 VisitCondition(comp);
920}
921
922void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
923 VisitCondition(comp);
924}
925
926void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
927 VisitCondition(comp);
928}
929
930void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
931 VisitCondition(comp);
932}
933
934void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
935 VisitCondition(comp);
936}
937
938void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
939 VisitCondition(comp);
940}
941
942void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
943 VisitCondition(comp);
944}
945
946void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
947 VisitCondition(comp);
948}
949
950void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
951 VisitCondition(comp);
952}
953
954void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
955 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100956}
957
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100958void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100959 LocationSummary* locations =
960 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000961 switch (compare->InputAt(0)->GetType()) {
962 case Primitive::kPrimLong: {
963 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -0400964 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +0000965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
966 break;
967 }
968 case Primitive::kPrimFloat:
969 case Primitive::kPrimDouble: {
970 locations->SetInAt(0, Location::RequiresFpuRegister());
971 locations->SetInAt(1, Location::RequiresFpuRegister());
972 locations->SetOut(Location::RequiresRegister());
973 break;
974 }
975 default:
976 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
977 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100978}
979
980void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100981 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000982 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000983 Location left = locations->InAt(0);
984 Location right = locations->InAt(1);
985
986 Label less, greater, done;
987 Primitive::Type type = compare->InputAt(0)->GetType();
988 switch (type) {
989 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -0400990 CpuRegister left_reg = left.AsRegister<CpuRegister>();
991 if (right.IsConstant()) {
992 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
993 DCHECK(IsInt<32>(value));
994 if (value == 0) {
995 __ testq(left_reg, left_reg);
996 } else {
997 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
998 }
999 } else {
1000 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1001 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001002 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001003 }
1004 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001005 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001006 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1007 break;
1008 }
1009 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001010 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001011 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1012 break;
1013 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001014 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001015 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001016 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001017 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001018 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001019 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001020
Calin Juravle91debbc2014-11-26 19:01:09 +00001021 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001022 __ movl(out, Immediate(1));
1023 __ jmp(&done);
1024
1025 __ Bind(&less);
1026 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001027
1028 __ Bind(&done);
1029}
1030
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001034 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001035}
1036
1037void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001038 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001039 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001040}
1041
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001042void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1043 LocationSummary* locations =
1044 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1045 locations->SetOut(Location::ConstantLocation(constant));
1046}
1047
1048void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1049 // Will be generated at use site.
1050 UNUSED(constant);
1051}
1052
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001053void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001054 LocationSummary* locations =
1055 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001056 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001057}
1058
1059void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001060 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001061 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062}
1063
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001064void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1065 LocationSummary* locations =
1066 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1067 locations->SetOut(Location::ConstantLocation(constant));
1068}
1069
1070void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1071 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001072 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001073}
1074
1075void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1076 LocationSummary* locations =
1077 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1078 locations->SetOut(Location::ConstantLocation(constant));
1079}
1080
1081void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1082 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001083 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001084}
1085
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001086void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1087 ret->SetLocations(nullptr);
1088}
1089
1090void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001091 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092 codegen_->GenerateFrameExit();
1093 __ ret();
1094}
1095
1096void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001097 LocationSummary* locations =
1098 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001099 switch (ret->InputAt(0)->GetType()) {
1100 case Primitive::kPrimBoolean:
1101 case Primitive::kPrimByte:
1102 case Primitive::kPrimChar:
1103 case Primitive::kPrimShort:
1104 case Primitive::kPrimInt:
1105 case Primitive::kPrimNot:
1106 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001107 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108 break;
1109
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 case Primitive::kPrimFloat:
1111 case Primitive::kPrimDouble:
1112 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001113 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001114 break;
1115
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001119}
1120
1121void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1122 if (kIsDebugBuild) {
1123 switch (ret->InputAt(0)->GetType()) {
1124 case Primitive::kPrimBoolean:
1125 case Primitive::kPrimByte:
1126 case Primitive::kPrimChar:
1127 case Primitive::kPrimShort:
1128 case Primitive::kPrimInt:
1129 case Primitive::kPrimNot:
1130 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001131 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001132 break;
1133
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001134 case Primitive::kPrimFloat:
1135 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001136 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001137 XMM0);
1138 break;
1139
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001140 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001141 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001142 }
1143 }
1144 codegen_->GenerateFrameExit();
1145 __ ret();
1146}
1147
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1149 switch (type) {
1150 case Primitive::kPrimBoolean:
1151 case Primitive::kPrimByte:
1152 case Primitive::kPrimChar:
1153 case Primitive::kPrimShort:
1154 case Primitive::kPrimInt:
1155 case Primitive::kPrimNot: {
1156 uint32_t index = gp_index_++;
1157 stack_index_++;
1158 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160 } else {
1161 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1162 }
1163 }
1164
1165 case Primitive::kPrimLong: {
1166 uint32_t index = gp_index_;
1167 stack_index_ += 2;
1168 if (index < calling_convention.GetNumberOfRegisters()) {
1169 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001171 } else {
1172 gp_index_ += 2;
1173 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1174 }
1175 }
1176
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001177 case Primitive::kPrimFloat: {
1178 uint32_t index = fp_index_++;
1179 stack_index_++;
1180 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001181 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001182 } else {
1183 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1184 }
1185 }
1186
1187 case Primitive::kPrimDouble: {
1188 uint32_t index = fp_index_++;
1189 stack_index_ += 2;
1190 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001191 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 } else {
1193 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1194 }
1195 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196
1197 case Primitive::kPrimVoid:
1198 LOG(FATAL) << "Unexpected parameter type " << type;
1199 break;
1200 }
1201 return Location();
1202}
1203
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001204void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001205 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1206 if (intrinsic.TryDispatch(invoke)) {
1207 return;
1208 }
1209
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210 HandleInvoke(invoke);
1211}
1212
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001213static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1214 if (invoke->GetLocations()->Intrinsified()) {
1215 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1216 intrinsic.Dispatch(invoke);
1217 return true;
1218 }
1219 return false;
1220}
1221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001223 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1224 return;
1225 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001226
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001227 codegen_->GenerateStaticOrDirectCall(
1228 invoke,
1229 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230}
1231
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001232void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001233 LocationSummary* locations =
1234 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001235 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001236
1237 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001238 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001239 HInstruction* input = invoke->InputAt(i);
1240 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1241 }
1242
1243 switch (invoke->GetType()) {
1244 case Primitive::kPrimBoolean:
1245 case Primitive::kPrimByte:
1246 case Primitive::kPrimChar:
1247 case Primitive::kPrimShort:
1248 case Primitive::kPrimInt:
1249 case Primitive::kPrimNot:
1250 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001251 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001252 break;
1253
1254 case Primitive::kPrimVoid:
1255 break;
1256
1257 case Primitive::kPrimDouble:
1258 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001259 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 break;
1261 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001262}
1263
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001264void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001265 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1266 if (intrinsic.TryDispatch(invoke)) {
1267 return;
1268 }
1269
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001270 HandleInvoke(invoke);
1271}
1272
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001273void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001274 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1275 return;
1276 }
1277
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1280 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1281 LocationSummary* locations = invoke->GetLocations();
1282 Location receiver = locations->InAt(0);
1283 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1284 // temp = object->GetClass();
1285 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1287 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001291 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001292 // temp = temp->GetMethodAt(method_offset);
1293 __ movl(temp, Address(temp, method_offset));
1294 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001295 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001296 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001297
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001298 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001300}
1301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1303 HandleInvoke(invoke);
1304 // Add the hidden argument.
1305 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1306}
1307
1308void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1309 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001311 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1312 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1313 LocationSummary* locations = invoke->GetLocations();
1314 Location receiver = locations->InAt(0);
1315 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1316
1317 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001318 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001319 Immediate(invoke->GetDexMethodIndex()));
1320
1321 // temp = object->GetClass();
1322 if (receiver.IsStackSlot()) {
1323 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1324 __ movl(temp, Address(temp, class_offset));
1325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329 // temp = temp->GetImtEntryAt(method_offset);
1330 __ movl(temp, Address(temp, method_offset));
1331 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001332 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001333 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001334
1335 DCHECK(!codegen_->IsLeafMethod());
1336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1337}
1338
Roland Levillain88cb1752014-10-20 16:36:47 +01001339void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1340 LocationSummary* locations =
1341 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1342 switch (neg->GetResultType()) {
1343 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001344 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetOut(Location::SameAsFirstInput());
1347 break;
1348
Roland Levillain88cb1752014-10-20 16:36:47 +01001349 case Primitive::kPrimFloat:
1350 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001352 locations->SetOut(Location::SameAsFirstInput());
1353 locations->AddTemp(Location::RequiresRegister());
1354 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 break;
1356
1357 default:
1358 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1359 }
1360}
1361
1362void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1363 LocationSummary* locations = neg->GetLocations();
1364 Location out = locations->Out();
1365 Location in = locations->InAt(0);
1366 switch (neg->GetResultType()) {
1367 case Primitive::kPrimInt:
1368 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001369 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001371 break;
1372
1373 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001374 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001375 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001376 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001377 break;
1378
Roland Levillain5368c212014-11-27 15:03:41 +00001379 case Primitive::kPrimFloat: {
1380 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001381 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1382 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001383 // Implement float negation with an exclusive or with value
1384 // 0x80000000 (mask for bit 31, representing the sign of a
1385 // single-precision floating-point number).
1386 __ movq(constant, Immediate(INT64_C(0x80000000)));
1387 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001388 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001389 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001390 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001391
Roland Levillain5368c212014-11-27 15:03:41 +00001392 case Primitive::kPrimDouble: {
1393 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001394 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1395 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001396 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001397 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001398 // a double-precision floating-point number).
1399 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1400 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001401 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001402 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001403 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001404
1405 default:
1406 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1407 }
1408}
1409
Roland Levillaindff1f282014-11-05 14:15:05 +00001410void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1411 LocationSummary* locations =
1412 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1413 Primitive::Type result_type = conversion->GetResultType();
1414 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001415 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001416 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001417 case Primitive::kPrimByte:
1418 switch (input_type) {
1419 case Primitive::kPrimShort:
1420 case Primitive::kPrimInt:
1421 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001422 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001423 locations->SetInAt(0, Location::Any());
1424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1425 break;
1426
1427 default:
1428 LOG(FATAL) << "Unexpected type conversion from " << input_type
1429 << " to " << result_type;
1430 }
1431 break;
1432
Roland Levillain01a8d712014-11-14 16:27:39 +00001433 case Primitive::kPrimShort:
1434 switch (input_type) {
1435 case Primitive::kPrimByte:
1436 case Primitive::kPrimInt:
1437 case Primitive::kPrimChar:
1438 // Processing a Dex `int-to-short' instruction.
1439 locations->SetInAt(0, Location::Any());
1440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1441 break;
1442
1443 default:
1444 LOG(FATAL) << "Unexpected type conversion from " << input_type
1445 << " to " << result_type;
1446 }
1447 break;
1448
Roland Levillain946e1432014-11-11 17:35:19 +00001449 case Primitive::kPrimInt:
1450 switch (input_type) {
1451 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001452 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001453 locations->SetInAt(0, Location::Any());
1454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1455 break;
1456
1457 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001458 // Processing a Dex `float-to-int' instruction.
1459 locations->SetInAt(0, Location::RequiresFpuRegister());
1460 locations->SetOut(Location::RequiresRegister());
1461 locations->AddTemp(Location::RequiresFpuRegister());
1462 break;
1463
Roland Levillain946e1432014-11-11 17:35:19 +00001464 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001465 // Processing a Dex `double-to-int' instruction.
1466 locations->SetInAt(0, Location::RequiresFpuRegister());
1467 locations->SetOut(Location::RequiresRegister());
1468 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 }
1475 break;
1476
Roland Levillaindff1f282014-11-05 14:15:05 +00001477 case Primitive::kPrimLong:
1478 switch (input_type) {
1479 case Primitive::kPrimByte:
1480 case Primitive::kPrimShort:
1481 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001482 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001483 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001484 // TODO: We would benefit from a (to-be-implemented)
1485 // Location::RegisterOrStackSlot requirement for this input.
1486 locations->SetInAt(0, Location::RequiresRegister());
1487 locations->SetOut(Location::RequiresRegister());
1488 break;
1489
1490 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001491 // Processing a Dex `float-to-long' instruction.
1492 locations->SetInAt(0, Location::RequiresFpuRegister());
1493 locations->SetOut(Location::RequiresRegister());
1494 locations->AddTemp(Location::RequiresFpuRegister());
1495 break;
1496
Roland Levillaindff1f282014-11-05 14:15:05 +00001497 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001498 // Processing a Dex `double-to-long' instruction.
1499 locations->SetInAt(0, Location::RequiresFpuRegister());
1500 locations->SetOut(Location::RequiresRegister());
1501 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001502 break;
1503
1504 default:
1505 LOG(FATAL) << "Unexpected type conversion from " << input_type
1506 << " to " << result_type;
1507 }
1508 break;
1509
Roland Levillain981e4542014-11-14 11:47:14 +00001510 case Primitive::kPrimChar:
1511 switch (input_type) {
1512 case Primitive::kPrimByte:
1513 case Primitive::kPrimShort:
1514 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001515 // Processing a Dex `int-to-char' instruction.
1516 locations->SetInAt(0, Location::Any());
1517 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1518 break;
1519
1520 default:
1521 LOG(FATAL) << "Unexpected type conversion from " << input_type
1522 << " to " << result_type;
1523 }
1524 break;
1525
Roland Levillaindff1f282014-11-05 14:15:05 +00001526 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001527 switch (input_type) {
1528 case Primitive::kPrimByte:
1529 case Primitive::kPrimShort:
1530 case Primitive::kPrimInt:
1531 case Primitive::kPrimChar:
1532 // Processing a Dex `int-to-float' instruction.
1533 locations->SetInAt(0, Location::RequiresRegister());
1534 locations->SetOut(Location::RequiresFpuRegister());
1535 break;
1536
1537 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001538 // Processing a Dex `long-to-float' instruction.
1539 locations->SetInAt(0, Location::RequiresRegister());
1540 locations->SetOut(Location::RequiresFpuRegister());
1541 break;
1542
Roland Levillaincff13742014-11-17 14:32:17 +00001543 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001544 // Processing a Dex `double-to-float' instruction.
1545 locations->SetInAt(0, Location::RequiresFpuRegister());
1546 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001547 break;
1548
1549 default:
1550 LOG(FATAL) << "Unexpected type conversion from " << input_type
1551 << " to " << result_type;
1552 };
1553 break;
1554
Roland Levillaindff1f282014-11-05 14:15:05 +00001555 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001556 switch (input_type) {
1557 case Primitive::kPrimByte:
1558 case Primitive::kPrimShort:
1559 case Primitive::kPrimInt:
1560 case Primitive::kPrimChar:
1561 // Processing a Dex `int-to-double' instruction.
1562 locations->SetInAt(0, Location::RequiresRegister());
1563 locations->SetOut(Location::RequiresFpuRegister());
1564 break;
1565
1566 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001567 // Processing a Dex `long-to-double' instruction.
1568 locations->SetInAt(0, Location::RequiresRegister());
1569 locations->SetOut(Location::RequiresFpuRegister());
1570 break;
1571
Roland Levillaincff13742014-11-17 14:32:17 +00001572 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001573 // Processing a Dex `float-to-double' instruction.
1574 locations->SetInAt(0, Location::RequiresFpuRegister());
1575 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001576 break;
1577
1578 default:
1579 LOG(FATAL) << "Unexpected type conversion from " << input_type
1580 << " to " << result_type;
1581 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001582 break;
1583
1584 default:
1585 LOG(FATAL) << "Unexpected type conversion from " << input_type
1586 << " to " << result_type;
1587 }
1588}
1589
1590void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1591 LocationSummary* locations = conversion->GetLocations();
1592 Location out = locations->Out();
1593 Location in = locations->InAt(0);
1594 Primitive::Type result_type = conversion->GetResultType();
1595 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001596 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001597 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001598 case Primitive::kPrimByte:
1599 switch (input_type) {
1600 case Primitive::kPrimShort:
1601 case Primitive::kPrimInt:
1602 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001603 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001604 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001606 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001608 Address(CpuRegister(RSP), in.GetStackIndex()));
1609 } else {
1610 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001611 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001612 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1613 }
1614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620 break;
1621
Roland Levillain01a8d712014-11-14 16:27:39 +00001622 case Primitive::kPrimShort:
1623 switch (input_type) {
1624 case Primitive::kPrimByte:
1625 case Primitive::kPrimInt:
1626 case Primitive::kPrimChar:
1627 // Processing a Dex `int-to-short' instruction.
1628 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001629 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001630 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001631 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001632 Address(CpuRegister(RSP), in.GetStackIndex()));
1633 } else {
1634 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001635 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001636 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1637 }
1638 break;
1639
1640 default:
1641 LOG(FATAL) << "Unexpected type conversion from " << input_type
1642 << " to " << result_type;
1643 }
1644 break;
1645
Roland Levillain946e1432014-11-11 17:35:19 +00001646 case Primitive::kPrimInt:
1647 switch (input_type) {
1648 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001649 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001650 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001651 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001652 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001653 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001654 Address(CpuRegister(RSP), in.GetStackIndex()));
1655 } else {
1656 DCHECK(in.IsConstant());
1657 DCHECK(in.GetConstant()->IsLongConstant());
1658 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001660 }
1661 break;
1662
Roland Levillain3f8f9362014-12-02 17:45:01 +00001663 case Primitive::kPrimFloat: {
1664 // Processing a Dex `float-to-int' instruction.
1665 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1666 CpuRegister output = out.AsRegister<CpuRegister>();
1667 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1668 Label done, nan;
1669
1670 __ movl(output, Immediate(kPrimIntMax));
1671 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001672 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001673 // if input >= temp goto done
1674 __ comiss(input, temp);
1675 __ j(kAboveEqual, &done);
1676 // if input == NaN goto nan
1677 __ j(kUnordered, &nan);
1678 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001679 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001680 __ jmp(&done);
1681 __ Bind(&nan);
1682 // output = 0
1683 __ xorl(output, output);
1684 __ Bind(&done);
1685 break;
1686 }
1687
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001688 case Primitive::kPrimDouble: {
1689 // Processing a Dex `double-to-int' instruction.
1690 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1691 CpuRegister output = out.AsRegister<CpuRegister>();
1692 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1693 Label done, nan;
1694
1695 __ movl(output, Immediate(kPrimIntMax));
1696 // temp = int-to-double(output)
1697 __ cvtsi2sd(temp, output);
1698 // if input >= temp goto done
1699 __ comisd(input, temp);
1700 __ j(kAboveEqual, &done);
1701 // if input == NaN goto nan
1702 __ j(kUnordered, &nan);
1703 // output = double-to-int-truncate(input)
1704 __ cvttsd2si(output, input);
1705 __ jmp(&done);
1706 __ Bind(&nan);
1707 // output = 0
1708 __ xorl(output, output);
1709 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001710 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001711 }
Roland Levillain946e1432014-11-11 17:35:19 +00001712
1713 default:
1714 LOG(FATAL) << "Unexpected type conversion from " << input_type
1715 << " to " << result_type;
1716 }
1717 break;
1718
Roland Levillaindff1f282014-11-05 14:15:05 +00001719 case Primitive::kPrimLong:
1720 switch (input_type) {
1721 DCHECK(out.IsRegister());
1722 case Primitive::kPrimByte:
1723 case Primitive::kPrimShort:
1724 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001725 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001726 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001727 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001729 break;
1730
Roland Levillain624279f2014-12-04 11:54:28 +00001731 case Primitive::kPrimFloat: {
1732 // Processing a Dex `float-to-long' instruction.
1733 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1734 CpuRegister output = out.AsRegister<CpuRegister>();
1735 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1736 Label done, nan;
1737
1738 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001739 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001740 __ cvtsi2ss(temp, output, true);
1741 // if input >= temp goto done
1742 __ comiss(input, temp);
1743 __ j(kAboveEqual, &done);
1744 // if input == NaN goto nan
1745 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001746 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001747 __ cvttss2si(output, input, true);
1748 __ jmp(&done);
1749 __ Bind(&nan);
1750 // output = 0
1751 __ xorq(output, output);
1752 __ Bind(&done);
1753 break;
1754 }
1755
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001756 case Primitive::kPrimDouble: {
1757 // Processing a Dex `double-to-long' instruction.
1758 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1759 CpuRegister output = out.AsRegister<CpuRegister>();
1760 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1761 Label done, nan;
1762
1763 __ movq(output, Immediate(kPrimLongMax));
1764 // temp = long-to-double(output)
1765 __ cvtsi2sd(temp, output, true);
1766 // if input >= temp goto done
1767 __ comisd(input, temp);
1768 __ j(kAboveEqual, &done);
1769 // if input == NaN goto nan
1770 __ j(kUnordered, &nan);
1771 // output = double-to-long-truncate(input)
1772 __ cvttsd2si(output, input, true);
1773 __ jmp(&done);
1774 __ Bind(&nan);
1775 // output = 0
1776 __ xorq(output, output);
1777 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001778 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001779 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001780
1781 default:
1782 LOG(FATAL) << "Unexpected type conversion from " << input_type
1783 << " to " << result_type;
1784 }
1785 break;
1786
Roland Levillain981e4542014-11-14 11:47:14 +00001787 case Primitive::kPrimChar:
1788 switch (input_type) {
1789 case Primitive::kPrimByte:
1790 case Primitive::kPrimShort:
1791 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001792 // Processing a Dex `int-to-char' instruction.
1793 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001795 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001796 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001797 Address(CpuRegister(RSP), in.GetStackIndex()));
1798 } else {
1799 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001801 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1802 }
1803 break;
1804
1805 default:
1806 LOG(FATAL) << "Unexpected type conversion from " << input_type
1807 << " to " << result_type;
1808 }
1809 break;
1810
Roland Levillaindff1f282014-11-05 14:15:05 +00001811 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001812 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001813 case Primitive::kPrimByte:
1814 case Primitive::kPrimShort:
1815 case Primitive::kPrimInt:
1816 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001817 // Processing a Dex `int-to-float' instruction.
1818 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001819 break;
1820
1821 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822 // Processing a Dex `long-to-float' instruction.
1823 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1824 break;
1825
Roland Levillaincff13742014-11-17 14:32:17 +00001826 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001827 // Processing a Dex `double-to-float' instruction.
1828 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001829 break;
1830
1831 default:
1832 LOG(FATAL) << "Unexpected type conversion from " << input_type
1833 << " to " << result_type;
1834 };
1835 break;
1836
Roland Levillaindff1f282014-11-05 14:15:05 +00001837 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001838 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001839 case Primitive::kPrimByte:
1840 case Primitive::kPrimShort:
1841 case Primitive::kPrimInt:
1842 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001843 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001844 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001845 break;
1846
1847 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001848 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001849 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001850 break;
1851
Roland Levillaincff13742014-11-17 14:32:17 +00001852 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001853 // Processing a Dex `float-to-double' instruction.
1854 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001855 break;
1856
1857 default:
1858 LOG(FATAL) << "Unexpected type conversion from " << input_type
1859 << " to " << result_type;
1860 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 break;
1862
1863 default:
1864 LOG(FATAL) << "Unexpected type conversion from " << input_type
1865 << " to " << result_type;
1866 }
1867}
1868
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001872 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001873 case Primitive::kPrimInt: {
1874 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001875 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1876 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001877 break;
1878 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001879
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001880 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001881 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001882 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001883 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001884 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001885 break;
1886 }
1887
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 case Primitive::kPrimDouble:
1889 case Primitive::kPrimFloat: {
1890 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001891 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001894 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001895
1896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001897 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001898 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001899}
1900
1901void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1902 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001903 Location first = locations->InAt(0);
1904 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001905 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001906
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001907 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001908 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001909 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001910 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1911 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1912 } else {
1913 __ leal(out.AsRegister<CpuRegister>(), Address(
1914 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1915 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001916 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001917 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1918 __ addl(out.AsRegister<CpuRegister>(),
1919 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
1920 } else {
1921 __ leal(out.AsRegister<CpuRegister>(), Address(
1922 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
1923 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001924 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001925 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001927 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001928 break;
1929 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001930
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001931 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05001932 if (second.IsRegister()) {
1933 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1934 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1935 } else {
1936 __ leaq(out.AsRegister<CpuRegister>(), Address(
1937 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1938 }
1939 } else {
1940 DCHECK(second.IsConstant());
1941 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1942 int32_t int32_value = Low32Bits(value);
1943 DCHECK_EQ(int32_value, value);
1944 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1945 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
1946 } else {
1947 __ leaq(out.AsRegister<CpuRegister>(), Address(
1948 first.AsRegister<CpuRegister>(), int32_value));
1949 }
1950 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951 break;
1952 }
1953
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001956 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 }
1958
1959 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001960 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001961 break;
1962 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001963
1964 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001965 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001966 }
1967}
1968
1969void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001970 LocationSummary* locations =
1971 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001972 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001973 case Primitive::kPrimInt: {
1974 locations->SetInAt(0, Location::RequiresRegister());
1975 locations->SetInAt(1, Location::Any());
1976 locations->SetOut(Location::SameAsFirstInput());
1977 break;
1978 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001979 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001980 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001981 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001982 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001983 break;
1984 }
Calin Juravle11351682014-10-23 15:38:15 +01001985 case Primitive::kPrimFloat:
1986 case Primitive::kPrimDouble: {
1987 locations->SetInAt(0, Location::RequiresFpuRegister());
1988 locations->SetInAt(1, Location::RequiresFpuRegister());
1989 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001990 break;
Calin Juravle11351682014-10-23 15:38:15 +01001991 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001992 default:
Calin Juravle11351682014-10-23 15:38:15 +01001993 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001994 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001995}
1996
1997void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1998 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001999 Location first = locations->InAt(0);
2000 Location second = locations->InAt(1);
2001 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002002 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002003 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002004 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002005 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002006 } else if (second.IsConstant()) {
2007 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002008 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002009 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002011 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002012 break;
2013 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002015 if (second.IsConstant()) {
2016 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2017 DCHECK(IsInt<32>(value));
2018 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2019 } else {
2020 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2021 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002022 break;
2023 }
2024
Calin Juravle11351682014-10-23 15:38:15 +01002025 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002027 break;
Calin Juravle11351682014-10-23 15:38:15 +01002028 }
2029
2030 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002032 break;
2033 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002034
2035 default:
Calin Juravle11351682014-10-23 15:38:15 +01002036 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002037 }
2038}
2039
Calin Juravle34bacdf2014-10-07 20:23:36 +01002040void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2041 LocationSummary* locations =
2042 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2043 switch (mul->GetResultType()) {
2044 case Primitive::kPrimInt: {
2045 locations->SetInAt(0, Location::RequiresRegister());
2046 locations->SetInAt(1, Location::Any());
2047 locations->SetOut(Location::SameAsFirstInput());
2048 break;
2049 }
2050 case Primitive::kPrimLong: {
2051 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002052 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2053 if (locations->InAt(1).IsConstant()) {
2054 // Can use 3 operand multiply.
2055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2056 } else {
2057 locations->SetOut(Location::SameAsFirstInput());
2058 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002059 break;
2060 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002061 case Primitive::kPrimFloat:
2062 case Primitive::kPrimDouble: {
2063 locations->SetInAt(0, Location::RequiresFpuRegister());
2064 locations->SetInAt(1, Location::RequiresFpuRegister());
2065 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002066 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002067 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002068
2069 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002070 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002071 }
2072}
2073
2074void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2075 LocationSummary* locations = mul->GetLocations();
2076 Location first = locations->InAt(0);
2077 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002078 switch (mul->GetResultType()) {
2079 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002080 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002081 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002083 } else if (second.IsConstant()) {
2084 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002085 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002086 } else {
2087 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002088 __ imull(first.AsRegister<CpuRegister>(),
2089 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002090 }
2091 break;
2092 }
2093 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002094 if (second.IsConstant()) {
2095 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2096 DCHECK(IsInt<32>(value));
2097 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2098 first.AsRegister<CpuRegister>(),
2099 Immediate(static_cast<int32_t>(value)));
2100 } else {
2101 DCHECK(first.Equals(locations->Out()));
2102 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2103 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002104 break;
2105 }
2106
Calin Juravleb5bfa962014-10-21 18:02:24 +01002107 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002108 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002109 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002110 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002111 }
2112
2113 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002114 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002115 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002116 break;
2117 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002118
2119 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002120 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002121 }
2122}
2123
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002124void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2125 uint32_t stack_adjustment, bool is_float) {
2126 if (source.IsStackSlot()) {
2127 DCHECK(is_float);
2128 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2129 } else if (source.IsDoubleStackSlot()) {
2130 DCHECK(!is_float);
2131 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2132 } else {
2133 // Write the value to the temporary location on the stack and load to FP stack.
2134 if (is_float) {
2135 Location stack_temp = Location::StackSlot(temp_offset);
2136 codegen_->Move(stack_temp, source);
2137 __ flds(Address(CpuRegister(RSP), temp_offset));
2138 } else {
2139 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2140 codegen_->Move(stack_temp, source);
2141 __ fldl(Address(CpuRegister(RSP), temp_offset));
2142 }
2143 }
2144}
2145
2146void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2147 Primitive::Type type = rem->GetResultType();
2148 bool is_float = type == Primitive::kPrimFloat;
2149 size_t elem_size = Primitive::ComponentSize(type);
2150 LocationSummary* locations = rem->GetLocations();
2151 Location first = locations->InAt(0);
2152 Location second = locations->InAt(1);
2153 Location out = locations->Out();
2154
2155 // Create stack space for 2 elements.
2156 // TODO: enhance register allocator to ask for stack temporaries.
2157 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2158
2159 // Load the values to the FP stack in reverse order, using temporaries if needed.
2160 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2161 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2162
2163 // Loop doing FPREM until we stabilize.
2164 Label retry;
2165 __ Bind(&retry);
2166 __ fprem();
2167
2168 // Move FP status to AX.
2169 __ fstsw();
2170
2171 // And see if the argument reduction is complete. This is signaled by the
2172 // C2 FPU flag bit set to 0.
2173 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2174 __ j(kNotEqual, &retry);
2175
2176 // We have settled on the final value. Retrieve it into an XMM register.
2177 // Store FP top of stack to real stack.
2178 if (is_float) {
2179 __ fsts(Address(CpuRegister(RSP), 0));
2180 } else {
2181 __ fstl(Address(CpuRegister(RSP), 0));
2182 }
2183
2184 // Pop the 2 items from the FP stack.
2185 __ fucompp();
2186
2187 // Load the value from the stack into an XMM register.
2188 DCHECK(out.IsFpuRegister()) << out;
2189 if (is_float) {
2190 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2191 } else {
2192 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2193 }
2194
2195 // And remove the temporary stack space we allocated.
2196 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2197}
2198
Calin Juravlebacfec32014-11-14 15:54:36 +00002199void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2200 DCHECK(instruction->IsDiv() || instruction->IsRem());
2201 Primitive::Type type = instruction->GetResultType();
2202 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2203
2204 bool is_div = instruction->IsDiv();
2205 LocationSummary* locations = instruction->GetLocations();
2206
Roland Levillain271ab9c2014-11-27 15:23:57 +00002207 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2208 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002209
Roland Levillain271ab9c2014-11-27 15:23:57 +00002210 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002211 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2212
2213 SlowPathCodeX86_64* slow_path =
2214 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2215 out_reg.AsRegister(), type, is_div);
2216 codegen_->AddSlowPath(slow_path);
2217
2218 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2219 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2220 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002221 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002222 __ cmpl(second_reg, Immediate(-1));
2223 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002224 // edx:eax <- sign-extended of eax
2225 __ cdq();
2226 // eax = quotient, edx = remainder
2227 __ idivl(second_reg);
2228 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002229 __ cmpq(second_reg, Immediate(-1));
2230 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002231 // rdx:rax <- sign-extended of rax
2232 __ cqo();
2233 // rax = quotient, rdx = remainder
2234 __ idivq(second_reg);
2235 }
2236
2237 __ Bind(slow_path->GetExitLabel());
2238}
2239
Calin Juravle7c4954d2014-10-28 16:57:40 +00002240void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2241 LocationSummary* locations =
2242 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2243 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002244 case Primitive::kPrimInt:
2245 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002246 locations->SetInAt(0, Location::RegisterLocation(RAX));
2247 locations->SetInAt(1, Location::RequiresRegister());
2248 locations->SetOut(Location::SameAsFirstInput());
2249 // Intel uses edx:eax as the dividend.
2250 locations->AddTemp(Location::RegisterLocation(RDX));
2251 break;
2252 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002253
Calin Juravle7c4954d2014-10-28 16:57:40 +00002254 case Primitive::kPrimFloat:
2255 case Primitive::kPrimDouble: {
2256 locations->SetInAt(0, Location::RequiresFpuRegister());
2257 locations->SetInAt(1, Location::RequiresFpuRegister());
2258 locations->SetOut(Location::SameAsFirstInput());
2259 break;
2260 }
2261
2262 default:
2263 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2264 }
2265}
2266
2267void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2268 LocationSummary* locations = div->GetLocations();
2269 Location first = locations->InAt(0);
2270 Location second = locations->InAt(1);
2271 DCHECK(first.Equals(locations->Out()));
2272
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002273 Primitive::Type type = div->GetResultType();
2274 switch (type) {
2275 case Primitive::kPrimInt:
2276 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002277 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002278 break;
2279 }
2280
Calin Juravle7c4954d2014-10-28 16:57:40 +00002281 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002282 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002283 break;
2284 }
2285
2286 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002287 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002288 break;
2289 }
2290
2291 default:
2292 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2293 }
2294}
2295
Calin Juravlebacfec32014-11-14 15:54:36 +00002296void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002297 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002298 LocationSummary* locations =
2299 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002300
2301 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002302 case Primitive::kPrimInt:
2303 case Primitive::kPrimLong: {
2304 locations->SetInAt(0, Location::RegisterLocation(RAX));
2305 locations->SetInAt(1, Location::RequiresRegister());
2306 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2307 locations->SetOut(Location::RegisterLocation(RDX));
2308 break;
2309 }
2310
2311 case Primitive::kPrimFloat:
2312 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002313 locations->SetInAt(0, Location::Any());
2314 locations->SetInAt(1, Location::Any());
2315 locations->SetOut(Location::RequiresFpuRegister());
2316 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002317 break;
2318 }
2319
2320 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002321 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002322 }
2323}
2324
2325void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2326 Primitive::Type type = rem->GetResultType();
2327 switch (type) {
2328 case Primitive::kPrimInt:
2329 case Primitive::kPrimLong: {
2330 GenerateDivRemIntegral(rem);
2331 break;
2332 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002333 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002334 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002335 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002336 break;
2337 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002338 default:
2339 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2340 }
2341}
2342
Calin Juravled0d48522014-11-04 16:40:20 +00002343void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2344 LocationSummary* locations =
2345 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2346 locations->SetInAt(0, Location::Any());
2347 if (instruction->HasUses()) {
2348 locations->SetOut(Location::SameAsFirstInput());
2349 }
2350}
2351
2352void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2353 SlowPathCodeX86_64* slow_path =
2354 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2355 codegen_->AddSlowPath(slow_path);
2356
2357 LocationSummary* locations = instruction->GetLocations();
2358 Location value = locations->InAt(0);
2359
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002360 switch (instruction->GetType()) {
2361 case Primitive::kPrimInt: {
2362 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002363 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002364 __ j(kEqual, slow_path->GetEntryLabel());
2365 } else if (value.IsStackSlot()) {
2366 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2367 __ j(kEqual, slow_path->GetEntryLabel());
2368 } else {
2369 DCHECK(value.IsConstant()) << value;
2370 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2371 __ jmp(slow_path->GetEntryLabel());
2372 }
2373 }
2374 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002375 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002376 case Primitive::kPrimLong: {
2377 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002378 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002379 __ j(kEqual, slow_path->GetEntryLabel());
2380 } else if (value.IsDoubleStackSlot()) {
2381 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2382 __ j(kEqual, slow_path->GetEntryLabel());
2383 } else {
2384 DCHECK(value.IsConstant()) << value;
2385 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2386 __ jmp(slow_path->GetEntryLabel());
2387 }
2388 }
2389 break;
2390 }
2391 default:
2392 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002393 }
Calin Juravled0d48522014-11-04 16:40:20 +00002394}
2395
Calin Juravle9aec02f2014-11-18 23:06:35 +00002396void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2397 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2398
2399 LocationSummary* locations =
2400 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2401
2402 switch (op->GetResultType()) {
2403 case Primitive::kPrimInt:
2404 case Primitive::kPrimLong: {
2405 locations->SetInAt(0, Location::RequiresRegister());
2406 // The shift count needs to be in CL.
2407 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2408 locations->SetOut(Location::SameAsFirstInput());
2409 break;
2410 }
2411 default:
2412 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2413 }
2414}
2415
2416void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2417 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2418
2419 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002420 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002421 Location second = locations->InAt(1);
2422
2423 switch (op->GetResultType()) {
2424 case Primitive::kPrimInt: {
2425 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002426 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002427 if (op->IsShl()) {
2428 __ shll(first_reg, second_reg);
2429 } else if (op->IsShr()) {
2430 __ sarl(first_reg, second_reg);
2431 } else {
2432 __ shrl(first_reg, second_reg);
2433 }
2434 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002435 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002436 if (op->IsShl()) {
2437 __ shll(first_reg, imm);
2438 } else if (op->IsShr()) {
2439 __ sarl(first_reg, imm);
2440 } else {
2441 __ shrl(first_reg, imm);
2442 }
2443 }
2444 break;
2445 }
2446 case Primitive::kPrimLong: {
2447 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002448 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002449 if (op->IsShl()) {
2450 __ shlq(first_reg, second_reg);
2451 } else if (op->IsShr()) {
2452 __ sarq(first_reg, second_reg);
2453 } else {
2454 __ shrq(first_reg, second_reg);
2455 }
2456 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002457 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002458 if (op->IsShl()) {
2459 __ shlq(first_reg, imm);
2460 } else if (op->IsShr()) {
2461 __ sarq(first_reg, imm);
2462 } else {
2463 __ shrq(first_reg, imm);
2464 }
2465 }
2466 break;
2467 }
2468 default:
2469 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2470 }
2471}
2472
2473void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2474 HandleShift(shl);
2475}
2476
2477void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2478 HandleShift(shl);
2479}
2480
2481void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2482 HandleShift(shr);
2483}
2484
2485void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2486 HandleShift(shr);
2487}
2488
2489void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2490 HandleShift(ushr);
2491}
2492
2493void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2494 HandleShift(ushr);
2495}
2496
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002497void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002498 LocationSummary* locations =
2499 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002500 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002501 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2502 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2503 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002504}
2505
2506void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2507 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002508 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002509 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2510
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002511 __ gs()->call(
2512 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002513
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002514 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002515 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002516}
2517
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002518void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2519 LocationSummary* locations =
2520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2521 InvokeRuntimeCallingConvention calling_convention;
2522 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002523 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002524 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002525 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002526}
2527
2528void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2529 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002530 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002531 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2532
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002533 __ gs()->call(
2534 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002535
2536 DCHECK(!codegen_->IsLeafMethod());
2537 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2538}
2539
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002540void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002541 LocationSummary* locations =
2542 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002543 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2544 if (location.IsStackSlot()) {
2545 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2546 } else if (location.IsDoubleStackSlot()) {
2547 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2548 }
2549 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002550}
2551
2552void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2553 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002554 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002555}
2556
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002557void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002558 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002559 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002560 locations->SetInAt(0, Location::RequiresRegister());
2561 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002562}
2563
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002564void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2565 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002566 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2567 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002568 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002569 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002570 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002571 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002572 break;
2573
2574 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002575 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002576 break;
2577
2578 default:
2579 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2580 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002581}
2582
2583void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002584 LocationSummary* locations =
2585 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002586 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2587 locations->SetInAt(i, Location::Any());
2588 }
2589 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002590}
2591
2592void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002593 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002594 LOG(FATAL) << "Unimplemented";
2595}
2596
Calin Juravle52c48962014-12-16 17:02:57 +00002597void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2598 /*
2599 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2600 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2601 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2602 */
2603 switch (kind) {
2604 case MemBarrierKind::kAnyAny: {
2605 __ mfence();
2606 break;
2607 }
2608 case MemBarrierKind::kAnyStore:
2609 case MemBarrierKind::kLoadAny:
2610 case MemBarrierKind::kStoreStore: {
2611 // nop
2612 break;
2613 }
2614 default:
2615 LOG(FATAL) << "Unexpected memory barier " << kind;
2616 }
2617}
2618
2619void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2620 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2621
Nicolas Geoffray39468442014-09-02 15:17:15 +01002622 LocationSummary* locations =
2623 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002624 locations->SetInAt(0, Location::RequiresRegister());
2625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2626}
2627
2628void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2629 const FieldInfo& field_info) {
2630 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2631
2632 LocationSummary* locations = instruction->GetLocations();
2633 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2634 Location out = locations->Out();
2635 bool is_volatile = field_info.IsVolatile();
2636 Primitive::Type field_type = field_info.GetFieldType();
2637 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2638
2639 switch (field_type) {
2640 case Primitive::kPrimBoolean: {
2641 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2642 break;
2643 }
2644
2645 case Primitive::kPrimByte: {
2646 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2647 break;
2648 }
2649
2650 case Primitive::kPrimShort: {
2651 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2652 break;
2653 }
2654
2655 case Primitive::kPrimChar: {
2656 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2657 break;
2658 }
2659
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimNot: {
2662 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2663 break;
2664 }
2665
2666 case Primitive::kPrimLong: {
2667 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2668 break;
2669 }
2670
2671 case Primitive::kPrimFloat: {
2672 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2673 break;
2674 }
2675
2676 case Primitive::kPrimDouble: {
2677 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2678 break;
2679 }
2680
2681 case Primitive::kPrimVoid:
2682 LOG(FATAL) << "Unreachable type " << field_type;
2683 UNREACHABLE();
2684 }
2685
Calin Juravle77520bc2015-01-12 18:45:46 +00002686 codegen_->MaybeRecordImplicitNullCheck(instruction);
2687
Calin Juravle52c48962014-12-16 17:02:57 +00002688 if (is_volatile) {
2689 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2690 }
2691}
2692
2693void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2694 const FieldInfo& field_info) {
2695 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2696
2697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002699 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002700 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2701
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002702 locations->SetInAt(0, Location::RequiresRegister());
2703 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002704 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002705 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002706 locations->AddTemp(Location::RequiresRegister());
2707 locations->AddTemp(Location::RequiresRegister());
2708 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709}
2710
Calin Juravle52c48962014-12-16 17:02:57 +00002711void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2712 const FieldInfo& field_info) {
2713 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2714
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002715 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002716 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2717 Location value = locations->InAt(1);
2718 bool is_volatile = field_info.IsVolatile();
2719 Primitive::Type field_type = field_info.GetFieldType();
2720 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2721
2722 if (is_volatile) {
2723 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2724 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002725
2726 switch (field_type) {
2727 case Primitive::kPrimBoolean:
2728 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002729 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730 break;
2731 }
2732
2733 case Primitive::kPrimShort:
2734 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002735 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736 break;
2737 }
2738
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002739 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002741 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002742 break;
2743 }
2744
2745 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002746 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002747 break;
2748 }
2749
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002750 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002751 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002752 break;
2753 }
2754
2755 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002756 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002757 break;
2758 }
2759
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002760 case Primitive::kPrimVoid:
2761 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002762 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002763 }
Calin Juravle52c48962014-12-16 17:02:57 +00002764
Calin Juravle77520bc2015-01-12 18:45:46 +00002765 codegen_->MaybeRecordImplicitNullCheck(instruction);
2766
2767 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2768 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2769 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2770 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2771 }
2772
Calin Juravle52c48962014-12-16 17:02:57 +00002773 if (is_volatile) {
2774 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2775 }
2776}
2777
2778void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2779 HandleFieldSet(instruction, instruction->GetFieldInfo());
2780}
2781
2782void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2783 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002784}
2785
2786void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002787 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002788}
2789
2790void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002791 HandleFieldGet(instruction, instruction->GetFieldInfo());
2792}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002793
Calin Juravle52c48962014-12-16 17:02:57 +00002794void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2795 HandleFieldGet(instruction);
2796}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797
Calin Juravle52c48962014-12-16 17:02:57 +00002798void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2799 HandleFieldGet(instruction, instruction->GetFieldInfo());
2800}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002801
Calin Juravle52c48962014-12-16 17:02:57 +00002802void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2803 HandleFieldSet(instruction, instruction->GetFieldInfo());
2804}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002805
Calin Juravle52c48962014-12-16 17:02:57 +00002806void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2807 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002808}
2809
2810void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002811 LocationSummary* locations =
2812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002813 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2814 ? Location::RequiresRegister()
2815 : Location::Any();
2816 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002817 if (instruction->HasUses()) {
2818 locations->SetOut(Location::SameAsFirstInput());
2819 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002820}
2821
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002822void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002823 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2824 return;
2825 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002826 LocationSummary* locations = instruction->GetLocations();
2827 Location obj = locations->InAt(0);
2828
2829 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2830 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2831}
2832
2833void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002834 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002835 codegen_->AddSlowPath(slow_path);
2836
2837 LocationSummary* locations = instruction->GetLocations();
2838 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002839
2840 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002841 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002842 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002843 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002844 } else {
2845 DCHECK(obj.IsConstant()) << obj;
2846 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2847 __ jmp(slow_path->GetEntryLabel());
2848 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002849 }
2850 __ j(kEqual, slow_path->GetEntryLabel());
2851}
2852
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002853void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2854 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2855 GenerateImplicitNullCheck(instruction);
2856 } else {
2857 GenerateExplicitNullCheck(instruction);
2858 }
2859}
2860
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002862 LocationSummary* locations =
2863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002864 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002865 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002866 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2867 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002868}
2869
2870void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2871 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002872 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002873 Location index = locations->InAt(1);
2874
2875 switch (instruction->GetType()) {
2876 case Primitive::kPrimBoolean: {
2877 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002878 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002879 if (index.IsConstant()) {
2880 __ movzxb(out, Address(obj,
2881 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2882 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002883 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002884 }
2885 break;
2886 }
2887
2888 case Primitive::kPrimByte: {
2889 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002890 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002891 if (index.IsConstant()) {
2892 __ movsxb(out, Address(obj,
2893 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2894 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002895 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002896 }
2897 break;
2898 }
2899
2900 case Primitive::kPrimShort: {
2901 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002902 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002903 if (index.IsConstant()) {
2904 __ movsxw(out, Address(obj,
2905 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2906 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002908 }
2909 break;
2910 }
2911
2912 case Primitive::kPrimChar: {
2913 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002914 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002915 if (index.IsConstant()) {
2916 __ movzxw(out, Address(obj,
2917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2918 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002920 }
2921 break;
2922 }
2923
2924 case Primitive::kPrimInt:
2925 case Primitive::kPrimNot: {
2926 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2927 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002929 if (index.IsConstant()) {
2930 __ movl(out, Address(obj,
2931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2932 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002933 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002934 }
2935 break;
2936 }
2937
2938 case Primitive::kPrimLong: {
2939 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002940 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002941 if (index.IsConstant()) {
2942 __ movq(out, Address(obj,
2943 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002945 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002946 }
2947 break;
2948 }
2949
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002950 case Primitive::kPrimFloat: {
2951 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002952 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002953 if (index.IsConstant()) {
2954 __ movss(out, Address(obj,
2955 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2956 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002958 }
2959 break;
2960 }
2961
2962 case Primitive::kPrimDouble: {
2963 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002964 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002965 if (index.IsConstant()) {
2966 __ movsd(out, Address(obj,
2967 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2968 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002970 }
2971 break;
2972 }
2973
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002974 case Primitive::kPrimVoid:
2975 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002976 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002977 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002978 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002979}
2980
2981void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002982 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002983
2984 bool needs_write_barrier =
2985 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2986 bool needs_runtime_call = instruction->NeedsTypeCheck();
2987
Nicolas Geoffray39468442014-09-02 15:17:15 +01002988 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002989 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2990 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002991 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002992 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2993 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2994 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002995 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002996 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002997 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002998 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2999 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003000 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003001 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003002 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3003 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003004 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003005 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003006 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003007
3008 if (needs_write_barrier) {
3009 // Temporary registers for the write barrier.
3010 locations->AddTemp(Location::RequiresRegister());
3011 locations->AddTemp(Location::RequiresRegister());
3012 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003013 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003014}
3015
3016void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3017 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003018 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003020 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003021 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003022 bool needs_runtime_call = locations->WillCall();
3023 bool needs_write_barrier =
3024 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003025
3026 switch (value_type) {
3027 case Primitive::kPrimBoolean:
3028 case Primitive::kPrimByte: {
3029 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003030 if (index.IsConstant()) {
3031 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003032 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003034 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003035 __ movb(Address(obj, offset),
3036 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003037 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003039 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003040 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3041 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003042 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003043 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003044 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3045 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003046 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003047 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003048 break;
3049 }
3050
3051 case Primitive::kPrimShort:
3052 case Primitive::kPrimChar: {
3053 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054 if (index.IsConstant()) {
3055 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003056 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003058 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003059 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003060 __ movw(Address(obj, offset),
3061 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003062 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003063 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003064 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003065 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3067 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003068 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003069 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003070 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003071 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3072 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003074 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003075 break;
3076 }
3077
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003078 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003079 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003080 if (!needs_runtime_call) {
3081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3082 if (index.IsConstant()) {
3083 size_t offset =
3084 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3085 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003086 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003087 } else {
3088 DCHECK(value.IsConstant()) << value;
3089 __ movl(Address(obj, offset),
3090 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3091 }
3092 } else {
3093 DCHECK(index.IsRegister()) << index;
3094 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003095 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3096 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003097 } else {
3098 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003099 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003100 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3101 }
3102 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003103 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003104 if (needs_write_barrier) {
3105 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3107 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3108 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003109 }
3110 } else {
3111 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003112 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3113 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003114 DCHECK(!codegen_->IsLeafMethod());
3115 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3116 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 break;
3118 }
3119
3120 case Primitive::kPrimLong: {
3121 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003122 if (index.IsConstant()) {
3123 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003124 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003125 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003126 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003127 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3129 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003131 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132 break;
3133 }
3134
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003135 case Primitive::kPrimFloat: {
3136 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3137 if (index.IsConstant()) {
3138 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3139 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003140 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003141 } else {
3142 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003143 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3144 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003146 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003147 break;
3148 }
3149
3150 case Primitive::kPrimDouble: {
3151 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3152 if (index.IsConstant()) {
3153 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3154 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003155 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003156 } else {
3157 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003158 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3159 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003161 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162 break;
3163 }
3164
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165 case Primitive::kPrimVoid:
3166 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003167 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003168 }
3169}
3170
3171void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 LocationSummary* locations =
3173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003174 locations->SetInAt(0, Location::RequiresRegister());
3175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003176}
3177
3178void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3179 LocationSummary* locations = instruction->GetLocations();
3180 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003181 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3182 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003183 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003184 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003185}
3186
3187void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003188 LocationSummary* locations =
3189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003190 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003192 if (instruction->HasUses()) {
3193 locations->SetOut(Location::SameAsFirstInput());
3194 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003195}
3196
3197void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3198 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003199 Location index_loc = locations->InAt(0);
3200 Location length_loc = locations->InAt(1);
3201 SlowPathCodeX86_64* slow_path =
3202 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003203 codegen_->AddSlowPath(slow_path);
3204
Mark Mendellf60c90b2015-03-04 15:12:59 -05003205 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3206 if (index_loc.IsConstant()) {
3207 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3208 __ cmpl(length, Immediate(value));
3209 } else {
3210 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3211 }
3212 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003213}
3214
3215void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3216 CpuRegister card,
3217 CpuRegister object,
3218 CpuRegister value) {
3219 Label is_null;
3220 __ testl(value, value);
3221 __ j(kEqual, &is_null);
3222 __ gs()->movq(card, Address::Absolute(
3223 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3224 __ movq(temp, object);
3225 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3226 __ movb(Address(temp, card, TIMES_1, 0), card);
3227 __ Bind(&is_null);
3228}
3229
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003230void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3231 temp->SetLocations(nullptr);
3232}
3233
3234void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3235 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003236 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003237}
3238
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003239void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003240 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003241 LOG(FATAL) << "Unimplemented";
3242}
3243
3244void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003245 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3246}
3247
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003248void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3250}
3251
3252void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003253 HBasicBlock* block = instruction->GetBlock();
3254 if (block->GetLoopInformation() != nullptr) {
3255 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3256 // The back edge will generate the suspend check.
3257 return;
3258 }
3259 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3260 // The goto will generate the suspend check.
3261 return;
3262 }
3263 GenerateSuspendCheck(instruction, nullptr);
3264}
3265
3266void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3267 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003268 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003269 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003270 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003271 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003272 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003273 if (successor == nullptr) {
3274 __ j(kNotEqual, slow_path->GetEntryLabel());
3275 __ Bind(slow_path->GetReturnLabel());
3276 } else {
3277 __ j(kEqual, codegen_->GetLabelOf(successor));
3278 __ jmp(slow_path->GetEntryLabel());
3279 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003280}
3281
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003282X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3283 return codegen_->GetAssembler();
3284}
3285
3286void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3287 MoveOperands* move = moves_.Get(index);
3288 Location source = move->GetSource();
3289 Location destination = move->GetDestination();
3290
3291 if (source.IsRegister()) {
3292 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003294 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003295 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003296 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003297 } else {
3298 DCHECK(destination.IsDoubleStackSlot());
3299 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003300 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003301 }
3302 } else if (source.IsStackSlot()) {
3303 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003304 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003305 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003306 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003307 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003308 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003309 } else {
3310 DCHECK(destination.IsStackSlot());
3311 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3312 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3313 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003314 } else if (source.IsDoubleStackSlot()) {
3315 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003316 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003317 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003318 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003319 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3320 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003321 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003322 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003323 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3324 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3325 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003326 } else if (source.IsConstant()) {
3327 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003328 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3329 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003330 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003331 if (value == 0) {
3332 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3333 } else {
3334 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3335 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003336 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003337 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003338 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003339 }
3340 } else if (constant->IsLongConstant()) {
3341 int64_t value = constant->AsLongConstant()->GetValue();
3342 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003344 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003345 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003346 __ movq(CpuRegister(TMP), Immediate(value));
3347 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3348 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003349 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003350 float fp_value = constant->AsFloatConstant()->GetValue();
3351 int32_t value = bit_cast<float, int32_t>(fp_value);
3352 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003353 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003354 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3355 if (value == 0) {
3356 // easy FP 0.0.
3357 __ xorps(dest, dest);
3358 } else {
3359 __ movl(CpuRegister(TMP), imm);
3360 __ movd(dest, CpuRegister(TMP));
3361 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003362 } else {
3363 DCHECK(destination.IsStackSlot()) << destination;
3364 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3365 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003366 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003367 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003368 double fp_value = constant->AsDoubleConstant()->GetValue();
3369 int64_t value = bit_cast<double, int64_t>(fp_value);
3370 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003371 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003372 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3373 if (value == 0) {
3374 __ xorpd(dest, dest);
3375 } else {
3376 __ movq(CpuRegister(TMP), imm);
3377 __ movd(dest, CpuRegister(TMP));
3378 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003379 } else {
3380 DCHECK(destination.IsDoubleStackSlot()) << destination;
3381 __ movq(CpuRegister(TMP), imm);
3382 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3383 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003384 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003385 } else if (source.IsFpuRegister()) {
3386 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003387 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003388 } else if (destination.IsStackSlot()) {
3389 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003390 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003391 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003392 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003393 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003394 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003395 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003396 }
3397}
3398
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003399void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003400 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003401 __ movl(Address(CpuRegister(RSP), mem), reg);
3402 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003403}
3404
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003405void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003406 ScratchRegisterScope ensure_scratch(
3407 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3408
3409 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3410 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3411 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3412 Address(CpuRegister(RSP), mem2 + stack_offset));
3413 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3414 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3415 CpuRegister(ensure_scratch.GetRegister()));
3416}
3417
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003418void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3419 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3420 __ movq(Address(CpuRegister(RSP), mem), reg);
3421 __ movq(reg, CpuRegister(TMP));
3422}
3423
3424void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3425 ScratchRegisterScope ensure_scratch(
3426 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3427
3428 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3429 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3430 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3431 Address(CpuRegister(RSP), mem2 + stack_offset));
3432 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3433 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3434 CpuRegister(ensure_scratch.GetRegister()));
3435}
3436
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003437void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3438 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3439 __ movss(Address(CpuRegister(RSP), mem), reg);
3440 __ movd(reg, CpuRegister(TMP));
3441}
3442
3443void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3444 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3445 __ movsd(Address(CpuRegister(RSP), mem), reg);
3446 __ movd(reg, CpuRegister(TMP));
3447}
3448
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003449void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3450 MoveOperands* move = moves_.Get(index);
3451 Location source = move->GetSource();
3452 Location destination = move->GetDestination();
3453
3454 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003456 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003458 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003459 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003460 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003461 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3462 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003464 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003465 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003466 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3467 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003468 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3470 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3471 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003472 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003473 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003474 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003475 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003476 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003477 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003478 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003480 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003481 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003482 }
3483}
3484
3485
3486void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3487 __ pushq(CpuRegister(reg));
3488}
3489
3490
3491void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3492 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003493}
3494
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003495void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3496 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3497 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3498 Immediate(mirror::Class::kStatusInitialized));
3499 __ j(kLess, slow_path->GetEntryLabel());
3500 __ Bind(slow_path->GetExitLabel());
3501 // No need for memory fence, thanks to the X86_64 memory model.
3502}
3503
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003504void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003505 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3506 ? LocationSummary::kCallOnSlowPath
3507 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003508 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003509 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003510 locations->SetOut(Location::RequiresRegister());
3511}
3512
3513void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003514 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003515 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003516 DCHECK(!cls->CanCallRuntime());
3517 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003518 codegen_->LoadCurrentMethod(out);
3519 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3520 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003521 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003522 codegen_->LoadCurrentMethod(out);
3523 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3524 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003525 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3526 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3527 codegen_->AddSlowPath(slow_path);
3528 __ testl(out, out);
3529 __ j(kEqual, slow_path->GetEntryLabel());
3530 if (cls->MustGenerateClinitCheck()) {
3531 GenerateClassInitializationCheck(slow_path, out);
3532 } else {
3533 __ Bind(slow_path->GetExitLabel());
3534 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003535 }
3536}
3537
3538void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3539 LocationSummary* locations =
3540 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3541 locations->SetInAt(0, Location::RequiresRegister());
3542 if (check->HasUses()) {
3543 locations->SetOut(Location::SameAsFirstInput());
3544 }
3545}
3546
3547void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003548 // We assume the class to not be null.
3549 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3550 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003551 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003552 GenerateClassInitializationCheck(slow_path,
3553 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003554}
3555
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003556void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3557 LocationSummary* locations =
3558 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3559 locations->SetOut(Location::RequiresRegister());
3560}
3561
3562void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3563 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3564 codegen_->AddSlowPath(slow_path);
3565
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003567 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003568 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3569 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003570 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3571 __ testl(out, out);
3572 __ j(kEqual, slow_path->GetEntryLabel());
3573 __ Bind(slow_path->GetExitLabel());
3574}
3575
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003576void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3577 LocationSummary* locations =
3578 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3579 locations->SetOut(Location::RequiresRegister());
3580}
3581
3582void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3583 Address address = Address::Absolute(
3584 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003585 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003586 __ gs()->movl(address, Immediate(0));
3587}
3588
3589void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3590 LocationSummary* locations =
3591 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3592 InvokeRuntimeCallingConvention calling_convention;
3593 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3594}
3595
3596void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3597 __ gs()->call(
3598 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3599 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3600}
3601
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003602void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003603 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3604 ? LocationSummary::kNoCall
3605 : LocationSummary::kCallOnSlowPath;
3606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3607 locations->SetInAt(0, Location::RequiresRegister());
3608 locations->SetInAt(1, Location::Any());
3609 locations->SetOut(Location::RequiresRegister());
3610}
3611
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003612void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003613 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003615 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003617 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3618 Label done, zero;
3619 SlowPathCodeX86_64* slow_path = nullptr;
3620
3621 // Return 0 if `obj` is null.
3622 // TODO: avoid this check if we know obj is not null.
3623 __ testl(obj, obj);
3624 __ j(kEqual, &zero);
3625 // Compare the class of `obj` with `cls`.
3626 __ movl(out, Address(obj, class_offset));
3627 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003629 } else {
3630 DCHECK(cls.IsStackSlot()) << cls;
3631 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3632 }
3633 if (instruction->IsClassFinal()) {
3634 // Classes must be equal for the instanceof to succeed.
3635 __ j(kNotEqual, &zero);
3636 __ movl(out, Immediate(1));
3637 __ jmp(&done);
3638 } else {
3639 // If the classes are not equal, we go into a slow path.
3640 DCHECK(locations->OnlyCallsOnSlowPath());
3641 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003642 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003643 codegen_->AddSlowPath(slow_path);
3644 __ j(kNotEqual, slow_path->GetEntryLabel());
3645 __ movl(out, Immediate(1));
3646 __ jmp(&done);
3647 }
3648 __ Bind(&zero);
3649 __ movl(out, Immediate(0));
3650 if (slow_path != nullptr) {
3651 __ Bind(slow_path->GetExitLabel());
3652 }
3653 __ Bind(&done);
3654}
3655
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003656void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3657 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3658 instruction, LocationSummary::kCallOnSlowPath);
3659 locations->SetInAt(0, Location::RequiresRegister());
3660 locations->SetInAt(1, Location::Any());
3661 locations->AddTemp(Location::RequiresRegister());
3662}
3663
3664void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3665 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003667 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003669 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3670 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3671 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3672 codegen_->AddSlowPath(slow_path);
3673
3674 // TODO: avoid this check if we know obj is not null.
3675 __ testl(obj, obj);
3676 __ j(kEqual, slow_path->GetExitLabel());
3677 // Compare the class of `obj` with `cls`.
3678 __ movl(temp, Address(obj, class_offset));
3679 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003681 } else {
3682 DCHECK(cls.IsStackSlot()) << cls;
3683 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3684 }
3685 // Classes must be equal for the checkcast to succeed.
3686 __ j(kNotEqual, slow_path->GetEntryLabel());
3687 __ Bind(slow_path->GetExitLabel());
3688}
3689
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003690void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3691 LocationSummary* locations =
3692 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3693 InvokeRuntimeCallingConvention calling_convention;
3694 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3695}
3696
3697void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3698 __ gs()->call(Address::Absolute(instruction->IsEnter()
3699 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3700 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3701 true));
3702 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3703}
3704
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003705void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3706void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3707void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3708
3709void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3710 LocationSummary* locations =
3711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3712 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3713 || instruction->GetResultType() == Primitive::kPrimLong);
3714 locations->SetInAt(0, Location::RequiresRegister());
3715 if (instruction->GetType() == Primitive::kPrimInt) {
3716 locations->SetInAt(1, Location::Any());
3717 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003718 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003719 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003720 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003721 }
3722 locations->SetOut(Location::SameAsFirstInput());
3723}
3724
3725void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3726 HandleBitwiseOperation(instruction);
3727}
3728
3729void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3730 HandleBitwiseOperation(instruction);
3731}
3732
3733void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3734 HandleBitwiseOperation(instruction);
3735}
3736
3737void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3738 LocationSummary* locations = instruction->GetLocations();
3739 Location first = locations->InAt(0);
3740 Location second = locations->InAt(1);
3741 DCHECK(first.Equals(locations->Out()));
3742
3743 if (instruction->GetResultType() == Primitive::kPrimInt) {
3744 if (second.IsRegister()) {
3745 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003747 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003748 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003749 } else {
3750 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003751 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003752 }
3753 } else if (second.IsConstant()) {
3754 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3755 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003757 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003758 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003759 } else {
3760 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003761 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003762 }
3763 } else {
3764 Address address(CpuRegister(RSP), second.GetStackIndex());
3765 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003767 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003768 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003769 } else {
3770 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003771 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003772 }
3773 }
3774 } else {
3775 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003776 CpuRegister first_reg = first.AsRegister<CpuRegister>();
3777 bool second_is_constant = false;
3778 int64_t value = 0;
3779 if (second.IsConstant()) {
3780 second_is_constant = true;
3781 value = second.GetConstant()->AsLongConstant()->GetValue();
3782 DCHECK(IsInt<32>(value));
3783 }
3784
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003785 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003786 if (second_is_constant) {
3787 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
3788 } else {
3789 __ andq(first_reg, second.AsRegister<CpuRegister>());
3790 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003791 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003792 if (second_is_constant) {
3793 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
3794 } else {
3795 __ orq(first_reg, second.AsRegister<CpuRegister>());
3796 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003797 } else {
3798 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003799 if (second_is_constant) {
3800 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
3801 } else {
3802 __ xorq(first_reg, second.AsRegister<CpuRegister>());
3803 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003804 }
3805 }
3806}
3807
Calin Juravleb1498f62015-02-16 13:13:29 +00003808void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
3809 // Nothing to do, this should be removed during prepare for register allocator.
3810 UNUSED(instruction);
3811 LOG(FATAL) << "Unreachable";
3812}
3813
3814void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
3815 // Nothing to do, this should be removed during prepare for register allocator.
3816 UNUSED(instruction);
3817 LOG(FATAL) << "Unreachable";
3818}
3819
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003820} // namespace x86_64
3821} // namespace art