blob: 2bb0349932f1003849beddd997728a225e0d7471 [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
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080022#include "intrinsics.h"
23#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010027#include "mirror/object_reference.h"
28#include "thread.h"
29#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/managed_register_x86_64.h"
33
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010034namespace art {
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036namespace x86_64 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038// Some x86_64 instructions require a register to be available as temp.
39static constexpr Register TMP = R11;
40
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000043static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
50
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010051class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Alexandre Rames2ed20af2015-03-06 13:55:35 +000055 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010057 __ gs()->call(
58 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000059 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 }
61
62 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010063 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
65};
66
Calin Juravled0d48522014-11-04 16:40:20 +000067class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
68 public:
69 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
70
Alexandre Rames2ed20af2015-03-06 13:55:35 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000072 __ Bind(GetEntryLabel());
73 __ gs()->call(
74 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000075 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000076 }
77
78 private:
79 HDivZeroCheck* const instruction_;
80 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
81};
82
Calin Juravlebacfec32014-11-14 15:54:36 +000083class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000085 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
86 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +000090 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +000091 if (is_div_) {
92 __ negl(cpu_reg_);
93 } else {
94 __ movl(cpu_reg_, Immediate(0));
95 }
96
Calin Juravled6fb6cf2014-11-11 19:07:44 +000097 } else {
98 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +000099 if (is_div_) {
100 __ negq(cpu_reg_);
101 } else {
102 __ movq(cpu_reg_, Immediate(0));
103 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000104 }
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ jmp(GetExitLabel());
106 }
107
108 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000110 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000111 const bool is_div_;
112 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000113};
114
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100117 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000124 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000125 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ jmp(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
139 private:
140 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100141 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 Label return_label_;
143
144 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
145};
146
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100149 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
150 Location index_location,
151 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 : instruction_(instruction),
153 index_location_(index_location),
154 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000158 // We're moving two locations to locations that could overlap, so we need a parallel
159 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 codegen->EmitParallelMoves(
162 index_location_,
163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
164 length_location_,
165 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 __ gs()->call(Address::Absolute(
167 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000168 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100169 }
170
171 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 const Location index_location_;
174 const Location length_location_;
175
176 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
177};
178
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000179class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100180 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000181 LoadClassSlowPathX86_64(HLoadClass* cls,
182 HInstruction* at,
183 uint32_t dex_pc,
184 bool do_clinit)
185 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
186 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
187 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100188
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000190 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100191 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
192 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000199 __ gs()->call(Address::Absolute((do_clinit_
200 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
201 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000202 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000204 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000205 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 }
210
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 __ jmp(GetExitLabel());
213 }
214
215 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216 // The class this slow path will load.
217 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219 // The instruction where this slow path is happening.
220 // (Might be the load class or an initialization check).
221 HInstruction* const at_;
222
223 // The dex PC of `at_`.
224 const uint32_t dex_pc_;
225
226 // Whether to initialize the class.
227 const bool do_clinit_;
228
229 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230};
231
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
233 public:
234 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
235
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237 LocationSummary* locations = instruction_->GetLocations();
238 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
239
240 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
241 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000242 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243
244 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800245 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
246 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 Immediate(instruction_->GetStringIndex()));
248 __ gs()->call(Address::Absolute(
249 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000251 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 __ jmp(GetExitLabel());
254 }
255
256 private:
257 HLoadString* const instruction_;
258
259 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
260};
261
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
263 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000264 TypeCheckSlowPathX86_64(HInstruction* instruction,
265 Location class_to_check,
266 Location object_class,
267 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000269 class_to_check_(class_to_check),
270 object_class_(object_class),
271 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 DCHECK(instruction_->IsCheckCast()
276 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
278 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 // We're moving two locations to locations that could overlap, so we need a parallel
283 // move resolver.
284 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000285 codegen->EmitParallelMoves(
286 class_to_check_,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
288 object_class_,
289 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 if (instruction_->IsInstanceOf()) {
292 __ gs()->call(
293 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
294 } else {
295 DCHECK(instruction_->IsCheckCast());
296 __ gs()->call(
297 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
298 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000299 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300
301 if (instruction_->IsInstanceOf()) {
302 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
303 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000305 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 __ jmp(GetExitLabel());
307 }
308
309 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 HInstruction* const instruction_;
311 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
315 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
316};
317
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100318#undef __
319#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
320
Dave Allison20dfc792014-06-16 20:44:29 -0700321inline Condition X86_64Condition(IfCondition cond) {
322 switch (cond) {
323 case kCondEQ: return kEqual;
324 case kCondNE: return kNotEqual;
325 case kCondLT: return kLess;
326 case kCondLE: return kLessEqual;
327 case kCondGT: return kGreater;
328 case kCondGE: return kGreaterEqual;
329 default:
330 LOG(FATAL) << "Unknown if condition";
331 }
332 return kEqual;
333}
334
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800335void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
336 CpuRegister temp) {
337 // All registers are assumed to be correctly set up.
338
339 // TODO: Implement all kinds of calls:
340 // 1) boot -> boot
341 // 2) app -> boot
342 // 3) app -> app
343 //
344 // Currently we implement the app -> app logic, which looks up in the resolve cache.
345
346 // temp = method;
347 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000348 if (!invoke->IsRecursive()) {
349 // temp = temp->dex_cache_resolved_methods_;
350 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
351 // temp = temp[index_in_cache]
352 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
353 // (temp + offset_of_quick_compiled_code)()
354 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
355 kX86_64WordSize).SizeValue()));
356 } else {
357 __ call(&frame_entry_label_);
358 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800359
360 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800361}
362
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100363void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
364 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
365}
366
367void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
368 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
369}
370
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100371size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
372 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
373 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100374}
375
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100376size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
377 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
378 return kX86_64WordSize;
379}
380
381size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
382 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
383 return kX86_64WordSize;
384}
385
386size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
387 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
388 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100389}
390
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000391static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000392// Use a fake return address register to mimic Quick.
393static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000394CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000395 : CodeGenerator(graph,
396 kNumberOfCpuRegisters,
397 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000398 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000399 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
400 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000401 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000402 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
403 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000404 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100405 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100406 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000407 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000408 move_resolver_(graph->GetArena(), this) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000409 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
410}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100411
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100412InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
413 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100414 : HGraphVisitor(graph),
415 assembler_(codegen->GetAssembler()),
416 codegen_(codegen) {}
417
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100418Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100419 switch (type) {
420 case Primitive::kPrimLong:
421 case Primitive::kPrimByte:
422 case Primitive::kPrimBoolean:
423 case Primitive::kPrimChar:
424 case Primitive::kPrimShort:
425 case Primitive::kPrimInt:
426 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100428 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100429 }
430
431 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100432 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100433 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100435 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436
437 case Primitive::kPrimVoid:
438 LOG(FATAL) << "Unreachable type " << type;
439 }
440
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100441 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100442}
443
Nicolas Geoffray98893962015-01-21 12:32:32 +0000444void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000448 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100449 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000450
Nicolas Geoffray98893962015-01-21 12:32:32 +0000451 if (is_baseline) {
452 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
453 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
454 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000455 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
456 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
457 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000458 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459}
460
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100461void CodeGeneratorX86_64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000462 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100463 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700464 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000465 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100466
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000467 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100468 __ testq(CpuRegister(RAX), Address(
469 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100471 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000472
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000473 if (HasEmptyFrame()) {
474 return;
475 }
476
Nicolas Geoffray98893962015-01-21 12:32:32 +0000477 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000479 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000480 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000481 }
482 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100483
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000484 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
485 uint32_t xmm_spill_location = GetFpuSpillStart();
486 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100487
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000488 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
489 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
490 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
491 XmmRegister(kFpuCalleeSaves[i]));
492 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100493 }
494
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100495 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
496}
497
498void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000499 if (HasEmptyFrame()) {
500 return;
501 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000502 uint32_t xmm_spill_location = GetFpuSpillStart();
503 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
504 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
505 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
506 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
507 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
508 }
509 }
510
511 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000512
513 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000514 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000515 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000516 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000517 }
518 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100519}
520
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100521void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
522 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100523}
524
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100525void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000526 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100527 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
528}
529
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100530Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
531 switch (load->GetType()) {
532 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100534 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
535 break;
536
537 case Primitive::kPrimInt:
538 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100539 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100540 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541
542 case Primitive::kPrimBoolean:
543 case Primitive::kPrimByte:
544 case Primitive::kPrimChar:
545 case Primitive::kPrimShort:
546 case Primitive::kPrimVoid:
547 LOG(FATAL) << "Unexpected type " << load->GetType();
548 }
549
550 LOG(FATAL) << "Unreachable";
551 return Location();
552}
553
554void CodeGeneratorX86_64::Move(Location destination, Location source) {
555 if (source.Equals(destination)) {
556 return;
557 }
558 if (destination.IsRegister()) {
559 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000560 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100561 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000562 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000564 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100565 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100566 } else {
567 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000568 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100569 Address(CpuRegister(RSP), source.GetStackIndex()));
570 }
571 } else if (destination.IsFpuRegister()) {
572 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000573 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000575 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100576 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 Address(CpuRegister(RSP), source.GetStackIndex()));
579 } else {
580 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 }
584 } else if (destination.IsStackSlot()) {
585 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else if (source.IsFpuRegister()) {
589 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500591 } else if (source.IsConstant()) {
592 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000593 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500594 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100595 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500596 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000597 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
598 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100599 }
600 } else {
601 DCHECK(destination.IsDoubleStackSlot());
602 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 __ movq(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 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500608 } else if (source.IsConstant()) {
609 HConstant* constant = source.GetConstant();
610 int64_t value = constant->AsLongConstant()->GetValue();
611 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000612 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500613 } else {
614 DCHECK(constant->IsLongConstant());
615 value = constant->AsLongConstant()->GetValue();
616 }
617 __ movq(CpuRegister(TMP), Immediate(value));
618 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100619 } else {
620 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000621 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
622 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100623 }
624 }
625}
626
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100627void CodeGeneratorX86_64::Move(HInstruction* instruction,
628 Location location,
629 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000630 LocationSummary* locations = instruction->GetLocations();
631 if (locations != nullptr && locations->Out().Equals(location)) {
632 return;
633 }
634
635 if (locations != nullptr && locations->Out().IsConstant()) {
636 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000637 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
638 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000639 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000641 } else if (location.IsStackSlot()) {
642 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
643 } else {
644 DCHECK(location.IsConstant());
645 DCHECK_EQ(location.GetConstant(), const_to_move);
646 }
647 } else if (const_to_move->IsLongConstant()) {
648 int64_t value = const_to_move->AsLongConstant()->GetValue();
649 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000651 } else if (location.IsDoubleStackSlot()) {
652 __ movq(CpuRegister(TMP), Immediate(value));
653 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
654 } else {
655 DCHECK(location.IsConstant());
656 DCHECK_EQ(location.GetConstant(), const_to_move);
657 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100658 }
Roland Levillain476df552014-10-09 17:51:36 +0100659 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100660 switch (instruction->GetType()) {
661 case Primitive::kPrimBoolean:
662 case Primitive::kPrimByte:
663 case Primitive::kPrimChar:
664 case Primitive::kPrimShort:
665 case Primitive::kPrimInt:
666 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100667 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100668 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
669 break;
670
671 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100672 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000673 Move(location,
674 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675 break;
676
677 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100679 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000680 } else if (instruction->IsTemporary()) {
681 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
682 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100683 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100684 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685 switch (instruction->GetType()) {
686 case Primitive::kPrimBoolean:
687 case Primitive::kPrimByte:
688 case Primitive::kPrimChar:
689 case Primitive::kPrimShort:
690 case Primitive::kPrimInt:
691 case Primitive::kPrimNot:
692 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100693 case Primitive::kPrimFloat:
694 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000695 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100696 break;
697
698 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100699 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100700 }
701 }
702}
703
704void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
705 got->SetLocations(nullptr);
706}
707
708void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
709 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100710 DCHECK(!successor->IsExitBlock());
711
712 HBasicBlock* block = got->GetBlock();
713 HInstruction* previous = got->GetPrevious();
714
715 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000716 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100717 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
718 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
719 return;
720 }
721
722 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
723 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
724 }
725 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726 __ jmp(codegen_->GetLabelOf(successor));
727 }
728}
729
730void LocationsBuilderX86_64::VisitExit(HExit* exit) {
731 exit->SetLocations(nullptr);
732}
733
734void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700735 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736}
737
Andreas Gampe0ba62732015-03-24 02:39:46 +0000738void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
739 LocationSummary* locations =
740 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
741 HInstruction* cond = if_instr->InputAt(0);
742 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
743 locations->SetInAt(0, Location::Any());
744 }
745}
746
747void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
748 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100749 if (cond->IsIntConstant()) {
750 // Constant condition, statically compared against 1.
751 int32_t cond_value = cond->AsIntConstant()->GetValue();
752 if (cond_value == 1) {
Andreas Gampe0ba62732015-03-24 02:39:46 +0000753 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
754 if_instr->IfTrueSuccessor())) {
755 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100756 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100757 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100758 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100759 DCHECK_EQ(cond_value, 0);
760 }
761 } else {
762 bool materialized =
763 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
764 // Moves do not affect the eflags register, so if the condition is
765 // evaluated just before the if, we don't need to evaluate it
766 // again.
767 bool eflags_set = cond->IsCondition()
Andreas Gampe0ba62732015-03-24 02:39:46 +0000768 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100769 if (materialized) {
770 if (!eflags_set) {
771 // Materialized condition, compare against 0.
Andreas Gampe0ba62732015-03-24 02:39:46 +0000772 Location lhs = if_instr->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100773 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000774 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100775 } else {
776 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
777 Immediate(0));
778 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000779 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100780 } else {
Andreas Gampe0ba62732015-03-24 02:39:46 +0000781 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
782 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100783 }
784 } else {
785 Location lhs = cond->GetLocations()->InAt(0);
786 Location rhs = cond->GetLocations()->InAt(1);
787 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000788 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100789 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000790 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000791 if (constant == 0) {
792 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
793 } else {
794 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
795 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100798 Address(CpuRegister(RSP), rhs.GetStackIndex()));
799 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000800 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
801 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700802 }
Dave Allison20dfc792014-06-16 20:44:29 -0700803 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000804 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
805 if_instr->IfFalseSuccessor())) {
806 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100807 }
808}
809
810void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
811 local->SetLocations(nullptr);
812}
813
814void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
815 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
816}
817
818void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
819 local->SetLocations(nullptr);
820}
821
822void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
823 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700824 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100825}
826
827void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100828 LocationSummary* locations =
829 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100830 switch (store->InputAt(1)->GetType()) {
831 case Primitive::kPrimBoolean:
832 case Primitive::kPrimByte:
833 case Primitive::kPrimChar:
834 case Primitive::kPrimShort:
835 case Primitive::kPrimInt:
836 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100837 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100838 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
839 break;
840
841 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100842 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100843 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
844 break;
845
846 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100849}
850
851void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700852 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100853}
854
Dave Allison20dfc792014-06-16 20:44:29 -0700855void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100856 LocationSummary* locations =
857 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100858 locations->SetInAt(0, Location::RequiresRegister());
859 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100860 if (comp->NeedsMaterialization()) {
861 locations->SetOut(Location::RequiresRegister());
862 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100863}
864
Dave Allison20dfc792014-06-16 20:44:29 -0700865void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
866 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100867 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100869 // Clear register: setcc only sets the low byte.
870 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000871 Location lhs = locations->InAt(0);
872 Location rhs = locations->InAt(1);
873 if (rhs.IsRegister()) {
874 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
875 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800876 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000877 if (constant == 0) {
878 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
879 } else {
880 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
881 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100882 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000883 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100884 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100885 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700886 }
887}
888
889void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
890 VisitCondition(comp);
891}
892
893void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
894 VisitCondition(comp);
895}
896
897void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
898 VisitCondition(comp);
899}
900
901void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
902 VisitCondition(comp);
903}
904
905void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
906 VisitCondition(comp);
907}
908
909void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
910 VisitCondition(comp);
911}
912
913void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
914 VisitCondition(comp);
915}
916
917void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
918 VisitCondition(comp);
919}
920
921void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
922 VisitCondition(comp);
923}
924
925void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
926 VisitCondition(comp);
927}
928
929void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
930 VisitCondition(comp);
931}
932
933void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
934 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100935}
936
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100937void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100938 LocationSummary* locations =
939 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000940 switch (compare->InputAt(0)->GetType()) {
941 case Primitive::kPrimLong: {
942 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -0400943 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +0000944 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
945 break;
946 }
947 case Primitive::kPrimFloat:
948 case Primitive::kPrimDouble: {
949 locations->SetInAt(0, Location::RequiresFpuRegister());
950 locations->SetInAt(1, Location::RequiresFpuRegister());
951 locations->SetOut(Location::RequiresRegister());
952 break;
953 }
954 default:
955 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
956 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100957}
958
959void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100960 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000961 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000962 Location left = locations->InAt(0);
963 Location right = locations->InAt(1);
964
965 Label less, greater, done;
966 Primitive::Type type = compare->InputAt(0)->GetType();
967 switch (type) {
968 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -0400969 CpuRegister left_reg = left.AsRegister<CpuRegister>();
970 if (right.IsConstant()) {
971 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
972 DCHECK(IsInt<32>(value));
973 if (value == 0) {
974 __ testq(left_reg, left_reg);
975 } else {
976 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
977 }
978 } else {
979 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
980 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100981 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000982 }
983 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000984 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000985 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
986 break;
987 }
988 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000989 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000990 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
991 break;
992 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100993 default:
Calin Juravleddb7df22014-11-25 20:56:51 +0000994 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100995 }
Calin Juravleddb7df22014-11-25 20:56:51 +0000996 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +0000997 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +0000998 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +0000999
Calin Juravle91debbc2014-11-26 19:01:09 +00001000 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001001 __ movl(out, Immediate(1));
1002 __ jmp(&done);
1003
1004 __ Bind(&less);
1005 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001006
1007 __ Bind(&done);
1008}
1009
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001010void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001011 LocationSummary* locations =
1012 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001013 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001014}
1015
1016void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001017 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001018 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001019}
1020
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001021void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1022 LocationSummary* locations =
1023 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1024 locations->SetOut(Location::ConstantLocation(constant));
1025}
1026
1027void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1028 // Will be generated at use site.
1029 UNUSED(constant);
1030}
1031
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001032void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001033 LocationSummary* locations =
1034 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001035 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001036}
1037
1038void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001039 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001040 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001041}
1042
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001043void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1044 LocationSummary* locations =
1045 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1046 locations->SetOut(Location::ConstantLocation(constant));
1047}
1048
1049void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1050 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001051 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001052}
1053
1054void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1055 LocationSummary* locations =
1056 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1057 locations->SetOut(Location::ConstantLocation(constant));
1058}
1059
1060void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1061 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001062 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001063}
1064
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001065void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1066 ret->SetLocations(nullptr);
1067}
1068
1069void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001070 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001071 codegen_->GenerateFrameExit();
1072 __ ret();
1073}
1074
1075void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001076 LocationSummary* locations =
1077 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001078 switch (ret->InputAt(0)->GetType()) {
1079 case Primitive::kPrimBoolean:
1080 case Primitive::kPrimByte:
1081 case Primitive::kPrimChar:
1082 case Primitive::kPrimShort:
1083 case Primitive::kPrimInt:
1084 case Primitive::kPrimNot:
1085 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001086 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001087 break;
1088
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 case Primitive::kPrimFloat:
1090 case Primitive::kPrimDouble:
1091 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 break;
1094
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098}
1099
1100void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1101 if (kIsDebugBuild) {
1102 switch (ret->InputAt(0)->GetType()) {
1103 case Primitive::kPrimBoolean:
1104 case Primitive::kPrimByte:
1105 case Primitive::kPrimChar:
1106 case Primitive::kPrimShort:
1107 case Primitive::kPrimInt:
1108 case Primitive::kPrimNot:
1109 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001110 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001111 break;
1112
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 case Primitive::kPrimFloat:
1114 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 XMM0);
1117 break;
1118
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001119 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001120 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001121 }
1122 }
1123 codegen_->GenerateFrameExit();
1124 __ ret();
1125}
1126
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001127Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1128 switch (type) {
1129 case Primitive::kPrimBoolean:
1130 case Primitive::kPrimByte:
1131 case Primitive::kPrimChar:
1132 case Primitive::kPrimShort:
1133 case Primitive::kPrimInt:
1134 case Primitive::kPrimNot: {
1135 uint32_t index = gp_index_++;
1136 stack_index_++;
1137 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001138 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139 } else {
1140 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1141 }
1142 }
1143
1144 case Primitive::kPrimLong: {
1145 uint32_t index = gp_index_;
1146 stack_index_ += 2;
1147 if (index < calling_convention.GetNumberOfRegisters()) {
1148 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150 } else {
1151 gp_index_ += 2;
1152 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1153 }
1154 }
1155
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 case Primitive::kPrimFloat: {
1157 uint32_t index = fp_index_++;
1158 stack_index_++;
1159 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001160 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 } else {
1162 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1163 }
1164 }
1165
1166 case Primitive::kPrimDouble: {
1167 uint32_t index = fp_index_++;
1168 stack_index_ += 2;
1169 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 } else {
1172 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1173 }
1174 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001175
1176 case Primitive::kPrimVoid:
1177 LOG(FATAL) << "Unexpected parameter type " << type;
1178 break;
1179 }
1180 return Location();
1181}
1182
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001183void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001184 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1185 if (intrinsic.TryDispatch(invoke)) {
1186 return;
1187 }
1188
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189 HandleInvoke(invoke);
1190}
1191
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001192static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1193 if (invoke->GetLocations()->Intrinsified()) {
1194 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1195 intrinsic.Dispatch(invoke);
1196 return true;
1197 }
1198 return false;
1199}
1200
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001201void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001202 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1203 return;
1204 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001205
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001206 codegen_->GenerateStaticOrDirectCall(
1207 invoke,
1208 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001209 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210}
1211
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001212void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001213 LocationSummary* locations =
1214 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001215 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216
1217 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001218 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219 HInstruction* input = invoke->InputAt(i);
1220 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1221 }
1222
1223 switch (invoke->GetType()) {
1224 case Primitive::kPrimBoolean:
1225 case Primitive::kPrimByte:
1226 case Primitive::kPrimChar:
1227 case Primitive::kPrimShort:
1228 case Primitive::kPrimInt:
1229 case Primitive::kPrimNot:
1230 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001231 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001232 break;
1233
1234 case Primitive::kPrimVoid:
1235 break;
1236
1237 case Primitive::kPrimDouble:
1238 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001239 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001240 break;
1241 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001242}
1243
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001244void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001245 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1246 if (intrinsic.TryDispatch(invoke)) {
1247 return;
1248 }
1249
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001250 HandleInvoke(invoke);
1251}
1252
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001253void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001254 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1255 return;
1256 }
1257
Roland Levillain271ab9c2014-11-27 15:23:57 +00001258 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001259 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1260 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1261 LocationSummary* locations = invoke->GetLocations();
1262 Location receiver = locations->InAt(0);
1263 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1264 // temp = object->GetClass();
1265 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1267 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001269 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001270 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001271 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001272 // temp = temp->GetMethodAt(method_offset);
1273 __ movl(temp, Address(temp, method_offset));
1274 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001275 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001276 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001278 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001279 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001280}
1281
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001282void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1283 HandleInvoke(invoke);
1284 // Add the hidden argument.
1285 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1286}
1287
1288void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1289 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001290 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001291 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1292 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1293 LocationSummary* locations = invoke->GetLocations();
1294 Location receiver = locations->InAt(0);
1295 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1296
1297 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001298 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001299 Immediate(invoke->GetDexMethodIndex()));
1300
1301 // temp = object->GetClass();
1302 if (receiver.IsStackSlot()) {
1303 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1304 __ movl(temp, Address(temp, class_offset));
1305 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001308 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001309 // temp = temp->GetImtEntryAt(method_offset);
1310 __ movl(temp, Address(temp, method_offset));
1311 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001312 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001313 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001314
1315 DCHECK(!codegen_->IsLeafMethod());
1316 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1317}
1318
Roland Levillain88cb1752014-10-20 16:36:47 +01001319void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1320 LocationSummary* locations =
1321 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1322 switch (neg->GetResultType()) {
1323 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001324 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001325 locations->SetInAt(0, Location::RequiresRegister());
1326 locations->SetOut(Location::SameAsFirstInput());
1327 break;
1328
Roland Levillain88cb1752014-10-20 16:36:47 +01001329 case Primitive::kPrimFloat:
1330 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001331 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001332 locations->SetOut(Location::SameAsFirstInput());
1333 locations->AddTemp(Location::RequiresRegister());
1334 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001335 break;
1336
1337 default:
1338 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1339 }
1340}
1341
1342void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1343 LocationSummary* locations = neg->GetLocations();
1344 Location out = locations->Out();
1345 Location in = locations->InAt(0);
1346 switch (neg->GetResultType()) {
1347 case Primitive::kPrimInt:
1348 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001349 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001350 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001351 break;
1352
1353 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001354 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001355 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001356 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001357 break;
1358
Roland Levillain5368c212014-11-27 15:03:41 +00001359 case Primitive::kPrimFloat: {
1360 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001361 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1362 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001363 // Implement float negation with an exclusive or with value
1364 // 0x80000000 (mask for bit 31, representing the sign of a
1365 // single-precision floating-point number).
1366 __ movq(constant, Immediate(INT64_C(0x80000000)));
1367 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001368 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001369 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001370 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001371
Roland Levillain5368c212014-11-27 15:03:41 +00001372 case Primitive::kPrimDouble: {
1373 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001374 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1375 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001376 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001377 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001378 // a double-precision floating-point number).
1379 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1380 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001381 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001382 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001383 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001384
1385 default:
1386 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1387 }
1388}
1389
Roland Levillaindff1f282014-11-05 14:15:05 +00001390void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1391 LocationSummary* locations =
1392 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1393 Primitive::Type result_type = conversion->GetResultType();
1394 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001395 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001396
David Brazdilb2bd1c52015-03-25 11:17:37 +00001397 // The Java language does not allow treating boolean as an integral type but
1398 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001399
Roland Levillaindff1f282014-11-05 14:15:05 +00001400 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001401 case Primitive::kPrimByte:
1402 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001403 case Primitive::kPrimBoolean:
1404 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001405 case Primitive::kPrimShort:
1406 case Primitive::kPrimInt:
1407 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001408 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001409 locations->SetInAt(0, Location::Any());
1410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1411 break;
1412
1413 default:
1414 LOG(FATAL) << "Unexpected type conversion from " << input_type
1415 << " to " << result_type;
1416 }
1417 break;
1418
Roland Levillain01a8d712014-11-14 16:27:39 +00001419 case Primitive::kPrimShort:
1420 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001421 case Primitive::kPrimBoolean:
1422 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001423 case Primitive::kPrimByte:
1424 case Primitive::kPrimInt:
1425 case Primitive::kPrimChar:
1426 // Processing a Dex `int-to-short' instruction.
1427 locations->SetInAt(0, Location::Any());
1428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1429 break;
1430
1431 default:
1432 LOG(FATAL) << "Unexpected type conversion from " << input_type
1433 << " to " << result_type;
1434 }
1435 break;
1436
Roland Levillain946e1432014-11-11 17:35:19 +00001437 case Primitive::kPrimInt:
1438 switch (input_type) {
1439 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001440 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001441 locations->SetInAt(0, Location::Any());
1442 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1443 break;
1444
1445 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001446 // Processing a Dex `float-to-int' instruction.
1447 locations->SetInAt(0, Location::RequiresFpuRegister());
1448 locations->SetOut(Location::RequiresRegister());
1449 locations->AddTemp(Location::RequiresFpuRegister());
1450 break;
1451
Roland Levillain946e1432014-11-11 17:35:19 +00001452 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001453 // Processing a Dex `double-to-int' instruction.
1454 locations->SetInAt(0, Location::RequiresFpuRegister());
1455 locations->SetOut(Location::RequiresRegister());
1456 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001457 break;
1458
1459 default:
1460 LOG(FATAL) << "Unexpected type conversion from " << input_type
1461 << " to " << result_type;
1462 }
1463 break;
1464
Roland Levillaindff1f282014-11-05 14:15:05 +00001465 case Primitive::kPrimLong:
1466 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001467 case Primitive::kPrimBoolean:
1468 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001469 case Primitive::kPrimByte:
1470 case Primitive::kPrimShort:
1471 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001472 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001473 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001474 // TODO: We would benefit from a (to-be-implemented)
1475 // Location::RegisterOrStackSlot requirement for this input.
1476 locations->SetInAt(0, Location::RequiresRegister());
1477 locations->SetOut(Location::RequiresRegister());
1478 break;
1479
1480 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001481 // Processing a Dex `float-to-long' instruction.
1482 locations->SetInAt(0, Location::RequiresFpuRegister());
1483 locations->SetOut(Location::RequiresRegister());
1484 locations->AddTemp(Location::RequiresFpuRegister());
1485 break;
1486
Roland Levillaindff1f282014-11-05 14:15:05 +00001487 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001488 // Processing a Dex `double-to-long' instruction.
1489 locations->SetInAt(0, Location::RequiresFpuRegister());
1490 locations->SetOut(Location::RequiresRegister());
1491 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001492 break;
1493
1494 default:
1495 LOG(FATAL) << "Unexpected type conversion from " << input_type
1496 << " to " << result_type;
1497 }
1498 break;
1499
Roland Levillain981e4542014-11-14 11:47:14 +00001500 case Primitive::kPrimChar:
1501 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001502 case Primitive::kPrimBoolean:
1503 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001504 case Primitive::kPrimByte:
1505 case Primitive::kPrimShort:
1506 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001507 // Processing a Dex `int-to-char' instruction.
1508 locations->SetInAt(0, Location::Any());
1509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1510 break;
1511
1512 default:
1513 LOG(FATAL) << "Unexpected type conversion from " << input_type
1514 << " to " << result_type;
1515 }
1516 break;
1517
Roland Levillaindff1f282014-11-05 14:15:05 +00001518 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001519 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001520 case Primitive::kPrimBoolean:
1521 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001522 case Primitive::kPrimByte:
1523 case Primitive::kPrimShort:
1524 case Primitive::kPrimInt:
1525 case Primitive::kPrimChar:
1526 // Processing a Dex `int-to-float' instruction.
1527 locations->SetInAt(0, Location::RequiresRegister());
1528 locations->SetOut(Location::RequiresFpuRegister());
1529 break;
1530
1531 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001532 // Processing a Dex `long-to-float' instruction.
1533 locations->SetInAt(0, Location::RequiresRegister());
1534 locations->SetOut(Location::RequiresFpuRegister());
1535 break;
1536
Roland Levillaincff13742014-11-17 14:32:17 +00001537 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001538 // Processing a Dex `double-to-float' instruction.
1539 locations->SetInAt(0, Location::RequiresFpuRegister());
1540 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001541 break;
1542
1543 default:
1544 LOG(FATAL) << "Unexpected type conversion from " << input_type
1545 << " to " << result_type;
1546 };
1547 break;
1548
Roland Levillaindff1f282014-11-05 14:15:05 +00001549 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001550 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001551 case Primitive::kPrimBoolean:
1552 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001553 case Primitive::kPrimByte:
1554 case Primitive::kPrimShort:
1555 case Primitive::kPrimInt:
1556 case Primitive::kPrimChar:
1557 // Processing a Dex `int-to-double' instruction.
1558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetOut(Location::RequiresFpuRegister());
1560 break;
1561
1562 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001563 // Processing a Dex `long-to-double' instruction.
1564 locations->SetInAt(0, Location::RequiresRegister());
1565 locations->SetOut(Location::RequiresFpuRegister());
1566 break;
1567
Roland Levillaincff13742014-11-17 14:32:17 +00001568 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001569 // Processing a Dex `float-to-double' instruction.
1570 locations->SetInAt(0, Location::RequiresFpuRegister());
1571 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001572 break;
1573
1574 default:
1575 LOG(FATAL) << "Unexpected type conversion from " << input_type
1576 << " to " << result_type;
1577 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001578 break;
1579
1580 default:
1581 LOG(FATAL) << "Unexpected type conversion from " << input_type
1582 << " to " << result_type;
1583 }
1584}
1585
1586void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1587 LocationSummary* locations = conversion->GetLocations();
1588 Location out = locations->Out();
1589 Location in = locations->InAt(0);
1590 Primitive::Type result_type = conversion->GetResultType();
1591 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001592 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001593 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001594 case Primitive::kPrimByte:
1595 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001596 case Primitive::kPrimBoolean:
1597 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001598 case Primitive::kPrimShort:
1599 case Primitive::kPrimInt:
1600 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001601 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001602 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001603 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001604 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001606 Address(CpuRegister(RSP), in.GetStackIndex()));
1607 } else {
1608 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001609 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001610 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1611 }
1612 break;
1613
1614 default:
1615 LOG(FATAL) << "Unexpected type conversion from " << input_type
1616 << " to " << result_type;
1617 }
1618 break;
1619
Roland Levillain01a8d712014-11-14 16:27:39 +00001620 case Primitive::kPrimShort:
1621 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001622 case Primitive::kPrimBoolean:
1623 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001624 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());
David Brazdil46e2a392015-03-16 17:31:52 +00001722 case Primitive::kPrimBoolean:
1723 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001724 case Primitive::kPrimByte:
1725 case Primitive::kPrimShort:
1726 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001727 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001728 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001729 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001730 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001731 break;
1732
Roland Levillain624279f2014-12-04 11:54:28 +00001733 case Primitive::kPrimFloat: {
1734 // Processing a Dex `float-to-long' instruction.
1735 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1736 CpuRegister output = out.AsRegister<CpuRegister>();
1737 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1738 Label done, nan;
1739
1740 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001741 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001742 __ cvtsi2ss(temp, output, true);
1743 // if input >= temp goto done
1744 __ comiss(input, temp);
1745 __ j(kAboveEqual, &done);
1746 // if input == NaN goto nan
1747 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001748 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001749 __ cvttss2si(output, input, true);
1750 __ jmp(&done);
1751 __ Bind(&nan);
1752 // output = 0
1753 __ xorq(output, output);
1754 __ Bind(&done);
1755 break;
1756 }
1757
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001758 case Primitive::kPrimDouble: {
1759 // Processing a Dex `double-to-long' instruction.
1760 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1761 CpuRegister output = out.AsRegister<CpuRegister>();
1762 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1763 Label done, nan;
1764
1765 __ movq(output, Immediate(kPrimLongMax));
1766 // temp = long-to-double(output)
1767 __ cvtsi2sd(temp, output, true);
1768 // if input >= temp goto done
1769 __ comisd(input, temp);
1770 __ j(kAboveEqual, &done);
1771 // if input == NaN goto nan
1772 __ j(kUnordered, &nan);
1773 // output = double-to-long-truncate(input)
1774 __ cvttsd2si(output, input, true);
1775 __ jmp(&done);
1776 __ Bind(&nan);
1777 // output = 0
1778 __ xorq(output, output);
1779 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001780 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001781 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001782
1783 default:
1784 LOG(FATAL) << "Unexpected type conversion from " << input_type
1785 << " to " << result_type;
1786 }
1787 break;
1788
Roland Levillain981e4542014-11-14 11:47:14 +00001789 case Primitive::kPrimChar:
1790 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001791 case Primitive::kPrimBoolean:
1792 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001793 case Primitive::kPrimByte:
1794 case Primitive::kPrimShort:
1795 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001796 // Processing a Dex `int-to-char' instruction.
1797 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001799 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001801 Address(CpuRegister(RSP), in.GetStackIndex()));
1802 } else {
1803 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001804 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001805 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1806 }
1807 break;
1808
1809 default:
1810 LOG(FATAL) << "Unexpected type conversion from " << input_type
1811 << " to " << result_type;
1812 }
1813 break;
1814
Roland Levillaindff1f282014-11-05 14:15:05 +00001815 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001817 case Primitive::kPrimBoolean:
1818 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001819 case Primitive::kPrimByte:
1820 case Primitive::kPrimShort:
1821 case Primitive::kPrimInt:
1822 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001823 // Processing a Dex `int-to-float' instruction.
1824 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001825 break;
1826
1827 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001828 // Processing a Dex `long-to-float' instruction.
1829 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1830 break;
1831
Roland Levillaincff13742014-11-17 14:32:17 +00001832 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001833 // Processing a Dex `double-to-float' instruction.
1834 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001835 break;
1836
1837 default:
1838 LOG(FATAL) << "Unexpected type conversion from " << input_type
1839 << " to " << result_type;
1840 };
1841 break;
1842
Roland Levillaindff1f282014-11-05 14:15:05 +00001843 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001844 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001845 case Primitive::kPrimBoolean:
1846 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001847 case Primitive::kPrimByte:
1848 case Primitive::kPrimShort:
1849 case Primitive::kPrimInt:
1850 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001851 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001852 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001853 break;
1854
1855 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001856 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001857 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001858 break;
1859
Roland Levillaincff13742014-11-17 14:32:17 +00001860 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001861 // Processing a Dex `float-to-double' instruction.
1862 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001863 break;
1864
1865 default:
1866 LOG(FATAL) << "Unexpected type conversion from " << input_type
1867 << " to " << result_type;
1868 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001869 break;
1870
1871 default:
1872 LOG(FATAL) << "Unexpected type conversion from " << input_type
1873 << " to " << result_type;
1874 }
1875}
1876
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001877void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001878 LocationSummary* locations =
1879 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001880 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001881 case Primitive::kPrimInt: {
1882 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001883 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1884 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001885 break;
1886 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001888 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001889 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001890 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001891 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893 break;
1894 }
1895
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001896 case Primitive::kPrimDouble:
1897 case Primitive::kPrimFloat: {
1898 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001899 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001900 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001901 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001902 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001903
1904 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001905 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001906 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001907}
1908
1909void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1910 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001911 Location first = locations->InAt(0);
1912 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001913 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001914
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001915 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001916 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001917 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001918 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1919 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1920 } else {
1921 __ leal(out.AsRegister<CpuRegister>(), Address(
1922 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1923 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001924 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001925 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1926 __ addl(out.AsRegister<CpuRegister>(),
1927 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
1928 } else {
1929 __ leal(out.AsRegister<CpuRegister>(), Address(
1930 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
1931 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001932 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001933 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001935 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001936 break;
1937 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001939 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05001940 if (second.IsRegister()) {
1941 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1942 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1943 } else {
1944 __ leaq(out.AsRegister<CpuRegister>(), Address(
1945 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1946 }
1947 } else {
1948 DCHECK(second.IsConstant());
1949 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1950 int32_t int32_value = Low32Bits(value);
1951 DCHECK_EQ(int32_value, value);
1952 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1953 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
1954 } else {
1955 __ leaq(out.AsRegister<CpuRegister>(), Address(
1956 first.AsRegister<CpuRegister>(), int32_value));
1957 }
1958 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001959 break;
1960 }
1961
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001964 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001965 }
1966
1967 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001968 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 break;
1970 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001971
1972 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001973 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001974 }
1975}
1976
1977void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001980 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001981 case Primitive::kPrimInt: {
1982 locations->SetInAt(0, Location::RequiresRegister());
1983 locations->SetInAt(1, Location::Any());
1984 locations->SetOut(Location::SameAsFirstInput());
1985 break;
1986 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001987 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001988 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001989 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001990 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001991 break;
1992 }
Calin Juravle11351682014-10-23 15:38:15 +01001993 case Primitive::kPrimFloat:
1994 case Primitive::kPrimDouble: {
1995 locations->SetInAt(0, Location::RequiresFpuRegister());
1996 locations->SetInAt(1, Location::RequiresFpuRegister());
1997 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001998 break;
Calin Juravle11351682014-10-23 15:38:15 +01001999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002000 default:
Calin Juravle11351682014-10-23 15:38:15 +01002001 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002002 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002003}
2004
2005void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2006 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002007 Location first = locations->InAt(0);
2008 Location second = locations->InAt(1);
2009 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002010 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002011 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002012 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002013 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002014 } else if (second.IsConstant()) {
2015 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002016 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002017 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002018 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002019 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002020 break;
2021 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002022 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002023 if (second.IsConstant()) {
2024 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2025 DCHECK(IsInt<32>(value));
2026 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2027 } else {
2028 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2029 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030 break;
2031 }
2032
Calin Juravle11351682014-10-23 15:38:15 +01002033 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002034 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002035 break;
Calin Juravle11351682014-10-23 15:38:15 +01002036 }
2037
2038 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002040 break;
2041 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002042
2043 default:
Calin Juravle11351682014-10-23 15:38:15 +01002044 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002045 }
2046}
2047
Calin Juravle34bacdf2014-10-07 20:23:36 +01002048void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2049 LocationSummary* locations =
2050 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2051 switch (mul->GetResultType()) {
2052 case Primitive::kPrimInt: {
2053 locations->SetInAt(0, Location::RequiresRegister());
2054 locations->SetInAt(1, Location::Any());
2055 locations->SetOut(Location::SameAsFirstInput());
2056 break;
2057 }
2058 case Primitive::kPrimLong: {
2059 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002060 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2061 if (locations->InAt(1).IsConstant()) {
2062 // Can use 3 operand multiply.
2063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2064 } else {
2065 locations->SetOut(Location::SameAsFirstInput());
2066 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002067 break;
2068 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002069 case Primitive::kPrimFloat:
2070 case Primitive::kPrimDouble: {
2071 locations->SetInAt(0, Location::RequiresFpuRegister());
2072 locations->SetInAt(1, Location::RequiresFpuRegister());
2073 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002074 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002075 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002076
2077 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002078 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002079 }
2080}
2081
2082void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2083 LocationSummary* locations = mul->GetLocations();
2084 Location first = locations->InAt(0);
2085 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002086 switch (mul->GetResultType()) {
2087 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002088 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002089 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002090 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002091 } else if (second.IsConstant()) {
2092 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002094 } else {
2095 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002096 __ imull(first.AsRegister<CpuRegister>(),
2097 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002098 }
2099 break;
2100 }
2101 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002102 if (second.IsConstant()) {
2103 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2104 DCHECK(IsInt<32>(value));
2105 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2106 first.AsRegister<CpuRegister>(),
2107 Immediate(static_cast<int32_t>(value)));
2108 } else {
2109 DCHECK(first.Equals(locations->Out()));
2110 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2111 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002112 break;
2113 }
2114
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002116 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002117 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002118 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002119 }
2120
2121 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002122 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002124 break;
2125 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002126
2127 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002128 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002129 }
2130}
2131
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002132void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2133 uint32_t stack_adjustment, bool is_float) {
2134 if (source.IsStackSlot()) {
2135 DCHECK(is_float);
2136 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2137 } else if (source.IsDoubleStackSlot()) {
2138 DCHECK(!is_float);
2139 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2140 } else {
2141 // Write the value to the temporary location on the stack and load to FP stack.
2142 if (is_float) {
2143 Location stack_temp = Location::StackSlot(temp_offset);
2144 codegen_->Move(stack_temp, source);
2145 __ flds(Address(CpuRegister(RSP), temp_offset));
2146 } else {
2147 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2148 codegen_->Move(stack_temp, source);
2149 __ fldl(Address(CpuRegister(RSP), temp_offset));
2150 }
2151 }
2152}
2153
2154void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2155 Primitive::Type type = rem->GetResultType();
2156 bool is_float = type == Primitive::kPrimFloat;
2157 size_t elem_size = Primitive::ComponentSize(type);
2158 LocationSummary* locations = rem->GetLocations();
2159 Location first = locations->InAt(0);
2160 Location second = locations->InAt(1);
2161 Location out = locations->Out();
2162
2163 // Create stack space for 2 elements.
2164 // TODO: enhance register allocator to ask for stack temporaries.
2165 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2166
2167 // Load the values to the FP stack in reverse order, using temporaries if needed.
2168 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2169 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2170
2171 // Loop doing FPREM until we stabilize.
2172 Label retry;
2173 __ Bind(&retry);
2174 __ fprem();
2175
2176 // Move FP status to AX.
2177 __ fstsw();
2178
2179 // And see if the argument reduction is complete. This is signaled by the
2180 // C2 FPU flag bit set to 0.
2181 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2182 __ j(kNotEqual, &retry);
2183
2184 // We have settled on the final value. Retrieve it into an XMM register.
2185 // Store FP top of stack to real stack.
2186 if (is_float) {
2187 __ fsts(Address(CpuRegister(RSP), 0));
2188 } else {
2189 __ fstl(Address(CpuRegister(RSP), 0));
2190 }
2191
2192 // Pop the 2 items from the FP stack.
2193 __ fucompp();
2194
2195 // Load the value from the stack into an XMM register.
2196 DCHECK(out.IsFpuRegister()) << out;
2197 if (is_float) {
2198 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2199 } else {
2200 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2201 }
2202
2203 // And remove the temporary stack space we allocated.
2204 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2205}
2206
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002207void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2208 DCHECK(instruction->IsDiv() || instruction->IsRem());
2209
2210 LocationSummary* locations = instruction->GetLocations();
2211 Location second = locations->InAt(1);
2212 DCHECK(second.IsConstant());
2213
2214 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2215 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002216 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002217
2218 DCHECK(imm == 1 || imm == -1);
2219
2220 switch (instruction->GetResultType()) {
2221 case Primitive::kPrimInt: {
2222 if (instruction->IsRem()) {
2223 __ xorl(output_register, output_register);
2224 } else {
2225 __ movl(output_register, input_register);
2226 if (imm == -1) {
2227 __ negl(output_register);
2228 }
2229 }
2230 break;
2231 }
2232
2233 case Primitive::kPrimLong: {
2234 if (instruction->IsRem()) {
2235 __ xorq(output_register, output_register);
2236 } else {
2237 __ movq(output_register, input_register);
2238 if (imm == -1) {
2239 __ negq(output_register);
2240 }
2241 }
2242 break;
2243 }
2244
2245 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002246 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002247 }
2248}
2249
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002250void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002251 LocationSummary* locations = instruction->GetLocations();
2252 Location second = locations->InAt(1);
2253
2254 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2255 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2256
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002257 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002258
2259 DCHECK(IsPowerOfTwo(std::abs(imm)));
2260
2261 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2262
2263 if (instruction->GetResultType() == Primitive::kPrimInt) {
2264 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2265 __ testl(numerator, numerator);
2266 __ cmov(kGreaterEqual, tmp, numerator);
2267 int shift = CTZ(imm);
2268 __ sarl(tmp, Immediate(shift));
2269
2270 if (imm < 0) {
2271 __ negl(tmp);
2272 }
2273
2274 __ movl(output_register, tmp);
2275 } else {
2276 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2277 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2278
2279 __ movq(rdx, Immediate(std::abs(imm) - 1));
2280 __ addq(rdx, numerator);
2281 __ testq(numerator, numerator);
2282 __ cmov(kGreaterEqual, rdx, numerator);
2283 int shift = CTZ(imm);
2284 __ sarq(rdx, Immediate(shift));
2285
2286 if (imm < 0) {
2287 __ negq(rdx);
2288 }
2289
2290 __ movq(output_register, rdx);
2291 }
2292}
2293
2294void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2295 DCHECK(instruction->IsDiv() || instruction->IsRem());
2296
2297 LocationSummary* locations = instruction->GetLocations();
2298 Location second = locations->InAt(1);
2299
2300 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2301 : locations->GetTemp(0).AsRegister<CpuRegister>();
2302 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2303 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2304 : locations->Out().AsRegister<CpuRegister>();
2305 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2306
2307 DCHECK_EQ(RAX, eax.AsRegister());
2308 DCHECK_EQ(RDX, edx.AsRegister());
2309 if (instruction->IsDiv()) {
2310 DCHECK_EQ(RAX, out.AsRegister());
2311 } else {
2312 DCHECK_EQ(RDX, out.AsRegister());
2313 }
2314
2315 int64_t magic;
2316 int shift;
2317
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002318 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002319 if (instruction->GetResultType() == Primitive::kPrimInt) {
2320 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2321
2322 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2323
2324 __ movl(numerator, eax);
2325
2326 Label no_div;
2327 Label end;
2328 __ testl(eax, eax);
2329 __ j(kNotEqual, &no_div);
2330
2331 __ xorl(out, out);
2332 __ jmp(&end);
2333
2334 __ Bind(&no_div);
2335
2336 __ movl(eax, Immediate(magic));
2337 __ imull(numerator);
2338
2339 if (imm > 0 && magic < 0) {
2340 __ addl(edx, numerator);
2341 } else if (imm < 0 && magic > 0) {
2342 __ subl(edx, numerator);
2343 }
2344
2345 if (shift != 0) {
2346 __ sarl(edx, Immediate(shift));
2347 }
2348
2349 __ movl(eax, edx);
2350 __ shrl(edx, Immediate(31));
2351 __ addl(edx, eax);
2352
2353 if (instruction->IsRem()) {
2354 __ movl(eax, numerator);
2355 __ imull(edx, Immediate(imm));
2356 __ subl(eax, edx);
2357 __ movl(edx, eax);
2358 } else {
2359 __ movl(eax, edx);
2360 }
2361 __ Bind(&end);
2362 } else {
2363 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2364
2365 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2366
2367 CpuRegister rax = eax;
2368 CpuRegister rdx = edx;
2369
2370 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2371
2372 // Save the numerator.
2373 __ movq(numerator, rax);
2374
2375 // RAX = magic
2376 __ movq(rax, Immediate(magic));
2377
2378 // RDX:RAX = magic * numerator
2379 __ imulq(numerator);
2380
2381 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002382 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002383 __ addq(rdx, numerator);
2384 } else if (imm < 0 && magic > 0) {
2385 // RDX -= numerator
2386 __ subq(rdx, numerator);
2387 }
2388
2389 // Shift if needed.
2390 if (shift != 0) {
2391 __ sarq(rdx, Immediate(shift));
2392 }
2393
2394 // RDX += 1 if RDX < 0
2395 __ movq(rax, rdx);
2396 __ shrq(rdx, Immediate(63));
2397 __ addq(rdx, rax);
2398
2399 if (instruction->IsRem()) {
2400 __ movq(rax, numerator);
2401
2402 if (IsInt<32>(imm)) {
2403 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2404 } else {
2405 __ movq(numerator, Immediate(imm));
2406 __ imulq(rdx, numerator);
2407 }
2408
2409 __ subq(rax, rdx);
2410 __ movq(rdx, rax);
2411 } else {
2412 __ movq(rax, rdx);
2413 }
2414 }
2415}
2416
Calin Juravlebacfec32014-11-14 15:54:36 +00002417void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2418 DCHECK(instruction->IsDiv() || instruction->IsRem());
2419 Primitive::Type type = instruction->GetResultType();
2420 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2421
2422 bool is_div = instruction->IsDiv();
2423 LocationSummary* locations = instruction->GetLocations();
2424
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002425 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2426 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002427
Roland Levillain271ab9c2014-11-27 15:23:57 +00002428 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002429 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002430
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002431 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002432 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002433
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002434 if (imm == 0) {
2435 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2436 } else if (imm == 1 || imm == -1) {
2437 DivRemOneOrMinusOne(instruction);
2438 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002439 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002440 } else {
2441 DCHECK(imm <= -2 || imm >= 2);
2442 GenerateDivRemWithAnyConstant(instruction);
2443 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002444 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002445 SlowPathCodeX86_64* slow_path =
2446 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2447 out.AsRegister(), type, is_div);
2448 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002449
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002450 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2451 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2452 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2453 // so it's safe to just use negl instead of more complex comparisons.
2454 if (type == Primitive::kPrimInt) {
2455 __ cmpl(second_reg, Immediate(-1));
2456 __ j(kEqual, slow_path->GetEntryLabel());
2457 // edx:eax <- sign-extended of eax
2458 __ cdq();
2459 // eax = quotient, edx = remainder
2460 __ idivl(second_reg);
2461 } else {
2462 __ cmpq(second_reg, Immediate(-1));
2463 __ j(kEqual, slow_path->GetEntryLabel());
2464 // rdx:rax <- sign-extended of rax
2465 __ cqo();
2466 // rax = quotient, rdx = remainder
2467 __ idivq(second_reg);
2468 }
2469 __ Bind(slow_path->GetExitLabel());
2470 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002471}
2472
Calin Juravle7c4954d2014-10-28 16:57:40 +00002473void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2474 LocationSummary* locations =
2475 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2476 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002477 case Primitive::kPrimInt:
2478 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002479 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002480 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002481 locations->SetOut(Location::SameAsFirstInput());
2482 // Intel uses edx:eax as the dividend.
2483 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002484 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2485 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2486 // output and request another temp.
2487 if (div->InputAt(1)->IsConstant()) {
2488 locations->AddTemp(Location::RequiresRegister());
2489 }
Calin Juravled0d48522014-11-04 16:40:20 +00002490 break;
2491 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002492
Calin Juravle7c4954d2014-10-28 16:57:40 +00002493 case Primitive::kPrimFloat:
2494 case Primitive::kPrimDouble: {
2495 locations->SetInAt(0, Location::RequiresFpuRegister());
2496 locations->SetInAt(1, Location::RequiresFpuRegister());
2497 locations->SetOut(Location::SameAsFirstInput());
2498 break;
2499 }
2500
2501 default:
2502 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2503 }
2504}
2505
2506void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2507 LocationSummary* locations = div->GetLocations();
2508 Location first = locations->InAt(0);
2509 Location second = locations->InAt(1);
2510 DCHECK(first.Equals(locations->Out()));
2511
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002512 Primitive::Type type = div->GetResultType();
2513 switch (type) {
2514 case Primitive::kPrimInt:
2515 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002516 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002517 break;
2518 }
2519
Calin Juravle7c4954d2014-10-28 16:57:40 +00002520 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002522 break;
2523 }
2524
2525 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002526 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002527 break;
2528 }
2529
2530 default:
2531 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2532 }
2533}
2534
Calin Juravlebacfec32014-11-14 15:54:36 +00002535void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002536 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002537 LocationSummary* locations =
2538 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002539
2540 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002541 case Primitive::kPrimInt:
2542 case Primitive::kPrimLong: {
2543 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002544 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002545 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2546 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002547 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2548 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2549 // output and request another temp.
2550 if (rem->InputAt(1)->IsConstant()) {
2551 locations->AddTemp(Location::RequiresRegister());
2552 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002553 break;
2554 }
2555
2556 case Primitive::kPrimFloat:
2557 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002558 locations->SetInAt(0, Location::Any());
2559 locations->SetInAt(1, Location::Any());
2560 locations->SetOut(Location::RequiresFpuRegister());
2561 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002562 break;
2563 }
2564
2565 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002566 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002567 }
2568}
2569
2570void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2571 Primitive::Type type = rem->GetResultType();
2572 switch (type) {
2573 case Primitive::kPrimInt:
2574 case Primitive::kPrimLong: {
2575 GenerateDivRemIntegral(rem);
2576 break;
2577 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002578 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002579 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002580 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002581 break;
2582 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002583 default:
2584 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2585 }
2586}
2587
Calin Juravled0d48522014-11-04 16:40:20 +00002588void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2591 locations->SetInAt(0, Location::Any());
2592 if (instruction->HasUses()) {
2593 locations->SetOut(Location::SameAsFirstInput());
2594 }
2595}
2596
2597void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2598 SlowPathCodeX86_64* slow_path =
2599 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2600 codegen_->AddSlowPath(slow_path);
2601
2602 LocationSummary* locations = instruction->GetLocations();
2603 Location value = locations->InAt(0);
2604
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002605 switch (instruction->GetType()) {
2606 case Primitive::kPrimInt: {
2607 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002608 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002609 __ j(kEqual, slow_path->GetEntryLabel());
2610 } else if (value.IsStackSlot()) {
2611 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2612 __ j(kEqual, slow_path->GetEntryLabel());
2613 } else {
2614 DCHECK(value.IsConstant()) << value;
2615 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2616 __ jmp(slow_path->GetEntryLabel());
2617 }
2618 }
2619 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002620 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002621 case Primitive::kPrimLong: {
2622 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002623 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002624 __ j(kEqual, slow_path->GetEntryLabel());
2625 } else if (value.IsDoubleStackSlot()) {
2626 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2627 __ j(kEqual, slow_path->GetEntryLabel());
2628 } else {
2629 DCHECK(value.IsConstant()) << value;
2630 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2631 __ jmp(slow_path->GetEntryLabel());
2632 }
2633 }
2634 break;
2635 }
2636 default:
2637 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002638 }
Calin Juravled0d48522014-11-04 16:40:20 +00002639}
2640
Calin Juravle9aec02f2014-11-18 23:06:35 +00002641void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2642 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2643
2644 LocationSummary* locations =
2645 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2646
2647 switch (op->GetResultType()) {
2648 case Primitive::kPrimInt:
2649 case Primitive::kPrimLong: {
2650 locations->SetInAt(0, Location::RequiresRegister());
2651 // The shift count needs to be in CL.
2652 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2653 locations->SetOut(Location::SameAsFirstInput());
2654 break;
2655 }
2656 default:
2657 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2658 }
2659}
2660
2661void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2662 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2663
2664 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002665 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002666 Location second = locations->InAt(1);
2667
2668 switch (op->GetResultType()) {
2669 case Primitive::kPrimInt: {
2670 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002671 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002672 if (op->IsShl()) {
2673 __ shll(first_reg, second_reg);
2674 } else if (op->IsShr()) {
2675 __ sarl(first_reg, second_reg);
2676 } else {
2677 __ shrl(first_reg, second_reg);
2678 }
2679 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002680 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002681 if (op->IsShl()) {
2682 __ shll(first_reg, imm);
2683 } else if (op->IsShr()) {
2684 __ sarl(first_reg, imm);
2685 } else {
2686 __ shrl(first_reg, imm);
2687 }
2688 }
2689 break;
2690 }
2691 case Primitive::kPrimLong: {
2692 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002693 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002694 if (op->IsShl()) {
2695 __ shlq(first_reg, second_reg);
2696 } else if (op->IsShr()) {
2697 __ sarq(first_reg, second_reg);
2698 } else {
2699 __ shrq(first_reg, second_reg);
2700 }
2701 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002702 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002703 if (op->IsShl()) {
2704 __ shlq(first_reg, imm);
2705 } else if (op->IsShr()) {
2706 __ sarq(first_reg, imm);
2707 } else {
2708 __ shrq(first_reg, imm);
2709 }
2710 }
2711 break;
2712 }
2713 default:
2714 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2715 }
2716}
2717
2718void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2719 HandleShift(shl);
2720}
2721
2722void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2723 HandleShift(shl);
2724}
2725
2726void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2727 HandleShift(shr);
2728}
2729
2730void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2731 HandleShift(shr);
2732}
2733
2734void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2735 HandleShift(ushr);
2736}
2737
2738void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2739 HandleShift(ushr);
2740}
2741
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002742void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002745 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002746 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2747 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2748 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002749}
2750
2751void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2752 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002753 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002754 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2755
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002756 __ gs()->call(
2757 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002758
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002759 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002760 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002761}
2762
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002763void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2764 LocationSummary* locations =
2765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2766 InvokeRuntimeCallingConvention calling_convention;
2767 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002768 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002769 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002770 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002771}
2772
2773void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2774 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002775 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002776 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2777
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002778 __ gs()->call(
2779 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002780
2781 DCHECK(!codegen_->IsLeafMethod());
2782 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2783}
2784
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002785void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002786 LocationSummary* locations =
2787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002788 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2789 if (location.IsStackSlot()) {
2790 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2791 } else if (location.IsDoubleStackSlot()) {
2792 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2793 }
2794 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002795}
2796
2797void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2798 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002799 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002800}
2801
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002802void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002803 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002804 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002805 locations->SetInAt(0, Location::RequiresRegister());
2806 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002807}
2808
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002809void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2810 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2812 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002813 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002814 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002815 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002817 break;
2818
2819 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002820 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002821 break;
2822
2823 default:
2824 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2825 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002826}
2827
2828void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002829 LocationSummary* locations =
2830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002831 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2832 locations->SetInAt(i, Location::Any());
2833 }
2834 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002835}
2836
2837void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002838 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002839 LOG(FATAL) << "Unimplemented";
2840}
2841
Calin Juravle52c48962014-12-16 17:02:57 +00002842void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2843 /*
2844 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2845 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2846 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2847 */
2848 switch (kind) {
2849 case MemBarrierKind::kAnyAny: {
2850 __ mfence();
2851 break;
2852 }
2853 case MemBarrierKind::kAnyStore:
2854 case MemBarrierKind::kLoadAny:
2855 case MemBarrierKind::kStoreStore: {
2856 // nop
2857 break;
2858 }
2859 default:
2860 LOG(FATAL) << "Unexpected memory barier " << kind;
2861 }
2862}
2863
2864void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2865 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2866
Nicolas Geoffray39468442014-09-02 15:17:15 +01002867 LocationSummary* locations =
2868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002869 locations->SetInAt(0, Location::RequiresRegister());
2870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2871}
2872
2873void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2874 const FieldInfo& field_info) {
2875 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2876
2877 LocationSummary* locations = instruction->GetLocations();
2878 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2879 Location out = locations->Out();
2880 bool is_volatile = field_info.IsVolatile();
2881 Primitive::Type field_type = field_info.GetFieldType();
2882 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2883
2884 switch (field_type) {
2885 case Primitive::kPrimBoolean: {
2886 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2887 break;
2888 }
2889
2890 case Primitive::kPrimByte: {
2891 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2892 break;
2893 }
2894
2895 case Primitive::kPrimShort: {
2896 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2897 break;
2898 }
2899
2900 case Primitive::kPrimChar: {
2901 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2902 break;
2903 }
2904
2905 case Primitive::kPrimInt:
2906 case Primitive::kPrimNot: {
2907 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2908 break;
2909 }
2910
2911 case Primitive::kPrimLong: {
2912 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2913 break;
2914 }
2915
2916 case Primitive::kPrimFloat: {
2917 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2918 break;
2919 }
2920
2921 case Primitive::kPrimDouble: {
2922 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2923 break;
2924 }
2925
2926 case Primitive::kPrimVoid:
2927 LOG(FATAL) << "Unreachable type " << field_type;
2928 UNREACHABLE();
2929 }
2930
Calin Juravle77520bc2015-01-12 18:45:46 +00002931 codegen_->MaybeRecordImplicitNullCheck(instruction);
2932
Calin Juravle52c48962014-12-16 17:02:57 +00002933 if (is_volatile) {
2934 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2935 }
2936}
2937
2938void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2939 const FieldInfo& field_info) {
2940 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2941
2942 LocationSummary* locations =
2943 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002944 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002945 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2946
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002949 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002950 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002951 locations->AddTemp(Location::RequiresRegister());
2952 locations->AddTemp(Location::RequiresRegister());
2953 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002954}
2955
Calin Juravle52c48962014-12-16 17:02:57 +00002956void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2957 const FieldInfo& field_info) {
2958 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002960 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002961 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2962 Location value = locations->InAt(1);
2963 bool is_volatile = field_info.IsVolatile();
2964 Primitive::Type field_type = field_info.GetFieldType();
2965 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2966
2967 if (is_volatile) {
2968 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2969 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002970
2971 switch (field_type) {
2972 case Primitive::kPrimBoolean:
2973 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002974 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002975 break;
2976 }
2977
2978 case Primitive::kPrimShort:
2979 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002980 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002981 break;
2982 }
2983
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002984 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002985 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002986 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002987 break;
2988 }
2989
2990 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002991 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002992 break;
2993 }
2994
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002995 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002996 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002997 break;
2998 }
2999
3000 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003001 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003002 break;
3003 }
3004
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003005 case Primitive::kPrimVoid:
3006 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003007 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003008 }
Calin Juravle52c48962014-12-16 17:02:57 +00003009
Calin Juravle77520bc2015-01-12 18:45:46 +00003010 codegen_->MaybeRecordImplicitNullCheck(instruction);
3011
3012 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3013 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3014 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3015 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3016 }
3017
Calin Juravle52c48962014-12-16 17:02:57 +00003018 if (is_volatile) {
3019 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3020 }
3021}
3022
3023void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3024 HandleFieldSet(instruction, instruction->GetFieldInfo());
3025}
3026
3027void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3028 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003029}
3030
3031void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003032 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003033}
3034
3035void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003036 HandleFieldGet(instruction, instruction->GetFieldInfo());
3037}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003038
Calin Juravle52c48962014-12-16 17:02:57 +00003039void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3040 HandleFieldGet(instruction);
3041}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003042
Calin Juravle52c48962014-12-16 17:02:57 +00003043void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3044 HandleFieldGet(instruction, instruction->GetFieldInfo());
3045}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003046
Calin Juravle52c48962014-12-16 17:02:57 +00003047void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3048 HandleFieldSet(instruction, instruction->GetFieldInfo());
3049}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003050
Calin Juravle52c48962014-12-16 17:02:57 +00003051void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3052 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003053}
3054
3055void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003056 LocationSummary* locations =
3057 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003058 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3059 ? Location::RequiresRegister()
3060 : Location::Any();
3061 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003062 if (instruction->HasUses()) {
3063 locations->SetOut(Location::SameAsFirstInput());
3064 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003065}
3066
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003067void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003068 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3069 return;
3070 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003071 LocationSummary* locations = instruction->GetLocations();
3072 Location obj = locations->InAt(0);
3073
3074 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3075 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3076}
3077
3078void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003079 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003080 codegen_->AddSlowPath(slow_path);
3081
3082 LocationSummary* locations = instruction->GetLocations();
3083 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003084
3085 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003086 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003087 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003088 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003089 } else {
3090 DCHECK(obj.IsConstant()) << obj;
3091 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3092 __ jmp(slow_path->GetEntryLabel());
3093 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003094 }
3095 __ j(kEqual, slow_path->GetEntryLabel());
3096}
3097
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003098void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3099 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3100 GenerateImplicitNullCheck(instruction);
3101 } else {
3102 GenerateExplicitNullCheck(instruction);
3103 }
3104}
3105
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003107 LocationSummary* locations =
3108 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003109 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003110 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003111 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113}
3114
3115void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3116 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003118 Location index = locations->InAt(1);
3119
3120 switch (instruction->GetType()) {
3121 case Primitive::kPrimBoolean: {
3122 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003124 if (index.IsConstant()) {
3125 __ movzxb(out, Address(obj,
3126 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3127 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 }
3130 break;
3131 }
3132
3133 case Primitive::kPrimByte: {
3134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003135 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003136 if (index.IsConstant()) {
3137 __ movsxb(out, Address(obj,
3138 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3139 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003140 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003141 }
3142 break;
3143 }
3144
3145 case Primitive::kPrimShort: {
3146 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003147 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003148 if (index.IsConstant()) {
3149 __ movsxw(out, Address(obj,
3150 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3151 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003152 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003153 }
3154 break;
3155 }
3156
3157 case Primitive::kPrimChar: {
3158 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003159 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003160 if (index.IsConstant()) {
3161 __ movzxw(out, Address(obj,
3162 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3163 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003164 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165 }
3166 break;
3167 }
3168
3169 case Primitive::kPrimInt:
3170 case Primitive::kPrimNot: {
3171 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3172 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003173 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003174 if (index.IsConstant()) {
3175 __ movl(out, Address(obj,
3176 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3177 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003178 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003179 }
3180 break;
3181 }
3182
3183 case Primitive::kPrimLong: {
3184 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003185 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003186 if (index.IsConstant()) {
3187 __ movq(out, Address(obj,
3188 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3189 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003190 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191 }
3192 break;
3193 }
3194
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003195 case Primitive::kPrimFloat: {
3196 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003197 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003198 if (index.IsConstant()) {
3199 __ movss(out, Address(obj,
3200 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3201 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003203 }
3204 break;
3205 }
3206
3207 case Primitive::kPrimDouble: {
3208 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003209 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003210 if (index.IsConstant()) {
3211 __ movsd(out, Address(obj,
3212 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3213 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003215 }
3216 break;
3217 }
3218
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003219 case Primitive::kPrimVoid:
3220 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003221 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003222 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003223 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003224}
3225
3226void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003227 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003228
3229 bool needs_write_barrier =
3230 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3231 bool needs_runtime_call = instruction->NeedsTypeCheck();
3232
Nicolas Geoffray39468442014-09-02 15:17:15 +01003233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003234 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3235 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003236 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003237 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3238 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3239 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003240 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003241 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003242 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003243 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3244 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003245 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003246 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003247 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3248 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003249 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003250 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003251 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003252
3253 if (needs_write_barrier) {
3254 // Temporary registers for the write barrier.
3255 locations->AddTemp(Location::RequiresRegister());
3256 locations->AddTemp(Location::RequiresRegister());
3257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003259}
3260
3261void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3262 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003263 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003265 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003266 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003267 bool needs_runtime_call = locations->WillCall();
3268 bool needs_write_barrier =
3269 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003270
3271 switch (value_type) {
3272 case Primitive::kPrimBoolean:
3273 case Primitive::kPrimByte: {
3274 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003275 if (index.IsConstant()) {
3276 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003277 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003278 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003279 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003280 __ movb(Address(obj, offset),
3281 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003282 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003284 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003285 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3286 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003287 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003288 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003289 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003291 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003293 break;
3294 }
3295
3296 case Primitive::kPrimShort:
3297 case Primitive::kPrimChar: {
3298 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003299 if (index.IsConstant()) {
3300 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003301 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003303 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003304 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003305 __ movw(Address(obj, offset),
3306 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003307 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003308 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003309 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003310 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003311 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3312 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003313 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003314 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003315 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003316 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3317 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003318 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003319 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003320 break;
3321 }
3322
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003323 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003324 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003325 if (!needs_runtime_call) {
3326 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3327 if (index.IsConstant()) {
3328 size_t offset =
3329 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3330 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003332 } else {
3333 DCHECK(value.IsConstant()) << value;
3334 __ movl(Address(obj, offset),
3335 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3336 }
3337 } else {
3338 DCHECK(index.IsRegister()) << index;
3339 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003340 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3341 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003342 } else {
3343 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003345 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3346 }
3347 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003348 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003349 if (needs_write_barrier) {
3350 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3352 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3353 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003354 }
3355 } else {
3356 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003357 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3358 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003359 DCHECK(!codegen_->IsLeafMethod());
3360 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3361 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003362 break;
3363 }
3364
3365 case Primitive::kPrimLong: {
3366 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367 if (index.IsConstant()) {
3368 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003369 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003370 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003371 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003372 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3374 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003375 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003376 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003377 break;
3378 }
3379
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003380 case Primitive::kPrimFloat: {
3381 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3382 if (index.IsConstant()) {
3383 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3384 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003385 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003386 } else {
3387 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003388 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3389 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003390 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003391 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003392 break;
3393 }
3394
3395 case Primitive::kPrimDouble: {
3396 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3397 if (index.IsConstant()) {
3398 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3399 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003400 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003401 } else {
3402 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003403 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3404 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003405 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003406 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003407 break;
3408 }
3409
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003410 case Primitive::kPrimVoid:
3411 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003412 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003413 }
3414}
3415
3416void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003417 LocationSummary* locations =
3418 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003419 locations->SetInAt(0, Location::RequiresRegister());
3420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003421}
3422
3423void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3424 LocationSummary* locations = instruction->GetLocations();
3425 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3427 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003428 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003429 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003430}
3431
3432void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003433 LocationSummary* locations =
3434 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003435 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003436 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003437 if (instruction->HasUses()) {
3438 locations->SetOut(Location::SameAsFirstInput());
3439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003440}
3441
3442void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3443 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003444 Location index_loc = locations->InAt(0);
3445 Location length_loc = locations->InAt(1);
3446 SlowPathCodeX86_64* slow_path =
3447 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003448 codegen_->AddSlowPath(slow_path);
3449
Mark Mendellf60c90b2015-03-04 15:12:59 -05003450 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3451 if (index_loc.IsConstant()) {
3452 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3453 __ cmpl(length, Immediate(value));
3454 } else {
3455 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3456 }
3457 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003458}
3459
3460void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3461 CpuRegister card,
3462 CpuRegister object,
3463 CpuRegister value) {
3464 Label is_null;
3465 __ testl(value, value);
3466 __ j(kEqual, &is_null);
3467 __ gs()->movq(card, Address::Absolute(
3468 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3469 __ movq(temp, object);
3470 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3471 __ movb(Address(temp, card, TIMES_1, 0), card);
3472 __ Bind(&is_null);
3473}
3474
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003475void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3476 temp->SetLocations(nullptr);
3477}
3478
3479void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3480 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003481 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003482}
3483
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003484void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003485 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003486 LOG(FATAL) << "Unimplemented";
3487}
3488
3489void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003490 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3491}
3492
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003493void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3495}
3496
3497void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003498 HBasicBlock* block = instruction->GetBlock();
3499 if (block->GetLoopInformation() != nullptr) {
3500 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3501 // The back edge will generate the suspend check.
3502 return;
3503 }
3504 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3505 // The goto will generate the suspend check.
3506 return;
3507 }
3508 GenerateSuspendCheck(instruction, nullptr);
3509}
3510
3511void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3512 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003513 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003514 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003515 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003516 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003517 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003518 if (successor == nullptr) {
3519 __ j(kNotEqual, slow_path->GetEntryLabel());
3520 __ Bind(slow_path->GetReturnLabel());
3521 } else {
3522 __ j(kEqual, codegen_->GetLabelOf(successor));
3523 __ jmp(slow_path->GetEntryLabel());
3524 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003525}
3526
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003527X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3528 return codegen_->GetAssembler();
3529}
3530
3531void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3532 MoveOperands* move = moves_.Get(index);
3533 Location source = move->GetSource();
3534 Location destination = move->GetDestination();
3535
3536 if (source.IsRegister()) {
3537 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003538 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003539 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003540 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003541 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003542 } else {
3543 DCHECK(destination.IsDoubleStackSlot());
3544 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003545 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003546 }
3547 } else if (source.IsStackSlot()) {
3548 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003550 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003551 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003553 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003554 } else {
3555 DCHECK(destination.IsStackSlot());
3556 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3557 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3558 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003559 } else if (source.IsDoubleStackSlot()) {
3560 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003561 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003562 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003563 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003564 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3565 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003566 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003567 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003568 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3569 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3570 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003571 } else if (source.IsConstant()) {
3572 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003573 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3574 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003575 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003576 if (value == 0) {
3577 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3578 } else {
3579 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3580 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003581 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003582 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003583 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003584 }
3585 } else if (constant->IsLongConstant()) {
3586 int64_t value = constant->AsLongConstant()->GetValue();
3587 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003588 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003589 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003590 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003591 __ movq(CpuRegister(TMP), Immediate(value));
3592 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3593 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003594 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003595 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003596 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003597 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003598 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003599 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3600 if (value == 0) {
3601 // easy FP 0.0.
3602 __ xorps(dest, dest);
3603 } else {
3604 __ movl(CpuRegister(TMP), imm);
3605 __ movd(dest, CpuRegister(TMP));
3606 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003607 } else {
3608 DCHECK(destination.IsStackSlot()) << destination;
3609 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3610 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003611 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003612 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003613 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003614 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003615 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003616 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003617 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3618 if (value == 0) {
3619 __ xorpd(dest, dest);
3620 } else {
3621 __ movq(CpuRegister(TMP), imm);
3622 __ movd(dest, CpuRegister(TMP));
3623 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003624 } else {
3625 DCHECK(destination.IsDoubleStackSlot()) << destination;
3626 __ movq(CpuRegister(TMP), imm);
3627 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3628 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003629 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003630 } else if (source.IsFpuRegister()) {
3631 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003633 } else if (destination.IsStackSlot()) {
3634 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003635 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003636 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003637 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003638 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003639 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003640 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003641 }
3642}
3643
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003644void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003645 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003646 __ movl(Address(CpuRegister(RSP), mem), reg);
3647 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003648}
3649
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003650void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003651 ScratchRegisterScope ensure_scratch(
3652 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3653
3654 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3655 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3656 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3657 Address(CpuRegister(RSP), mem2 + stack_offset));
3658 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3659 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3660 CpuRegister(ensure_scratch.GetRegister()));
3661}
3662
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003663void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3664 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3665 __ movq(Address(CpuRegister(RSP), mem), reg);
3666 __ movq(reg, CpuRegister(TMP));
3667}
3668
3669void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3670 ScratchRegisterScope ensure_scratch(
3671 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3672
3673 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3674 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3675 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3676 Address(CpuRegister(RSP), mem2 + stack_offset));
3677 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3678 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3679 CpuRegister(ensure_scratch.GetRegister()));
3680}
3681
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003682void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3683 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3684 __ movss(Address(CpuRegister(RSP), mem), reg);
3685 __ movd(reg, CpuRegister(TMP));
3686}
3687
3688void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3689 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3690 __ movsd(Address(CpuRegister(RSP), mem), reg);
3691 __ movd(reg, CpuRegister(TMP));
3692}
3693
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003694void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3695 MoveOperands* move = moves_.Get(index);
3696 Location source = move->GetSource();
3697 Location destination = move->GetDestination();
3698
3699 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003701 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003702 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003703 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003705 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3707 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003709 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003710 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003711 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3712 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003713 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003714 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3715 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3716 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003717 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003718 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003719 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003720 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003721 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003723 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003725 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003726 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003727 }
3728}
3729
3730
3731void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3732 __ pushq(CpuRegister(reg));
3733}
3734
3735
3736void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3737 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003738}
3739
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003740void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3741 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3742 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3743 Immediate(mirror::Class::kStatusInitialized));
3744 __ j(kLess, slow_path->GetEntryLabel());
3745 __ Bind(slow_path->GetExitLabel());
3746 // No need for memory fence, thanks to the X86_64 memory model.
3747}
3748
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003749void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003750 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3751 ? LocationSummary::kCallOnSlowPath
3752 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003753 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003754 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003755 locations->SetOut(Location::RequiresRegister());
3756}
3757
3758void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003759 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003760 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003761 DCHECK(!cls->CanCallRuntime());
3762 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003763 codegen_->LoadCurrentMethod(out);
3764 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3765 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003766 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003767 codegen_->LoadCurrentMethod(out);
3768 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3769 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003770 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3771 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3772 codegen_->AddSlowPath(slow_path);
3773 __ testl(out, out);
3774 __ j(kEqual, slow_path->GetEntryLabel());
3775 if (cls->MustGenerateClinitCheck()) {
3776 GenerateClassInitializationCheck(slow_path, out);
3777 } else {
3778 __ Bind(slow_path->GetExitLabel());
3779 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003780 }
3781}
3782
3783void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3784 LocationSummary* locations =
3785 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3786 locations->SetInAt(0, Location::RequiresRegister());
3787 if (check->HasUses()) {
3788 locations->SetOut(Location::SameAsFirstInput());
3789 }
3790}
3791
3792void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003793 // We assume the class to not be null.
3794 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3795 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003796 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003797 GenerateClassInitializationCheck(slow_path,
3798 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003799}
3800
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003801void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3802 LocationSummary* locations =
3803 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3804 locations->SetOut(Location::RequiresRegister());
3805}
3806
3807void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3808 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3809 codegen_->AddSlowPath(slow_path);
3810
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003812 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003813 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3814 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003815 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3816 __ testl(out, out);
3817 __ j(kEqual, slow_path->GetEntryLabel());
3818 __ Bind(slow_path->GetExitLabel());
3819}
3820
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003821void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3822 LocationSummary* locations =
3823 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3824 locations->SetOut(Location::RequiresRegister());
3825}
3826
3827void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3828 Address address = Address::Absolute(
3829 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003830 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003831 __ gs()->movl(address, Immediate(0));
3832}
3833
3834void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3835 LocationSummary* locations =
3836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3837 InvokeRuntimeCallingConvention calling_convention;
3838 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3839}
3840
3841void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3842 __ gs()->call(
3843 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3844 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3845}
3846
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003847void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003848 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3849 ? LocationSummary::kNoCall
3850 : LocationSummary::kCallOnSlowPath;
3851 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3852 locations->SetInAt(0, Location::RequiresRegister());
3853 locations->SetInAt(1, Location::Any());
3854 locations->SetOut(Location::RequiresRegister());
3855}
3856
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003857void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003858 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003859 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003860 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003861 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003862 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3863 Label done, zero;
3864 SlowPathCodeX86_64* slow_path = nullptr;
3865
3866 // Return 0 if `obj` is null.
3867 // TODO: avoid this check if we know obj is not null.
3868 __ testl(obj, obj);
3869 __ j(kEqual, &zero);
3870 // Compare the class of `obj` with `cls`.
3871 __ movl(out, Address(obj, class_offset));
3872 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003874 } else {
3875 DCHECK(cls.IsStackSlot()) << cls;
3876 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3877 }
3878 if (instruction->IsClassFinal()) {
3879 // Classes must be equal for the instanceof to succeed.
3880 __ j(kNotEqual, &zero);
3881 __ movl(out, Immediate(1));
3882 __ jmp(&done);
3883 } else {
3884 // If the classes are not equal, we go into a slow path.
3885 DCHECK(locations->OnlyCallsOnSlowPath());
3886 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003887 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003888 codegen_->AddSlowPath(slow_path);
3889 __ j(kNotEqual, slow_path->GetEntryLabel());
3890 __ movl(out, Immediate(1));
3891 __ jmp(&done);
3892 }
3893 __ Bind(&zero);
3894 __ movl(out, Immediate(0));
3895 if (slow_path != nullptr) {
3896 __ Bind(slow_path->GetExitLabel());
3897 }
3898 __ Bind(&done);
3899}
3900
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003901void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3903 instruction, LocationSummary::kCallOnSlowPath);
3904 locations->SetInAt(0, Location::RequiresRegister());
3905 locations->SetInAt(1, Location::Any());
3906 locations->AddTemp(Location::RequiresRegister());
3907}
3908
3909void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3910 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003911 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003912 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003914 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3915 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3916 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3917 codegen_->AddSlowPath(slow_path);
3918
3919 // TODO: avoid this check if we know obj is not null.
3920 __ testl(obj, obj);
3921 __ j(kEqual, slow_path->GetExitLabel());
3922 // Compare the class of `obj` with `cls`.
3923 __ movl(temp, Address(obj, class_offset));
3924 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003925 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003926 } else {
3927 DCHECK(cls.IsStackSlot()) << cls;
3928 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3929 }
3930 // Classes must be equal for the checkcast to succeed.
3931 __ j(kNotEqual, slow_path->GetEntryLabel());
3932 __ Bind(slow_path->GetExitLabel());
3933}
3934
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003935void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3936 LocationSummary* locations =
3937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3938 InvokeRuntimeCallingConvention calling_convention;
3939 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3940}
3941
3942void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3943 __ gs()->call(Address::Absolute(instruction->IsEnter()
3944 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3945 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3946 true));
3947 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3948}
3949
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003950void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3951void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3952void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3953
3954void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3955 LocationSummary* locations =
3956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3957 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3958 || instruction->GetResultType() == Primitive::kPrimLong);
3959 locations->SetInAt(0, Location::RequiresRegister());
3960 if (instruction->GetType() == Primitive::kPrimInt) {
3961 locations->SetInAt(1, Location::Any());
3962 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003963 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003964 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003965 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003966 }
3967 locations->SetOut(Location::SameAsFirstInput());
3968}
3969
3970void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3971 HandleBitwiseOperation(instruction);
3972}
3973
3974void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3975 HandleBitwiseOperation(instruction);
3976}
3977
3978void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3979 HandleBitwiseOperation(instruction);
3980}
3981
3982void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3983 LocationSummary* locations = instruction->GetLocations();
3984 Location first = locations->InAt(0);
3985 Location second = locations->InAt(1);
3986 DCHECK(first.Equals(locations->Out()));
3987
3988 if (instruction->GetResultType() == Primitive::kPrimInt) {
3989 if (second.IsRegister()) {
3990 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003991 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003992 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003993 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003994 } else {
3995 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003996 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003997 }
3998 } else if (second.IsConstant()) {
3999 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4000 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004002 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004003 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004004 } else {
4005 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004007 }
4008 } else {
4009 Address address(CpuRegister(RSP), second.GetStackIndex());
4010 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004011 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004012 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004014 } else {
4015 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004016 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004017 }
4018 }
4019 } else {
4020 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004021 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4022 bool second_is_constant = false;
4023 int64_t value = 0;
4024 if (second.IsConstant()) {
4025 second_is_constant = true;
4026 value = second.GetConstant()->AsLongConstant()->GetValue();
4027 DCHECK(IsInt<32>(value));
4028 }
4029
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004030 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004031 if (second_is_constant) {
4032 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4033 } else {
4034 __ andq(first_reg, second.AsRegister<CpuRegister>());
4035 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004036 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004037 if (second_is_constant) {
4038 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4039 } else {
4040 __ orq(first_reg, second.AsRegister<CpuRegister>());
4041 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004042 } else {
4043 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004044 if (second_is_constant) {
4045 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4046 } else {
4047 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4048 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004049 }
4050 }
4051}
4052
Calin Juravleb1498f62015-02-16 13:13:29 +00004053void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4054 // Nothing to do, this should be removed during prepare for register allocator.
4055 UNUSED(instruction);
4056 LOG(FATAL) << "Unreachable";
4057}
4058
4059void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4060 // Nothing to do, this should be removed during prepare for register allocator.
4061 UNUSED(instruction);
4062 LOG(FATAL) << "Unreachable";
4063}
4064
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004065} // namespace x86_64
4066} // namespace art