blob: b404f8de3dd5194c960a2c4620e57e3e37c2f78f [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 Mendell40741f32015-04-20 22:10:34 -04001026 locations->SetInAt(1, Location::Any());
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());
Mark Mendell40741f32015-04-20 22:10:34 -04001033 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001034 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();
Mark Mendell40741f32015-04-20 22:10:34 -04001055 if (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 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001061 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001062 // Value won't fit in an int.
1063 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001064 }
Mark Mendell40741f32015-04-20 22:10:34 -04001065 } else if (right.IsDoubleStackSlot()) {
1066 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001067 } else {
1068 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1069 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001070 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001071 }
1072 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001073 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1074 if (right.IsConstant()) {
1075 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1076 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1077 } else if (right.IsStackSlot()) {
1078 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1079 } else {
1080 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1081 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001082 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1083 break;
1084 }
1085 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001086 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1087 if (right.IsConstant()) {
1088 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1089 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1090 } else if (right.IsDoubleStackSlot()) {
1091 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1092 } else {
1093 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1094 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001095 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1096 break;
1097 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001098 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001099 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001100 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001101 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001102 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001103 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001104
Calin Juravle91debbc2014-11-26 19:01:09 +00001105 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001106 __ movl(out, Immediate(1));
1107 __ jmp(&done);
1108
1109 __ Bind(&less);
1110 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001111
1112 __ Bind(&done);
1113}
1114
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001115void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* 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::VisitIntConstant(HIntConstant* 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 Geoffrayd6138ef2015-02-18 14:48:53 +00001126void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1127 LocationSummary* locations =
1128 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1129 locations->SetOut(Location::ConstantLocation(constant));
1130}
1131
1132void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1133 // Will be generated at use site.
1134 UNUSED(constant);
1135}
1136
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001138 LocationSummary* locations =
1139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001140 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141}
1142
1143void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001144 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001145 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001146}
1147
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001148void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1149 LocationSummary* locations =
1150 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1151 locations->SetOut(Location::ConstantLocation(constant));
1152}
1153
1154void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1155 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001156 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001157}
1158
1159void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1160 LocationSummary* locations =
1161 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1162 locations->SetOut(Location::ConstantLocation(constant));
1163}
1164
1165void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1166 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001167 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001168}
1169
Calin Juravle27df7582015-04-17 19:12:31 +01001170void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1171 memory_barrier->SetLocations(nullptr);
1172}
1173
1174void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1175 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1176}
1177
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001178void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1179 ret->SetLocations(nullptr);
1180}
1181
1182void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001183 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001184 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185}
1186
1187void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001188 LocationSummary* locations =
1189 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190 switch (ret->InputAt(0)->GetType()) {
1191 case Primitive::kPrimBoolean:
1192 case Primitive::kPrimByte:
1193 case Primitive::kPrimChar:
1194 case Primitive::kPrimShort:
1195 case Primitive::kPrimInt:
1196 case Primitive::kPrimNot:
1197 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001198 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001199 break;
1200
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001201 case Primitive::kPrimFloat:
1202 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001203 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 break;
1205
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001208 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001209}
1210
1211void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1212 if (kIsDebugBuild) {
1213 switch (ret->InputAt(0)->GetType()) {
1214 case Primitive::kPrimBoolean:
1215 case Primitive::kPrimByte:
1216 case Primitive::kPrimChar:
1217 case Primitive::kPrimShort:
1218 case Primitive::kPrimInt:
1219 case Primitive::kPrimNot:
1220 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001222 break;
1223
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 case Primitive::kPrimFloat:
1225 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 XMM0);
1228 break;
1229
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001232 }
1233 }
1234 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001235}
1236
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001237Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1238 switch (type) {
1239 case Primitive::kPrimBoolean:
1240 case Primitive::kPrimByte:
1241 case Primitive::kPrimChar:
1242 case Primitive::kPrimShort:
1243 case Primitive::kPrimInt:
1244 case Primitive::kPrimNot: {
1245 uint32_t index = gp_index_++;
1246 stack_index_++;
1247 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001248 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 } else {
1250 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1251 }
1252 }
1253
1254 case Primitive::kPrimLong: {
1255 uint32_t index = gp_index_;
1256 stack_index_ += 2;
1257 if (index < calling_convention.GetNumberOfRegisters()) {
1258 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001259 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 } else {
1261 gp_index_ += 2;
1262 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1263 }
1264 }
1265
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001266 case Primitive::kPrimFloat: {
1267 uint32_t index = fp_index_++;
1268 stack_index_++;
1269 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001270 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001271 } else {
1272 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1273 }
1274 }
1275
1276 case Primitive::kPrimDouble: {
1277 uint32_t index = fp_index_++;
1278 stack_index_ += 2;
1279 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001280 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001281 } else {
1282 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1283 }
1284 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001285
1286 case Primitive::kPrimVoid:
1287 LOG(FATAL) << "Unexpected parameter type " << type;
1288 break;
1289 }
1290 return Location();
1291}
1292
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001293void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001294 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001295 if (intrinsic.TryDispatch(invoke)) {
1296 return;
1297 }
1298
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001299 HandleInvoke(invoke);
1300}
1301
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001302static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1303 if (invoke->GetLocations()->Intrinsified()) {
1304 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1305 intrinsic.Dispatch(invoke);
1306 return true;
1307 }
1308 return false;
1309}
1310
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001311void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001312 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1313 return;
1314 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001315
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001316 codegen_->GenerateStaticOrDirectCall(
1317 invoke,
1318 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001319 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001320}
1321
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001322void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001323 LocationSummary* locations =
1324 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001325 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001326
1327 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001328 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001329 HInstruction* input = invoke->InputAt(i);
1330 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1331 }
1332
1333 switch (invoke->GetType()) {
1334 case Primitive::kPrimBoolean:
1335 case Primitive::kPrimByte:
1336 case Primitive::kPrimChar:
1337 case Primitive::kPrimShort:
1338 case Primitive::kPrimInt:
1339 case Primitive::kPrimNot:
1340 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001341 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001342 break;
1343
1344 case Primitive::kPrimVoid:
1345 break;
1346
1347 case Primitive::kPrimDouble:
1348 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001349 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001350 break;
1351 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001352}
1353
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001354void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001355 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001356 if (intrinsic.TryDispatch(invoke)) {
1357 return;
1358 }
1359
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001360 HandleInvoke(invoke);
1361}
1362
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001363void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001364 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1365 return;
1366 }
1367
Roland Levillain271ab9c2014-11-27 15:23:57 +00001368 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001369 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1370 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1371 LocationSummary* locations = invoke->GetLocations();
1372 Location receiver = locations->InAt(0);
1373 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1374 // temp = object->GetClass();
1375 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1377 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001378 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001379 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001380 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001381 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001382 // temp = temp->GetMethodAt(method_offset);
1383 __ movl(temp, Address(temp, method_offset));
1384 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001385 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001386 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001387
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001388 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001389 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001390}
1391
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001392void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1393 HandleInvoke(invoke);
1394 // Add the hidden argument.
1395 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1396}
1397
1398void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1399 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001401 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1402 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1403 LocationSummary* locations = invoke->GetLocations();
1404 Location receiver = locations->InAt(0);
1405 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1406
1407 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001408 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001409 Immediate(invoke->GetDexMethodIndex()));
1410
1411 // temp = object->GetClass();
1412 if (receiver.IsStackSlot()) {
1413 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1414 __ movl(temp, Address(temp, class_offset));
1415 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001416 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001417 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001418 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001419 // temp = temp->GetImtEntryAt(method_offset);
1420 __ movl(temp, Address(temp, method_offset));
1421 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001422 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001423 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001424
1425 DCHECK(!codegen_->IsLeafMethod());
1426 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1427}
1428
Roland Levillain88cb1752014-10-20 16:36:47 +01001429void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1430 LocationSummary* locations =
1431 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1432 switch (neg->GetResultType()) {
1433 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001434 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001435 locations->SetInAt(0, Location::RequiresRegister());
1436 locations->SetOut(Location::SameAsFirstInput());
1437 break;
1438
Roland Levillain88cb1752014-10-20 16:36:47 +01001439 case Primitive::kPrimFloat:
1440 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001441 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001442 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001443 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001444 break;
1445
1446 default:
1447 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1448 }
1449}
1450
1451void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1452 LocationSummary* locations = neg->GetLocations();
1453 Location out = locations->Out();
1454 Location in = locations->InAt(0);
1455 switch (neg->GetResultType()) {
1456 case Primitive::kPrimInt:
1457 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001458 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001459 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001460 break;
1461
1462 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001463 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001464 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001465 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001466 break;
1467
Roland Levillain5368c212014-11-27 15:03:41 +00001468 case Primitive::kPrimFloat: {
1469 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001470 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001471 // Implement float negation with an exclusive or with value
1472 // 0x80000000 (mask for bit 31, representing the sign of a
1473 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001474 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001475 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001476 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001477 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001478
Roland Levillain5368c212014-11-27 15:03:41 +00001479 case Primitive::kPrimDouble: {
1480 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001481 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001482 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001483 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001484 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001485 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001486 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001487 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001488 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001489
1490 default:
1491 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1492 }
1493}
1494
Roland Levillaindff1f282014-11-05 14:15:05 +00001495void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1496 LocationSummary* locations =
1497 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1498 Primitive::Type result_type = conversion->GetResultType();
1499 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001500 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001501
David Brazdilb2bd1c52015-03-25 11:17:37 +00001502 // The Java language does not allow treating boolean as an integral type but
1503 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001504
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001506 case Primitive::kPrimByte:
1507 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001508 case Primitive::kPrimBoolean:
1509 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001510 case Primitive::kPrimShort:
1511 case Primitive::kPrimInt:
1512 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001513 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001514 locations->SetInAt(0, Location::Any());
1515 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1516 break;
1517
1518 default:
1519 LOG(FATAL) << "Unexpected type conversion from " << input_type
1520 << " to " << result_type;
1521 }
1522 break;
1523
Roland Levillain01a8d712014-11-14 16:27:39 +00001524 case Primitive::kPrimShort:
1525 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001526 case Primitive::kPrimBoolean:
1527 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001528 case Primitive::kPrimByte:
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimChar:
1531 // Processing a Dex `int-to-short' instruction.
1532 locations->SetInAt(0, Location::Any());
1533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1534 break;
1535
1536 default:
1537 LOG(FATAL) << "Unexpected type conversion from " << input_type
1538 << " to " << result_type;
1539 }
1540 break;
1541
Roland Levillain946e1432014-11-11 17:35:19 +00001542 case Primitive::kPrimInt:
1543 switch (input_type) {
1544 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001545 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001546 locations->SetInAt(0, Location::Any());
1547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1548 break;
1549
1550 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001551 // Processing a Dex `float-to-int' instruction.
1552 locations->SetInAt(0, Location::RequiresFpuRegister());
1553 locations->SetOut(Location::RequiresRegister());
1554 locations->AddTemp(Location::RequiresFpuRegister());
1555 break;
1556
Roland Levillain946e1432014-11-11 17:35:19 +00001557 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001558 // Processing a Dex `double-to-int' instruction.
1559 locations->SetInAt(0, Location::RequiresFpuRegister());
1560 locations->SetOut(Location::RequiresRegister());
1561 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001562 break;
1563
1564 default:
1565 LOG(FATAL) << "Unexpected type conversion from " << input_type
1566 << " to " << result_type;
1567 }
1568 break;
1569
Roland Levillaindff1f282014-11-05 14:15:05 +00001570 case Primitive::kPrimLong:
1571 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001572 case Primitive::kPrimBoolean:
1573 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001574 case Primitive::kPrimByte:
1575 case Primitive::kPrimShort:
1576 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001577 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001578 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001579 // TODO: We would benefit from a (to-be-implemented)
1580 // Location::RegisterOrStackSlot requirement for this input.
1581 locations->SetInAt(0, Location::RequiresRegister());
1582 locations->SetOut(Location::RequiresRegister());
1583 break;
1584
1585 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001586 // Processing a Dex `float-to-long' instruction.
1587 locations->SetInAt(0, Location::RequiresFpuRegister());
1588 locations->SetOut(Location::RequiresRegister());
1589 locations->AddTemp(Location::RequiresFpuRegister());
1590 break;
1591
Roland Levillaindff1f282014-11-05 14:15:05 +00001592 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001593 // Processing a Dex `double-to-long' instruction.
1594 locations->SetInAt(0, Location::RequiresFpuRegister());
1595 locations->SetOut(Location::RequiresRegister());
1596 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001597 break;
1598
1599 default:
1600 LOG(FATAL) << "Unexpected type conversion from " << input_type
1601 << " to " << result_type;
1602 }
1603 break;
1604
Roland Levillain981e4542014-11-14 11:47:14 +00001605 case Primitive::kPrimChar:
1606 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001607 case Primitive::kPrimBoolean:
1608 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001609 case Primitive::kPrimByte:
1610 case Primitive::kPrimShort:
1611 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001612 // Processing a Dex `int-to-char' instruction.
1613 locations->SetInAt(0, Location::Any());
1614 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1615 break;
1616
1617 default:
1618 LOG(FATAL) << "Unexpected type conversion from " << input_type
1619 << " to " << result_type;
1620 }
1621 break;
1622
Roland Levillaindff1f282014-11-05 14:15:05 +00001623 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001624 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001625 case Primitive::kPrimBoolean:
1626 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001627 case Primitive::kPrimByte:
1628 case Primitive::kPrimShort:
1629 case Primitive::kPrimInt:
1630 case Primitive::kPrimChar:
1631 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001632 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001633 locations->SetOut(Location::RequiresFpuRegister());
1634 break;
1635
1636 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001637 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001638 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001639 locations->SetOut(Location::RequiresFpuRegister());
1640 break;
1641
Roland Levillaincff13742014-11-17 14:32:17 +00001642 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001643 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001644 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001645 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001646 break;
1647
1648 default:
1649 LOG(FATAL) << "Unexpected type conversion from " << input_type
1650 << " to " << result_type;
1651 };
1652 break;
1653
Roland Levillaindff1f282014-11-05 14:15:05 +00001654 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001655 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001656 case Primitive::kPrimBoolean:
1657 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001658 case Primitive::kPrimByte:
1659 case Primitive::kPrimShort:
1660 case Primitive::kPrimInt:
1661 case Primitive::kPrimChar:
1662 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001663 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001664 locations->SetOut(Location::RequiresFpuRegister());
1665 break;
1666
1667 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001668 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001669 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001670 locations->SetOut(Location::RequiresFpuRegister());
1671 break;
1672
Roland Levillaincff13742014-11-17 14:32:17 +00001673 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001674 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001675 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001676 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001677 break;
1678
1679 default:
1680 LOG(FATAL) << "Unexpected type conversion from " << input_type
1681 << " to " << result_type;
1682 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001683 break;
1684
1685 default:
1686 LOG(FATAL) << "Unexpected type conversion from " << input_type
1687 << " to " << result_type;
1688 }
1689}
1690
1691void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1692 LocationSummary* locations = conversion->GetLocations();
1693 Location out = locations->Out();
1694 Location in = locations->InAt(0);
1695 Primitive::Type result_type = conversion->GetResultType();
1696 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001697 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001698 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001699 case Primitive::kPrimByte:
1700 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001701 case Primitive::kPrimBoolean:
1702 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001703 case Primitive::kPrimShort:
1704 case Primitive::kPrimInt:
1705 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001706 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001707 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001708 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001709 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001710 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001711 Address(CpuRegister(RSP), in.GetStackIndex()));
1712 } else {
1713 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001714 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001715 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1716 }
1717 break;
1718
1719 default:
1720 LOG(FATAL) << "Unexpected type conversion from " << input_type
1721 << " to " << result_type;
1722 }
1723 break;
1724
Roland Levillain01a8d712014-11-14 16:27:39 +00001725 case Primitive::kPrimShort:
1726 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001727 case Primitive::kPrimBoolean:
1728 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001729 case Primitive::kPrimByte:
1730 case Primitive::kPrimInt:
1731 case Primitive::kPrimChar:
1732 // Processing a Dex `int-to-short' instruction.
1733 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001734 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001735 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001736 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001737 Address(CpuRegister(RSP), in.GetStackIndex()));
1738 } else {
1739 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001740 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001741 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1742 }
1743 break;
1744
1745 default:
1746 LOG(FATAL) << "Unexpected type conversion from " << input_type
1747 << " to " << result_type;
1748 }
1749 break;
1750
Roland Levillain946e1432014-11-11 17:35:19 +00001751 case Primitive::kPrimInt:
1752 switch (input_type) {
1753 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001754 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001755 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001757 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001759 Address(CpuRegister(RSP), in.GetStackIndex()));
1760 } else {
1761 DCHECK(in.IsConstant());
1762 DCHECK(in.GetConstant()->IsLongConstant());
1763 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001765 }
1766 break;
1767
Roland Levillain3f8f9362014-12-02 17:45:01 +00001768 case Primitive::kPrimFloat: {
1769 // Processing a Dex `float-to-int' instruction.
1770 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1771 CpuRegister output = out.AsRegister<CpuRegister>();
1772 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1773 Label done, nan;
1774
1775 __ movl(output, Immediate(kPrimIntMax));
1776 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001777 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001778 // if input >= temp goto done
1779 __ comiss(input, temp);
1780 __ j(kAboveEqual, &done);
1781 // if input == NaN goto nan
1782 __ j(kUnordered, &nan);
1783 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001784 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001785 __ jmp(&done);
1786 __ Bind(&nan);
1787 // output = 0
1788 __ xorl(output, output);
1789 __ Bind(&done);
1790 break;
1791 }
1792
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001793 case Primitive::kPrimDouble: {
1794 // Processing a Dex `double-to-int' instruction.
1795 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1796 CpuRegister output = out.AsRegister<CpuRegister>();
1797 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1798 Label done, nan;
1799
1800 __ movl(output, Immediate(kPrimIntMax));
1801 // temp = int-to-double(output)
1802 __ cvtsi2sd(temp, output);
1803 // if input >= temp goto done
1804 __ comisd(input, temp);
1805 __ j(kAboveEqual, &done);
1806 // if input == NaN goto nan
1807 __ j(kUnordered, &nan);
1808 // output = double-to-int-truncate(input)
1809 __ cvttsd2si(output, input);
1810 __ jmp(&done);
1811 __ Bind(&nan);
1812 // output = 0
1813 __ xorl(output, output);
1814 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001815 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001816 }
Roland Levillain946e1432014-11-11 17:35:19 +00001817
1818 default:
1819 LOG(FATAL) << "Unexpected type conversion from " << input_type
1820 << " to " << result_type;
1821 }
1822 break;
1823
Roland Levillaindff1f282014-11-05 14:15:05 +00001824 case Primitive::kPrimLong:
1825 switch (input_type) {
1826 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001827 case Primitive::kPrimBoolean:
1828 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001829 case Primitive::kPrimByte:
1830 case Primitive::kPrimShort:
1831 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001832 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001833 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001834 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001835 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001836 break;
1837
Roland Levillain624279f2014-12-04 11:54:28 +00001838 case Primitive::kPrimFloat: {
1839 // Processing a Dex `float-to-long' instruction.
1840 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1841 CpuRegister output = out.AsRegister<CpuRegister>();
1842 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1843 Label done, nan;
1844
1845 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001846 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001847 __ cvtsi2ss(temp, output, true);
1848 // if input >= temp goto done
1849 __ comiss(input, temp);
1850 __ j(kAboveEqual, &done);
1851 // if input == NaN goto nan
1852 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001853 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001854 __ cvttss2si(output, input, true);
1855 __ jmp(&done);
1856 __ Bind(&nan);
1857 // output = 0
1858 __ xorq(output, output);
1859 __ Bind(&done);
1860 break;
1861 }
1862
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001863 case Primitive::kPrimDouble: {
1864 // Processing a Dex `double-to-long' instruction.
1865 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1866 CpuRegister output = out.AsRegister<CpuRegister>();
1867 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1868 Label done, nan;
1869
1870 __ movq(output, Immediate(kPrimLongMax));
1871 // temp = long-to-double(output)
1872 __ cvtsi2sd(temp, output, true);
1873 // if input >= temp goto done
1874 __ comisd(input, temp);
1875 __ j(kAboveEqual, &done);
1876 // if input == NaN goto nan
1877 __ j(kUnordered, &nan);
1878 // output = double-to-long-truncate(input)
1879 __ cvttsd2si(output, input, true);
1880 __ jmp(&done);
1881 __ Bind(&nan);
1882 // output = 0
1883 __ xorq(output, output);
1884 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001885 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001886 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001887
1888 default:
1889 LOG(FATAL) << "Unexpected type conversion from " << input_type
1890 << " to " << result_type;
1891 }
1892 break;
1893
Roland Levillain981e4542014-11-14 11:47:14 +00001894 case Primitive::kPrimChar:
1895 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001896 case Primitive::kPrimBoolean:
1897 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001898 case Primitive::kPrimByte:
1899 case Primitive::kPrimShort:
1900 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001901 // Processing a Dex `int-to-char' instruction.
1902 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001903 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001904 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001905 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001906 Address(CpuRegister(RSP), in.GetStackIndex()));
1907 } else {
1908 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001909 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001910 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1911 }
1912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 }
1918 break;
1919
Roland Levillaindff1f282014-11-05 14:15:05 +00001920 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001921 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001922 case Primitive::kPrimBoolean:
1923 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001924 case Primitive::kPrimByte:
1925 case Primitive::kPrimShort:
1926 case Primitive::kPrimInt:
1927 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001928 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001929 if (in.IsRegister()) {
1930 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1931 } else if (in.IsConstant()) {
1932 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
1933 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1934 if (v == 0) {
1935 __ xorps(dest, dest);
1936 } else {
1937 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1938 }
1939 } else {
1940 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1941 Address(CpuRegister(RSP), in.GetStackIndex()), false);
1942 }
Roland Levillaincff13742014-11-17 14:32:17 +00001943 break;
1944
1945 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001946 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001947 if (in.IsRegister()) {
1948 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1949 } else if (in.IsConstant()) {
1950 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
1951 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1952 if (v == 0) {
1953 __ xorps(dest, dest);
1954 } else {
1955 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1956 }
1957 } else {
1958 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1959 Address(CpuRegister(RSP), in.GetStackIndex()), true);
1960 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001961 break;
1962
Roland Levillaincff13742014-11-17 14:32:17 +00001963 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001964 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001965 if (in.IsFpuRegister()) {
1966 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
1967 } else if (in.IsConstant()) {
1968 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
1969 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1970 if (bit_cast<int64_t, double>(v) == 0) {
1971 __ xorps(dest, dest);
1972 } else {
1973 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1974 }
1975 } else {
1976 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
1977 Address(CpuRegister(RSP), in.GetStackIndex()));
1978 }
Roland Levillaincff13742014-11-17 14:32:17 +00001979 break;
1980
1981 default:
1982 LOG(FATAL) << "Unexpected type conversion from " << input_type
1983 << " to " << result_type;
1984 };
1985 break;
1986
Roland Levillaindff1f282014-11-05 14:15:05 +00001987 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001988 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001989 case Primitive::kPrimBoolean:
1990 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001991 case Primitive::kPrimByte:
1992 case Primitive::kPrimShort:
1993 case Primitive::kPrimInt:
1994 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001995 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001996 if (in.IsRegister()) {
1997 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1998 } else if (in.IsConstant()) {
1999 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2000 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2001 if (v == 0) {
2002 __ xorpd(dest, dest);
2003 } else {
2004 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2005 }
2006 } else {
2007 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2008 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2009 }
Roland Levillaincff13742014-11-17 14:32:17 +00002010 break;
2011
2012 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002013 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002014 if (in.IsRegister()) {
2015 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2016 } else if (in.IsConstant()) {
2017 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2018 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2019 if (v == 0) {
2020 __ xorpd(dest, dest);
2021 } else {
2022 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2023 }
2024 } else {
2025 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2026 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2027 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002028 break;
2029
Roland Levillaincff13742014-11-17 14:32:17 +00002030 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002031 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002032 if (in.IsFpuRegister()) {
2033 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2034 } else if (in.IsConstant()) {
2035 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2036 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2037 if (bit_cast<int32_t, float>(v) == 0) {
2038 __ xorpd(dest, dest);
2039 } else {
2040 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2041 }
2042 } else {
2043 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2044 Address(CpuRegister(RSP), in.GetStackIndex()));
2045 }
Roland Levillaincff13742014-11-17 14:32:17 +00002046 break;
2047
2048 default:
2049 LOG(FATAL) << "Unexpected type conversion from " << input_type
2050 << " to " << result_type;
2051 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002052 break;
2053
2054 default:
2055 LOG(FATAL) << "Unexpected type conversion from " << input_type
2056 << " to " << result_type;
2057 }
2058}
2059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002060void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002063 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002064 case Primitive::kPrimInt: {
2065 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002066 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002068 break;
2069 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002070
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002071 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002072 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002073 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002074 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002076 break;
2077 }
2078
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002079 case Primitive::kPrimDouble:
2080 case Primitive::kPrimFloat: {
2081 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002082 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002083 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002084 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002085 }
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 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002090}
2091
2092void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2093 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002094 Location first = locations->InAt(0);
2095 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002096 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002097
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002098 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002099 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002100 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002101 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2102 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2103 } else {
2104 __ leal(out.AsRegister<CpuRegister>(), Address(
2105 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2106 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002107 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002108 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2109 __ addl(out.AsRegister<CpuRegister>(),
2110 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2111 } else {
2112 __ leal(out.AsRegister<CpuRegister>(), Address(
2113 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2114 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002115 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002116 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002117 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002118 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002119 break;
2120 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002122 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002123 if (second.IsRegister()) {
2124 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2125 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2126 } else {
2127 __ leaq(out.AsRegister<CpuRegister>(), Address(
2128 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2129 }
2130 } else {
2131 DCHECK(second.IsConstant());
2132 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2133 int32_t int32_value = Low32Bits(value);
2134 DCHECK_EQ(int32_value, value);
2135 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2136 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2137 } else {
2138 __ leaq(out.AsRegister<CpuRegister>(), Address(
2139 first.AsRegister<CpuRegister>(), int32_value));
2140 }
2141 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002142 break;
2143 }
2144
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002145 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002146 if (second.IsFpuRegister()) {
2147 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2148 } else if (second.IsConstant()) {
2149 __ addss(first.AsFpuRegister<XmmRegister>(),
2150 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2151 } else {
2152 DCHECK(second.IsStackSlot());
2153 __ addss(first.AsFpuRegister<XmmRegister>(),
2154 Address(CpuRegister(RSP), second.GetStackIndex()));
2155 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002156 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002157 }
2158
2159 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002160 if (second.IsFpuRegister()) {
2161 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2162 } else if (second.IsConstant()) {
2163 __ addsd(first.AsFpuRegister<XmmRegister>(),
2164 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2165 } else {
2166 DCHECK(second.IsDoubleStackSlot());
2167 __ addsd(first.AsFpuRegister<XmmRegister>(),
2168 Address(CpuRegister(RSP), second.GetStackIndex()));
2169 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002170 break;
2171 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002172
2173 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002174 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002175 }
2176}
2177
2178void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002179 LocationSummary* locations =
2180 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002181 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002182 case Primitive::kPrimInt: {
2183 locations->SetInAt(0, Location::RequiresRegister());
2184 locations->SetInAt(1, Location::Any());
2185 locations->SetOut(Location::SameAsFirstInput());
2186 break;
2187 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002188 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002189 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002190 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002191 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002192 break;
2193 }
Calin Juravle11351682014-10-23 15:38:15 +01002194 case Primitive::kPrimFloat:
2195 case Primitive::kPrimDouble: {
2196 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002197 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002198 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199 break;
Calin Juravle11351682014-10-23 15:38:15 +01002200 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002201 default:
Calin Juravle11351682014-10-23 15:38:15 +01002202 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002203 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002204}
2205
2206void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2207 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002208 Location first = locations->InAt(0);
2209 Location second = locations->InAt(1);
2210 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002211 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002212 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002213 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002214 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002215 } else if (second.IsConstant()) {
2216 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002218 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002219 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002220 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002221 break;
2222 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002223 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002224 if (second.IsConstant()) {
2225 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2226 DCHECK(IsInt<32>(value));
2227 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2228 } else {
2229 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2230 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002231 break;
2232 }
2233
Calin Juravle11351682014-10-23 15:38:15 +01002234 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002235 if (second.IsFpuRegister()) {
2236 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2237 } else if (second.IsConstant()) {
2238 __ subss(first.AsFpuRegister<XmmRegister>(),
2239 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2240 } else {
2241 DCHECK(second.IsStackSlot());
2242 __ subss(first.AsFpuRegister<XmmRegister>(),
2243 Address(CpuRegister(RSP), second.GetStackIndex()));
2244 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002245 break;
Calin Juravle11351682014-10-23 15:38:15 +01002246 }
2247
2248 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002249 if (second.IsFpuRegister()) {
2250 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2251 } else if (second.IsConstant()) {
2252 __ subsd(first.AsFpuRegister<XmmRegister>(),
2253 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2254 } else {
2255 DCHECK(second.IsDoubleStackSlot());
2256 __ subsd(first.AsFpuRegister<XmmRegister>(),
2257 Address(CpuRegister(RSP), second.GetStackIndex()));
2258 }
Calin Juravle11351682014-10-23 15:38:15 +01002259 break;
2260 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002261
2262 default:
Calin Juravle11351682014-10-23 15:38:15 +01002263 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002264 }
2265}
2266
Calin Juravle34bacdf2014-10-07 20:23:36 +01002267void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2268 LocationSummary* locations =
2269 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2270 switch (mul->GetResultType()) {
2271 case Primitive::kPrimInt: {
2272 locations->SetInAt(0, Location::RequiresRegister());
2273 locations->SetInAt(1, Location::Any());
2274 locations->SetOut(Location::SameAsFirstInput());
2275 break;
2276 }
2277 case Primitive::kPrimLong: {
2278 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002279 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2280 if (locations->InAt(1).IsConstant()) {
2281 // Can use 3 operand multiply.
2282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2283 } else {
2284 locations->SetOut(Location::SameAsFirstInput());
2285 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002286 break;
2287 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002288 case Primitive::kPrimFloat:
2289 case Primitive::kPrimDouble: {
2290 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002291 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002292 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002293 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002294 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002295
2296 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002297 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002298 }
2299}
2300
2301void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2302 LocationSummary* locations = mul->GetLocations();
2303 Location first = locations->InAt(0);
2304 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002305 switch (mul->GetResultType()) {
2306 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002307 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002308 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002309 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002310 } else if (second.IsConstant()) {
2311 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002313 } else {
2314 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002315 __ imull(first.AsRegister<CpuRegister>(),
2316 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002317 }
2318 break;
2319 }
2320 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002321 if (second.IsConstant()) {
2322 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2323 DCHECK(IsInt<32>(value));
2324 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2325 first.AsRegister<CpuRegister>(),
2326 Immediate(static_cast<int32_t>(value)));
2327 } else {
2328 DCHECK(first.Equals(locations->Out()));
2329 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2330 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002331 break;
2332 }
2333
Calin Juravleb5bfa962014-10-21 18:02:24 +01002334 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002335 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002336 if (second.IsFpuRegister()) {
2337 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2338 } else if (second.IsConstant()) {
2339 __ mulss(first.AsFpuRegister<XmmRegister>(),
2340 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2341 } else {
2342 DCHECK(second.IsStackSlot());
2343 __ mulss(first.AsFpuRegister<XmmRegister>(),
2344 Address(CpuRegister(RSP), second.GetStackIndex()));
2345 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002346 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002347 }
2348
2349 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002350 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002351 if (second.IsFpuRegister()) {
2352 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2353 } else if (second.IsConstant()) {
2354 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2355 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2356 } else {
2357 DCHECK(second.IsDoubleStackSlot());
2358 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2359 Address(CpuRegister(RSP), second.GetStackIndex()));
2360 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002361 break;
2362 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002363
2364 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002365 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002366 }
2367}
2368
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002369void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2370 uint32_t stack_adjustment, bool is_float) {
2371 if (source.IsStackSlot()) {
2372 DCHECK(is_float);
2373 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2374 } else if (source.IsDoubleStackSlot()) {
2375 DCHECK(!is_float);
2376 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2377 } else {
2378 // Write the value to the temporary location on the stack and load to FP stack.
2379 if (is_float) {
2380 Location stack_temp = Location::StackSlot(temp_offset);
2381 codegen_->Move(stack_temp, source);
2382 __ flds(Address(CpuRegister(RSP), temp_offset));
2383 } else {
2384 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2385 codegen_->Move(stack_temp, source);
2386 __ fldl(Address(CpuRegister(RSP), temp_offset));
2387 }
2388 }
2389}
2390
2391void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2392 Primitive::Type type = rem->GetResultType();
2393 bool is_float = type == Primitive::kPrimFloat;
2394 size_t elem_size = Primitive::ComponentSize(type);
2395 LocationSummary* locations = rem->GetLocations();
2396 Location first = locations->InAt(0);
2397 Location second = locations->InAt(1);
2398 Location out = locations->Out();
2399
2400 // Create stack space for 2 elements.
2401 // TODO: enhance register allocator to ask for stack temporaries.
2402 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2403
2404 // Load the values to the FP stack in reverse order, using temporaries if needed.
2405 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2406 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2407
2408 // Loop doing FPREM until we stabilize.
2409 Label retry;
2410 __ Bind(&retry);
2411 __ fprem();
2412
2413 // Move FP status to AX.
2414 __ fstsw();
2415
2416 // And see if the argument reduction is complete. This is signaled by the
2417 // C2 FPU flag bit set to 0.
2418 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2419 __ j(kNotEqual, &retry);
2420
2421 // We have settled on the final value. Retrieve it into an XMM register.
2422 // Store FP top of stack to real stack.
2423 if (is_float) {
2424 __ fsts(Address(CpuRegister(RSP), 0));
2425 } else {
2426 __ fstl(Address(CpuRegister(RSP), 0));
2427 }
2428
2429 // Pop the 2 items from the FP stack.
2430 __ fucompp();
2431
2432 // Load the value from the stack into an XMM register.
2433 DCHECK(out.IsFpuRegister()) << out;
2434 if (is_float) {
2435 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2436 } else {
2437 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2438 }
2439
2440 // And remove the temporary stack space we allocated.
2441 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2442}
2443
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002444void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2445 DCHECK(instruction->IsDiv() || instruction->IsRem());
2446
2447 LocationSummary* locations = instruction->GetLocations();
2448 Location second = locations->InAt(1);
2449 DCHECK(second.IsConstant());
2450
2451 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2452 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002453 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002454
2455 DCHECK(imm == 1 || imm == -1);
2456
2457 switch (instruction->GetResultType()) {
2458 case Primitive::kPrimInt: {
2459 if (instruction->IsRem()) {
2460 __ xorl(output_register, output_register);
2461 } else {
2462 __ movl(output_register, input_register);
2463 if (imm == -1) {
2464 __ negl(output_register);
2465 }
2466 }
2467 break;
2468 }
2469
2470 case Primitive::kPrimLong: {
2471 if (instruction->IsRem()) {
2472 __ xorq(output_register, output_register);
2473 } else {
2474 __ movq(output_register, input_register);
2475 if (imm == -1) {
2476 __ negq(output_register);
2477 }
2478 }
2479 break;
2480 }
2481
2482 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002483 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002484 }
2485}
2486
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002487void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002488 LocationSummary* locations = instruction->GetLocations();
2489 Location second = locations->InAt(1);
2490
2491 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2492 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2493
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002494 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002495
2496 DCHECK(IsPowerOfTwo(std::abs(imm)));
2497
2498 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2499
2500 if (instruction->GetResultType() == Primitive::kPrimInt) {
2501 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2502 __ testl(numerator, numerator);
2503 __ cmov(kGreaterEqual, tmp, numerator);
2504 int shift = CTZ(imm);
2505 __ sarl(tmp, Immediate(shift));
2506
2507 if (imm < 0) {
2508 __ negl(tmp);
2509 }
2510
2511 __ movl(output_register, tmp);
2512 } else {
2513 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2514 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2515
2516 __ movq(rdx, Immediate(std::abs(imm) - 1));
2517 __ addq(rdx, numerator);
2518 __ testq(numerator, numerator);
2519 __ cmov(kGreaterEqual, rdx, numerator);
2520 int shift = CTZ(imm);
2521 __ sarq(rdx, Immediate(shift));
2522
2523 if (imm < 0) {
2524 __ negq(rdx);
2525 }
2526
2527 __ movq(output_register, rdx);
2528 }
2529}
2530
2531void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2532 DCHECK(instruction->IsDiv() || instruction->IsRem());
2533
2534 LocationSummary* locations = instruction->GetLocations();
2535 Location second = locations->InAt(1);
2536
2537 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2538 : locations->GetTemp(0).AsRegister<CpuRegister>();
2539 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2540 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2541 : locations->Out().AsRegister<CpuRegister>();
2542 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2543
2544 DCHECK_EQ(RAX, eax.AsRegister());
2545 DCHECK_EQ(RDX, edx.AsRegister());
2546 if (instruction->IsDiv()) {
2547 DCHECK_EQ(RAX, out.AsRegister());
2548 } else {
2549 DCHECK_EQ(RDX, out.AsRegister());
2550 }
2551
2552 int64_t magic;
2553 int shift;
2554
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002555 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002556 if (instruction->GetResultType() == Primitive::kPrimInt) {
2557 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2558
2559 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2560
2561 __ movl(numerator, eax);
2562
2563 Label no_div;
2564 Label end;
2565 __ testl(eax, eax);
2566 __ j(kNotEqual, &no_div);
2567
2568 __ xorl(out, out);
2569 __ jmp(&end);
2570
2571 __ Bind(&no_div);
2572
2573 __ movl(eax, Immediate(magic));
2574 __ imull(numerator);
2575
2576 if (imm > 0 && magic < 0) {
2577 __ addl(edx, numerator);
2578 } else if (imm < 0 && magic > 0) {
2579 __ subl(edx, numerator);
2580 }
2581
2582 if (shift != 0) {
2583 __ sarl(edx, Immediate(shift));
2584 }
2585
2586 __ movl(eax, edx);
2587 __ shrl(edx, Immediate(31));
2588 __ addl(edx, eax);
2589
2590 if (instruction->IsRem()) {
2591 __ movl(eax, numerator);
2592 __ imull(edx, Immediate(imm));
2593 __ subl(eax, edx);
2594 __ movl(edx, eax);
2595 } else {
2596 __ movl(eax, edx);
2597 }
2598 __ Bind(&end);
2599 } else {
2600 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2601
2602 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2603
2604 CpuRegister rax = eax;
2605 CpuRegister rdx = edx;
2606
2607 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2608
2609 // Save the numerator.
2610 __ movq(numerator, rax);
2611
2612 // RAX = magic
2613 __ movq(rax, Immediate(magic));
2614
2615 // RDX:RAX = magic * numerator
2616 __ imulq(numerator);
2617
2618 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002619 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002620 __ addq(rdx, numerator);
2621 } else if (imm < 0 && magic > 0) {
2622 // RDX -= numerator
2623 __ subq(rdx, numerator);
2624 }
2625
2626 // Shift if needed.
2627 if (shift != 0) {
2628 __ sarq(rdx, Immediate(shift));
2629 }
2630
2631 // RDX += 1 if RDX < 0
2632 __ movq(rax, rdx);
2633 __ shrq(rdx, Immediate(63));
2634 __ addq(rdx, rax);
2635
2636 if (instruction->IsRem()) {
2637 __ movq(rax, numerator);
2638
2639 if (IsInt<32>(imm)) {
2640 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2641 } else {
2642 __ movq(numerator, Immediate(imm));
2643 __ imulq(rdx, numerator);
2644 }
2645
2646 __ subq(rax, rdx);
2647 __ movq(rdx, rax);
2648 } else {
2649 __ movq(rax, rdx);
2650 }
2651 }
2652}
2653
Calin Juravlebacfec32014-11-14 15:54:36 +00002654void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2655 DCHECK(instruction->IsDiv() || instruction->IsRem());
2656 Primitive::Type type = instruction->GetResultType();
2657 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2658
2659 bool is_div = instruction->IsDiv();
2660 LocationSummary* locations = instruction->GetLocations();
2661
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002662 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2663 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002664
Roland Levillain271ab9c2014-11-27 15:23:57 +00002665 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002666 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002667
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002668 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002669 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002670
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002671 if (imm == 0) {
2672 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2673 } else if (imm == 1 || imm == -1) {
2674 DivRemOneOrMinusOne(instruction);
2675 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002676 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002677 } else {
2678 DCHECK(imm <= -2 || imm >= 2);
2679 GenerateDivRemWithAnyConstant(instruction);
2680 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002681 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002682 SlowPathCodeX86_64* slow_path =
2683 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2684 out.AsRegister(), type, is_div);
2685 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002686
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002687 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2688 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2689 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2690 // so it's safe to just use negl instead of more complex comparisons.
2691 if (type == Primitive::kPrimInt) {
2692 __ cmpl(second_reg, Immediate(-1));
2693 __ j(kEqual, slow_path->GetEntryLabel());
2694 // edx:eax <- sign-extended of eax
2695 __ cdq();
2696 // eax = quotient, edx = remainder
2697 __ idivl(second_reg);
2698 } else {
2699 __ cmpq(second_reg, Immediate(-1));
2700 __ j(kEqual, slow_path->GetEntryLabel());
2701 // rdx:rax <- sign-extended of rax
2702 __ cqo();
2703 // rax = quotient, rdx = remainder
2704 __ idivq(second_reg);
2705 }
2706 __ Bind(slow_path->GetExitLabel());
2707 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002708}
2709
Calin Juravle7c4954d2014-10-28 16:57:40 +00002710void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2711 LocationSummary* locations =
2712 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2713 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002714 case Primitive::kPrimInt:
2715 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002716 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002717 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002718 locations->SetOut(Location::SameAsFirstInput());
2719 // Intel uses edx:eax as the dividend.
2720 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002721 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2722 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2723 // output and request another temp.
2724 if (div->InputAt(1)->IsConstant()) {
2725 locations->AddTemp(Location::RequiresRegister());
2726 }
Calin Juravled0d48522014-11-04 16:40:20 +00002727 break;
2728 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002729
Calin Juravle7c4954d2014-10-28 16:57:40 +00002730 case Primitive::kPrimFloat:
2731 case Primitive::kPrimDouble: {
2732 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002733 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002734 locations->SetOut(Location::SameAsFirstInput());
2735 break;
2736 }
2737
2738 default:
2739 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2740 }
2741}
2742
2743void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2744 LocationSummary* locations = div->GetLocations();
2745 Location first = locations->InAt(0);
2746 Location second = locations->InAt(1);
2747 DCHECK(first.Equals(locations->Out()));
2748
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002749 Primitive::Type type = div->GetResultType();
2750 switch (type) {
2751 case Primitive::kPrimInt:
2752 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002753 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002754 break;
2755 }
2756
Calin Juravle7c4954d2014-10-28 16:57:40 +00002757 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002758 if (second.IsFpuRegister()) {
2759 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2760 } else if (second.IsConstant()) {
2761 __ divss(first.AsFpuRegister<XmmRegister>(),
2762 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2763 } else {
2764 DCHECK(second.IsStackSlot());
2765 __ divss(first.AsFpuRegister<XmmRegister>(),
2766 Address(CpuRegister(RSP), second.GetStackIndex()));
2767 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002768 break;
2769 }
2770
2771 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002772 if (second.IsFpuRegister()) {
2773 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2774 } else if (second.IsConstant()) {
2775 __ divsd(first.AsFpuRegister<XmmRegister>(),
2776 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2777 } else {
2778 DCHECK(second.IsDoubleStackSlot());
2779 __ divsd(first.AsFpuRegister<XmmRegister>(),
2780 Address(CpuRegister(RSP), second.GetStackIndex()));
2781 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002782 break;
2783 }
2784
2785 default:
2786 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2787 }
2788}
2789
Calin Juravlebacfec32014-11-14 15:54:36 +00002790void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002791 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002792 LocationSummary* locations =
2793 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002794
2795 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002796 case Primitive::kPrimInt:
2797 case Primitive::kPrimLong: {
2798 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002799 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002800 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2801 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002802 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2803 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2804 // output and request another temp.
2805 if (rem->InputAt(1)->IsConstant()) {
2806 locations->AddTemp(Location::RequiresRegister());
2807 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002808 break;
2809 }
2810
2811 case Primitive::kPrimFloat:
2812 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002813 locations->SetInAt(0, Location::Any());
2814 locations->SetInAt(1, Location::Any());
2815 locations->SetOut(Location::RequiresFpuRegister());
2816 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002817 break;
2818 }
2819
2820 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002821 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002822 }
2823}
2824
2825void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2826 Primitive::Type type = rem->GetResultType();
2827 switch (type) {
2828 case Primitive::kPrimInt:
2829 case Primitive::kPrimLong: {
2830 GenerateDivRemIntegral(rem);
2831 break;
2832 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002833 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002834 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002835 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002836 break;
2837 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002838 default:
2839 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2840 }
2841}
2842
Calin Juravled0d48522014-11-04 16:40:20 +00002843void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2844 LocationSummary* locations =
2845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2846 locations->SetInAt(0, Location::Any());
2847 if (instruction->HasUses()) {
2848 locations->SetOut(Location::SameAsFirstInput());
2849 }
2850}
2851
2852void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2853 SlowPathCodeX86_64* slow_path =
2854 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2855 codegen_->AddSlowPath(slow_path);
2856
2857 LocationSummary* locations = instruction->GetLocations();
2858 Location value = locations->InAt(0);
2859
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002860 switch (instruction->GetType()) {
2861 case Primitive::kPrimInt: {
2862 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002863 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002864 __ j(kEqual, slow_path->GetEntryLabel());
2865 } else if (value.IsStackSlot()) {
2866 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2867 __ j(kEqual, slow_path->GetEntryLabel());
2868 } else {
2869 DCHECK(value.IsConstant()) << value;
2870 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2871 __ jmp(slow_path->GetEntryLabel());
2872 }
2873 }
2874 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002875 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002876 case Primitive::kPrimLong: {
2877 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002878 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002879 __ j(kEqual, slow_path->GetEntryLabel());
2880 } else if (value.IsDoubleStackSlot()) {
2881 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2882 __ j(kEqual, slow_path->GetEntryLabel());
2883 } else {
2884 DCHECK(value.IsConstant()) << value;
2885 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2886 __ jmp(slow_path->GetEntryLabel());
2887 }
2888 }
2889 break;
2890 }
2891 default:
2892 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002893 }
Calin Juravled0d48522014-11-04 16:40:20 +00002894}
2895
Calin Juravle9aec02f2014-11-18 23:06:35 +00002896void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2897 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2898
2899 LocationSummary* locations =
2900 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2901
2902 switch (op->GetResultType()) {
2903 case Primitive::kPrimInt:
2904 case Primitive::kPrimLong: {
2905 locations->SetInAt(0, Location::RequiresRegister());
2906 // The shift count needs to be in CL.
2907 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2908 locations->SetOut(Location::SameAsFirstInput());
2909 break;
2910 }
2911 default:
2912 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2913 }
2914}
2915
2916void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2917 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2918
2919 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002920 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002921 Location second = locations->InAt(1);
2922
2923 switch (op->GetResultType()) {
2924 case Primitive::kPrimInt: {
2925 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002927 if (op->IsShl()) {
2928 __ shll(first_reg, second_reg);
2929 } else if (op->IsShr()) {
2930 __ sarl(first_reg, second_reg);
2931 } else {
2932 __ shrl(first_reg, second_reg);
2933 }
2934 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002935 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002936 if (op->IsShl()) {
2937 __ shll(first_reg, imm);
2938 } else if (op->IsShr()) {
2939 __ sarl(first_reg, imm);
2940 } else {
2941 __ shrl(first_reg, imm);
2942 }
2943 }
2944 break;
2945 }
2946 case Primitive::kPrimLong: {
2947 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002948 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002949 if (op->IsShl()) {
2950 __ shlq(first_reg, second_reg);
2951 } else if (op->IsShr()) {
2952 __ sarq(first_reg, second_reg);
2953 } else {
2954 __ shrq(first_reg, second_reg);
2955 }
2956 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002957 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002958 if (op->IsShl()) {
2959 __ shlq(first_reg, imm);
2960 } else if (op->IsShr()) {
2961 __ sarq(first_reg, imm);
2962 } else {
2963 __ shrq(first_reg, imm);
2964 }
2965 }
2966 break;
2967 }
2968 default:
2969 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2970 }
2971}
2972
2973void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2974 HandleShift(shl);
2975}
2976
2977void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2978 HandleShift(shl);
2979}
2980
2981void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2982 HandleShift(shr);
2983}
2984
2985void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2986 HandleShift(shr);
2987}
2988
2989void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2990 HandleShift(ushr);
2991}
2992
2993void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2994 HandleShift(ushr);
2995}
2996
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002997void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002998 LocationSummary* locations =
2999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003000 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003001 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3002 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3003 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003004}
3005
3006void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3007 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003008 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003009 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
3010
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003011 __ gs()->call(
3012 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003013
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003014 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003015 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003016}
3017
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003018void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3019 LocationSummary* locations =
3020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3021 InvokeRuntimeCallingConvention calling_convention;
3022 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003023 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003024 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003025 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003026}
3027
3028void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3029 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003030 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003031 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
3032
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003033 __ gs()->call(
3034 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003035
3036 DCHECK(!codegen_->IsLeafMethod());
3037 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3038}
3039
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003040void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003041 LocationSummary* locations =
3042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003043 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3044 if (location.IsStackSlot()) {
3045 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3046 } else if (location.IsDoubleStackSlot()) {
3047 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3048 }
3049 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003050}
3051
3052void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
3053 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003054 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003055}
3056
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003057void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003058 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003059 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003060 locations->SetInAt(0, Location::RequiresRegister());
3061 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003062}
3063
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003064void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3065 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3067 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003068 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003069 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003070 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003071 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003072 break;
3073
3074 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003075 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003076 break;
3077
3078 default:
3079 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3080 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003081}
3082
David Brazdil66d126e2015-04-03 16:02:44 +01003083void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3084 LocationSummary* locations =
3085 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3086 locations->SetInAt(0, Location::RequiresRegister());
3087 locations->SetOut(Location::SameAsFirstInput());
3088}
3089
3090void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003091 LocationSummary* locations = bool_not->GetLocations();
3092 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3093 locations->Out().AsRegister<CpuRegister>().AsRegister());
3094 Location out = locations->Out();
3095 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3096}
3097
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003098void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003099 LocationSummary* locations =
3100 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003101 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3102 locations->SetInAt(i, Location::Any());
3103 }
3104 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003105}
3106
3107void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003108 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003109 LOG(FATAL) << "Unimplemented";
3110}
3111
Calin Juravle52c48962014-12-16 17:02:57 +00003112void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3113 /*
3114 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3115 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3116 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3117 */
3118 switch (kind) {
3119 case MemBarrierKind::kAnyAny: {
3120 __ mfence();
3121 break;
3122 }
3123 case MemBarrierKind::kAnyStore:
3124 case MemBarrierKind::kLoadAny:
3125 case MemBarrierKind::kStoreStore: {
3126 // nop
3127 break;
3128 }
3129 default:
3130 LOG(FATAL) << "Unexpected memory barier " << kind;
3131 }
3132}
3133
3134void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3135 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3136
Nicolas Geoffray39468442014-09-02 15:17:15 +01003137 LocationSummary* locations =
3138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003139 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003140 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3141 locations->SetOut(Location::RequiresFpuRegister());
3142 } else {
3143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3144 }
Calin Juravle52c48962014-12-16 17:02:57 +00003145}
3146
3147void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3148 const FieldInfo& field_info) {
3149 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3150
3151 LocationSummary* locations = instruction->GetLocations();
3152 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3153 Location out = locations->Out();
3154 bool is_volatile = field_info.IsVolatile();
3155 Primitive::Type field_type = field_info.GetFieldType();
3156 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3157
3158 switch (field_type) {
3159 case Primitive::kPrimBoolean: {
3160 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3161 break;
3162 }
3163
3164 case Primitive::kPrimByte: {
3165 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3166 break;
3167 }
3168
3169 case Primitive::kPrimShort: {
3170 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3171 break;
3172 }
3173
3174 case Primitive::kPrimChar: {
3175 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3176 break;
3177 }
3178
3179 case Primitive::kPrimInt:
3180 case Primitive::kPrimNot: {
3181 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3182 break;
3183 }
3184
3185 case Primitive::kPrimLong: {
3186 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3187 break;
3188 }
3189
3190 case Primitive::kPrimFloat: {
3191 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3192 break;
3193 }
3194
3195 case Primitive::kPrimDouble: {
3196 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3197 break;
3198 }
3199
3200 case Primitive::kPrimVoid:
3201 LOG(FATAL) << "Unreachable type " << field_type;
3202 UNREACHABLE();
3203 }
3204
Calin Juravle77520bc2015-01-12 18:45:46 +00003205 codegen_->MaybeRecordImplicitNullCheck(instruction);
3206
Calin Juravle52c48962014-12-16 17:02:57 +00003207 if (is_volatile) {
3208 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3209 }
3210}
3211
3212void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3213 const FieldInfo& field_info) {
3214 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3215
3216 LocationSummary* locations =
3217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003218 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003219 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3220
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003221 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003222 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3223 locations->SetInAt(1, Location::RequiresFpuRegister());
3224 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003225 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003226 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003227 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003228 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003229 locations->AddTemp(Location::RequiresRegister());
3230 locations->AddTemp(Location::RequiresRegister());
3231 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003232}
3233
Calin Juravle52c48962014-12-16 17:02:57 +00003234void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3235 const FieldInfo& field_info) {
3236 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3237
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003239 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3240 Location value = locations->InAt(1);
3241 bool is_volatile = field_info.IsVolatile();
3242 Primitive::Type field_type = field_info.GetFieldType();
3243 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3244
3245 if (is_volatile) {
3246 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3247 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003248
3249 switch (field_type) {
3250 case Primitive::kPrimBoolean:
3251 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003252 if (value.IsConstant()) {
3253 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3254 __ movb(Address(base, offset), Immediate(v));
3255 } else {
3256 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3257 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003258 break;
3259 }
3260
3261 case Primitive::kPrimShort:
3262 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003263 if (value.IsConstant()) {
3264 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3265 __ movw(Address(base, offset), Immediate(v));
3266 } else {
3267 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3268 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003269 break;
3270 }
3271
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003273 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003274 if (value.IsConstant()) {
3275 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3276 __ movw(Address(base, offset), Immediate(v));
3277 } else {
3278 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3279 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280 break;
3281 }
3282
3283 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003284 if (value.IsConstant()) {
3285 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3286 DCHECK(IsInt<32>(v));
3287 int32_t v_32 = v;
3288 __ movq(Address(base, offset), Immediate(v_32));
3289 } else {
3290 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3291 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003292 break;
3293 }
3294
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003295 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003296 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003297 break;
3298 }
3299
3300 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003301 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003302 break;
3303 }
3304
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003305 case Primitive::kPrimVoid:
3306 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003307 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003308 }
Calin Juravle52c48962014-12-16 17:02:57 +00003309
Calin Juravle77520bc2015-01-12 18:45:46 +00003310 codegen_->MaybeRecordImplicitNullCheck(instruction);
3311
3312 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3313 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3314 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3315 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3316 }
3317
Calin Juravle52c48962014-12-16 17:02:57 +00003318 if (is_volatile) {
3319 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3320 }
3321}
3322
3323void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3324 HandleFieldSet(instruction, instruction->GetFieldInfo());
3325}
3326
3327void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3328 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003329}
3330
3331void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003332 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003333}
3334
3335void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003336 HandleFieldGet(instruction, instruction->GetFieldInfo());
3337}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003338
Calin Juravle52c48962014-12-16 17:02:57 +00003339void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3340 HandleFieldGet(instruction);
3341}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003342
Calin Juravle52c48962014-12-16 17:02:57 +00003343void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3344 HandleFieldGet(instruction, instruction->GetFieldInfo());
3345}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003346
Calin Juravle52c48962014-12-16 17:02:57 +00003347void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3348 HandleFieldSet(instruction, instruction->GetFieldInfo());
3349}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003350
Calin Juravle52c48962014-12-16 17:02:57 +00003351void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3352 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003353}
3354
3355void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003356 LocationSummary* locations =
3357 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003358 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3359 ? Location::RequiresRegister()
3360 : Location::Any();
3361 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003362 if (instruction->HasUses()) {
3363 locations->SetOut(Location::SameAsFirstInput());
3364 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003365}
3366
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003367void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003368 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3369 return;
3370 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003371 LocationSummary* locations = instruction->GetLocations();
3372 Location obj = locations->InAt(0);
3373
3374 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3375 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3376}
3377
3378void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003379 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003380 codegen_->AddSlowPath(slow_path);
3381
3382 LocationSummary* locations = instruction->GetLocations();
3383 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003384
3385 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003386 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003387 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003388 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003389 } else {
3390 DCHECK(obj.IsConstant()) << obj;
3391 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3392 __ jmp(slow_path->GetEntryLabel());
3393 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003394 }
3395 __ j(kEqual, slow_path->GetEntryLabel());
3396}
3397
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003398void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3399 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3400 GenerateImplicitNullCheck(instruction);
3401 } else {
3402 GenerateExplicitNullCheck(instruction);
3403 }
3404}
3405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003406void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003407 LocationSummary* locations =
3408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003409 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003410 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003411 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3412 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3413 } else {
3414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3415 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003416}
3417
3418void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3419 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003420 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003421 Location index = locations->InAt(1);
3422
3423 switch (instruction->GetType()) {
3424 case Primitive::kPrimBoolean: {
3425 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003427 if (index.IsConstant()) {
3428 __ movzxb(out, Address(obj,
3429 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3430 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003431 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003432 }
3433 break;
3434 }
3435
3436 case Primitive::kPrimByte: {
3437 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003438 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003439 if (index.IsConstant()) {
3440 __ movsxb(out, Address(obj,
3441 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3442 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003443 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003444 }
3445 break;
3446 }
3447
3448 case Primitive::kPrimShort: {
3449 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003451 if (index.IsConstant()) {
3452 __ movsxw(out, Address(obj,
3453 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3454 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456 }
3457 break;
3458 }
3459
3460 case Primitive::kPrimChar: {
3461 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003462 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003463 if (index.IsConstant()) {
3464 __ movzxw(out, Address(obj,
3465 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3466 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003467 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003468 }
3469 break;
3470 }
3471
3472 case Primitive::kPrimInt:
3473 case Primitive::kPrimNot: {
3474 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3475 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003476 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003477 if (index.IsConstant()) {
3478 __ movl(out, Address(obj,
3479 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3480 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003482 }
3483 break;
3484 }
3485
3486 case Primitive::kPrimLong: {
3487 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003488 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003489 if (index.IsConstant()) {
3490 __ movq(out, Address(obj,
3491 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3492 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003493 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003494 }
3495 break;
3496 }
3497
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003498 case Primitive::kPrimFloat: {
3499 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003500 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003501 if (index.IsConstant()) {
3502 __ movss(out, Address(obj,
3503 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3504 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003505 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003506 }
3507 break;
3508 }
3509
3510 case Primitive::kPrimDouble: {
3511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003512 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003513 if (index.IsConstant()) {
3514 __ movsd(out, Address(obj,
3515 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3516 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003517 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003518 }
3519 break;
3520 }
3521
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003522 case Primitive::kPrimVoid:
3523 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003524 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003525 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003526 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003527}
3528
3529void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003530 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003531
3532 bool needs_write_barrier =
3533 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3534 bool needs_runtime_call = instruction->NeedsTypeCheck();
3535
Nicolas Geoffray39468442014-09-02 15:17:15 +01003536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003537 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3538 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003539 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003540 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3541 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3542 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003543 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003544 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003545 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003546 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3547 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003548 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003549 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003550 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3551 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003552 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003553 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003554 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003555
3556 if (needs_write_barrier) {
3557 // Temporary registers for the write barrier.
3558 locations->AddTemp(Location::RequiresRegister());
3559 locations->AddTemp(Location::RequiresRegister());
3560 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003561 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003562}
3563
3564void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3565 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003568 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003569 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003570 bool needs_runtime_call = locations->WillCall();
3571 bool needs_write_barrier =
3572 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573
3574 switch (value_type) {
3575 case Primitive::kPrimBoolean:
3576 case Primitive::kPrimByte: {
3577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 if (index.IsConstant()) {
3579 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003580 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003581 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003582 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003583 __ movb(Address(obj, offset),
3584 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003585 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003586 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003587 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003588 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3589 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003590 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003592 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3593 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003594 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003595 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 break;
3597 }
3598
3599 case Primitive::kPrimShort:
3600 case Primitive::kPrimChar: {
3601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 if (index.IsConstant()) {
3603 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003604 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003606 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003607 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003608 __ movw(Address(obj, offset),
3609 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003612 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003613 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3615 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003616 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003617 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003618 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003619 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3620 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003622 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003623 break;
3624 }
3625
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003626 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003627 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003628 if (!needs_runtime_call) {
3629 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3630 if (index.IsConstant()) {
3631 size_t offset =
3632 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3633 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003635 } else {
3636 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003637 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3638 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003639 }
3640 } else {
3641 DCHECK(index.IsRegister()) << index;
3642 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3644 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003645 } else {
3646 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003647 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003649 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003650 }
3651 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003652 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003653 if (needs_write_barrier) {
3654 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003655 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3656 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3657 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003658 }
3659 } else {
3660 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003661 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3662 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003663 DCHECK(!codegen_->IsLeafMethod());
3664 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3665 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666 break;
3667 }
3668
3669 case Primitive::kPrimLong: {
3670 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003671 if (index.IsConstant()) {
3672 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04003673 if (value.IsRegister()) {
3674 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
3675 } else {
3676 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3677 DCHECK(IsInt<32>(v));
3678 int32_t v_32 = v;
3679 __ movq(Address(obj, offset), Immediate(v_32));
3680 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003681 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003682 if (value.IsRegister()) {
3683 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3684 value.AsRegister<CpuRegister>());
3685 } else {
3686 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3687 DCHECK(IsInt<32>(v));
3688 int32_t v_32 = v;
3689 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3690 Immediate(v_32));
3691 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003692 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003693 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003694 break;
3695 }
3696
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003697 case Primitive::kPrimFloat: {
3698 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3699 if (index.IsConstant()) {
3700 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3701 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003702 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003703 } else {
3704 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003705 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3706 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003707 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003709 break;
3710 }
3711
3712 case Primitive::kPrimDouble: {
3713 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3714 if (index.IsConstant()) {
3715 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3716 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003717 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003718 } else {
3719 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003720 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3721 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003722 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003723 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003724 break;
3725 }
3726
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 case Primitive::kPrimVoid:
3728 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003729 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730 }
3731}
3732
3733void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003734 LocationSummary* locations =
3735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003736 locations->SetInAt(0, Location::RequiresRegister());
3737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738}
3739
3740void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3741 LocationSummary* locations = instruction->GetLocations();
3742 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003743 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3744 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003745 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003746 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003747}
3748
3749void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003750 LocationSummary* locations =
3751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003752 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003753 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003754 if (instruction->HasUses()) {
3755 locations->SetOut(Location::SameAsFirstInput());
3756 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003757}
3758
3759void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3760 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003761 Location index_loc = locations->InAt(0);
3762 Location length_loc = locations->InAt(1);
3763 SlowPathCodeX86_64* slow_path =
3764 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003765 codegen_->AddSlowPath(slow_path);
3766
Mark Mendellf60c90b2015-03-04 15:12:59 -05003767 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3768 if (index_loc.IsConstant()) {
3769 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3770 __ cmpl(length, Immediate(value));
3771 } else {
3772 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3773 }
3774 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775}
3776
3777void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3778 CpuRegister card,
3779 CpuRegister object,
3780 CpuRegister value) {
3781 Label is_null;
3782 __ testl(value, value);
3783 __ j(kEqual, &is_null);
3784 __ gs()->movq(card, Address::Absolute(
3785 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3786 __ movq(temp, object);
3787 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3788 __ movb(Address(temp, card, TIMES_1, 0), card);
3789 __ Bind(&is_null);
3790}
3791
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003792void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3793 temp->SetLocations(nullptr);
3794}
3795
3796void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3797 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003798 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003799}
3800
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003801void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003802 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003803 LOG(FATAL) << "Unimplemented";
3804}
3805
3806void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003807 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3808}
3809
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003810void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3811 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3812}
3813
3814void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003815 HBasicBlock* block = instruction->GetBlock();
3816 if (block->GetLoopInformation() != nullptr) {
3817 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3818 // The back edge will generate the suspend check.
3819 return;
3820 }
3821 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3822 // The goto will generate the suspend check.
3823 return;
3824 }
3825 GenerateSuspendCheck(instruction, nullptr);
3826}
3827
3828void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3829 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003830 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003831 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003832 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003833 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003834 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003835 if (successor == nullptr) {
3836 __ j(kNotEqual, slow_path->GetEntryLabel());
3837 __ Bind(slow_path->GetReturnLabel());
3838 } else {
3839 __ j(kEqual, codegen_->GetLabelOf(successor));
3840 __ jmp(slow_path->GetEntryLabel());
3841 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003842}
3843
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003844X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3845 return codegen_->GetAssembler();
3846}
3847
3848void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3849 MoveOperands* move = moves_.Get(index);
3850 Location source = move->GetSource();
3851 Location destination = move->GetDestination();
3852
3853 if (source.IsRegister()) {
3854 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003856 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003857 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003858 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003859 } else {
3860 DCHECK(destination.IsDoubleStackSlot());
3861 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003862 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003863 }
3864 } else if (source.IsStackSlot()) {
3865 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003867 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003868 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003870 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003871 } else {
3872 DCHECK(destination.IsStackSlot());
3873 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3874 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3875 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003876 } else if (source.IsDoubleStackSlot()) {
3877 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003878 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003879 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003880 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003881 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3882 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003883 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003884 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003885 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3886 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3887 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003888 } else if (source.IsConstant()) {
3889 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003890 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3891 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003892 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003893 if (value == 0) {
3894 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3895 } else {
3896 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3897 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003898 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003899 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003900 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003901 }
3902 } else if (constant->IsLongConstant()) {
3903 int64_t value = constant->AsLongConstant()->GetValue();
3904 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003905 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003906 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003907 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003908 __ movq(CpuRegister(TMP), Immediate(value));
3909 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3910 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003911 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003912 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003913 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003914 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003915 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003916 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3917 if (value == 0) {
3918 // easy FP 0.0.
3919 __ xorps(dest, dest);
3920 } else {
3921 __ movl(CpuRegister(TMP), imm);
3922 __ movd(dest, CpuRegister(TMP));
3923 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003924 } else {
3925 DCHECK(destination.IsStackSlot()) << destination;
3926 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3927 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003928 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003929 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003930 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003931 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003932 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003933 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003934 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3935 if (value == 0) {
3936 __ xorpd(dest, dest);
3937 } else {
3938 __ movq(CpuRegister(TMP), imm);
3939 __ movd(dest, CpuRegister(TMP));
3940 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003941 } else {
3942 DCHECK(destination.IsDoubleStackSlot()) << destination;
3943 __ movq(CpuRegister(TMP), imm);
3944 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3945 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003946 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003947 } else if (source.IsFpuRegister()) {
3948 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003950 } else if (destination.IsStackSlot()) {
3951 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003952 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003953 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003954 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003955 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003956 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003957 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003958 }
3959}
3960
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003961void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003962 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003963 __ movl(Address(CpuRegister(RSP), mem), reg);
3964 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003965}
3966
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003967void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003968 ScratchRegisterScope ensure_scratch(
3969 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3970
3971 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3972 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3973 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3974 Address(CpuRegister(RSP), mem2 + stack_offset));
3975 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3976 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3977 CpuRegister(ensure_scratch.GetRegister()));
3978}
3979
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003980void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3981 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3982 __ movq(Address(CpuRegister(RSP), mem), reg);
3983 __ movq(reg, CpuRegister(TMP));
3984}
3985
3986void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3987 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003988 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003989
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003990 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3991 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3992 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3993 Address(CpuRegister(RSP), mem2 + stack_offset));
3994 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3995 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3996 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003997}
3998
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003999void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4000 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4001 __ movss(Address(CpuRegister(RSP), mem), reg);
4002 __ movd(reg, CpuRegister(TMP));
4003}
4004
4005void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4006 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4007 __ movsd(Address(CpuRegister(RSP), mem), reg);
4008 __ movd(reg, CpuRegister(TMP));
4009}
4010
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004011void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4012 MoveOperands* move = moves_.Get(index);
4013 Location source = move->GetSource();
4014 Location destination = move->GetDestination();
4015
4016 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004017 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004018 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004019 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004020 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004021 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004022 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004023 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4024 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004025 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004026 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004027 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004028 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4029 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004030 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004031 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4032 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4033 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004034 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004035 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004036 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004037 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004038 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004039 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004040 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004041 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004042 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004043 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004044 }
4045}
4046
4047
4048void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4049 __ pushq(CpuRegister(reg));
4050}
4051
4052
4053void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4054 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004055}
4056
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004057void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4058 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4059 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4060 Immediate(mirror::Class::kStatusInitialized));
4061 __ j(kLess, slow_path->GetEntryLabel());
4062 __ Bind(slow_path->GetExitLabel());
4063 // No need for memory fence, thanks to the X86_64 memory model.
4064}
4065
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004066void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004067 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4068 ? LocationSummary::kCallOnSlowPath
4069 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004070 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004071 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004072 locations->SetOut(Location::RequiresRegister());
4073}
4074
4075void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004076 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004077 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004078 DCHECK(!cls->CanCallRuntime());
4079 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004080 codegen_->LoadCurrentMethod(out);
4081 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4082 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004083 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004084 codegen_->LoadCurrentMethod(out);
4085 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4086 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004087 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4088 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4089 codegen_->AddSlowPath(slow_path);
4090 __ testl(out, out);
4091 __ j(kEqual, slow_path->GetEntryLabel());
4092 if (cls->MustGenerateClinitCheck()) {
4093 GenerateClassInitializationCheck(slow_path, out);
4094 } else {
4095 __ Bind(slow_path->GetExitLabel());
4096 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004097 }
4098}
4099
4100void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4101 LocationSummary* locations =
4102 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4103 locations->SetInAt(0, Location::RequiresRegister());
4104 if (check->HasUses()) {
4105 locations->SetOut(Location::SameAsFirstInput());
4106 }
4107}
4108
4109void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004110 // We assume the class to not be null.
4111 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4112 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004113 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004114 GenerateClassInitializationCheck(slow_path,
4115 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004116}
4117
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004118void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4119 LocationSummary* locations =
4120 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4121 locations->SetOut(Location::RequiresRegister());
4122}
4123
4124void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4125 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4126 codegen_->AddSlowPath(slow_path);
4127
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004129 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004130 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4131 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004132 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4133 __ testl(out, out);
4134 __ j(kEqual, slow_path->GetEntryLabel());
4135 __ Bind(slow_path->GetExitLabel());
4136}
4137
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004138void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4139 LocationSummary* locations =
4140 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4141 locations->SetOut(Location::RequiresRegister());
4142}
4143
4144void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4145 Address address = Address::Absolute(
4146 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004147 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004148 __ gs()->movl(address, Immediate(0));
4149}
4150
4151void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4152 LocationSummary* locations =
4153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4154 InvokeRuntimeCallingConvention calling_convention;
4155 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4156}
4157
4158void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4159 __ gs()->call(
4160 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4161 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4162}
4163
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004164void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004165 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4166 ? LocationSummary::kNoCall
4167 : LocationSummary::kCallOnSlowPath;
4168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4169 locations->SetInAt(0, Location::RequiresRegister());
4170 locations->SetInAt(1, Location::Any());
4171 locations->SetOut(Location::RequiresRegister());
4172}
4173
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004174void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004175 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004176 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004177 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004178 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004179 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4180 Label done, zero;
4181 SlowPathCodeX86_64* slow_path = nullptr;
4182
4183 // Return 0 if `obj` is null.
4184 // TODO: avoid this check if we know obj is not null.
4185 __ testl(obj, obj);
4186 __ j(kEqual, &zero);
4187 // Compare the class of `obj` with `cls`.
4188 __ movl(out, Address(obj, class_offset));
4189 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004190 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004191 } else {
4192 DCHECK(cls.IsStackSlot()) << cls;
4193 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4194 }
4195 if (instruction->IsClassFinal()) {
4196 // Classes must be equal for the instanceof to succeed.
4197 __ j(kNotEqual, &zero);
4198 __ movl(out, Immediate(1));
4199 __ jmp(&done);
4200 } else {
4201 // If the classes are not equal, we go into a slow path.
4202 DCHECK(locations->OnlyCallsOnSlowPath());
4203 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004204 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004205 codegen_->AddSlowPath(slow_path);
4206 __ j(kNotEqual, slow_path->GetEntryLabel());
4207 __ movl(out, Immediate(1));
4208 __ jmp(&done);
4209 }
4210 __ Bind(&zero);
4211 __ movl(out, Immediate(0));
4212 if (slow_path != nullptr) {
4213 __ Bind(slow_path->GetExitLabel());
4214 }
4215 __ Bind(&done);
4216}
4217
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004218void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4219 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4220 instruction, LocationSummary::kCallOnSlowPath);
4221 locations->SetInAt(0, Location::RequiresRegister());
4222 locations->SetInAt(1, Location::Any());
4223 locations->AddTemp(Location::RequiresRegister());
4224}
4225
4226void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4227 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004228 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004229 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004230 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004231 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4232 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4233 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4234 codegen_->AddSlowPath(slow_path);
4235
4236 // TODO: avoid this check if we know obj is not null.
4237 __ testl(obj, obj);
4238 __ j(kEqual, slow_path->GetExitLabel());
4239 // Compare the class of `obj` with `cls`.
4240 __ movl(temp, Address(obj, class_offset));
4241 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004242 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004243 } else {
4244 DCHECK(cls.IsStackSlot()) << cls;
4245 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4246 }
4247 // Classes must be equal for the checkcast to succeed.
4248 __ j(kNotEqual, slow_path->GetEntryLabel());
4249 __ Bind(slow_path->GetExitLabel());
4250}
4251
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004252void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4253 LocationSummary* locations =
4254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4255 InvokeRuntimeCallingConvention calling_convention;
4256 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4257}
4258
4259void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4260 __ gs()->call(Address::Absolute(instruction->IsEnter()
4261 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4262 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4263 true));
4264 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4265}
4266
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004267void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4268void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4269void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4270
4271void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4272 LocationSummary* locations =
4273 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4274 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4275 || instruction->GetResultType() == Primitive::kPrimLong);
4276 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004277 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004278 locations->SetOut(Location::SameAsFirstInput());
4279}
4280
4281void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4282 HandleBitwiseOperation(instruction);
4283}
4284
4285void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4286 HandleBitwiseOperation(instruction);
4287}
4288
4289void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4290 HandleBitwiseOperation(instruction);
4291}
4292
4293void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4294 LocationSummary* locations = instruction->GetLocations();
4295 Location first = locations->InAt(0);
4296 Location second = locations->InAt(1);
4297 DCHECK(first.Equals(locations->Out()));
4298
4299 if (instruction->GetResultType() == Primitive::kPrimInt) {
4300 if (second.IsRegister()) {
4301 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004302 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004303 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004304 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004305 } else {
4306 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004307 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004308 }
4309 } else if (second.IsConstant()) {
4310 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4311 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004312 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004313 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004314 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004315 } else {
4316 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004317 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004318 }
4319 } else {
4320 Address address(CpuRegister(RSP), second.GetStackIndex());
4321 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004322 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004323 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004324 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004325 } else {
4326 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004327 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004328 }
4329 }
4330 } else {
4331 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004332 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4333 bool second_is_constant = false;
4334 int64_t value = 0;
4335 if (second.IsConstant()) {
4336 second_is_constant = true;
4337 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004338 }
Mark Mendell40741f32015-04-20 22:10:34 -04004339 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004340
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004341 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004342 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004343 if (is_int32_value) {
4344 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4345 } else {
4346 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4347 }
4348 } else if (second.IsDoubleStackSlot()) {
4349 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004350 } else {
4351 __ andq(first_reg, second.AsRegister<CpuRegister>());
4352 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004353 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004354 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004355 if (is_int32_value) {
4356 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4357 } else {
4358 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4359 }
4360 } else if (second.IsDoubleStackSlot()) {
4361 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004362 } else {
4363 __ orq(first_reg, second.AsRegister<CpuRegister>());
4364 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004365 } else {
4366 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004367 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004368 if (is_int32_value) {
4369 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4370 } else {
4371 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4372 }
4373 } else if (second.IsDoubleStackSlot()) {
4374 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004375 } else {
4376 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4377 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004378 }
4379 }
4380}
4381
Calin Juravleb1498f62015-02-16 13:13:29 +00004382void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4383 // Nothing to do, this should be removed during prepare for register allocator.
4384 UNUSED(instruction);
4385 LOG(FATAL) << "Unreachable";
4386}
4387
4388void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4389 // Nothing to do, this should be removed during prepare for register allocator.
4390 UNUSED(instruction);
4391 LOG(FATAL) << "Unreachable";
4392}
4393
Mark Mendellf55c3e02015-03-26 21:07:46 -04004394void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4395 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004396 X86_64Assembler* assembler = GetAssembler();
4397 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004398 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4399 // byte values. If used for vectors at a later time, this will need to be
4400 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004401 assembler->Align(4, 0);
4402 constant_area_start_ = assembler->CodeSize();
4403 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004404 }
4405
4406 // And finish up.
4407 CodeGenerator::Finalize(allocator);
4408}
4409
4410/**
4411 * Class to handle late fixup of offsets into constant area.
4412 */
4413class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4414 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004415 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004416 : codegen_(codegen), offset_into_constant_area_(offset) {}
4417
4418 private:
4419 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4420 // Patch the correct offset for the instruction. We use the address of the
4421 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4422 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4423 int relative_position = constant_offset - pos;
4424
4425 // Patch in the right value.
4426 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4427 }
4428
Mark Mendell39dcf552015-04-09 20:42:42 -04004429 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004430
4431 // Location in constant area that the fixup refers to.
4432 int offset_into_constant_area_;
4433};
4434
4435Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4436 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4437 return Address::RIP(fixup);
4438}
4439
4440Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4441 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4442 return Address::RIP(fixup);
4443}
4444
4445Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4446 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4447 return Address::RIP(fixup);
4448}
4449
4450Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4451 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4452 return Address::RIP(fixup);
4453}
4454
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004455} // namespace x86_64
4456} // namespace art