blob: 1f24046c5e28b34c9fdd8b9af8754e3682a0c827 [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>();
2216 int64_t imm;
2217 if (second.GetConstant()->IsLongConstant()) {
2218 imm = second.GetConstant()->AsLongConstant()->GetValue();
2219 } else {
2220 imm = second.GetConstant()->AsIntConstant()->GetValue();
2221 }
2222
2223 DCHECK(imm == 1 || imm == -1);
2224
2225 switch (instruction->GetResultType()) {
2226 case Primitive::kPrimInt: {
2227 if (instruction->IsRem()) {
2228 __ xorl(output_register, output_register);
2229 } else {
2230 __ movl(output_register, input_register);
2231 if (imm == -1) {
2232 __ negl(output_register);
2233 }
2234 }
2235 break;
2236 }
2237
2238 case Primitive::kPrimLong: {
2239 if (instruction->IsRem()) {
2240 __ xorq(output_register, output_register);
2241 } else {
2242 __ movq(output_register, input_register);
2243 if (imm == -1) {
2244 __ negq(output_register);
2245 }
2246 }
2247 break;
2248 }
2249
2250 default:
2251 LOG(FATAL) << "Unreachable";
2252 }
2253}
2254
2255void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HBinaryOperation* instruction) {
2256 DCHECK(instruction->IsDiv());
2257
2258 LocationSummary* locations = instruction->GetLocations();
2259 Location second = locations->InAt(1);
2260
2261 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2262 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2263
2264 int64_t imm;
2265 if (instruction->GetResultType() == Primitive::kPrimLong) {
2266 imm = second.GetConstant()->AsLongConstant()->GetValue();
2267 } else {
2268 imm = second.GetConstant()->AsIntConstant()->GetValue();
2269 }
2270
2271 DCHECK(IsPowerOfTwo(std::abs(imm)));
2272
2273 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2274
2275 if (instruction->GetResultType() == Primitive::kPrimInt) {
2276 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2277 __ testl(numerator, numerator);
2278 __ cmov(kGreaterEqual, tmp, numerator);
2279 int shift = CTZ(imm);
2280 __ sarl(tmp, Immediate(shift));
2281
2282 if (imm < 0) {
2283 __ negl(tmp);
2284 }
2285
2286 __ movl(output_register, tmp);
2287 } else {
2288 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2289 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2290
2291 __ movq(rdx, Immediate(std::abs(imm) - 1));
2292 __ addq(rdx, numerator);
2293 __ testq(numerator, numerator);
2294 __ cmov(kGreaterEqual, rdx, numerator);
2295 int shift = CTZ(imm);
2296 __ sarq(rdx, Immediate(shift));
2297
2298 if (imm < 0) {
2299 __ negq(rdx);
2300 }
2301
2302 __ movq(output_register, rdx);
2303 }
2304}
2305
2306void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2307 DCHECK(instruction->IsDiv() || instruction->IsRem());
2308
2309 LocationSummary* locations = instruction->GetLocations();
2310 Location second = locations->InAt(1);
2311
2312 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2313 : locations->GetTemp(0).AsRegister<CpuRegister>();
2314 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2315 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2316 : locations->Out().AsRegister<CpuRegister>();
2317 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2318
2319 DCHECK_EQ(RAX, eax.AsRegister());
2320 DCHECK_EQ(RDX, edx.AsRegister());
2321 if (instruction->IsDiv()) {
2322 DCHECK_EQ(RAX, out.AsRegister());
2323 } else {
2324 DCHECK_EQ(RDX, out.AsRegister());
2325 }
2326
2327 int64_t magic;
2328 int shift;
2329
2330 // TODO: can these branch be written as one?
2331 if (instruction->GetResultType() == Primitive::kPrimInt) {
2332 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2333
2334 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2335
2336 __ movl(numerator, eax);
2337
2338 Label no_div;
2339 Label end;
2340 __ testl(eax, eax);
2341 __ j(kNotEqual, &no_div);
2342
2343 __ xorl(out, out);
2344 __ jmp(&end);
2345
2346 __ Bind(&no_div);
2347
2348 __ movl(eax, Immediate(magic));
2349 __ imull(numerator);
2350
2351 if (imm > 0 && magic < 0) {
2352 __ addl(edx, numerator);
2353 } else if (imm < 0 && magic > 0) {
2354 __ subl(edx, numerator);
2355 }
2356
2357 if (shift != 0) {
2358 __ sarl(edx, Immediate(shift));
2359 }
2360
2361 __ movl(eax, edx);
2362 __ shrl(edx, Immediate(31));
2363 __ addl(edx, eax);
2364
2365 if (instruction->IsRem()) {
2366 __ movl(eax, numerator);
2367 __ imull(edx, Immediate(imm));
2368 __ subl(eax, edx);
2369 __ movl(edx, eax);
2370 } else {
2371 __ movl(eax, edx);
2372 }
2373 __ Bind(&end);
2374 } else {
2375 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2376
2377 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2378
2379 CpuRegister rax = eax;
2380 CpuRegister rdx = edx;
2381
2382 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2383
2384 // Save the numerator.
2385 __ movq(numerator, rax);
2386
2387 // RAX = magic
2388 __ movq(rax, Immediate(magic));
2389
2390 // RDX:RAX = magic * numerator
2391 __ imulq(numerator);
2392
2393 if (imm > 0 && magic < 0) {
2394 // RDX += numeratorerator
2395 __ addq(rdx, numerator);
2396 } else if (imm < 0 && magic > 0) {
2397 // RDX -= numerator
2398 __ subq(rdx, numerator);
2399 }
2400
2401 // Shift if needed.
2402 if (shift != 0) {
2403 __ sarq(rdx, Immediate(shift));
2404 }
2405
2406 // RDX += 1 if RDX < 0
2407 __ movq(rax, rdx);
2408 __ shrq(rdx, Immediate(63));
2409 __ addq(rdx, rax);
2410
2411 if (instruction->IsRem()) {
2412 __ movq(rax, numerator);
2413
2414 if (IsInt<32>(imm)) {
2415 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2416 } else {
2417 __ movq(numerator, Immediate(imm));
2418 __ imulq(rdx, numerator);
2419 }
2420
2421 __ subq(rax, rdx);
2422 __ movq(rdx, rax);
2423 } else {
2424 __ movq(rax, rdx);
2425 }
2426 }
2427}
2428
Calin Juravlebacfec32014-11-14 15:54:36 +00002429void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2430 DCHECK(instruction->IsDiv() || instruction->IsRem());
2431 Primitive::Type type = instruction->GetResultType();
2432 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2433
2434 bool is_div = instruction->IsDiv();
2435 LocationSummary* locations = instruction->GetLocations();
2436
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002437 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2438 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002439
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002441 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002442
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002443 if (second.IsConstant()) {
2444 int64_t imm;
2445 if (second.GetConstant()->AsLongConstant()) {
2446 imm = second.GetConstant()->AsLongConstant()->GetValue();
2447 } else {
2448 imm = second.GetConstant()->AsIntConstant()->GetValue();
2449 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002450
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002451 if (imm == 0) {
2452 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2453 } else if (imm == 1 || imm == -1) {
2454 DivRemOneOrMinusOne(instruction);
2455 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
2456 DivByPowerOfTwo(instruction);
2457 } else {
2458 DCHECK(imm <= -2 || imm >= 2);
2459 GenerateDivRemWithAnyConstant(instruction);
2460 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002461 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002462 SlowPathCodeX86_64* slow_path =
2463 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2464 out.AsRegister(), type, is_div);
2465 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002466
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002467 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2468 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2469 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2470 // so it's safe to just use negl instead of more complex comparisons.
2471 if (type == Primitive::kPrimInt) {
2472 __ cmpl(second_reg, Immediate(-1));
2473 __ j(kEqual, slow_path->GetEntryLabel());
2474 // edx:eax <- sign-extended of eax
2475 __ cdq();
2476 // eax = quotient, edx = remainder
2477 __ idivl(second_reg);
2478 } else {
2479 __ cmpq(second_reg, Immediate(-1));
2480 __ j(kEqual, slow_path->GetEntryLabel());
2481 // rdx:rax <- sign-extended of rax
2482 __ cqo();
2483 // rax = quotient, rdx = remainder
2484 __ idivq(second_reg);
2485 }
2486 __ Bind(slow_path->GetExitLabel());
2487 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002488}
2489
Calin Juravle7c4954d2014-10-28 16:57:40 +00002490void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2491 LocationSummary* locations =
2492 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2493 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002494 case Primitive::kPrimInt:
2495 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002496 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002497 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002498 locations->SetOut(Location::SameAsFirstInput());
2499 // Intel uses edx:eax as the dividend.
2500 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002501 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2502 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2503 // output and request another temp.
2504 if (div->InputAt(1)->IsConstant()) {
2505 locations->AddTemp(Location::RequiresRegister());
2506 }
Calin Juravled0d48522014-11-04 16:40:20 +00002507 break;
2508 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002509
Calin Juravle7c4954d2014-10-28 16:57:40 +00002510 case Primitive::kPrimFloat:
2511 case Primitive::kPrimDouble: {
2512 locations->SetInAt(0, Location::RequiresFpuRegister());
2513 locations->SetInAt(1, Location::RequiresFpuRegister());
2514 locations->SetOut(Location::SameAsFirstInput());
2515 break;
2516 }
2517
2518 default:
2519 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2520 }
2521}
2522
2523void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2524 LocationSummary* locations = div->GetLocations();
2525 Location first = locations->InAt(0);
2526 Location second = locations->InAt(1);
2527 DCHECK(first.Equals(locations->Out()));
2528
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002529 Primitive::Type type = div->GetResultType();
2530 switch (type) {
2531 case Primitive::kPrimInt:
2532 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002533 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002534 break;
2535 }
2536
Calin Juravle7c4954d2014-10-28 16:57:40 +00002537 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002538 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002539 break;
2540 }
2541
2542 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002544 break;
2545 }
2546
2547 default:
2548 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2549 }
2550}
2551
Calin Juravlebacfec32014-11-14 15:54:36 +00002552void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002553 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002554 LocationSummary* locations =
2555 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002556
2557 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002558 case Primitive::kPrimInt:
2559 case Primitive::kPrimLong: {
2560 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002561 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002562 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2563 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002564 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2565 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2566 // output and request another temp.
2567 if (rem->InputAt(1)->IsConstant()) {
2568 locations->AddTemp(Location::RequiresRegister());
2569 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002570 break;
2571 }
2572
2573 case Primitive::kPrimFloat:
2574 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002575 locations->SetInAt(0, Location::Any());
2576 locations->SetInAt(1, Location::Any());
2577 locations->SetOut(Location::RequiresFpuRegister());
2578 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002579 break;
2580 }
2581
2582 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002583 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002584 }
2585}
2586
2587void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2588 Primitive::Type type = rem->GetResultType();
2589 switch (type) {
2590 case Primitive::kPrimInt:
2591 case Primitive::kPrimLong: {
2592 GenerateDivRemIntegral(rem);
2593 break;
2594 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002595 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002596 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002597 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002598 break;
2599 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002600 default:
2601 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2602 }
2603}
2604
Calin Juravled0d48522014-11-04 16:40:20 +00002605void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2606 LocationSummary* locations =
2607 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2608 locations->SetInAt(0, Location::Any());
2609 if (instruction->HasUses()) {
2610 locations->SetOut(Location::SameAsFirstInput());
2611 }
2612}
2613
2614void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2615 SlowPathCodeX86_64* slow_path =
2616 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2617 codegen_->AddSlowPath(slow_path);
2618
2619 LocationSummary* locations = instruction->GetLocations();
2620 Location value = locations->InAt(0);
2621
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002622 switch (instruction->GetType()) {
2623 case Primitive::kPrimInt: {
2624 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002625 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002626 __ j(kEqual, slow_path->GetEntryLabel());
2627 } else if (value.IsStackSlot()) {
2628 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2629 __ j(kEqual, slow_path->GetEntryLabel());
2630 } else {
2631 DCHECK(value.IsConstant()) << value;
2632 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2633 __ jmp(slow_path->GetEntryLabel());
2634 }
2635 }
2636 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002637 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002638 case Primitive::kPrimLong: {
2639 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002641 __ j(kEqual, slow_path->GetEntryLabel());
2642 } else if (value.IsDoubleStackSlot()) {
2643 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2644 __ j(kEqual, slow_path->GetEntryLabel());
2645 } else {
2646 DCHECK(value.IsConstant()) << value;
2647 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2648 __ jmp(slow_path->GetEntryLabel());
2649 }
2650 }
2651 break;
2652 }
2653 default:
2654 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002655 }
Calin Juravled0d48522014-11-04 16:40:20 +00002656}
2657
Calin Juravle9aec02f2014-11-18 23:06:35 +00002658void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2659 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2660
2661 LocationSummary* locations =
2662 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2663
2664 switch (op->GetResultType()) {
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimLong: {
2667 locations->SetInAt(0, Location::RequiresRegister());
2668 // The shift count needs to be in CL.
2669 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2670 locations->SetOut(Location::SameAsFirstInput());
2671 break;
2672 }
2673 default:
2674 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2675 }
2676}
2677
2678void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2679 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2680
2681 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002682 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002683 Location second = locations->InAt(1);
2684
2685 switch (op->GetResultType()) {
2686 case Primitive::kPrimInt: {
2687 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002689 if (op->IsShl()) {
2690 __ shll(first_reg, second_reg);
2691 } else if (op->IsShr()) {
2692 __ sarl(first_reg, second_reg);
2693 } else {
2694 __ shrl(first_reg, second_reg);
2695 }
2696 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002697 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002698 if (op->IsShl()) {
2699 __ shll(first_reg, imm);
2700 } else if (op->IsShr()) {
2701 __ sarl(first_reg, imm);
2702 } else {
2703 __ shrl(first_reg, imm);
2704 }
2705 }
2706 break;
2707 }
2708 case Primitive::kPrimLong: {
2709 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002710 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002711 if (op->IsShl()) {
2712 __ shlq(first_reg, second_reg);
2713 } else if (op->IsShr()) {
2714 __ sarq(first_reg, second_reg);
2715 } else {
2716 __ shrq(first_reg, second_reg);
2717 }
2718 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002719 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002720 if (op->IsShl()) {
2721 __ shlq(first_reg, imm);
2722 } else if (op->IsShr()) {
2723 __ sarq(first_reg, imm);
2724 } else {
2725 __ shrq(first_reg, imm);
2726 }
2727 }
2728 break;
2729 }
2730 default:
2731 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2732 }
2733}
2734
2735void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2736 HandleShift(shl);
2737}
2738
2739void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2740 HandleShift(shl);
2741}
2742
2743void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2744 HandleShift(shr);
2745}
2746
2747void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2748 HandleShift(shr);
2749}
2750
2751void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2752 HandleShift(ushr);
2753}
2754
2755void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2756 HandleShift(ushr);
2757}
2758
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002759void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002762 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002763 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2764 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2765 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002766}
2767
2768void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2769 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002770 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002771 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2772
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002773 __ gs()->call(
2774 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002775
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002776 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002777 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002778}
2779
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002780void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2781 LocationSummary* locations =
2782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2783 InvokeRuntimeCallingConvention calling_convention;
2784 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002785 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002786 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002787 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002788}
2789
2790void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2791 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002792 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002793 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2794
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002795 __ gs()->call(
2796 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002797
2798 DCHECK(!codegen_->IsLeafMethod());
2799 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2800}
2801
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002802void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002803 LocationSummary* locations =
2804 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002805 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2806 if (location.IsStackSlot()) {
2807 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2808 } else if (location.IsDoubleStackSlot()) {
2809 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2810 }
2811 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002812}
2813
2814void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2815 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002816 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002817}
2818
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002819void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002820 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002821 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002822 locations->SetInAt(0, Location::RequiresRegister());
2823 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002824}
2825
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002826void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2827 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2829 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002830 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002831 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002832 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002833 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002834 break;
2835
2836 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002837 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002838 break;
2839
2840 default:
2841 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2842 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002843}
2844
2845void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002846 LocationSummary* locations =
2847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002848 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2849 locations->SetInAt(i, Location::Any());
2850 }
2851 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002852}
2853
2854void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002855 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002856 LOG(FATAL) << "Unimplemented";
2857}
2858
Calin Juravle52c48962014-12-16 17:02:57 +00002859void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2860 /*
2861 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2862 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2863 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2864 */
2865 switch (kind) {
2866 case MemBarrierKind::kAnyAny: {
2867 __ mfence();
2868 break;
2869 }
2870 case MemBarrierKind::kAnyStore:
2871 case MemBarrierKind::kLoadAny:
2872 case MemBarrierKind::kStoreStore: {
2873 // nop
2874 break;
2875 }
2876 default:
2877 LOG(FATAL) << "Unexpected memory barier " << kind;
2878 }
2879}
2880
2881void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2882 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2883
Nicolas Geoffray39468442014-09-02 15:17:15 +01002884 LocationSummary* locations =
2885 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002886 locations->SetInAt(0, Location::RequiresRegister());
2887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2888}
2889
2890void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2891 const FieldInfo& field_info) {
2892 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2893
2894 LocationSummary* locations = instruction->GetLocations();
2895 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2896 Location out = locations->Out();
2897 bool is_volatile = field_info.IsVolatile();
2898 Primitive::Type field_type = field_info.GetFieldType();
2899 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2900
2901 switch (field_type) {
2902 case Primitive::kPrimBoolean: {
2903 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2904 break;
2905 }
2906
2907 case Primitive::kPrimByte: {
2908 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2909 break;
2910 }
2911
2912 case Primitive::kPrimShort: {
2913 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2914 break;
2915 }
2916
2917 case Primitive::kPrimChar: {
2918 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2919 break;
2920 }
2921
2922 case Primitive::kPrimInt:
2923 case Primitive::kPrimNot: {
2924 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2925 break;
2926 }
2927
2928 case Primitive::kPrimLong: {
2929 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2930 break;
2931 }
2932
2933 case Primitive::kPrimFloat: {
2934 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2935 break;
2936 }
2937
2938 case Primitive::kPrimDouble: {
2939 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2940 break;
2941 }
2942
2943 case Primitive::kPrimVoid:
2944 LOG(FATAL) << "Unreachable type " << field_type;
2945 UNREACHABLE();
2946 }
2947
Calin Juravle77520bc2015-01-12 18:45:46 +00002948 codegen_->MaybeRecordImplicitNullCheck(instruction);
2949
Calin Juravle52c48962014-12-16 17:02:57 +00002950 if (is_volatile) {
2951 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2952 }
2953}
2954
2955void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2956 const FieldInfo& field_info) {
2957 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2958
2959 LocationSummary* locations =
2960 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002961 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002962 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2963
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002964 locations->SetInAt(0, Location::RequiresRegister());
2965 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002966 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002967 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002968 locations->AddTemp(Location::RequiresRegister());
2969 locations->AddTemp(Location::RequiresRegister());
2970 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002971}
2972
Calin Juravle52c48962014-12-16 17:02:57 +00002973void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2974 const FieldInfo& field_info) {
2975 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2976
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002977 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002978 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2979 Location value = locations->InAt(1);
2980 bool is_volatile = field_info.IsVolatile();
2981 Primitive::Type field_type = field_info.GetFieldType();
2982 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2983
2984 if (is_volatile) {
2985 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2986 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002987
2988 switch (field_type) {
2989 case Primitive::kPrimBoolean:
2990 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002991 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002992 break;
2993 }
2994
2995 case Primitive::kPrimShort:
2996 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002997 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002998 break;
2999 }
3000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003002 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003003 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003004 break;
3005 }
3006
3007 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003008 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003009 break;
3010 }
3011
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003012 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003013 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003014 break;
3015 }
3016
3017 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003018 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003019 break;
3020 }
3021
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003022 case Primitive::kPrimVoid:
3023 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003024 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003025 }
Calin Juravle52c48962014-12-16 17:02:57 +00003026
Calin Juravle77520bc2015-01-12 18:45:46 +00003027 codegen_->MaybeRecordImplicitNullCheck(instruction);
3028
3029 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3030 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3031 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3032 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3033 }
3034
Calin Juravle52c48962014-12-16 17:02:57 +00003035 if (is_volatile) {
3036 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3037 }
3038}
3039
3040void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3041 HandleFieldSet(instruction, instruction->GetFieldInfo());
3042}
3043
3044void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3045 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003046}
3047
3048void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003049 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003050}
3051
3052void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003053 HandleFieldGet(instruction, instruction->GetFieldInfo());
3054}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003055
Calin Juravle52c48962014-12-16 17:02:57 +00003056void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3057 HandleFieldGet(instruction);
3058}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003059
Calin Juravle52c48962014-12-16 17:02:57 +00003060void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3061 HandleFieldGet(instruction, instruction->GetFieldInfo());
3062}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003063
Calin Juravle52c48962014-12-16 17:02:57 +00003064void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3065 HandleFieldSet(instruction, instruction->GetFieldInfo());
3066}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003067
Calin Juravle52c48962014-12-16 17:02:57 +00003068void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3069 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003070}
3071
3072void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003073 LocationSummary* locations =
3074 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003075 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3076 ? Location::RequiresRegister()
3077 : Location::Any();
3078 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003079 if (instruction->HasUses()) {
3080 locations->SetOut(Location::SameAsFirstInput());
3081 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003082}
3083
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003084void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003085 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3086 return;
3087 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003088 LocationSummary* locations = instruction->GetLocations();
3089 Location obj = locations->InAt(0);
3090
3091 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3092 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3093}
3094
3095void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003096 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003097 codegen_->AddSlowPath(slow_path);
3098
3099 LocationSummary* locations = instruction->GetLocations();
3100 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003101
3102 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003103 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003104 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003105 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003106 } else {
3107 DCHECK(obj.IsConstant()) << obj;
3108 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3109 __ jmp(slow_path->GetEntryLabel());
3110 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003111 }
3112 __ j(kEqual, slow_path->GetEntryLabel());
3113}
3114
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003115void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3116 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3117 GenerateImplicitNullCheck(instruction);
3118 } else {
3119 GenerateExplicitNullCheck(instruction);
3120 }
3121}
3122
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003123void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003124 LocationSummary* locations =
3125 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003126 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003127 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003128 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3129 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130}
3131
3132void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3133 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003134 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003135 Location index = locations->InAt(1);
3136
3137 switch (instruction->GetType()) {
3138 case Primitive::kPrimBoolean: {
3139 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003140 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003141 if (index.IsConstant()) {
3142 __ movzxb(out, Address(obj,
3143 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3144 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003145 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 }
3147 break;
3148 }
3149
3150 case Primitive::kPrimByte: {
3151 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003152 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003153 if (index.IsConstant()) {
3154 __ movsxb(out, Address(obj,
3155 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3156 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003158 }
3159 break;
3160 }
3161
3162 case Primitive::kPrimShort: {
3163 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003164 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165 if (index.IsConstant()) {
3166 __ movsxw(out, Address(obj,
3167 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3168 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003169 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003170 }
3171 break;
3172 }
3173
3174 case Primitive::kPrimChar: {
3175 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003176 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003177 if (index.IsConstant()) {
3178 __ movzxw(out, Address(obj,
3179 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3180 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003181 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003182 }
3183 break;
3184 }
3185
3186 case Primitive::kPrimInt:
3187 case Primitive::kPrimNot: {
3188 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3189 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003190 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191 if (index.IsConstant()) {
3192 __ movl(out, Address(obj,
3193 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3194 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003195 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003196 }
3197 break;
3198 }
3199
3200 case Primitive::kPrimLong: {
3201 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003203 if (index.IsConstant()) {
3204 __ movq(out, Address(obj,
3205 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3206 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003208 }
3209 break;
3210 }
3211
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003212 case Primitive::kPrimFloat: {
3213 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003215 if (index.IsConstant()) {
3216 __ movss(out, Address(obj,
3217 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3218 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003220 }
3221 break;
3222 }
3223
3224 case Primitive::kPrimDouble: {
3225 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003227 if (index.IsConstant()) {
3228 __ movsd(out, Address(obj,
3229 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3230 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003231 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003232 }
3233 break;
3234 }
3235
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003236 case Primitive::kPrimVoid:
3237 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003238 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003240 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003241}
3242
3243void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003244 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003245
3246 bool needs_write_barrier =
3247 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3248 bool needs_runtime_call = instruction->NeedsTypeCheck();
3249
Nicolas Geoffray39468442014-09-02 15:17:15 +01003250 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003251 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3252 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003253 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003254 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3255 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3256 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003257 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003258 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003259 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003260 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3261 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003262 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003263 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003264 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3265 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003266 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003267 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003268 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003269
3270 if (needs_write_barrier) {
3271 // Temporary registers for the write barrier.
3272 locations->AddTemp(Location::RequiresRegister());
3273 locations->AddTemp(Location::RequiresRegister());
3274 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003275 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003276}
3277
3278void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3279 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003280 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003281 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003282 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003283 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003284 bool needs_runtime_call = locations->WillCall();
3285 bool needs_write_barrier =
3286 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003287
3288 switch (value_type) {
3289 case Primitive::kPrimBoolean:
3290 case Primitive::kPrimByte: {
3291 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003292 if (index.IsConstant()) {
3293 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003294 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003296 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003297 __ movb(Address(obj, offset),
3298 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003299 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003300 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003301 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3303 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003304 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003305 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003306 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3307 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003308 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003309 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003310 break;
3311 }
3312
3313 case Primitive::kPrimShort:
3314 case Primitive::kPrimChar: {
3315 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003316 if (index.IsConstant()) {
3317 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003318 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003320 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003321 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003322 __ movw(Address(obj, offset),
3323 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003325 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003326 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003327 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003328 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3329 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003330 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003331 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003332 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003333 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3334 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003335 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003336 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 break;
3338 }
3339
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003340 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003341 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003342 if (!needs_runtime_call) {
3343 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3344 if (index.IsConstant()) {
3345 size_t offset =
3346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3347 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003348 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003349 } else {
3350 DCHECK(value.IsConstant()) << value;
3351 __ movl(Address(obj, offset),
3352 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3353 }
3354 } else {
3355 DCHECK(index.IsRegister()) << index;
3356 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003357 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3358 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003359 } else {
3360 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003361 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003362 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3363 }
3364 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003365 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003366 if (needs_write_barrier) {
3367 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003368 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3369 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3370 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003371 }
3372 } else {
3373 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003374 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3375 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003376 DCHECK(!codegen_->IsLeafMethod());
3377 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3378 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003379 break;
3380 }
3381
3382 case Primitive::kPrimLong: {
3383 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003384 if (index.IsConstant()) {
3385 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003386 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003387 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003388 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003389 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003390 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3391 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003392 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003393 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003394 break;
3395 }
3396
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003397 case Primitive::kPrimFloat: {
3398 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3399 if (index.IsConstant()) {
3400 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3401 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003402 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003403 } else {
3404 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003405 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3406 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003407 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003408 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003409 break;
3410 }
3411
3412 case Primitive::kPrimDouble: {
3413 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3414 if (index.IsConstant()) {
3415 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3416 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003417 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003418 } else {
3419 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003420 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3421 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003422 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003423 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003424 break;
3425 }
3426
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003427 case Primitive::kPrimVoid:
3428 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003429 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003430 }
3431}
3432
3433void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003434 LocationSummary* locations =
3435 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003436 locations->SetInAt(0, Location::RequiresRegister());
3437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003438}
3439
3440void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3441 LocationSummary* locations = instruction->GetLocations();
3442 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003443 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3444 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003446 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003447}
3448
3449void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003450 LocationSummary* locations =
3451 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003452 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003454 if (instruction->HasUses()) {
3455 locations->SetOut(Location::SameAsFirstInput());
3456 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003457}
3458
3459void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3460 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003461 Location index_loc = locations->InAt(0);
3462 Location length_loc = locations->InAt(1);
3463 SlowPathCodeX86_64* slow_path =
3464 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003465 codegen_->AddSlowPath(slow_path);
3466
Mark Mendellf60c90b2015-03-04 15:12:59 -05003467 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3468 if (index_loc.IsConstant()) {
3469 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3470 __ cmpl(length, Immediate(value));
3471 } else {
3472 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3473 }
3474 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003475}
3476
3477void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3478 CpuRegister card,
3479 CpuRegister object,
3480 CpuRegister value) {
3481 Label is_null;
3482 __ testl(value, value);
3483 __ j(kEqual, &is_null);
3484 __ gs()->movq(card, Address::Absolute(
3485 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3486 __ movq(temp, object);
3487 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3488 __ movb(Address(temp, card, TIMES_1, 0), card);
3489 __ Bind(&is_null);
3490}
3491
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003492void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3493 temp->SetLocations(nullptr);
3494}
3495
3496void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3497 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003498 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003499}
3500
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003501void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003502 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003503 LOG(FATAL) << "Unimplemented";
3504}
3505
3506void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003507 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3508}
3509
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003510void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3512}
3513
3514void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003515 HBasicBlock* block = instruction->GetBlock();
3516 if (block->GetLoopInformation() != nullptr) {
3517 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3518 // The back edge will generate the suspend check.
3519 return;
3520 }
3521 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3522 // The goto will generate the suspend check.
3523 return;
3524 }
3525 GenerateSuspendCheck(instruction, nullptr);
3526}
3527
3528void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3529 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003530 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003531 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003532 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003533 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003534 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003535 if (successor == nullptr) {
3536 __ j(kNotEqual, slow_path->GetEntryLabel());
3537 __ Bind(slow_path->GetReturnLabel());
3538 } else {
3539 __ j(kEqual, codegen_->GetLabelOf(successor));
3540 __ jmp(slow_path->GetEntryLabel());
3541 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003542}
3543
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003544X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3545 return codegen_->GetAssembler();
3546}
3547
3548void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3549 MoveOperands* move = moves_.Get(index);
3550 Location source = move->GetSource();
3551 Location destination = move->GetDestination();
3552
3553 if (source.IsRegister()) {
3554 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003555 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003556 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003557 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003558 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003559 } else {
3560 DCHECK(destination.IsDoubleStackSlot());
3561 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003562 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003563 }
3564 } else if (source.IsStackSlot()) {
3565 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003567 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003568 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003569 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003570 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003571 } else {
3572 DCHECK(destination.IsStackSlot());
3573 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3574 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3575 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003576 } else if (source.IsDoubleStackSlot()) {
3577 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003579 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003580 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003581 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3582 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003583 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003584 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003585 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3586 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3587 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003588 } else if (source.IsConstant()) {
3589 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003590 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3591 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003592 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003593 if (value == 0) {
3594 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3595 } else {
3596 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3597 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003598 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003599 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003600 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003601 }
3602 } else if (constant->IsLongConstant()) {
3603 int64_t value = constant->AsLongConstant()->GetValue();
3604 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003606 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003607 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003608 __ movq(CpuRegister(TMP), Immediate(value));
3609 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3610 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003611 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003612 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003613 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003614 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003615 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003616 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3617 if (value == 0) {
3618 // easy FP 0.0.
3619 __ xorps(dest, dest);
3620 } else {
3621 __ movl(CpuRegister(TMP), imm);
3622 __ movd(dest, CpuRegister(TMP));
3623 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003624 } else {
3625 DCHECK(destination.IsStackSlot()) << destination;
3626 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3627 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003628 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003629 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003630 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003631 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003632 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003633 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003634 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3635 if (value == 0) {
3636 __ xorpd(dest, dest);
3637 } else {
3638 __ movq(CpuRegister(TMP), imm);
3639 __ movd(dest, CpuRegister(TMP));
3640 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003641 } else {
3642 DCHECK(destination.IsDoubleStackSlot()) << destination;
3643 __ movq(CpuRegister(TMP), imm);
3644 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3645 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003646 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003647 } else if (source.IsFpuRegister()) {
3648 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003650 } else if (destination.IsStackSlot()) {
3651 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003653 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003654 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003655 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003657 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003658 }
3659}
3660
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003661void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003662 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003663 __ movl(Address(CpuRegister(RSP), mem), reg);
3664 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003665}
3666
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003667void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003668 ScratchRegisterScope ensure_scratch(
3669 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3670
3671 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3672 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3673 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3674 Address(CpuRegister(RSP), mem2 + stack_offset));
3675 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3676 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3677 CpuRegister(ensure_scratch.GetRegister()));
3678}
3679
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003680void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3681 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3682 __ movq(Address(CpuRegister(RSP), mem), reg);
3683 __ movq(reg, CpuRegister(TMP));
3684}
3685
3686void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3687 ScratchRegisterScope ensure_scratch(
3688 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3689
3690 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3691 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3692 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3693 Address(CpuRegister(RSP), mem2 + stack_offset));
3694 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3695 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3696 CpuRegister(ensure_scratch.GetRegister()));
3697}
3698
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003699void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3700 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3701 __ movss(Address(CpuRegister(RSP), mem), reg);
3702 __ movd(reg, CpuRegister(TMP));
3703}
3704
3705void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3706 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3707 __ movsd(Address(CpuRegister(RSP), mem), reg);
3708 __ movd(reg, CpuRegister(TMP));
3709}
3710
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003711void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3712 MoveOperands* move = moves_.Get(index);
3713 Location source = move->GetSource();
3714 Location destination = move->GetDestination();
3715
3716 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003717 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003718 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003719 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003720 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003721 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003722 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003723 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3724 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003725 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003726 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003727 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003728 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3729 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003730 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003731 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3732 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3733 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003734 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003735 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003736 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003737 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003738 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003739 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003740 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003742 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003743 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003744 }
3745}
3746
3747
3748void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3749 __ pushq(CpuRegister(reg));
3750}
3751
3752
3753void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3754 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003755}
3756
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003757void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3758 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3759 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3760 Immediate(mirror::Class::kStatusInitialized));
3761 __ j(kLess, slow_path->GetEntryLabel());
3762 __ Bind(slow_path->GetExitLabel());
3763 // No need for memory fence, thanks to the X86_64 memory model.
3764}
3765
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003766void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003767 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3768 ? LocationSummary::kCallOnSlowPath
3769 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003770 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003771 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003772 locations->SetOut(Location::RequiresRegister());
3773}
3774
3775void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003777 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003778 DCHECK(!cls->CanCallRuntime());
3779 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003780 codegen_->LoadCurrentMethod(out);
3781 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3782 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003783 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003784 codegen_->LoadCurrentMethod(out);
3785 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3786 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003787 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3788 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3789 codegen_->AddSlowPath(slow_path);
3790 __ testl(out, out);
3791 __ j(kEqual, slow_path->GetEntryLabel());
3792 if (cls->MustGenerateClinitCheck()) {
3793 GenerateClassInitializationCheck(slow_path, out);
3794 } else {
3795 __ Bind(slow_path->GetExitLabel());
3796 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003797 }
3798}
3799
3800void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3801 LocationSummary* locations =
3802 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3803 locations->SetInAt(0, Location::RequiresRegister());
3804 if (check->HasUses()) {
3805 locations->SetOut(Location::SameAsFirstInput());
3806 }
3807}
3808
3809void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003810 // We assume the class to not be null.
3811 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3812 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003813 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003814 GenerateClassInitializationCheck(slow_path,
3815 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003816}
3817
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003818void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3819 LocationSummary* locations =
3820 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3821 locations->SetOut(Location::RequiresRegister());
3822}
3823
3824void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3825 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3826 codegen_->AddSlowPath(slow_path);
3827
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003829 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003830 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3831 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003832 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3833 __ testl(out, out);
3834 __ j(kEqual, slow_path->GetEntryLabel());
3835 __ Bind(slow_path->GetExitLabel());
3836}
3837
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003838void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3839 LocationSummary* locations =
3840 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3841 locations->SetOut(Location::RequiresRegister());
3842}
3843
3844void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3845 Address address = Address::Absolute(
3846 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003847 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003848 __ gs()->movl(address, Immediate(0));
3849}
3850
3851void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3852 LocationSummary* locations =
3853 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3854 InvokeRuntimeCallingConvention calling_convention;
3855 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3856}
3857
3858void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3859 __ gs()->call(
3860 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3861 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3862}
3863
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003864void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003865 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3866 ? LocationSummary::kNoCall
3867 : LocationSummary::kCallOnSlowPath;
3868 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3869 locations->SetInAt(0, Location::RequiresRegister());
3870 locations->SetInAt(1, Location::Any());
3871 locations->SetOut(Location::RequiresRegister());
3872}
3873
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003874void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003875 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003876 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003877 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003878 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003879 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3880 Label done, zero;
3881 SlowPathCodeX86_64* slow_path = nullptr;
3882
3883 // Return 0 if `obj` is null.
3884 // TODO: avoid this check if we know obj is not null.
3885 __ testl(obj, obj);
3886 __ j(kEqual, &zero);
3887 // Compare the class of `obj` with `cls`.
3888 __ movl(out, Address(obj, class_offset));
3889 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003890 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003891 } else {
3892 DCHECK(cls.IsStackSlot()) << cls;
3893 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3894 }
3895 if (instruction->IsClassFinal()) {
3896 // Classes must be equal for the instanceof to succeed.
3897 __ j(kNotEqual, &zero);
3898 __ movl(out, Immediate(1));
3899 __ jmp(&done);
3900 } else {
3901 // If the classes are not equal, we go into a slow path.
3902 DCHECK(locations->OnlyCallsOnSlowPath());
3903 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003904 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003905 codegen_->AddSlowPath(slow_path);
3906 __ j(kNotEqual, slow_path->GetEntryLabel());
3907 __ movl(out, Immediate(1));
3908 __ jmp(&done);
3909 }
3910 __ Bind(&zero);
3911 __ movl(out, Immediate(0));
3912 if (slow_path != nullptr) {
3913 __ Bind(slow_path->GetExitLabel());
3914 }
3915 __ Bind(&done);
3916}
3917
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003918void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3920 instruction, LocationSummary::kCallOnSlowPath);
3921 locations->SetInAt(0, Location::RequiresRegister());
3922 locations->SetInAt(1, Location::Any());
3923 locations->AddTemp(Location::RequiresRegister());
3924}
3925
3926void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3927 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003928 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003929 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003931 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3932 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3933 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3934 codegen_->AddSlowPath(slow_path);
3935
3936 // TODO: avoid this check if we know obj is not null.
3937 __ testl(obj, obj);
3938 __ j(kEqual, slow_path->GetExitLabel());
3939 // Compare the class of `obj` with `cls`.
3940 __ movl(temp, Address(obj, class_offset));
3941 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003942 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003943 } else {
3944 DCHECK(cls.IsStackSlot()) << cls;
3945 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3946 }
3947 // Classes must be equal for the checkcast to succeed.
3948 __ j(kNotEqual, slow_path->GetEntryLabel());
3949 __ Bind(slow_path->GetExitLabel());
3950}
3951
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003952void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3953 LocationSummary* locations =
3954 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3955 InvokeRuntimeCallingConvention calling_convention;
3956 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3957}
3958
3959void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3960 __ gs()->call(Address::Absolute(instruction->IsEnter()
3961 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3962 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3963 true));
3964 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3965}
3966
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003967void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3968void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3969void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3970
3971void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3972 LocationSummary* locations =
3973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3974 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3975 || instruction->GetResultType() == Primitive::kPrimLong);
3976 locations->SetInAt(0, Location::RequiresRegister());
3977 if (instruction->GetType() == Primitive::kPrimInt) {
3978 locations->SetInAt(1, Location::Any());
3979 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003980 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003981 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003982 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003983 }
3984 locations->SetOut(Location::SameAsFirstInput());
3985}
3986
3987void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3988 HandleBitwiseOperation(instruction);
3989}
3990
3991void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3992 HandleBitwiseOperation(instruction);
3993}
3994
3995void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3996 HandleBitwiseOperation(instruction);
3997}
3998
3999void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4000 LocationSummary* locations = instruction->GetLocations();
4001 Location first = locations->InAt(0);
4002 Location second = locations->InAt(1);
4003 DCHECK(first.Equals(locations->Out()));
4004
4005 if (instruction->GetResultType() == Primitive::kPrimInt) {
4006 if (second.IsRegister()) {
4007 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004008 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004009 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004010 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004011 } else {
4012 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004014 }
4015 } else if (second.IsConstant()) {
4016 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4017 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004019 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004020 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004021 } else {
4022 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004023 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004024 }
4025 } else {
4026 Address address(CpuRegister(RSP), second.GetStackIndex());
4027 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004028 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004029 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004030 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004031 } else {
4032 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004033 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004034 }
4035 }
4036 } else {
4037 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004038 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4039 bool second_is_constant = false;
4040 int64_t value = 0;
4041 if (second.IsConstant()) {
4042 second_is_constant = true;
4043 value = second.GetConstant()->AsLongConstant()->GetValue();
4044 DCHECK(IsInt<32>(value));
4045 }
4046
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004047 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004048 if (second_is_constant) {
4049 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4050 } else {
4051 __ andq(first_reg, second.AsRegister<CpuRegister>());
4052 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004053 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004054 if (second_is_constant) {
4055 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4056 } else {
4057 __ orq(first_reg, second.AsRegister<CpuRegister>());
4058 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004059 } else {
4060 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004061 if (second_is_constant) {
4062 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4063 } else {
4064 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4065 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004066 }
4067 }
4068}
4069
Calin Juravleb1498f62015-02-16 13:13:29 +00004070void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4071 // Nothing to do, this should be removed during prepare for register allocator.
4072 UNUSED(instruction);
4073 LOG(FATAL) << "Unreachable";
4074}
4075
4076void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4077 // Nothing to do, this should be removed during prepare for register allocator.
4078 UNUSED(instruction);
4079 LOG(FATAL) << "Unreachable";
4080}
4081
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004082} // namespace x86_64
4083} // namespace art