blob: e996e4613d43c1c069e2d28c6ab9c330afdc8609 [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)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100164 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 __ gs()->call(Address::Absolute(
169 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000170 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 }
172
173 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100174 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175 const Location index_location_;
176 const Location length_location_;
177
178 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
179};
180
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000181class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100182 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000183 LoadClassSlowPathX86_64(HLoadClass* cls,
184 HInstruction* at,
185 uint32_t dex_pc,
186 bool do_clinit)
187 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
188 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
189 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000191 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000192 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
194 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100195
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000196 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000199 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100200 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000201 __ gs()->call(Address::Absolute((do_clinit_
202 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
203 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000204 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000206 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000208 if (out.IsValid()) {
209 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
210 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 }
212
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000213 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 __ jmp(GetExitLabel());
215 }
216
217 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000218 // The class this slow path will load.
219 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 // The instruction where this slow path is happening.
222 // (Might be the load class or an initialization check).
223 HInstruction* const at_;
224
225 // The dex PC of `at_`.
226 const uint32_t dex_pc_;
227
228 // Whether to initialize the class.
229 const bool do_clinit_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232};
233
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000234class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
235 public:
236 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
237
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000238 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000239 LocationSummary* locations = instruction_->GetLocations();
240 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
241
242 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
243 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000244 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000245
246 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800247 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
248 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000249 Immediate(instruction_->GetStringIndex()));
250 __ gs()->call(Address::Absolute(
251 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000254 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255 __ jmp(GetExitLabel());
256 }
257
258 private:
259 HLoadString* const instruction_;
260
261 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
262};
263
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
265 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000266 TypeCheckSlowPathX86_64(HInstruction* instruction,
267 Location class_to_check,
268 Location object_class,
269 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000271 class_to_check_(class_to_check),
272 object_class_(object_class),
273 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000277 DCHECK(instruction_->IsCheckCast()
278 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279
280 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
281 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000282 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 // We're moving two locations to locations that could overlap, so we need a parallel
285 // move resolver.
286 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000287 codegen->EmitParallelMoves(
288 class_to_check_,
289 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100290 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000291 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100292 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
293 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 if (instruction_->IsInstanceOf()) {
296 __ gs()->call(
297 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
298 } else {
299 DCHECK(instruction_->IsCheckCast());
300 __ gs()->call(
301 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
302 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000303 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304
305 if (instruction_->IsInstanceOf()) {
306 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
307 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310 __ jmp(GetExitLabel());
311 }
312
313 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 HInstruction* const instruction_;
315 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
319 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
320};
321
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700322class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
323 public:
324 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
325 : instruction_(instruction) {}
326
327 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
328 __ Bind(GetEntryLabel());
329 SaveLiveRegisters(codegen, instruction_->GetLocations());
330 __ gs()->call(
331 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeoptimize), true));
332 DCHECK(instruction_->IsDeoptimize());
333 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
334 uint32_t dex_pc = deoptimize->GetDexPc();
335 codegen->RecordPcInfo(instruction_, dex_pc, this);
336 }
337
338 private:
339 HInstruction* const instruction_;
340 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
341};
342
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100343#undef __
344#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
345
Dave Allison20dfc792014-06-16 20:44:29 -0700346inline Condition X86_64Condition(IfCondition cond) {
347 switch (cond) {
348 case kCondEQ: return kEqual;
349 case kCondNE: return kNotEqual;
350 case kCondLT: return kLess;
351 case kCondLE: return kLessEqual;
352 case kCondGT: return kGreater;
353 case kCondGE: return kGreaterEqual;
354 default:
355 LOG(FATAL) << "Unknown if condition";
356 }
357 return kEqual;
358}
359
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800360void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
361 CpuRegister temp) {
362 // All registers are assumed to be correctly set up.
363
364 // TODO: Implement all kinds of calls:
365 // 1) boot -> boot
366 // 2) app -> boot
367 // 3) app -> app
368 //
369 // Currently we implement the app -> app logic, which looks up in the resolve cache.
370
371 // temp = method;
372 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000373 if (!invoke->IsRecursive()) {
374 // temp = temp->dex_cache_resolved_methods_;
375 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
376 // temp = temp[index_in_cache]
377 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
378 // (temp + offset_of_quick_compiled_code)()
379 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
380 kX86_64WordSize).SizeValue()));
381 } else {
382 __ call(&frame_entry_label_);
383 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800384
385 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800386}
387
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100388void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
389 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
390}
391
392void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
393 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
394}
395
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100396size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
397 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
398 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100399}
400
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100401size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
402 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
403 return kX86_64WordSize;
404}
405
406size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
407 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
408 return kX86_64WordSize;
409}
410
411size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
412 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
413 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100414}
415
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000416static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000417// Use a fake return address register to mimic Quick.
418static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400419CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
420 const X86_64InstructionSetFeatures& isa_features,
421 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000422 : CodeGenerator(graph,
423 kNumberOfCpuRegisters,
424 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000425 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000426 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
427 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000428 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000429 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
430 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000431 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100432 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000434 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400435 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400436 isa_features_(isa_features),
437 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000438 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
439}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100441InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
442 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100443 : HGraphVisitor(graph),
444 assembler_(codegen->GetAssembler()),
445 codegen_(codegen) {}
446
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100447Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100448 switch (type) {
449 case Primitive::kPrimLong:
450 case Primitive::kPrimByte:
451 case Primitive::kPrimBoolean:
452 case Primitive::kPrimChar:
453 case Primitive::kPrimShort:
454 case Primitive::kPrimInt:
455 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100457 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100458 }
459
460 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100461 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100462 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100463 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100464 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100465
466 case Primitive::kPrimVoid:
467 LOG(FATAL) << "Unreachable type " << type;
468 }
469
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100470 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100471}
472
Nicolas Geoffray98893962015-01-21 12:32:32 +0000473void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100474 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100475 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100476
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000477 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100478 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000479
Nicolas Geoffray98893962015-01-21 12:32:32 +0000480 if (is_baseline) {
481 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
482 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
483 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000484 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
485 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
486 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000487 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100488}
489
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100490static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100491 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100492}
David Srbecky9d8606d2015-04-12 09:35:32 +0100493
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100495 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100496}
497
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100498void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100499 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000500 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100501 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700502 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000503 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100504
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000505 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100506 __ testq(CpuRegister(RAX), Address(
507 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100508 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100509 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000510
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000511 if (HasEmptyFrame()) {
512 return;
513 }
514
Nicolas Geoffray98893962015-01-21 12:32:32 +0000515 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000516 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000517 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000518 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100519 __ cfi().AdjustCFAOffset(kX86_64WordSize);
520 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000521 }
522 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100523
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100524 int adjust = GetFrameSize() - GetCoreSpillSize();
525 __ subq(CpuRegister(RSP), Immediate(adjust));
526 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000527 uint32_t xmm_spill_location = GetFpuSpillStart();
528 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100529
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000530 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
531 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100532 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
533 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
534 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000535 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100536 }
537
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100538 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
539}
540
541void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100542 __ cfi().RememberState();
543 if (!HasEmptyFrame()) {
544 uint32_t xmm_spill_location = GetFpuSpillStart();
545 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
546 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
547 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
548 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
549 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
550 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
551 }
552 }
553
554 int adjust = GetFrameSize() - GetCoreSpillSize();
555 __ addq(CpuRegister(RSP), Immediate(adjust));
556 __ cfi().AdjustCFAOffset(-adjust);
557
558 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
559 Register reg = kCoreCalleeSaves[i];
560 if (allocated_registers_.ContainsCoreRegister(reg)) {
561 __ popq(CpuRegister(reg));
562 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
563 __ cfi().Restore(DWARFReg(reg));
564 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000565 }
566 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100567 __ ret();
568 __ cfi().RestoreState();
569 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100570}
571
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100572void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
573 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574}
575
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100576void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000577 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100578 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
579}
580
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100581Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
582 switch (load->GetType()) {
583 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100585 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586
587 case Primitive::kPrimInt:
588 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100589 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591
592 case Primitive::kPrimBoolean:
593 case Primitive::kPrimByte:
594 case Primitive::kPrimChar:
595 case Primitive::kPrimShort:
596 case Primitive::kPrimVoid:
597 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700598 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100599 }
600
601 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700602 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100603}
604
605void CodeGeneratorX86_64::Move(Location destination, Location source) {
606 if (source.Equals(destination)) {
607 return;
608 }
609 if (destination.IsRegister()) {
610 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000611 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100612 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000613 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100614 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000615 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100616 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100617 } else {
618 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 Address(CpuRegister(RSP), source.GetStackIndex()));
621 }
622 } else if (destination.IsFpuRegister()) {
623 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 Address(CpuRegister(RSP), source.GetStackIndex()));
630 } else {
631 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000632 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100633 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100634 }
635 } else if (destination.IsStackSlot()) {
636 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else if (source.IsFpuRegister()) {
640 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000641 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500642 } else if (source.IsConstant()) {
643 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000644 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500645 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100646 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500647 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000648 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
649 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100650 }
651 } else {
652 DCHECK(destination.IsDoubleStackSlot());
653 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100654 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000655 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100656 } else if (source.IsFpuRegister()) {
657 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000658 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500659 } else if (source.IsConstant()) {
660 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800661 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500662 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000663 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500664 } else {
665 DCHECK(constant->IsLongConstant());
666 value = constant->AsLongConstant()->GetValue();
667 }
668 __ movq(CpuRegister(TMP), Immediate(value));
669 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100670 } else {
671 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000672 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
673 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100674 }
675 }
676}
677
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100678void CodeGeneratorX86_64::Move(HInstruction* instruction,
679 Location location,
680 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000681 LocationSummary* locations = instruction->GetLocations();
682 if (locations != nullptr && locations->Out().Equals(location)) {
683 return;
684 }
685
686 if (locations != nullptr && locations->Out().IsConstant()) {
687 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000688 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
689 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000690 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000691 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000692 } else if (location.IsStackSlot()) {
693 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
694 } else {
695 DCHECK(location.IsConstant());
696 DCHECK_EQ(location.GetConstant(), const_to_move);
697 }
698 } else if (const_to_move->IsLongConstant()) {
699 int64_t value = const_to_move->AsLongConstant()->GetValue();
700 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000701 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000702 } else if (location.IsDoubleStackSlot()) {
703 __ movq(CpuRegister(TMP), Immediate(value));
704 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
705 } else {
706 DCHECK(location.IsConstant());
707 DCHECK_EQ(location.GetConstant(), const_to_move);
708 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100709 }
Roland Levillain476df552014-10-09 17:51:36 +0100710 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100711 switch (instruction->GetType()) {
712 case Primitive::kPrimBoolean:
713 case Primitive::kPrimByte:
714 case Primitive::kPrimChar:
715 case Primitive::kPrimShort:
716 case Primitive::kPrimInt:
717 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100719 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
720 break;
721
722 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000724 Move(location,
725 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726 break;
727
728 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000731 } else if (instruction->IsTemporary()) {
732 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
733 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100734 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100735 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736 switch (instruction->GetType()) {
737 case Primitive::kPrimBoolean:
738 case Primitive::kPrimByte:
739 case Primitive::kPrimChar:
740 case Primitive::kPrimShort:
741 case Primitive::kPrimInt:
742 case Primitive::kPrimNot:
743 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 case Primitive::kPrimFloat:
745 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000746 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100747 break;
748
749 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100750 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100751 }
752 }
753}
754
755void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
756 got->SetLocations(nullptr);
757}
758
759void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
760 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100761 DCHECK(!successor->IsExitBlock());
762
763 HBasicBlock* block = got->GetBlock();
764 HInstruction* previous = got->GetPrevious();
765
766 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000767 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100768 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
769 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
770 return;
771 }
772
773 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
774 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
775 }
776 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100777 __ jmp(codegen_->GetLabelOf(successor));
778 }
779}
780
781void LocationsBuilderX86_64::VisitExit(HExit* exit) {
782 exit->SetLocations(nullptr);
783}
784
785void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700786 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100787}
788
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700789void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
790 Label* true_target,
791 Label* false_target,
792 Label* always_true_target) {
793 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100794 if (cond->IsIntConstant()) {
795 // Constant condition, statically compared against 1.
796 int32_t cond_value = cond->AsIntConstant()->GetValue();
797 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700798 if (always_true_target != nullptr) {
799 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100800 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100801 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100802 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100803 DCHECK_EQ(cond_value, 0);
804 }
805 } else {
806 bool materialized =
807 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
808 // Moves do not affect the eflags register, so if the condition is
809 // evaluated just before the if, we don't need to evaluate it
810 // again.
811 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700812 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100813 if (materialized) {
814 if (!eflags_set) {
815 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700816 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000818 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 } else {
820 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
821 Immediate(0));
822 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700823 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700825 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100826 }
827 } else {
828 Location lhs = cond->GetLocations()->InAt(0);
829 Location rhs = cond->GetLocations()->InAt(1);
830 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000831 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000833 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000834 if (constant == 0) {
835 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
836 } else {
837 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
838 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000840 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 Address(CpuRegister(RSP), rhs.GetStackIndex()));
842 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700843 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700844 }
Dave Allison20dfc792014-06-16 20:44:29 -0700845 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700846 if (false_target != nullptr) {
847 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848 }
849}
850
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700851void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
852 LocationSummary* locations =
853 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
854 HInstruction* cond = if_instr->InputAt(0);
855 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
856 locations->SetInAt(0, Location::Any());
857 }
858}
859
860void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
861 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
862 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
863 Label* always_true_target = true_target;
864 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
865 if_instr->IfTrueSuccessor())) {
866 always_true_target = nullptr;
867 }
868 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
869 if_instr->IfFalseSuccessor())) {
870 false_target = nullptr;
871 }
872 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
873}
874
875void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
876 LocationSummary* locations = new (GetGraph()->GetArena())
877 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
878 HInstruction* cond = deoptimize->InputAt(0);
879 DCHECK(cond->IsCondition());
880 if (cond->AsCondition()->NeedsMaterialization()) {
881 locations->SetInAt(0, Location::Any());
882 }
883}
884
885void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
886 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
887 DeoptimizationSlowPathX86_64(deoptimize);
888 codegen_->AddSlowPath(slow_path);
889 Label* slow_path_entry = slow_path->GetEntryLabel();
890 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
891}
892
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100893void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
894 local->SetLocations(nullptr);
895}
896
897void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
898 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
899}
900
901void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
902 local->SetLocations(nullptr);
903}
904
905void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
906 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700907 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908}
909
910void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100911 LocationSummary* locations =
912 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100913 switch (store->InputAt(1)->GetType()) {
914 case Primitive::kPrimBoolean:
915 case Primitive::kPrimByte:
916 case Primitive::kPrimChar:
917 case Primitive::kPrimShort:
918 case Primitive::kPrimInt:
919 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
922 break;
923
924 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100925 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
927 break;
928
929 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100930 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100932}
933
934void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700935 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100936}
937
Dave Allison20dfc792014-06-16 20:44:29 -0700938void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100939 LocationSummary* locations =
940 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100941 locations->SetInAt(0, Location::RequiresRegister());
942 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100943 if (comp->NeedsMaterialization()) {
944 locations->SetOut(Location::RequiresRegister());
945 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100946}
947
Dave Allison20dfc792014-06-16 20:44:29 -0700948void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
949 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100950 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000951 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100952 // Clear register: setcc only sets the low byte.
953 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000954 Location lhs = locations->InAt(0);
955 Location rhs = locations->InAt(1);
956 if (rhs.IsRegister()) {
957 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
958 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800959 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000960 if (constant == 0) {
961 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
962 } else {
963 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
964 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100965 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000966 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100967 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100968 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700969 }
970}
971
972void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
973 VisitCondition(comp);
974}
975
976void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
977 VisitCondition(comp);
978}
979
980void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
981 VisitCondition(comp);
982}
983
984void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
985 VisitCondition(comp);
986}
987
988void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
989 VisitCondition(comp);
990}
991
992void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
993 VisitCondition(comp);
994}
995
996void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
997 VisitCondition(comp);
998}
999
1000void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1001 VisitCondition(comp);
1002}
1003
1004void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1005 VisitCondition(comp);
1006}
1007
1008void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1009 VisitCondition(comp);
1010}
1011
1012void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1013 VisitCondition(comp);
1014}
1015
1016void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1017 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001018}
1019
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001020void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001021 LocationSummary* locations =
1022 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001023 switch (compare->InputAt(0)->GetType()) {
1024 case Primitive::kPrimLong: {
1025 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001026 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00001027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1028 break;
1029 }
1030 case Primitive::kPrimFloat:
1031 case Primitive::kPrimDouble: {
1032 locations->SetInAt(0, Location::RequiresFpuRegister());
1033 locations->SetInAt(1, Location::RequiresFpuRegister());
1034 locations->SetOut(Location::RequiresRegister());
1035 break;
1036 }
1037 default:
1038 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1039 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001040}
1041
1042void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001043 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001044 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001045 Location left = locations->InAt(0);
1046 Location right = locations->InAt(1);
1047
1048 Label less, greater, done;
1049 Primitive::Type type = compare->InputAt(0)->GetType();
1050 switch (type) {
1051 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001052 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1053 if (right.IsConstant()) {
1054 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1055 DCHECK(IsInt<32>(value));
1056 if (value == 0) {
1057 __ testq(left_reg, left_reg);
1058 } else {
1059 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1060 }
1061 } else {
1062 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1063 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001064 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001065 }
1066 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001067 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001068 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1069 break;
1070 }
1071 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001072 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001073 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1074 break;
1075 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001076 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001077 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001078 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001079 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001080 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001081 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001082
Calin Juravle91debbc2014-11-26 19:01:09 +00001083 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001084 __ movl(out, Immediate(1));
1085 __ jmp(&done);
1086
1087 __ Bind(&less);
1088 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001089
1090 __ Bind(&done);
1091}
1092
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001093void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001094 LocationSummary* locations =
1095 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001096 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097}
1098
1099void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001100 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001101 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001102}
1103
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001104void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1105 LocationSummary* locations =
1106 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1107 locations->SetOut(Location::ConstantLocation(constant));
1108}
1109
1110void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1111 // Will be generated at use site.
1112 UNUSED(constant);
1113}
1114
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001115void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001116 LocationSummary* locations =
1117 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001118 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001119}
1120
1121void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001122 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001123 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001124}
1125
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001126void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1127 LocationSummary* locations =
1128 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1129 locations->SetOut(Location::ConstantLocation(constant));
1130}
1131
1132void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1133 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001135}
1136
1137void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1138 LocationSummary* locations =
1139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1140 locations->SetOut(Location::ConstantLocation(constant));
1141}
1142
1143void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1144 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001145 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001146}
1147
Calin Juravle27df7582015-04-17 19:12:31 +01001148void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1149 memory_barrier->SetLocations(nullptr);
1150}
1151
1152void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1153 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1154}
1155
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001156void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1157 ret->SetLocations(nullptr);
1158}
1159
1160void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001161 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001162 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001163}
1164
1165void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001166 LocationSummary* locations =
1167 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001168 switch (ret->InputAt(0)->GetType()) {
1169 case Primitive::kPrimBoolean:
1170 case Primitive::kPrimByte:
1171 case Primitive::kPrimChar:
1172 case Primitive::kPrimShort:
1173 case Primitive::kPrimInt:
1174 case Primitive::kPrimNot:
1175 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001177 break;
1178
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 case Primitive::kPrimFloat:
1180 case Primitive::kPrimDouble:
1181 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001182 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001183 break;
1184
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001187 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188}
1189
1190void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1191 if (kIsDebugBuild) {
1192 switch (ret->InputAt(0)->GetType()) {
1193 case Primitive::kPrimBoolean:
1194 case Primitive::kPrimByte:
1195 case Primitive::kPrimChar:
1196 case Primitive::kPrimShort:
1197 case Primitive::kPrimInt:
1198 case Primitive::kPrimNot:
1199 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001200 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201 break;
1202
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 case Primitive::kPrimFloat:
1204 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001205 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001206 XMM0);
1207 break;
1208
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001209 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001211 }
1212 }
1213 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001214}
1215
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1217 switch (type) {
1218 case Primitive::kPrimBoolean:
1219 case Primitive::kPrimByte:
1220 case Primitive::kPrimChar:
1221 case Primitive::kPrimShort:
1222 case Primitive::kPrimInt:
1223 case Primitive::kPrimNot: {
1224 uint32_t index = gp_index_++;
1225 stack_index_++;
1226 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001227 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 } else {
1229 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1230 }
1231 }
1232
1233 case Primitive::kPrimLong: {
1234 uint32_t index = gp_index_;
1235 stack_index_ += 2;
1236 if (index < calling_convention.GetNumberOfRegisters()) {
1237 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001238 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001239 } else {
1240 gp_index_ += 2;
1241 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1242 }
1243 }
1244
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 case Primitive::kPrimFloat: {
1246 uint32_t index = fp_index_++;
1247 stack_index_++;
1248 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001249 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001250 } else {
1251 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1252 }
1253 }
1254
1255 case Primitive::kPrimDouble: {
1256 uint32_t index = fp_index_++;
1257 stack_index_ += 2;
1258 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001259 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001260 } else {
1261 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1262 }
1263 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264
1265 case Primitive::kPrimVoid:
1266 LOG(FATAL) << "Unexpected parameter type " << type;
1267 break;
1268 }
1269 return Location();
1270}
1271
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001272void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001273 // Explicit clinit checks triggered by static invokes must have been
1274 // pruned by art::PrepareForRegisterAllocation.
1275 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1276
Mark Mendellfb8d2792015-03-31 22:16:59 -04001277 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001278 if (intrinsic.TryDispatch(invoke)) {
1279 return;
1280 }
1281
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001282 HandleInvoke(invoke);
1283}
1284
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001285static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1286 if (invoke->GetLocations()->Intrinsified()) {
1287 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1288 intrinsic.Dispatch(invoke);
1289 return true;
1290 }
1291 return false;
1292}
1293
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001294void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001295 // Explicit clinit checks triggered by static invokes must have been
1296 // pruned by art::PrepareForRegisterAllocation.
1297 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1298
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001299 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1300 return;
1301 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001302
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001303 codegen_->GenerateStaticOrDirectCall(
1304 invoke,
1305 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001306 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001307}
1308
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001309void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001310 LocationSummary* locations =
1311 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001312 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001313
1314 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001315 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316 HInstruction* input = invoke->InputAt(i);
1317 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1318 }
1319
1320 switch (invoke->GetType()) {
1321 case Primitive::kPrimBoolean:
1322 case Primitive::kPrimByte:
1323 case Primitive::kPrimChar:
1324 case Primitive::kPrimShort:
1325 case Primitive::kPrimInt:
1326 case Primitive::kPrimNot:
1327 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001328 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001329 break;
1330
1331 case Primitive::kPrimVoid:
1332 break;
1333
1334 case Primitive::kPrimDouble:
1335 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001336 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001337 break;
1338 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001339}
1340
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001341void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001342 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001343 if (intrinsic.TryDispatch(invoke)) {
1344 return;
1345 }
1346
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001347 HandleInvoke(invoke);
1348}
1349
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001350void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001351 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1352 return;
1353 }
1354
Roland Levillain271ab9c2014-11-27 15:23:57 +00001355 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001356 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1357 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1358 LocationSummary* locations = invoke->GetLocations();
1359 Location receiver = locations->InAt(0);
1360 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1361 // temp = object->GetClass();
1362 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1364 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001365 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001366 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001367 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001368 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001369 // temp = temp->GetMethodAt(method_offset);
1370 __ movl(temp, Address(temp, method_offset));
1371 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001372 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001373 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001374
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001375 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001376 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001377}
1378
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001379void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1380 HandleInvoke(invoke);
1381 // Add the hidden argument.
1382 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1383}
1384
1385void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1386 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001387 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001388 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1389 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1390 LocationSummary* locations = invoke->GetLocations();
1391 Location receiver = locations->InAt(0);
1392 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1393
1394 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001395 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001396 Immediate(invoke->GetDexMethodIndex()));
1397
1398 // temp = object->GetClass();
1399 if (receiver.IsStackSlot()) {
1400 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1401 __ movl(temp, Address(temp, class_offset));
1402 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001403 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001404 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001405 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001406 // temp = temp->GetImtEntryAt(method_offset);
1407 __ movl(temp, Address(temp, method_offset));
1408 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001409 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001410 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001411
1412 DCHECK(!codegen_->IsLeafMethod());
1413 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1414}
1415
Roland Levillain88cb1752014-10-20 16:36:47 +01001416void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1417 LocationSummary* locations =
1418 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1419 switch (neg->GetResultType()) {
1420 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001421 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001422 locations->SetInAt(0, Location::RequiresRegister());
1423 locations->SetOut(Location::SameAsFirstInput());
1424 break;
1425
Roland Levillain88cb1752014-10-20 16:36:47 +01001426 case Primitive::kPrimFloat:
1427 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001428 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001429 locations->SetOut(Location::SameAsFirstInput());
1430 locations->AddTemp(Location::RequiresRegister());
1431 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001432 break;
1433
1434 default:
1435 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1436 }
1437}
1438
1439void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1440 LocationSummary* locations = neg->GetLocations();
1441 Location out = locations->Out();
1442 Location in = locations->InAt(0);
1443 switch (neg->GetResultType()) {
1444 case Primitive::kPrimInt:
1445 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001446 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001447 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001448 break;
1449
1450 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001451 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001452 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001453 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001454 break;
1455
Roland Levillain5368c212014-11-27 15:03:41 +00001456 case Primitive::kPrimFloat: {
1457 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001458 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1459 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001460 // Implement float negation with an exclusive or with value
1461 // 0x80000000 (mask for bit 31, representing the sign of a
1462 // single-precision floating-point number).
1463 __ movq(constant, Immediate(INT64_C(0x80000000)));
1464 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001465 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001466 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001467 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001468
Roland Levillain5368c212014-11-27 15:03:41 +00001469 case Primitive::kPrimDouble: {
1470 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001471 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1472 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001473 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001474 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001475 // a double-precision floating-point number).
1476 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1477 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001478 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001479 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001480 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001481
1482 default:
1483 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1484 }
1485}
1486
Roland Levillaindff1f282014-11-05 14:15:05 +00001487void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1488 LocationSummary* locations =
1489 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1490 Primitive::Type result_type = conversion->GetResultType();
1491 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001492 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001493
David Brazdilb2bd1c52015-03-25 11:17:37 +00001494 // The Java language does not allow treating boolean as an integral type but
1495 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001496
Roland Levillaindff1f282014-11-05 14:15:05 +00001497 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001498 case Primitive::kPrimByte:
1499 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001500 case Primitive::kPrimBoolean:
1501 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001502 case Primitive::kPrimShort:
1503 case Primitive::kPrimInt:
1504 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001505 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001506 locations->SetInAt(0, Location::Any());
1507 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1508 break;
1509
1510 default:
1511 LOG(FATAL) << "Unexpected type conversion from " << input_type
1512 << " to " << result_type;
1513 }
1514 break;
1515
Roland Levillain01a8d712014-11-14 16:27:39 +00001516 case Primitive::kPrimShort:
1517 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001518 case Primitive::kPrimBoolean:
1519 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001520 case Primitive::kPrimByte:
1521 case Primitive::kPrimInt:
1522 case Primitive::kPrimChar:
1523 // Processing a Dex `int-to-short' instruction.
1524 locations->SetInAt(0, Location::Any());
1525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1526 break;
1527
1528 default:
1529 LOG(FATAL) << "Unexpected type conversion from " << input_type
1530 << " to " << result_type;
1531 }
1532 break;
1533
Roland Levillain946e1432014-11-11 17:35:19 +00001534 case Primitive::kPrimInt:
1535 switch (input_type) {
1536 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001537 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001538 locations->SetInAt(0, Location::Any());
1539 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1540 break;
1541
1542 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001543 // Processing a Dex `float-to-int' instruction.
1544 locations->SetInAt(0, Location::RequiresFpuRegister());
1545 locations->SetOut(Location::RequiresRegister());
1546 locations->AddTemp(Location::RequiresFpuRegister());
1547 break;
1548
Roland Levillain946e1432014-11-11 17:35:19 +00001549 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001550 // Processing a Dex `double-to-int' instruction.
1551 locations->SetInAt(0, Location::RequiresFpuRegister());
1552 locations->SetOut(Location::RequiresRegister());
1553 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001554 break;
1555
1556 default:
1557 LOG(FATAL) << "Unexpected type conversion from " << input_type
1558 << " to " << result_type;
1559 }
1560 break;
1561
Roland Levillaindff1f282014-11-05 14:15:05 +00001562 case Primitive::kPrimLong:
1563 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001564 case Primitive::kPrimBoolean:
1565 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001566 case Primitive::kPrimByte:
1567 case Primitive::kPrimShort:
1568 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001569 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001570 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001571 // TODO: We would benefit from a (to-be-implemented)
1572 // Location::RegisterOrStackSlot requirement for this input.
1573 locations->SetInAt(0, Location::RequiresRegister());
1574 locations->SetOut(Location::RequiresRegister());
1575 break;
1576
1577 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001578 // Processing a Dex `float-to-long' instruction.
1579 locations->SetInAt(0, Location::RequiresFpuRegister());
1580 locations->SetOut(Location::RequiresRegister());
1581 locations->AddTemp(Location::RequiresFpuRegister());
1582 break;
1583
Roland Levillaindff1f282014-11-05 14:15:05 +00001584 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001585 // Processing a Dex `double-to-long' instruction.
1586 locations->SetInAt(0, Location::RequiresFpuRegister());
1587 locations->SetOut(Location::RequiresRegister());
1588 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001589 break;
1590
1591 default:
1592 LOG(FATAL) << "Unexpected type conversion from " << input_type
1593 << " to " << result_type;
1594 }
1595 break;
1596
Roland Levillain981e4542014-11-14 11:47:14 +00001597 case Primitive::kPrimChar:
1598 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001599 case Primitive::kPrimBoolean:
1600 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001601 case Primitive::kPrimByte:
1602 case Primitive::kPrimShort:
1603 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001604 // Processing a Dex `int-to-char' instruction.
1605 locations->SetInAt(0, Location::Any());
1606 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1607 break;
1608
1609 default:
1610 LOG(FATAL) << "Unexpected type conversion from " << input_type
1611 << " to " << result_type;
1612 }
1613 break;
1614
Roland Levillaindff1f282014-11-05 14:15:05 +00001615 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001616 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001617 case Primitive::kPrimBoolean:
1618 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001619 case Primitive::kPrimByte:
1620 case Primitive::kPrimShort:
1621 case Primitive::kPrimInt:
1622 case Primitive::kPrimChar:
1623 // Processing a Dex `int-to-float' instruction.
1624 locations->SetInAt(0, Location::RequiresRegister());
1625 locations->SetOut(Location::RequiresFpuRegister());
1626 break;
1627
1628 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001629 // Processing a Dex `long-to-float' instruction.
1630 locations->SetInAt(0, Location::RequiresRegister());
1631 locations->SetOut(Location::RequiresFpuRegister());
1632 break;
1633
Roland Levillaincff13742014-11-17 14:32:17 +00001634 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001635 // Processing a Dex `double-to-float' instruction.
1636 locations->SetInAt(0, Location::RequiresFpuRegister());
1637 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001638 break;
1639
1640 default:
1641 LOG(FATAL) << "Unexpected type conversion from " << input_type
1642 << " to " << result_type;
1643 };
1644 break;
1645
Roland Levillaindff1f282014-11-05 14:15:05 +00001646 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001647 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001648 case Primitive::kPrimBoolean:
1649 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001650 case Primitive::kPrimByte:
1651 case Primitive::kPrimShort:
1652 case Primitive::kPrimInt:
1653 case Primitive::kPrimChar:
1654 // Processing a Dex `int-to-double' instruction.
1655 locations->SetInAt(0, Location::RequiresRegister());
1656 locations->SetOut(Location::RequiresFpuRegister());
1657 break;
1658
1659 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001660 // Processing a Dex `long-to-double' instruction.
1661 locations->SetInAt(0, Location::RequiresRegister());
1662 locations->SetOut(Location::RequiresFpuRegister());
1663 break;
1664
Roland Levillaincff13742014-11-17 14:32:17 +00001665 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001666 // Processing a Dex `float-to-double' instruction.
1667 locations->SetInAt(0, Location::RequiresFpuRegister());
1668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001669 break;
1670
1671 default:
1672 LOG(FATAL) << "Unexpected type conversion from " << input_type
1673 << " to " << result_type;
1674 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001675 break;
1676
1677 default:
1678 LOG(FATAL) << "Unexpected type conversion from " << input_type
1679 << " to " << result_type;
1680 }
1681}
1682
1683void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1684 LocationSummary* locations = conversion->GetLocations();
1685 Location out = locations->Out();
1686 Location in = locations->InAt(0);
1687 Primitive::Type result_type = conversion->GetResultType();
1688 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001689 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001690 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001691 case Primitive::kPrimByte:
1692 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001693 case Primitive::kPrimBoolean:
1694 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001695 case Primitive::kPrimShort:
1696 case Primitive::kPrimInt:
1697 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001698 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001699 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001700 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001701 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001702 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001703 Address(CpuRegister(RSP), in.GetStackIndex()));
1704 } else {
1705 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001706 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001707 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1708 }
1709 break;
1710
1711 default:
1712 LOG(FATAL) << "Unexpected type conversion from " << input_type
1713 << " to " << result_type;
1714 }
1715 break;
1716
Roland Levillain01a8d712014-11-14 16:27:39 +00001717 case Primitive::kPrimShort:
1718 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001719 case Primitive::kPrimBoolean:
1720 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001721 case Primitive::kPrimByte:
1722 case Primitive::kPrimInt:
1723 case Primitive::kPrimChar:
1724 // Processing a Dex `int-to-short' instruction.
1725 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001726 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001727 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001729 Address(CpuRegister(RSP), in.GetStackIndex()));
1730 } else {
1731 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001732 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001733 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1734 }
1735 break;
1736
1737 default:
1738 LOG(FATAL) << "Unexpected type conversion from " << input_type
1739 << " to " << result_type;
1740 }
1741 break;
1742
Roland Levillain946e1432014-11-11 17:35:19 +00001743 case Primitive::kPrimInt:
1744 switch (input_type) {
1745 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001746 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001747 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001749 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001751 Address(CpuRegister(RSP), in.GetStackIndex()));
1752 } else {
1753 DCHECK(in.IsConstant());
1754 DCHECK(in.GetConstant()->IsLongConstant());
1755 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001757 }
1758 break;
1759
Roland Levillain3f8f9362014-12-02 17:45:01 +00001760 case Primitive::kPrimFloat: {
1761 // Processing a Dex `float-to-int' instruction.
1762 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1763 CpuRegister output = out.AsRegister<CpuRegister>();
1764 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1765 Label done, nan;
1766
1767 __ movl(output, Immediate(kPrimIntMax));
1768 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001769 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001770 // if input >= temp goto done
1771 __ comiss(input, temp);
1772 __ j(kAboveEqual, &done);
1773 // if input == NaN goto nan
1774 __ j(kUnordered, &nan);
1775 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001776 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001777 __ jmp(&done);
1778 __ Bind(&nan);
1779 // output = 0
1780 __ xorl(output, output);
1781 __ Bind(&done);
1782 break;
1783 }
1784
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001785 case Primitive::kPrimDouble: {
1786 // Processing a Dex `double-to-int' instruction.
1787 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1788 CpuRegister output = out.AsRegister<CpuRegister>();
1789 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1790 Label done, nan;
1791
1792 __ movl(output, Immediate(kPrimIntMax));
1793 // temp = int-to-double(output)
1794 __ cvtsi2sd(temp, output);
1795 // if input >= temp goto done
1796 __ comisd(input, temp);
1797 __ j(kAboveEqual, &done);
1798 // if input == NaN goto nan
1799 __ j(kUnordered, &nan);
1800 // output = double-to-int-truncate(input)
1801 __ cvttsd2si(output, input);
1802 __ jmp(&done);
1803 __ Bind(&nan);
1804 // output = 0
1805 __ xorl(output, output);
1806 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001807 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001808 }
Roland Levillain946e1432014-11-11 17:35:19 +00001809
1810 default:
1811 LOG(FATAL) << "Unexpected type conversion from " << input_type
1812 << " to " << result_type;
1813 }
1814 break;
1815
Roland Levillaindff1f282014-11-05 14:15:05 +00001816 case Primitive::kPrimLong:
1817 switch (input_type) {
1818 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001819 case Primitive::kPrimBoolean:
1820 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001821 case Primitive::kPrimByte:
1822 case Primitive::kPrimShort:
1823 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001824 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001825 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001826 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001827 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001828 break;
1829
Roland Levillain624279f2014-12-04 11:54:28 +00001830 case Primitive::kPrimFloat: {
1831 // Processing a Dex `float-to-long' instruction.
1832 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1833 CpuRegister output = out.AsRegister<CpuRegister>();
1834 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1835 Label done, nan;
1836
1837 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001838 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001839 __ cvtsi2ss(temp, output, true);
1840 // if input >= temp goto done
1841 __ comiss(input, temp);
1842 __ j(kAboveEqual, &done);
1843 // if input == NaN goto nan
1844 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001845 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001846 __ cvttss2si(output, input, true);
1847 __ jmp(&done);
1848 __ Bind(&nan);
1849 // output = 0
1850 __ xorq(output, output);
1851 __ Bind(&done);
1852 break;
1853 }
1854
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001855 case Primitive::kPrimDouble: {
1856 // Processing a Dex `double-to-long' instruction.
1857 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1858 CpuRegister output = out.AsRegister<CpuRegister>();
1859 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1860 Label done, nan;
1861
1862 __ movq(output, Immediate(kPrimLongMax));
1863 // temp = long-to-double(output)
1864 __ cvtsi2sd(temp, output, true);
1865 // if input >= temp goto done
1866 __ comisd(input, temp);
1867 __ j(kAboveEqual, &done);
1868 // if input == NaN goto nan
1869 __ j(kUnordered, &nan);
1870 // output = double-to-long-truncate(input)
1871 __ cvttsd2si(output, input, true);
1872 __ jmp(&done);
1873 __ Bind(&nan);
1874 // output = 0
1875 __ xorq(output, output);
1876 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001877 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001878 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001879
1880 default:
1881 LOG(FATAL) << "Unexpected type conversion from " << input_type
1882 << " to " << result_type;
1883 }
1884 break;
1885
Roland Levillain981e4542014-11-14 11:47:14 +00001886 case Primitive::kPrimChar:
1887 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001888 case Primitive::kPrimBoolean:
1889 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001890 case Primitive::kPrimByte:
1891 case Primitive::kPrimShort:
1892 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001893 // Processing a Dex `int-to-char' instruction.
1894 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001895 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001896 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001897 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001898 Address(CpuRegister(RSP), in.GetStackIndex()));
1899 } else {
1900 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001901 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001902 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1903 }
1904 break;
1905
1906 default:
1907 LOG(FATAL) << "Unexpected type conversion from " << input_type
1908 << " to " << result_type;
1909 }
1910 break;
1911
Roland Levillaindff1f282014-11-05 14:15:05 +00001912 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001913 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001914 case Primitive::kPrimBoolean:
1915 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001916 case Primitive::kPrimByte:
1917 case Primitive::kPrimShort:
1918 case Primitive::kPrimInt:
1919 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001920 // Processing a Dex `int-to-float' instruction.
1921 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001922 break;
1923
1924 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001925 // Processing a Dex `long-to-float' instruction.
1926 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1927 break;
1928
Roland Levillaincff13742014-11-17 14:32:17 +00001929 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001930 // Processing a Dex `double-to-float' instruction.
1931 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001932 break;
1933
1934 default:
1935 LOG(FATAL) << "Unexpected type conversion from " << input_type
1936 << " to " << result_type;
1937 };
1938 break;
1939
Roland Levillaindff1f282014-11-05 14:15:05 +00001940 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001941 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001942 case Primitive::kPrimBoolean:
1943 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001944 case Primitive::kPrimByte:
1945 case Primitive::kPrimShort:
1946 case Primitive::kPrimInt:
1947 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001948 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001949 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001950 break;
1951
1952 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001953 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001955 break;
1956
Roland Levillaincff13742014-11-17 14:32:17 +00001957 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001958 // Processing a Dex `float-to-double' instruction.
1959 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001960 break;
1961
1962 default:
1963 LOG(FATAL) << "Unexpected type conversion from " << input_type
1964 << " to " << result_type;
1965 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001966 break;
1967
1968 default:
1969 LOG(FATAL) << "Unexpected type conversion from " << input_type
1970 << " to " << result_type;
1971 }
1972}
1973
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001974void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001975 LocationSummary* locations =
1976 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001978 case Primitive::kPrimInt: {
1979 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001980 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001982 break;
1983 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001985 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001986 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001987 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001988 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001990 break;
1991 }
1992
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001993 case Primitive::kPrimDouble:
1994 case Primitive::kPrimFloat: {
1995 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04001996 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001997 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001998 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002000
2001 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002003 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002004}
2005
2006void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2007 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002008 Location first = locations->InAt(0);
2009 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002010 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002011
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002012 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002013 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002014 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002015 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2016 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2017 } else {
2018 __ leal(out.AsRegister<CpuRegister>(), Address(
2019 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2020 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002021 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002022 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2023 __ addl(out.AsRegister<CpuRegister>(),
2024 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2025 } else {
2026 __ leal(out.AsRegister<CpuRegister>(), Address(
2027 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2028 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002029 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002030 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002032 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002033 break;
2034 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002035
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002037 if (second.IsRegister()) {
2038 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2039 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2040 } else {
2041 __ leaq(out.AsRegister<CpuRegister>(), Address(
2042 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2043 }
2044 } else {
2045 DCHECK(second.IsConstant());
2046 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2047 int32_t int32_value = Low32Bits(value);
2048 DCHECK_EQ(int32_value, value);
2049 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2050 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2051 } else {
2052 __ leaq(out.AsRegister<CpuRegister>(), Address(
2053 first.AsRegister<CpuRegister>(), int32_value));
2054 }
2055 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002056 break;
2057 }
2058
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002059 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002060 if (second.IsFpuRegister()) {
2061 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2062 } else if (second.IsConstant()) {
2063 __ addss(first.AsFpuRegister<XmmRegister>(),
2064 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2065 } else {
2066 DCHECK(second.IsStackSlot());
2067 __ addss(first.AsFpuRegister<XmmRegister>(),
2068 Address(CpuRegister(RSP), second.GetStackIndex()));
2069 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002070 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002071 }
2072
2073 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002074 if (second.IsFpuRegister()) {
2075 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2076 } else if (second.IsConstant()) {
2077 __ addsd(first.AsFpuRegister<XmmRegister>(),
2078 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2079 } else {
2080 DCHECK(second.IsDoubleStackSlot());
2081 __ addsd(first.AsFpuRegister<XmmRegister>(),
2082 Address(CpuRegister(RSP), second.GetStackIndex()));
2083 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002084 break;
2085 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002086
2087 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002088 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002089 }
2090}
2091
2092void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002093 LocationSummary* locations =
2094 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002095 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002096 case Primitive::kPrimInt: {
2097 locations->SetInAt(0, Location::RequiresRegister());
2098 locations->SetInAt(1, Location::Any());
2099 locations->SetOut(Location::SameAsFirstInput());
2100 break;
2101 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002102 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002103 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002104 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002105 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002106 break;
2107 }
Calin Juravle11351682014-10-23 15:38:15 +01002108 case Primitive::kPrimFloat:
2109 case Primitive::kPrimDouble: {
2110 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002111 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002112 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002113 break;
Calin Juravle11351682014-10-23 15:38:15 +01002114 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002115 default:
Calin Juravle11351682014-10-23 15:38:15 +01002116 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002117 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002118}
2119
2120void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2121 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002122 Location first = locations->InAt(0);
2123 Location second = locations->InAt(1);
2124 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002126 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002127 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002128 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002129 } else if (second.IsConstant()) {
2130 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002132 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002134 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002135 break;
2136 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002137 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002138 if (second.IsConstant()) {
2139 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2140 DCHECK(IsInt<32>(value));
2141 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2142 } else {
2143 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2144 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002145 break;
2146 }
2147
Calin Juravle11351682014-10-23 15:38:15 +01002148 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002149 if (second.IsFpuRegister()) {
2150 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2151 } else if (second.IsConstant()) {
2152 __ subss(first.AsFpuRegister<XmmRegister>(),
2153 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2154 } else {
2155 DCHECK(second.IsStackSlot());
2156 __ subss(first.AsFpuRegister<XmmRegister>(),
2157 Address(CpuRegister(RSP), second.GetStackIndex()));
2158 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002159 break;
Calin Juravle11351682014-10-23 15:38:15 +01002160 }
2161
2162 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002163 if (second.IsFpuRegister()) {
2164 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2165 } else if (second.IsConstant()) {
2166 __ subsd(first.AsFpuRegister<XmmRegister>(),
2167 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2168 } else {
2169 DCHECK(second.IsDoubleStackSlot());
2170 __ subsd(first.AsFpuRegister<XmmRegister>(),
2171 Address(CpuRegister(RSP), second.GetStackIndex()));
2172 }
Calin Juravle11351682014-10-23 15:38:15 +01002173 break;
2174 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002175
2176 default:
Calin Juravle11351682014-10-23 15:38:15 +01002177 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002178 }
2179}
2180
Calin Juravle34bacdf2014-10-07 20:23:36 +01002181void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2182 LocationSummary* locations =
2183 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2184 switch (mul->GetResultType()) {
2185 case Primitive::kPrimInt: {
2186 locations->SetInAt(0, Location::RequiresRegister());
2187 locations->SetInAt(1, Location::Any());
2188 locations->SetOut(Location::SameAsFirstInput());
2189 break;
2190 }
2191 case Primitive::kPrimLong: {
2192 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002193 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2194 if (locations->InAt(1).IsConstant()) {
2195 // Can use 3 operand multiply.
2196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2197 } else {
2198 locations->SetOut(Location::SameAsFirstInput());
2199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002200 break;
2201 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002202 case Primitive::kPrimFloat:
2203 case Primitive::kPrimDouble: {
2204 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002205 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002206 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002207 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002208 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002209
2210 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002211 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002212 }
2213}
2214
2215void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2216 LocationSummary* locations = mul->GetLocations();
2217 Location first = locations->InAt(0);
2218 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002219 switch (mul->GetResultType()) {
2220 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002221 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002222 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002223 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002224 } else if (second.IsConstant()) {
2225 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002226 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002227 } else {
2228 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002229 __ imull(first.AsRegister<CpuRegister>(),
2230 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002231 }
2232 break;
2233 }
2234 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002235 if (second.IsConstant()) {
2236 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2237 DCHECK(IsInt<32>(value));
2238 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2239 first.AsRegister<CpuRegister>(),
2240 Immediate(static_cast<int32_t>(value)));
2241 } else {
2242 DCHECK(first.Equals(locations->Out()));
2243 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2244 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002245 break;
2246 }
2247
Calin Juravleb5bfa962014-10-21 18:02:24 +01002248 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002249 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002250 if (second.IsFpuRegister()) {
2251 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2252 } else if (second.IsConstant()) {
2253 __ mulss(first.AsFpuRegister<XmmRegister>(),
2254 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2255 } else {
2256 DCHECK(second.IsStackSlot());
2257 __ mulss(first.AsFpuRegister<XmmRegister>(),
2258 Address(CpuRegister(RSP), second.GetStackIndex()));
2259 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002260 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002261 }
2262
2263 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002264 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002265 if (second.IsFpuRegister()) {
2266 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2267 } else if (second.IsConstant()) {
2268 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2269 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2270 } else {
2271 DCHECK(second.IsDoubleStackSlot());
2272 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2273 Address(CpuRegister(RSP), second.GetStackIndex()));
2274 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002275 break;
2276 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002277
2278 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002279 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002280 }
2281}
2282
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002283void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2284 uint32_t stack_adjustment, bool is_float) {
2285 if (source.IsStackSlot()) {
2286 DCHECK(is_float);
2287 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2288 } else if (source.IsDoubleStackSlot()) {
2289 DCHECK(!is_float);
2290 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2291 } else {
2292 // Write the value to the temporary location on the stack and load to FP stack.
2293 if (is_float) {
2294 Location stack_temp = Location::StackSlot(temp_offset);
2295 codegen_->Move(stack_temp, source);
2296 __ flds(Address(CpuRegister(RSP), temp_offset));
2297 } else {
2298 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2299 codegen_->Move(stack_temp, source);
2300 __ fldl(Address(CpuRegister(RSP), temp_offset));
2301 }
2302 }
2303}
2304
2305void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2306 Primitive::Type type = rem->GetResultType();
2307 bool is_float = type == Primitive::kPrimFloat;
2308 size_t elem_size = Primitive::ComponentSize(type);
2309 LocationSummary* locations = rem->GetLocations();
2310 Location first = locations->InAt(0);
2311 Location second = locations->InAt(1);
2312 Location out = locations->Out();
2313
2314 // Create stack space for 2 elements.
2315 // TODO: enhance register allocator to ask for stack temporaries.
2316 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2317
2318 // Load the values to the FP stack in reverse order, using temporaries if needed.
2319 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2320 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2321
2322 // Loop doing FPREM until we stabilize.
2323 Label retry;
2324 __ Bind(&retry);
2325 __ fprem();
2326
2327 // Move FP status to AX.
2328 __ fstsw();
2329
2330 // And see if the argument reduction is complete. This is signaled by the
2331 // C2 FPU flag bit set to 0.
2332 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2333 __ j(kNotEqual, &retry);
2334
2335 // We have settled on the final value. Retrieve it into an XMM register.
2336 // Store FP top of stack to real stack.
2337 if (is_float) {
2338 __ fsts(Address(CpuRegister(RSP), 0));
2339 } else {
2340 __ fstl(Address(CpuRegister(RSP), 0));
2341 }
2342
2343 // Pop the 2 items from the FP stack.
2344 __ fucompp();
2345
2346 // Load the value from the stack into an XMM register.
2347 DCHECK(out.IsFpuRegister()) << out;
2348 if (is_float) {
2349 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2350 } else {
2351 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2352 }
2353
2354 // And remove the temporary stack space we allocated.
2355 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2356}
2357
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002358void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2359 DCHECK(instruction->IsDiv() || instruction->IsRem());
2360
2361 LocationSummary* locations = instruction->GetLocations();
2362 Location second = locations->InAt(1);
2363 DCHECK(second.IsConstant());
2364
2365 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2366 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002367 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002368
2369 DCHECK(imm == 1 || imm == -1);
2370
2371 switch (instruction->GetResultType()) {
2372 case Primitive::kPrimInt: {
2373 if (instruction->IsRem()) {
2374 __ xorl(output_register, output_register);
2375 } else {
2376 __ movl(output_register, input_register);
2377 if (imm == -1) {
2378 __ negl(output_register);
2379 }
2380 }
2381 break;
2382 }
2383
2384 case Primitive::kPrimLong: {
2385 if (instruction->IsRem()) {
2386 __ xorq(output_register, output_register);
2387 } else {
2388 __ movq(output_register, input_register);
2389 if (imm == -1) {
2390 __ negq(output_register);
2391 }
2392 }
2393 break;
2394 }
2395
2396 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002397 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002398 }
2399}
2400
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002401void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002402 LocationSummary* locations = instruction->GetLocations();
2403 Location second = locations->InAt(1);
2404
2405 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2406 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2407
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002408 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002409
2410 DCHECK(IsPowerOfTwo(std::abs(imm)));
2411
2412 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2413
2414 if (instruction->GetResultType() == Primitive::kPrimInt) {
2415 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2416 __ testl(numerator, numerator);
2417 __ cmov(kGreaterEqual, tmp, numerator);
2418 int shift = CTZ(imm);
2419 __ sarl(tmp, Immediate(shift));
2420
2421 if (imm < 0) {
2422 __ negl(tmp);
2423 }
2424
2425 __ movl(output_register, tmp);
2426 } else {
2427 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2428 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2429
2430 __ movq(rdx, Immediate(std::abs(imm) - 1));
2431 __ addq(rdx, numerator);
2432 __ testq(numerator, numerator);
2433 __ cmov(kGreaterEqual, rdx, numerator);
2434 int shift = CTZ(imm);
2435 __ sarq(rdx, Immediate(shift));
2436
2437 if (imm < 0) {
2438 __ negq(rdx);
2439 }
2440
2441 __ movq(output_register, rdx);
2442 }
2443}
2444
2445void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2446 DCHECK(instruction->IsDiv() || instruction->IsRem());
2447
2448 LocationSummary* locations = instruction->GetLocations();
2449 Location second = locations->InAt(1);
2450
2451 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2452 : locations->GetTemp(0).AsRegister<CpuRegister>();
2453 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2454 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2455 : locations->Out().AsRegister<CpuRegister>();
2456 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2457
2458 DCHECK_EQ(RAX, eax.AsRegister());
2459 DCHECK_EQ(RDX, edx.AsRegister());
2460 if (instruction->IsDiv()) {
2461 DCHECK_EQ(RAX, out.AsRegister());
2462 } else {
2463 DCHECK_EQ(RDX, out.AsRegister());
2464 }
2465
2466 int64_t magic;
2467 int shift;
2468
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002469 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002470 if (instruction->GetResultType() == Primitive::kPrimInt) {
2471 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2472
2473 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2474
2475 __ movl(numerator, eax);
2476
2477 Label no_div;
2478 Label end;
2479 __ testl(eax, eax);
2480 __ j(kNotEqual, &no_div);
2481
2482 __ xorl(out, out);
2483 __ jmp(&end);
2484
2485 __ Bind(&no_div);
2486
2487 __ movl(eax, Immediate(magic));
2488 __ imull(numerator);
2489
2490 if (imm > 0 && magic < 0) {
2491 __ addl(edx, numerator);
2492 } else if (imm < 0 && magic > 0) {
2493 __ subl(edx, numerator);
2494 }
2495
2496 if (shift != 0) {
2497 __ sarl(edx, Immediate(shift));
2498 }
2499
2500 __ movl(eax, edx);
2501 __ shrl(edx, Immediate(31));
2502 __ addl(edx, eax);
2503
2504 if (instruction->IsRem()) {
2505 __ movl(eax, numerator);
2506 __ imull(edx, Immediate(imm));
2507 __ subl(eax, edx);
2508 __ movl(edx, eax);
2509 } else {
2510 __ movl(eax, edx);
2511 }
2512 __ Bind(&end);
2513 } else {
2514 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2515
2516 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2517
2518 CpuRegister rax = eax;
2519 CpuRegister rdx = edx;
2520
2521 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2522
2523 // Save the numerator.
2524 __ movq(numerator, rax);
2525
2526 // RAX = magic
2527 __ movq(rax, Immediate(magic));
2528
2529 // RDX:RAX = magic * numerator
2530 __ imulq(numerator);
2531
2532 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002533 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002534 __ addq(rdx, numerator);
2535 } else if (imm < 0 && magic > 0) {
2536 // RDX -= numerator
2537 __ subq(rdx, numerator);
2538 }
2539
2540 // Shift if needed.
2541 if (shift != 0) {
2542 __ sarq(rdx, Immediate(shift));
2543 }
2544
2545 // RDX += 1 if RDX < 0
2546 __ movq(rax, rdx);
2547 __ shrq(rdx, Immediate(63));
2548 __ addq(rdx, rax);
2549
2550 if (instruction->IsRem()) {
2551 __ movq(rax, numerator);
2552
2553 if (IsInt<32>(imm)) {
2554 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2555 } else {
2556 __ movq(numerator, Immediate(imm));
2557 __ imulq(rdx, numerator);
2558 }
2559
2560 __ subq(rax, rdx);
2561 __ movq(rdx, rax);
2562 } else {
2563 __ movq(rax, rdx);
2564 }
2565 }
2566}
2567
Calin Juravlebacfec32014-11-14 15:54:36 +00002568void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2569 DCHECK(instruction->IsDiv() || instruction->IsRem());
2570 Primitive::Type type = instruction->GetResultType();
2571 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2572
2573 bool is_div = instruction->IsDiv();
2574 LocationSummary* locations = instruction->GetLocations();
2575
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002576 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2577 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002578
Roland Levillain271ab9c2014-11-27 15:23:57 +00002579 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002580 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002581
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002582 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002583 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002584
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002585 if (imm == 0) {
2586 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2587 } else if (imm == 1 || imm == -1) {
2588 DivRemOneOrMinusOne(instruction);
2589 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002590 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002591 } else {
2592 DCHECK(imm <= -2 || imm >= 2);
2593 GenerateDivRemWithAnyConstant(instruction);
2594 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002595 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002596 SlowPathCodeX86_64* slow_path =
2597 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2598 out.AsRegister(), type, is_div);
2599 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002600
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002601 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2602 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2603 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2604 // so it's safe to just use negl instead of more complex comparisons.
2605 if (type == Primitive::kPrimInt) {
2606 __ cmpl(second_reg, Immediate(-1));
2607 __ j(kEqual, slow_path->GetEntryLabel());
2608 // edx:eax <- sign-extended of eax
2609 __ cdq();
2610 // eax = quotient, edx = remainder
2611 __ idivl(second_reg);
2612 } else {
2613 __ cmpq(second_reg, Immediate(-1));
2614 __ j(kEqual, slow_path->GetEntryLabel());
2615 // rdx:rax <- sign-extended of rax
2616 __ cqo();
2617 // rax = quotient, rdx = remainder
2618 __ idivq(second_reg);
2619 }
2620 __ Bind(slow_path->GetExitLabel());
2621 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002622}
2623
Calin Juravle7c4954d2014-10-28 16:57:40 +00002624void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2625 LocationSummary* locations =
2626 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2627 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002628 case Primitive::kPrimInt:
2629 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002630 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002631 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002632 locations->SetOut(Location::SameAsFirstInput());
2633 // Intel uses edx:eax as the dividend.
2634 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002635 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2636 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2637 // output and request another temp.
2638 if (div->InputAt(1)->IsConstant()) {
2639 locations->AddTemp(Location::RequiresRegister());
2640 }
Calin Juravled0d48522014-11-04 16:40:20 +00002641 break;
2642 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002643
Calin Juravle7c4954d2014-10-28 16:57:40 +00002644 case Primitive::kPrimFloat:
2645 case Primitive::kPrimDouble: {
2646 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002647 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002648 locations->SetOut(Location::SameAsFirstInput());
2649 break;
2650 }
2651
2652 default:
2653 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2654 }
2655}
2656
2657void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2658 LocationSummary* locations = div->GetLocations();
2659 Location first = locations->InAt(0);
2660 Location second = locations->InAt(1);
2661 DCHECK(first.Equals(locations->Out()));
2662
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002663 Primitive::Type type = div->GetResultType();
2664 switch (type) {
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002667 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002668 break;
2669 }
2670
Calin Juravle7c4954d2014-10-28 16:57:40 +00002671 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002672 if (second.IsFpuRegister()) {
2673 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2674 } else if (second.IsConstant()) {
2675 __ divss(first.AsFpuRegister<XmmRegister>(),
2676 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2677 } else {
2678 DCHECK(second.IsStackSlot());
2679 __ divss(first.AsFpuRegister<XmmRegister>(),
2680 Address(CpuRegister(RSP), second.GetStackIndex()));
2681 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002682 break;
2683 }
2684
2685 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002686 if (second.IsFpuRegister()) {
2687 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2688 } else if (second.IsConstant()) {
2689 __ divsd(first.AsFpuRegister<XmmRegister>(),
2690 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2691 } else {
2692 DCHECK(second.IsDoubleStackSlot());
2693 __ divsd(first.AsFpuRegister<XmmRegister>(),
2694 Address(CpuRegister(RSP), second.GetStackIndex()));
2695 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002696 break;
2697 }
2698
2699 default:
2700 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2701 }
2702}
2703
Calin Juravlebacfec32014-11-14 15:54:36 +00002704void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002705 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002706 LocationSummary* locations =
2707 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002708
2709 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002710 case Primitive::kPrimInt:
2711 case Primitive::kPrimLong: {
2712 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002713 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002714 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2715 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002716 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2717 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2718 // output and request another temp.
2719 if (rem->InputAt(1)->IsConstant()) {
2720 locations->AddTemp(Location::RequiresRegister());
2721 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002722 break;
2723 }
2724
2725 case Primitive::kPrimFloat:
2726 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002727 locations->SetInAt(0, Location::Any());
2728 locations->SetInAt(1, Location::Any());
2729 locations->SetOut(Location::RequiresFpuRegister());
2730 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002731 break;
2732 }
2733
2734 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002735 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002736 }
2737}
2738
2739void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2740 Primitive::Type type = rem->GetResultType();
2741 switch (type) {
2742 case Primitive::kPrimInt:
2743 case Primitive::kPrimLong: {
2744 GenerateDivRemIntegral(rem);
2745 break;
2746 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002747 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002748 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002749 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002750 break;
2751 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002752 default:
2753 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2754 }
2755}
2756
Calin Juravled0d48522014-11-04 16:40:20 +00002757void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2758 LocationSummary* locations =
2759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2760 locations->SetInAt(0, Location::Any());
2761 if (instruction->HasUses()) {
2762 locations->SetOut(Location::SameAsFirstInput());
2763 }
2764}
2765
2766void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2767 SlowPathCodeX86_64* slow_path =
2768 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2769 codegen_->AddSlowPath(slow_path);
2770
2771 LocationSummary* locations = instruction->GetLocations();
2772 Location value = locations->InAt(0);
2773
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002774 switch (instruction->GetType()) {
2775 case Primitive::kPrimInt: {
2776 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002777 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002778 __ j(kEqual, slow_path->GetEntryLabel());
2779 } else if (value.IsStackSlot()) {
2780 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2781 __ j(kEqual, slow_path->GetEntryLabel());
2782 } else {
2783 DCHECK(value.IsConstant()) << value;
2784 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2785 __ jmp(slow_path->GetEntryLabel());
2786 }
2787 }
2788 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002789 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002790 case Primitive::kPrimLong: {
2791 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002792 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002793 __ j(kEqual, slow_path->GetEntryLabel());
2794 } else if (value.IsDoubleStackSlot()) {
2795 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2796 __ j(kEqual, slow_path->GetEntryLabel());
2797 } else {
2798 DCHECK(value.IsConstant()) << value;
2799 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2800 __ jmp(slow_path->GetEntryLabel());
2801 }
2802 }
2803 break;
2804 }
2805 default:
2806 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002807 }
Calin Juravled0d48522014-11-04 16:40:20 +00002808}
2809
Calin Juravle9aec02f2014-11-18 23:06:35 +00002810void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2811 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2812
2813 LocationSummary* locations =
2814 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2815
2816 switch (op->GetResultType()) {
2817 case Primitive::kPrimInt:
2818 case Primitive::kPrimLong: {
2819 locations->SetInAt(0, Location::RequiresRegister());
2820 // The shift count needs to be in CL.
2821 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2822 locations->SetOut(Location::SameAsFirstInput());
2823 break;
2824 }
2825 default:
2826 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2827 }
2828}
2829
2830void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2831 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2832
2833 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002835 Location second = locations->InAt(1);
2836
2837 switch (op->GetResultType()) {
2838 case Primitive::kPrimInt: {
2839 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002840 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002841 if (op->IsShl()) {
2842 __ shll(first_reg, second_reg);
2843 } else if (op->IsShr()) {
2844 __ sarl(first_reg, second_reg);
2845 } else {
2846 __ shrl(first_reg, second_reg);
2847 }
2848 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002849 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002850 if (op->IsShl()) {
2851 __ shll(first_reg, imm);
2852 } else if (op->IsShr()) {
2853 __ sarl(first_reg, imm);
2854 } else {
2855 __ shrl(first_reg, imm);
2856 }
2857 }
2858 break;
2859 }
2860 case Primitive::kPrimLong: {
2861 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002862 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002863 if (op->IsShl()) {
2864 __ shlq(first_reg, second_reg);
2865 } else if (op->IsShr()) {
2866 __ sarq(first_reg, second_reg);
2867 } else {
2868 __ shrq(first_reg, second_reg);
2869 }
2870 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002871 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002872 if (op->IsShl()) {
2873 __ shlq(first_reg, imm);
2874 } else if (op->IsShr()) {
2875 __ sarq(first_reg, imm);
2876 } else {
2877 __ shrq(first_reg, imm);
2878 }
2879 }
2880 break;
2881 }
2882 default:
2883 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2884 }
2885}
2886
2887void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2888 HandleShift(shl);
2889}
2890
2891void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2892 HandleShift(shl);
2893}
2894
2895void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2896 HandleShift(shr);
2897}
2898
2899void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2900 HandleShift(shr);
2901}
2902
2903void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2904 HandleShift(ushr);
2905}
2906
2907void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2908 HandleShift(ushr);
2909}
2910
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002911void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002912 LocationSummary* locations =
2913 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002914 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002915 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2916 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2917 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002918}
2919
2920void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2921 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002922 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002923 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2924
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002925 __ gs()->call(
2926 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002927
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002928 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002929 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002930}
2931
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002932void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2933 LocationSummary* locations =
2934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2935 InvokeRuntimeCallingConvention calling_convention;
2936 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002937 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002938 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002939 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002940}
2941
2942void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2943 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002944 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002945 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2946
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002947 __ gs()->call(
2948 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002949
2950 DCHECK(!codegen_->IsLeafMethod());
2951 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2952}
2953
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002954void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002955 LocationSummary* locations =
2956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002957 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2958 if (location.IsStackSlot()) {
2959 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2960 } else if (location.IsDoubleStackSlot()) {
2961 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2962 }
2963 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002964}
2965
2966void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2967 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002968 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002969}
2970
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002971void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002972 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002973 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002974 locations->SetInAt(0, Location::RequiresRegister());
2975 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002976}
2977
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002978void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2979 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002980 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2981 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002982 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002983 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002984 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002985 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002986 break;
2987
2988 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002989 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002990 break;
2991
2992 default:
2993 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2994 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002995}
2996
David Brazdil66d126e2015-04-03 16:02:44 +01002997void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
2998 LocationSummary* locations =
2999 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3000 locations->SetInAt(0, Location::RequiresRegister());
3001 locations->SetOut(Location::SameAsFirstInput());
3002}
3003
3004void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003005 LocationSummary* locations = bool_not->GetLocations();
3006 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3007 locations->Out().AsRegister<CpuRegister>().AsRegister());
3008 Location out = locations->Out();
3009 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3010}
3011
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003012void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003013 LocationSummary* locations =
3014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003015 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3016 locations->SetInAt(i, Location::Any());
3017 }
3018 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019}
3020
3021void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003022 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003023 LOG(FATAL) << "Unimplemented";
3024}
3025
Calin Juravle52c48962014-12-16 17:02:57 +00003026void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3027 /*
3028 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3029 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3030 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3031 */
3032 switch (kind) {
3033 case MemBarrierKind::kAnyAny: {
3034 __ mfence();
3035 break;
3036 }
3037 case MemBarrierKind::kAnyStore:
3038 case MemBarrierKind::kLoadAny:
3039 case MemBarrierKind::kStoreStore: {
3040 // nop
3041 break;
3042 }
3043 default:
3044 LOG(FATAL) << "Unexpected memory barier " << kind;
3045 }
3046}
3047
3048void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3049 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3050
Nicolas Geoffray39468442014-09-02 15:17:15 +01003051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003053 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003054 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3055 locations->SetOut(Location::RequiresFpuRegister());
3056 } else {
3057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3058 }
Calin Juravle52c48962014-12-16 17:02:57 +00003059}
3060
3061void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3062 const FieldInfo& field_info) {
3063 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3064
3065 LocationSummary* locations = instruction->GetLocations();
3066 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3067 Location out = locations->Out();
3068 bool is_volatile = field_info.IsVolatile();
3069 Primitive::Type field_type = field_info.GetFieldType();
3070 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3071
3072 switch (field_type) {
3073 case Primitive::kPrimBoolean: {
3074 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3075 break;
3076 }
3077
3078 case Primitive::kPrimByte: {
3079 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3080 break;
3081 }
3082
3083 case Primitive::kPrimShort: {
3084 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3085 break;
3086 }
3087
3088 case Primitive::kPrimChar: {
3089 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3090 break;
3091 }
3092
3093 case Primitive::kPrimInt:
3094 case Primitive::kPrimNot: {
3095 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3096 break;
3097 }
3098
3099 case Primitive::kPrimLong: {
3100 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3101 break;
3102 }
3103
3104 case Primitive::kPrimFloat: {
3105 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3106 break;
3107 }
3108
3109 case Primitive::kPrimDouble: {
3110 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3111 break;
3112 }
3113
3114 case Primitive::kPrimVoid:
3115 LOG(FATAL) << "Unreachable type " << field_type;
3116 UNREACHABLE();
3117 }
3118
Calin Juravle77520bc2015-01-12 18:45:46 +00003119 codegen_->MaybeRecordImplicitNullCheck(instruction);
3120
Calin Juravle52c48962014-12-16 17:02:57 +00003121 if (is_volatile) {
3122 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3123 }
3124}
3125
3126void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3127 const FieldInfo& field_info) {
3128 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3129
3130 LocationSummary* locations =
3131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003132 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003133 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3134
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003135 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003136 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3137 locations->SetInAt(1, Location::RequiresFpuRegister());
3138 } else {
3139 locations->SetInAt(1, Location::RequiresRegister());
3140 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003141 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003142 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003143 locations->AddTemp(Location::RequiresRegister());
3144 locations->AddTemp(Location::RequiresRegister());
3145 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146}
3147
Calin Juravle52c48962014-12-16 17:02:57 +00003148void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3149 const FieldInfo& field_info) {
3150 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3151
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003152 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003153 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3154 Location value = locations->InAt(1);
3155 bool is_volatile = field_info.IsVolatile();
3156 Primitive::Type field_type = field_info.GetFieldType();
3157 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3158
3159 if (is_volatile) {
3160 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3161 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003162
3163 switch (field_type) {
3164 case Primitive::kPrimBoolean:
3165 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003166 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003167 break;
3168 }
3169
3170 case Primitive::kPrimShort:
3171 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003172 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003173 break;
3174 }
3175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003176 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003177 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003178 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003179 break;
3180 }
3181
3182 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003183 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184 break;
3185 }
3186
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003187 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003188 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003189 break;
3190 }
3191
3192 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003193 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003194 break;
3195 }
3196
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003197 case Primitive::kPrimVoid:
3198 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003199 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003200 }
Calin Juravle52c48962014-12-16 17:02:57 +00003201
Calin Juravle77520bc2015-01-12 18:45:46 +00003202 codegen_->MaybeRecordImplicitNullCheck(instruction);
3203
3204 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3205 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3206 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3207 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3208 }
3209
Calin Juravle52c48962014-12-16 17:02:57 +00003210 if (is_volatile) {
3211 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3212 }
3213}
3214
3215void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3216 HandleFieldSet(instruction, instruction->GetFieldInfo());
3217}
3218
3219void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3220 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003221}
3222
3223void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003224 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003225}
3226
3227void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003228 HandleFieldGet(instruction, instruction->GetFieldInfo());
3229}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003230
Calin Juravle52c48962014-12-16 17:02:57 +00003231void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3232 HandleFieldGet(instruction);
3233}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003234
Calin Juravle52c48962014-12-16 17:02:57 +00003235void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3236 HandleFieldGet(instruction, instruction->GetFieldInfo());
3237}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238
Calin Juravle52c48962014-12-16 17:02:57 +00003239void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3240 HandleFieldSet(instruction, instruction->GetFieldInfo());
3241}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003242
Calin Juravle52c48962014-12-16 17:02:57 +00003243void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3244 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003245}
3246
3247void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003248 LocationSummary* locations =
3249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003250 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3251 ? Location::RequiresRegister()
3252 : Location::Any();
3253 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003254 if (instruction->HasUses()) {
3255 locations->SetOut(Location::SameAsFirstInput());
3256 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003257}
3258
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003259void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003260 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3261 return;
3262 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003263 LocationSummary* locations = instruction->GetLocations();
3264 Location obj = locations->InAt(0);
3265
3266 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3267 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3268}
3269
3270void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003271 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003272 codegen_->AddSlowPath(slow_path);
3273
3274 LocationSummary* locations = instruction->GetLocations();
3275 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003276
3277 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003278 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003279 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003281 } else {
3282 DCHECK(obj.IsConstant()) << obj;
3283 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3284 __ jmp(slow_path->GetEntryLabel());
3285 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003286 }
3287 __ j(kEqual, slow_path->GetEntryLabel());
3288}
3289
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003290void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3291 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3292 GenerateImplicitNullCheck(instruction);
3293 } else {
3294 GenerateExplicitNullCheck(instruction);
3295 }
3296}
3297
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003298void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003299 LocationSummary* locations =
3300 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003301 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003302 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003303 1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003304 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3305 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3306 } else {
3307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3308 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003309}
3310
3311void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3312 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003313 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003314 Location index = locations->InAt(1);
3315
3316 switch (instruction->GetType()) {
3317 case Primitive::kPrimBoolean: {
3318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003320 if (index.IsConstant()) {
3321 __ movzxb(out, Address(obj,
3322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3323 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003325 }
3326 break;
3327 }
3328
3329 case Primitive::kPrimByte: {
3330 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003332 if (index.IsConstant()) {
3333 __ movsxb(out, Address(obj,
3334 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3335 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 }
3338 break;
3339 }
3340
3341 case Primitive::kPrimShort: {
3342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003344 if (index.IsConstant()) {
3345 __ movsxw(out, Address(obj,
3346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3347 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003348 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003349 }
3350 break;
3351 }
3352
3353 case Primitive::kPrimChar: {
3354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003356 if (index.IsConstant()) {
3357 __ movzxw(out, Address(obj,
3358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3359 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003360 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003361 }
3362 break;
3363 }
3364
3365 case Primitive::kPrimInt:
3366 case Primitive::kPrimNot: {
3367 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3368 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003369 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370 if (index.IsConstant()) {
3371 __ movl(out, Address(obj,
3372 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3373 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003374 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003375 }
3376 break;
3377 }
3378
3379 case Primitive::kPrimLong: {
3380 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003381 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003382 if (index.IsConstant()) {
3383 __ movq(out, Address(obj,
3384 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3385 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003386 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003387 }
3388 break;
3389 }
3390
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003391 case Primitive::kPrimFloat: {
3392 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003393 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003394 if (index.IsConstant()) {
3395 __ movss(out, Address(obj,
3396 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3397 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003398 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003399 }
3400 break;
3401 }
3402
3403 case Primitive::kPrimDouble: {
3404 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003405 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003406 if (index.IsConstant()) {
3407 __ movsd(out, Address(obj,
3408 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3409 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003410 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003411 }
3412 break;
3413 }
3414
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003415 case Primitive::kPrimVoid:
3416 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003417 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003418 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003419 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003420}
3421
3422void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003423 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003424
3425 bool needs_write_barrier =
3426 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3427 bool needs_runtime_call = instruction->NeedsTypeCheck();
3428
Nicolas Geoffray39468442014-09-02 15:17:15 +01003429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003430 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3431 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003432 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003433 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3434 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3435 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003436 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003437 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003438 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003439 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3440 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003441 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003442 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003443 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3444 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003445 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003446 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003447 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003448
3449 if (needs_write_barrier) {
3450 // Temporary registers for the write barrier.
3451 locations->AddTemp(Location::RequiresRegister());
3452 locations->AddTemp(Location::RequiresRegister());
3453 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003454 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003455}
3456
3457void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3458 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003459 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003460 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003461 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003462 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003463 bool needs_runtime_call = locations->WillCall();
3464 bool needs_write_barrier =
3465 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466
3467 switch (value_type) {
3468 case Primitive::kPrimBoolean:
3469 case Primitive::kPrimByte: {
3470 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003471 if (index.IsConstant()) {
3472 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003473 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003474 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003475 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003476 __ movb(Address(obj, offset),
3477 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003478 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003479 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003480 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3482 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003483 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003485 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3486 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003487 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003488 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003489 break;
3490 }
3491
3492 case Primitive::kPrimShort:
3493 case Primitive::kPrimChar: {
3494 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003495 if (index.IsConstant()) {
3496 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003497 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003498 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003499 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003500 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003501 __ movw(Address(obj, offset),
3502 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003503 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003504 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003505 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003506 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003507 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3508 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003509 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003510 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003511 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003512 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3513 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003514 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003515 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003516 break;
3517 }
3518
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003519 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003520 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003521 if (!needs_runtime_call) {
3522 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3523 if (index.IsConstant()) {
3524 size_t offset =
3525 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3526 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003527 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003528 } else {
3529 DCHECK(value.IsConstant()) << value;
3530 __ movl(Address(obj, offset),
3531 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3532 }
3533 } else {
3534 DCHECK(index.IsRegister()) << index;
3535 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3537 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003538 } else {
3539 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003541 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3542 }
3543 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003544 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003545 if (needs_write_barrier) {
3546 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003547 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3548 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3549 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003550 }
3551 } else {
3552 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003553 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3554 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003555 DCHECK(!codegen_->IsLeafMethod());
3556 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3557 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003558 break;
3559 }
3560
3561 case Primitive::kPrimLong: {
3562 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003563 if (index.IsConstant()) {
3564 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003565 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003568 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003569 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3570 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003571 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003572 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573 break;
3574 }
3575
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003576 case Primitive::kPrimFloat: {
3577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3578 if (index.IsConstant()) {
3579 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3580 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003581 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003582 } else {
3583 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003584 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3585 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003586 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003587 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003588 break;
3589 }
3590
3591 case Primitive::kPrimDouble: {
3592 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3593 if (index.IsConstant()) {
3594 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3595 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003596 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003597 } else {
3598 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003599 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3600 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003601 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003602 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003603 break;
3604 }
3605
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 case Primitive::kPrimVoid:
3607 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003608 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003609 }
3610}
3611
3612void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003613 LocationSummary* locations =
3614 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003615 locations->SetInAt(0, Location::RequiresRegister());
3616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003617}
3618
3619void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3620 LocationSummary* locations = instruction->GetLocations();
3621 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003622 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3623 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003624 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003625 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003626}
3627
3628void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003629 LocationSummary* locations =
3630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003631 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003633 if (instruction->HasUses()) {
3634 locations->SetOut(Location::SameAsFirstInput());
3635 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003636}
3637
3638void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3639 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003640 Location index_loc = locations->InAt(0);
3641 Location length_loc = locations->InAt(1);
3642 SlowPathCodeX86_64* slow_path =
3643 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003644 codegen_->AddSlowPath(slow_path);
3645
Mark Mendellf60c90b2015-03-04 15:12:59 -05003646 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3647 if (index_loc.IsConstant()) {
3648 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3649 __ cmpl(length, Immediate(value));
3650 } else {
3651 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3652 }
3653 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003654}
3655
3656void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3657 CpuRegister card,
3658 CpuRegister object,
3659 CpuRegister value) {
3660 Label is_null;
3661 __ testl(value, value);
3662 __ j(kEqual, &is_null);
3663 __ gs()->movq(card, Address::Absolute(
3664 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3665 __ movq(temp, object);
3666 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3667 __ movb(Address(temp, card, TIMES_1, 0), card);
3668 __ Bind(&is_null);
3669}
3670
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003671void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3672 temp->SetLocations(nullptr);
3673}
3674
3675void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3676 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003677 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003678}
3679
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003680void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003681 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003682 LOG(FATAL) << "Unimplemented";
3683}
3684
3685void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003686 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3687}
3688
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003689void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3691}
3692
3693void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003694 HBasicBlock* block = instruction->GetBlock();
3695 if (block->GetLoopInformation() != nullptr) {
3696 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3697 // The back edge will generate the suspend check.
3698 return;
3699 }
3700 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3701 // The goto will generate the suspend check.
3702 return;
3703 }
3704 GenerateSuspendCheck(instruction, nullptr);
3705}
3706
3707void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3708 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003709 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003710 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003711 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003712 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003713 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003714 if (successor == nullptr) {
3715 __ j(kNotEqual, slow_path->GetEntryLabel());
3716 __ Bind(slow_path->GetReturnLabel());
3717 } else {
3718 __ j(kEqual, codegen_->GetLabelOf(successor));
3719 __ jmp(slow_path->GetEntryLabel());
3720 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003721}
3722
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003723X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3724 return codegen_->GetAssembler();
3725}
3726
3727void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3728 MoveOperands* move = moves_.Get(index);
3729 Location source = move->GetSource();
3730 Location destination = move->GetDestination();
3731
3732 if (source.IsRegister()) {
3733 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003734 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003735 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003736 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003737 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003738 } else {
3739 DCHECK(destination.IsDoubleStackSlot());
3740 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003742 }
3743 } else if (source.IsStackSlot()) {
3744 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003746 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003747 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003748 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003749 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003750 } else {
3751 DCHECK(destination.IsStackSlot());
3752 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3753 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3754 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003755 } else if (source.IsDoubleStackSlot()) {
3756 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003757 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003758 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003759 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003760 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3761 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003762 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003763 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003764 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3765 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3766 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003767 } else if (source.IsConstant()) {
3768 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003769 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3770 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003771 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003772 if (value == 0) {
3773 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3774 } else {
3775 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3776 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003777 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003778 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003779 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003780 }
3781 } else if (constant->IsLongConstant()) {
3782 int64_t value = constant->AsLongConstant()->GetValue();
3783 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003784 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003785 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003786 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003787 __ movq(CpuRegister(TMP), Immediate(value));
3788 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3789 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003790 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003791 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003792 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003793 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003794 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003795 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3796 if (value == 0) {
3797 // easy FP 0.0.
3798 __ xorps(dest, dest);
3799 } else {
3800 __ movl(CpuRegister(TMP), imm);
3801 __ movd(dest, CpuRegister(TMP));
3802 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003803 } else {
3804 DCHECK(destination.IsStackSlot()) << destination;
3805 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3806 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003807 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003808 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003809 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003810 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003811 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003812 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003813 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3814 if (value == 0) {
3815 __ xorpd(dest, dest);
3816 } else {
3817 __ movq(CpuRegister(TMP), imm);
3818 __ movd(dest, CpuRegister(TMP));
3819 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003820 } else {
3821 DCHECK(destination.IsDoubleStackSlot()) << destination;
3822 __ movq(CpuRegister(TMP), imm);
3823 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3824 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003825 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003826 } else if (source.IsFpuRegister()) {
3827 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003829 } else if (destination.IsStackSlot()) {
3830 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003831 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003832 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003833 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003834 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003836 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003837 }
3838}
3839
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003840void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003841 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003842 __ movl(Address(CpuRegister(RSP), mem), reg);
3843 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003844}
3845
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003846void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003847 ScratchRegisterScope ensure_scratch(
3848 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3849
3850 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3851 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3852 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3853 Address(CpuRegister(RSP), mem2 + stack_offset));
3854 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3855 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3856 CpuRegister(ensure_scratch.GetRegister()));
3857}
3858
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003859void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3860 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3861 __ movq(Address(CpuRegister(RSP), mem), reg);
3862 __ movq(reg, CpuRegister(TMP));
3863}
3864
3865void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3866 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003867 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003868
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003869 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3870 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3871 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3872 Address(CpuRegister(RSP), mem2 + stack_offset));
3873 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3874 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3875 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003876}
3877
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003878void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3879 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3880 __ movss(Address(CpuRegister(RSP), mem), reg);
3881 __ movd(reg, CpuRegister(TMP));
3882}
3883
3884void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3885 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3886 __ movsd(Address(CpuRegister(RSP), mem), reg);
3887 __ movd(reg, CpuRegister(TMP));
3888}
3889
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003890void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3891 MoveOperands* move = moves_.Get(index);
3892 Location source = move->GetSource();
3893 Location destination = move->GetDestination();
3894
3895 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003896 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003897 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003898 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003899 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003900 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003901 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003902 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3903 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003905 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003907 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3908 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003909 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003910 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3911 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3912 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003913 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003914 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003915 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003916 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003917 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003918 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003919 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003920 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003921 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003922 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003923 }
3924}
3925
3926
3927void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3928 __ pushq(CpuRegister(reg));
3929}
3930
3931
3932void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3933 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003934}
3935
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003936void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3937 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3938 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3939 Immediate(mirror::Class::kStatusInitialized));
3940 __ j(kLess, slow_path->GetEntryLabel());
3941 __ Bind(slow_path->GetExitLabel());
3942 // No need for memory fence, thanks to the X86_64 memory model.
3943}
3944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003945void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003946 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3947 ? LocationSummary::kCallOnSlowPath
3948 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003949 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003950 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003951 locations->SetOut(Location::RequiresRegister());
3952}
3953
3954void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003955 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003956 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003957 DCHECK(!cls->CanCallRuntime());
3958 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003959 codegen_->LoadCurrentMethod(out);
3960 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3961 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003962 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003963 codegen_->LoadCurrentMethod(out);
3964 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3965 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003966 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3967 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3968 codegen_->AddSlowPath(slow_path);
3969 __ testl(out, out);
3970 __ j(kEqual, slow_path->GetEntryLabel());
3971 if (cls->MustGenerateClinitCheck()) {
3972 GenerateClassInitializationCheck(slow_path, out);
3973 } else {
3974 __ Bind(slow_path->GetExitLabel());
3975 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003976 }
3977}
3978
3979void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3980 LocationSummary* locations =
3981 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3982 locations->SetInAt(0, Location::RequiresRegister());
3983 if (check->HasUses()) {
3984 locations->SetOut(Location::SameAsFirstInput());
3985 }
3986}
3987
3988void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003989 // We assume the class to not be null.
3990 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3991 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003992 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003993 GenerateClassInitializationCheck(slow_path,
3994 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003995}
3996
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003997void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3998 LocationSummary* locations =
3999 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4000 locations->SetOut(Location::RequiresRegister());
4001}
4002
4003void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4004 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4005 codegen_->AddSlowPath(slow_path);
4006
Roland Levillain271ab9c2014-11-27 15:23:57 +00004007 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004008 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004009 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4010 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004011 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4012 __ testl(out, out);
4013 __ j(kEqual, slow_path->GetEntryLabel());
4014 __ Bind(slow_path->GetExitLabel());
4015}
4016
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004017void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4018 LocationSummary* locations =
4019 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4020 locations->SetOut(Location::RequiresRegister());
4021}
4022
4023void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4024 Address address = Address::Absolute(
4025 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004026 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004027 __ gs()->movl(address, Immediate(0));
4028}
4029
4030void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4031 LocationSummary* locations =
4032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4033 InvokeRuntimeCallingConvention calling_convention;
4034 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4035}
4036
4037void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4038 __ gs()->call(
4039 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4040 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4041}
4042
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004043void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004044 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4045 ? LocationSummary::kNoCall
4046 : LocationSummary::kCallOnSlowPath;
4047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4048 locations->SetInAt(0, Location::RequiresRegister());
4049 locations->SetInAt(1, Location::Any());
4050 locations->SetOut(Location::RequiresRegister());
4051}
4052
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004053void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004054 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004055 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004056 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004057 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004058 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4059 Label done, zero;
4060 SlowPathCodeX86_64* slow_path = nullptr;
4061
4062 // Return 0 if `obj` is null.
4063 // TODO: avoid this check if we know obj is not null.
4064 __ testl(obj, obj);
4065 __ j(kEqual, &zero);
4066 // Compare the class of `obj` with `cls`.
4067 __ movl(out, Address(obj, class_offset));
4068 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004069 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004070 } else {
4071 DCHECK(cls.IsStackSlot()) << cls;
4072 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4073 }
4074 if (instruction->IsClassFinal()) {
4075 // Classes must be equal for the instanceof to succeed.
4076 __ j(kNotEqual, &zero);
4077 __ movl(out, Immediate(1));
4078 __ jmp(&done);
4079 } else {
4080 // If the classes are not equal, we go into a slow path.
4081 DCHECK(locations->OnlyCallsOnSlowPath());
4082 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004083 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004084 codegen_->AddSlowPath(slow_path);
4085 __ j(kNotEqual, slow_path->GetEntryLabel());
4086 __ movl(out, Immediate(1));
4087 __ jmp(&done);
4088 }
4089 __ Bind(&zero);
4090 __ movl(out, Immediate(0));
4091 if (slow_path != nullptr) {
4092 __ Bind(slow_path->GetExitLabel());
4093 }
4094 __ Bind(&done);
4095}
4096
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004097void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4099 instruction, LocationSummary::kCallOnSlowPath);
4100 locations->SetInAt(0, Location::RequiresRegister());
4101 locations->SetInAt(1, Location::Any());
4102 locations->AddTemp(Location::RequiresRegister());
4103}
4104
4105void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4106 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004108 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004110 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4111 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4112 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4113 codegen_->AddSlowPath(slow_path);
4114
4115 // TODO: avoid this check if we know obj is not null.
4116 __ testl(obj, obj);
4117 __ j(kEqual, slow_path->GetExitLabel());
4118 // Compare the class of `obj` with `cls`.
4119 __ movl(temp, Address(obj, class_offset));
4120 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004121 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004122 } else {
4123 DCHECK(cls.IsStackSlot()) << cls;
4124 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4125 }
4126 // Classes must be equal for the checkcast to succeed.
4127 __ j(kNotEqual, slow_path->GetEntryLabel());
4128 __ Bind(slow_path->GetExitLabel());
4129}
4130
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004131void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4132 LocationSummary* locations =
4133 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4134 InvokeRuntimeCallingConvention calling_convention;
4135 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4136}
4137
4138void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4139 __ gs()->call(Address::Absolute(instruction->IsEnter()
4140 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4141 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4142 true));
4143 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4144}
4145
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004146void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4147void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4148void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4149
4150void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4151 LocationSummary* locations =
4152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4153 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4154 || instruction->GetResultType() == Primitive::kPrimLong);
4155 locations->SetInAt(0, Location::RequiresRegister());
4156 if (instruction->GetType() == Primitive::kPrimInt) {
4157 locations->SetInAt(1, Location::Any());
4158 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004159 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004160 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004161 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004162 }
4163 locations->SetOut(Location::SameAsFirstInput());
4164}
4165
4166void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4167 HandleBitwiseOperation(instruction);
4168}
4169
4170void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4171 HandleBitwiseOperation(instruction);
4172}
4173
4174void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4175 HandleBitwiseOperation(instruction);
4176}
4177
4178void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4179 LocationSummary* locations = instruction->GetLocations();
4180 Location first = locations->InAt(0);
4181 Location second = locations->InAt(1);
4182 DCHECK(first.Equals(locations->Out()));
4183
4184 if (instruction->GetResultType() == Primitive::kPrimInt) {
4185 if (second.IsRegister()) {
4186 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004187 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004188 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004190 } else {
4191 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004192 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004193 }
4194 } else if (second.IsConstant()) {
4195 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4196 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004197 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004198 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004199 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004200 } else {
4201 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004202 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004203 }
4204 } else {
4205 Address address(CpuRegister(RSP), second.GetStackIndex());
4206 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004207 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004208 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004209 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004210 } else {
4211 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004212 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004213 }
4214 }
4215 } else {
4216 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004217 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4218 bool second_is_constant = false;
4219 int64_t value = 0;
4220 if (second.IsConstant()) {
4221 second_is_constant = true;
4222 value = second.GetConstant()->AsLongConstant()->GetValue();
4223 DCHECK(IsInt<32>(value));
4224 }
4225
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004226 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004227 if (second_is_constant) {
4228 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4229 } else {
4230 __ andq(first_reg, second.AsRegister<CpuRegister>());
4231 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004232 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004233 if (second_is_constant) {
4234 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4235 } else {
4236 __ orq(first_reg, second.AsRegister<CpuRegister>());
4237 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004238 } else {
4239 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004240 if (second_is_constant) {
4241 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4242 } else {
4243 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4244 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004245 }
4246 }
4247}
4248
Calin Juravleb1498f62015-02-16 13:13:29 +00004249void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4250 // Nothing to do, this should be removed during prepare for register allocator.
4251 UNUSED(instruction);
4252 LOG(FATAL) << "Unreachable";
4253}
4254
4255void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4256 // Nothing to do, this should be removed during prepare for register allocator.
4257 UNUSED(instruction);
4258 LOG(FATAL) << "Unreachable";
4259}
4260
Mark Mendellf55c3e02015-03-26 21:07:46 -04004261void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4262 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004263 X86_64Assembler* assembler = GetAssembler();
4264 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004265 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4266 // byte values. If used for vectors at a later time, this will need to be
4267 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004268 assembler->Align(4, 0);
4269 constant_area_start_ = assembler->CodeSize();
4270 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004271 }
4272
4273 // And finish up.
4274 CodeGenerator::Finalize(allocator);
4275}
4276
4277/**
4278 * Class to handle late fixup of offsets into constant area.
4279 */
4280class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4281 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004282 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004283 : codegen_(codegen), offset_into_constant_area_(offset) {}
4284
4285 private:
4286 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4287 // Patch the correct offset for the instruction. We use the address of the
4288 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4289 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4290 int relative_position = constant_offset - pos;
4291
4292 // Patch in the right value.
4293 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4294 }
4295
Mark Mendell39dcf552015-04-09 20:42:42 -04004296 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004297
4298 // Location in constant area that the fixup refers to.
4299 int offset_into_constant_area_;
4300};
4301
4302Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4303 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4304 return Address::RIP(fixup);
4305}
4306
4307Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4308 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4309 return Address::RIP(fixup);
4310}
4311
4312Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4313 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4314 return Address::RIP(fixup);
4315}
4316
4317Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4318 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4319 return Address::RIP(fixup);
4320}
4321
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004322} // namespace x86_64
4323} // namespace art