blob: 0dd44239effdebc5c1eb4401f2510353b8c248e8 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080022#include "intrinsics.h"
23#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010027#include "mirror/object_reference.h"
28#include "thread.h"
29#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/managed_register_x86_64.h"
33
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010034namespace art {
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036namespace x86_64 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038// Some x86_64 instructions require a register to be available as temp.
39static constexpr Register TMP = R11;
40
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000043static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
50
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010051class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Alexandre Rames2ed20af2015-03-06 13:55:35 +000055 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010057 __ gs()->call(
58 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000059 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 }
61
62 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010063 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
65};
66
Calin Juravled0d48522014-11-04 16:40:20 +000067class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
68 public:
69 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
70
Alexandre Rames2ed20af2015-03-06 13:55:35 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000072 __ Bind(GetEntryLabel());
73 __ gs()->call(
74 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000075 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000076 }
77
78 private:
79 HDivZeroCheck* const instruction_;
80 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
81};
82
Calin Juravlebacfec32014-11-14 15:54:36 +000083class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000085 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
86 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +000090 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +000091 if (is_div_) {
92 __ negl(cpu_reg_);
93 } else {
94 __ movl(cpu_reg_, Immediate(0));
95 }
96
Calin Juravled6fb6cf2014-11-11 19:07:44 +000097 } else {
98 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +000099 if (is_div_) {
100 __ negq(cpu_reg_);
101 } else {
102 __ movq(cpu_reg_, Immediate(0));
103 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000104 }
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ jmp(GetExitLabel());
106 }
107
108 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000110 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000111 const bool is_div_;
112 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000113};
114
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100117 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000124 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000125 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ jmp(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
139 private:
140 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100141 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 Label return_label_;
143
144 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
145};
146
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100149 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
150 Location index_location,
151 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 : instruction_(instruction),
153 index_location_(index_location),
154 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000158 // We're moving two locations to locations that could overlap, so we need a parallel
159 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 codegen->EmitParallelMoves(
162 index_location_,
163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
164 length_location_,
165 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 __ gs()->call(Address::Absolute(
167 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000168 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100169 }
170
171 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 const Location index_location_;
174 const Location length_location_;
175
176 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
177};
178
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000179class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100180 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000181 LoadClassSlowPathX86_64(HLoadClass* cls,
182 HInstruction* at,
183 uint32_t dex_pc,
184 bool do_clinit)
185 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
186 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
187 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100188
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000190 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100191 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
192 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000199 __ gs()->call(Address::Absolute((do_clinit_
200 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
201 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000202 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000204 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000205 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 }
210
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 __ jmp(GetExitLabel());
213 }
214
215 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216 // The class this slow path will load.
217 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219 // The instruction where this slow path is happening.
220 // (Might be the load class or an initialization check).
221 HInstruction* const at_;
222
223 // The dex PC of `at_`.
224 const uint32_t dex_pc_;
225
226 // Whether to initialize the class.
227 const bool do_clinit_;
228
229 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230};
231
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
233 public:
234 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
235
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237 LocationSummary* locations = instruction_->GetLocations();
238 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
239
240 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
241 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000242 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243
244 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800245 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
246 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 Immediate(instruction_->GetStringIndex()));
248 __ gs()->call(Address::Absolute(
249 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000251 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 __ jmp(GetExitLabel());
254 }
255
256 private:
257 HLoadString* const instruction_;
258
259 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
260};
261
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
263 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000264 TypeCheckSlowPathX86_64(HInstruction* instruction,
265 Location class_to_check,
266 Location object_class,
267 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000269 class_to_check_(class_to_check),
270 object_class_(object_class),
271 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 DCHECK(instruction_->IsCheckCast()
276 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
278 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 // We're moving two locations to locations that could overlap, so we need a parallel
283 // move resolver.
284 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000285 codegen->EmitParallelMoves(
286 class_to_check_,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
288 object_class_,
289 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 if (instruction_->IsInstanceOf()) {
292 __ gs()->call(
293 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
294 } else {
295 DCHECK(instruction_->IsCheckCast());
296 __ gs()->call(
297 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
298 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000299 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300
301 if (instruction_->IsInstanceOf()) {
302 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
303 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000305 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 __ jmp(GetExitLabel());
307 }
308
309 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 HInstruction* const instruction_;
311 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
315 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
316};
317
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700318class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
319 public:
320 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
321 : instruction_(instruction) {}
322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 __ Bind(GetEntryLabel());
325 SaveLiveRegisters(codegen, instruction_->GetLocations());
326 __ gs()->call(
327 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeoptimize), true));
328 DCHECK(instruction_->IsDeoptimize());
329 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
330 uint32_t dex_pc = deoptimize->GetDexPc();
331 codegen->RecordPcInfo(instruction_, dex_pc, this);
332 }
333
334 private:
335 HInstruction* const instruction_;
336 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
337};
338
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100339#undef __
340#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
341
Dave Allison20dfc792014-06-16 20:44:29 -0700342inline Condition X86_64Condition(IfCondition cond) {
343 switch (cond) {
344 case kCondEQ: return kEqual;
345 case kCondNE: return kNotEqual;
346 case kCondLT: return kLess;
347 case kCondLE: return kLessEqual;
348 case kCondGT: return kGreater;
349 case kCondGE: return kGreaterEqual;
350 default:
351 LOG(FATAL) << "Unknown if condition";
352 }
353 return kEqual;
354}
355
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800356void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
357 CpuRegister temp) {
358 // All registers are assumed to be correctly set up.
359
360 // TODO: Implement all kinds of calls:
361 // 1) boot -> boot
362 // 2) app -> boot
363 // 3) app -> app
364 //
365 // Currently we implement the app -> app logic, which looks up in the resolve cache.
366
367 // temp = method;
368 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000369 if (!invoke->IsRecursive()) {
370 // temp = temp->dex_cache_resolved_methods_;
371 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
372 // temp = temp[index_in_cache]
373 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
374 // (temp + offset_of_quick_compiled_code)()
375 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
376 kX86_64WordSize).SizeValue()));
377 } else {
378 __ call(&frame_entry_label_);
379 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800380
381 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800382}
383
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100384void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
385 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
386}
387
388void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
389 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
390}
391
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100392size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
393 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
394 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100395}
396
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100397size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
398 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
399 return kX86_64WordSize;
400}
401
402size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
403 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
404 return kX86_64WordSize;
405}
406
407size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
408 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
409 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100410}
411
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000412static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000413// Use a fake return address register to mimic Quick.
414static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400415CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
416 const X86_64InstructionSetFeatures& isa_features,
417 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000418 : CodeGenerator(graph,
419 kNumberOfCpuRegisters,
420 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000421 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000422 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
423 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000424 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000425 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
426 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000427 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100428 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100429 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000430 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400431 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400432 isa_features_(isa_features),
433 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000434 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
435}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100437InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
438 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100439 : HGraphVisitor(graph),
440 assembler_(codegen->GetAssembler()),
441 codegen_(codegen) {}
442
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100444 switch (type) {
445 case Primitive::kPrimLong:
446 case Primitive::kPrimByte:
447 case Primitive::kPrimBoolean:
448 case Primitive::kPrimChar:
449 case Primitive::kPrimShort:
450 case Primitive::kPrimInt:
451 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100452 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100453 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454 }
455
456 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100457 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100459 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100460 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100461
462 case Primitive::kPrimVoid:
463 LOG(FATAL) << "Unreachable type " << type;
464 }
465
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100466 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100467}
468
Nicolas Geoffray98893962015-01-21 12:32:32 +0000469void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100470 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100471 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100472
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000473 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100474 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000475
Nicolas Geoffray98893962015-01-21 12:32:32 +0000476 if (is_baseline) {
477 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
478 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
479 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000480 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
481 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
482 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000483 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100484}
485
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100486static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100487 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100488}
David Srbecky9d8606d2015-04-12 09:35:32 +0100489
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100490static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100491 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100492}
493
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100494void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100495 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000496 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100497 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700498 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000499 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100500
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100502 __ testq(CpuRegister(RAX), Address(
503 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100504 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100505 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000506
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000507 if (HasEmptyFrame()) {
508 return;
509 }
510
Nicolas Geoffray98893962015-01-21 12:32:32 +0000511 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000512 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000513 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000514 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100515 __ cfi().AdjustCFAOffset(kX86_64WordSize);
516 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000517 }
518 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100519
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100520 int adjust = GetFrameSize() - GetCoreSpillSize();
521 __ subq(CpuRegister(RSP), Immediate(adjust));
522 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000523 uint32_t xmm_spill_location = GetFpuSpillStart();
524 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100525
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000526 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
527 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100528 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
529 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
530 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100532 }
533
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100534 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
535}
536
537void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100538 __ cfi().RememberState();
539 if (!HasEmptyFrame()) {
540 uint32_t xmm_spill_location = GetFpuSpillStart();
541 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
542 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
543 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
544 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
545 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
546 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
547 }
548 }
549
550 int adjust = GetFrameSize() - GetCoreSpillSize();
551 __ addq(CpuRegister(RSP), Immediate(adjust));
552 __ cfi().AdjustCFAOffset(-adjust);
553
554 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
555 Register reg = kCoreCalleeSaves[i];
556 if (allocated_registers_.ContainsCoreRegister(reg)) {
557 __ popq(CpuRegister(reg));
558 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
559 __ cfi().Restore(DWARFReg(reg));
560 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000561 }
562 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100563 __ ret();
564 __ cfi().RestoreState();
565 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100566}
567
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100568void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
569 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100570}
571
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100572void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000573 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
575}
576
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100577Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
578 switch (load->GetType()) {
579 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100581 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100582
583 case Primitive::kPrimInt:
584 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100585 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100587
588 case Primitive::kPrimBoolean:
589 case Primitive::kPrimByte:
590 case Primitive::kPrimChar:
591 case Primitive::kPrimShort:
592 case Primitive::kPrimVoid:
593 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700594 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100595 }
596
597 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700598 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100599}
600
601void CodeGeneratorX86_64::Move(Location destination, Location source) {
602 if (source.Equals(destination)) {
603 return;
604 }
605 if (destination.IsRegister()) {
606 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000609 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100610 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000611 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100612 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100613 } else {
614 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000615 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100616 Address(CpuRegister(RSP), source.GetStackIndex()));
617 }
618 } else if (destination.IsFpuRegister()) {
619 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000620 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100621 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 Address(CpuRegister(RSP), source.GetStackIndex()));
626 } else {
627 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100630 }
631 } else if (destination.IsStackSlot()) {
632 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100633 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000634 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 } else if (source.IsFpuRegister()) {
636 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000637 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500638 } else if (source.IsConstant()) {
639 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000640 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500641 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100642 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500643 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000644 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
645 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100646 }
647 } else {
648 DCHECK(destination.IsDoubleStackSlot());
649 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000651 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100652 } else if (source.IsFpuRegister()) {
653 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000654 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500655 } else if (source.IsConstant()) {
656 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800657 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500658 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000659 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500660 } else {
661 DCHECK(constant->IsLongConstant());
662 value = constant->AsLongConstant()->GetValue();
663 }
664 __ movq(CpuRegister(TMP), Immediate(value));
665 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100666 } else {
667 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000668 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
669 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100670 }
671 }
672}
673
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100674void CodeGeneratorX86_64::Move(HInstruction* instruction,
675 Location location,
676 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000677 LocationSummary* locations = instruction->GetLocations();
678 if (locations != nullptr && locations->Out().Equals(location)) {
679 return;
680 }
681
682 if (locations != nullptr && locations->Out().IsConstant()) {
683 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000684 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
685 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000686 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000687 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000688 } else if (location.IsStackSlot()) {
689 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
690 } else {
691 DCHECK(location.IsConstant());
692 DCHECK_EQ(location.GetConstant(), const_to_move);
693 }
694 } else if (const_to_move->IsLongConstant()) {
695 int64_t value = const_to_move->AsLongConstant()->GetValue();
696 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000697 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000698 } else if (location.IsDoubleStackSlot()) {
699 __ movq(CpuRegister(TMP), Immediate(value));
700 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
701 } else {
702 DCHECK(location.IsConstant());
703 DCHECK_EQ(location.GetConstant(), const_to_move);
704 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100705 }
Roland Levillain476df552014-10-09 17:51:36 +0100706 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100707 switch (instruction->GetType()) {
708 case Primitive::kPrimBoolean:
709 case Primitive::kPrimByte:
710 case Primitive::kPrimChar:
711 case Primitive::kPrimShort:
712 case Primitive::kPrimInt:
713 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100714 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100715 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
716 break;
717
718 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100719 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000720 Move(location,
721 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722 break;
723
724 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000727 } else if (instruction->IsTemporary()) {
728 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
729 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100731 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732 switch (instruction->GetType()) {
733 case Primitive::kPrimBoolean:
734 case Primitive::kPrimByte:
735 case Primitive::kPrimChar:
736 case Primitive::kPrimShort:
737 case Primitive::kPrimInt:
738 case Primitive::kPrimNot:
739 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 case Primitive::kPrimFloat:
741 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000742 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743 break;
744
745 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100746 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100747 }
748 }
749}
750
751void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
752 got->SetLocations(nullptr);
753}
754
755void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
756 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100757 DCHECK(!successor->IsExitBlock());
758
759 HBasicBlock* block = got->GetBlock();
760 HInstruction* previous = got->GetPrevious();
761
762 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000763 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100764 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
765 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
766 return;
767 }
768
769 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
770 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
771 }
772 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100773 __ jmp(codegen_->GetLabelOf(successor));
774 }
775}
776
777void LocationsBuilderX86_64::VisitExit(HExit* exit) {
778 exit->SetLocations(nullptr);
779}
780
781void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700782 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100783}
784
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700785void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
786 Label* true_target,
787 Label* false_target,
788 Label* always_true_target) {
789 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100790 if (cond->IsIntConstant()) {
791 // Constant condition, statically compared against 1.
792 int32_t cond_value = cond->AsIntConstant()->GetValue();
793 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700794 if (always_true_target != nullptr) {
795 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100796 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100797 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100798 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100799 DCHECK_EQ(cond_value, 0);
800 }
801 } else {
802 bool materialized =
803 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
804 // Moves do not affect the eflags register, so if the condition is
805 // evaluated just before the if, we don't need to evaluate it
806 // again.
807 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700808 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100809 if (materialized) {
810 if (!eflags_set) {
811 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700812 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100813 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000814 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100815 } else {
816 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
817 Immediate(0));
818 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700819 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100820 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700821 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 }
823 } else {
824 Location lhs = cond->GetLocations()->InAt(0);
825 Location rhs = cond->GetLocations()->InAt(1);
826 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000827 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100828 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000829 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000830 if (constant == 0) {
831 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
832 } else {
833 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
834 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100835 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000836 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100837 Address(CpuRegister(RSP), rhs.GetStackIndex()));
838 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700839 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700840 }
Dave Allison20dfc792014-06-16 20:44:29 -0700841 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700842 if (false_target != nullptr) {
843 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100844 }
845}
846
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700847void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
848 LocationSummary* locations =
849 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
850 HInstruction* cond = if_instr->InputAt(0);
851 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
852 locations->SetInAt(0, Location::Any());
853 }
854}
855
856void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
857 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
858 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
859 Label* always_true_target = true_target;
860 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
861 if_instr->IfTrueSuccessor())) {
862 always_true_target = nullptr;
863 }
864 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
865 if_instr->IfFalseSuccessor())) {
866 false_target = nullptr;
867 }
868 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
869}
870
871void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
872 LocationSummary* locations = new (GetGraph()->GetArena())
873 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
874 HInstruction* cond = deoptimize->InputAt(0);
875 DCHECK(cond->IsCondition());
876 if (cond->AsCondition()->NeedsMaterialization()) {
877 locations->SetInAt(0, Location::Any());
878 }
879}
880
881void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
882 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
883 DeoptimizationSlowPathX86_64(deoptimize);
884 codegen_->AddSlowPath(slow_path);
885 Label* slow_path_entry = slow_path->GetEntryLabel();
886 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
887}
888
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100889void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
890 local->SetLocations(nullptr);
891}
892
893void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
894 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
895}
896
897void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
898 local->SetLocations(nullptr);
899}
900
901void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
902 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700903 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100904}
905
906void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100907 LocationSummary* locations =
908 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100909 switch (store->InputAt(1)->GetType()) {
910 case Primitive::kPrimBoolean:
911 case Primitive::kPrimByte:
912 case Primitive::kPrimChar:
913 case Primitive::kPrimShort:
914 case Primitive::kPrimInt:
915 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100916 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100917 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
918 break;
919
920 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
923 break;
924
925 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100926 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100928}
929
930void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700931 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100932}
933
Dave Allison20dfc792014-06-16 20:44:29 -0700934void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100935 LocationSummary* locations =
936 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100937 locations->SetInAt(0, Location::RequiresRegister());
938 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100939 if (comp->NeedsMaterialization()) {
940 locations->SetOut(Location::RequiresRegister());
941 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942}
943
Dave Allison20dfc792014-06-16 20:44:29 -0700944void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
945 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100946 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000947 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100948 // Clear register: setcc only sets the low byte.
949 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000950 Location lhs = locations->InAt(0);
951 Location rhs = locations->InAt(1);
952 if (rhs.IsRegister()) {
953 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
954 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800955 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000956 if (constant == 0) {
957 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
958 } else {
959 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
960 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100961 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000962 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100963 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100964 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700965 }
966}
967
968void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
969 VisitCondition(comp);
970}
971
972void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
973 VisitCondition(comp);
974}
975
976void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
977 VisitCondition(comp);
978}
979
980void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
981 VisitCondition(comp);
982}
983
984void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
985 VisitCondition(comp);
986}
987
988void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
989 VisitCondition(comp);
990}
991
992void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
993 VisitCondition(comp);
994}
995
996void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
997 VisitCondition(comp);
998}
999
1000void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1001 VisitCondition(comp);
1002}
1003
1004void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1005 VisitCondition(comp);
1006}
1007
1008void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1009 VisitCondition(comp);
1010}
1011
1012void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1013 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001014}
1015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001016void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 LocationSummary* locations =
1018 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001019 switch (compare->InputAt(0)->GetType()) {
1020 case Primitive::kPrimLong: {
1021 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001022 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00001023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1024 break;
1025 }
1026 case Primitive::kPrimFloat:
1027 case Primitive::kPrimDouble: {
1028 locations->SetInAt(0, Location::RequiresFpuRegister());
1029 locations->SetInAt(1, Location::RequiresFpuRegister());
1030 locations->SetOut(Location::RequiresRegister());
1031 break;
1032 }
1033 default:
1034 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1035 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001036}
1037
1038void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001039 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001040 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001041 Location left = locations->InAt(0);
1042 Location right = locations->InAt(1);
1043
1044 Label less, greater, done;
1045 Primitive::Type type = compare->InputAt(0)->GetType();
1046 switch (type) {
1047 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001048 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1049 if (right.IsConstant()) {
1050 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1051 DCHECK(IsInt<32>(value));
1052 if (value == 0) {
1053 __ testq(left_reg, left_reg);
1054 } else {
1055 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1056 }
1057 } else {
1058 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1059 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001060 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001061 }
1062 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001063 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001064 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1065 break;
1066 }
1067 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001068 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001069 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1070 break;
1071 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001072 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001073 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001074 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001075 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001076 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001077 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001078
Calin Juravle91debbc2014-11-26 19:01:09 +00001079 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001080 __ movl(out, Immediate(1));
1081 __ jmp(&done);
1082
1083 __ Bind(&less);
1084 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001085
1086 __ Bind(&done);
1087}
1088
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001089void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001090 LocationSummary* locations =
1091 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001092 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001093}
1094
1095void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001096 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001097 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098}
1099
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001100void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1101 LocationSummary* locations =
1102 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1103 locations->SetOut(Location::ConstantLocation(constant));
1104}
1105
1106void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1107 // Will be generated at use site.
1108 UNUSED(constant);
1109}
1110
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001111void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001112 LocationSummary* locations =
1113 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001114 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001115}
1116
1117void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001118 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001119 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001120}
1121
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001122void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1123 LocationSummary* locations =
1124 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1125 locations->SetOut(Location::ConstantLocation(constant));
1126}
1127
1128void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1129 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001130 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001131}
1132
1133void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1134 LocationSummary* locations =
1135 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1136 locations->SetOut(Location::ConstantLocation(constant));
1137}
1138
1139void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1140 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001141 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001142}
1143
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001144void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1145 ret->SetLocations(nullptr);
1146}
1147
1148void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001149 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151}
1152
1153void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001154 LocationSummary* locations =
1155 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001156 switch (ret->InputAt(0)->GetType()) {
1157 case Primitive::kPrimBoolean:
1158 case Primitive::kPrimByte:
1159 case Primitive::kPrimChar:
1160 case Primitive::kPrimShort:
1161 case Primitive::kPrimInt:
1162 case Primitive::kPrimNot:
1163 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001164 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001165 break;
1166
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001167 case Primitive::kPrimFloat:
1168 case Primitive::kPrimDouble:
1169 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 break;
1172
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001173 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001174 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001175 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001176}
1177
1178void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1179 if (kIsDebugBuild) {
1180 switch (ret->InputAt(0)->GetType()) {
1181 case Primitive::kPrimBoolean:
1182 case Primitive::kPrimByte:
1183 case Primitive::kPrimChar:
1184 case Primitive::kPrimShort:
1185 case Primitive::kPrimInt:
1186 case Primitive::kPrimNot:
1187 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001189 break;
1190
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 case Primitive::kPrimFloat:
1192 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001193 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 XMM0);
1195 break;
1196
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001197 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001198 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001199 }
1200 }
1201 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202}
1203
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001204Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1205 switch (type) {
1206 case Primitive::kPrimBoolean:
1207 case Primitive::kPrimByte:
1208 case Primitive::kPrimChar:
1209 case Primitive::kPrimShort:
1210 case Primitive::kPrimInt:
1211 case Primitive::kPrimNot: {
1212 uint32_t index = gp_index_++;
1213 stack_index_++;
1214 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001215 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216 } else {
1217 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1218 }
1219 }
1220
1221 case Primitive::kPrimLong: {
1222 uint32_t index = gp_index_;
1223 stack_index_ += 2;
1224 if (index < calling_convention.GetNumberOfRegisters()) {
1225 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001226 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001227 } else {
1228 gp_index_ += 2;
1229 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1230 }
1231 }
1232
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 case Primitive::kPrimFloat: {
1234 uint32_t index = fp_index_++;
1235 stack_index_++;
1236 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001237 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 } else {
1239 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1240 }
1241 }
1242
1243 case Primitive::kPrimDouble: {
1244 uint32_t index = fp_index_++;
1245 stack_index_ += 2;
1246 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001247 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 } else {
1249 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1250 }
1251 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001252
1253 case Primitive::kPrimVoid:
1254 LOG(FATAL) << "Unexpected parameter type " << type;
1255 break;
1256 }
1257 return Location();
1258}
1259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001260void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001261 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001262 if (intrinsic.TryDispatch(invoke)) {
1263 return;
1264 }
1265
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001266 HandleInvoke(invoke);
1267}
1268
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001269static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1270 if (invoke->GetLocations()->Intrinsified()) {
1271 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1272 intrinsic.Dispatch(invoke);
1273 return true;
1274 }
1275 return false;
1276}
1277
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001278void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001279 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1280 return;
1281 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001282
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001283 codegen_->GenerateStaticOrDirectCall(
1284 invoke,
1285 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001286 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001287}
1288
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001289void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001290 LocationSummary* locations =
1291 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001292 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001293
1294 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001295 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296 HInstruction* input = invoke->InputAt(i);
1297 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1298 }
1299
1300 switch (invoke->GetType()) {
1301 case Primitive::kPrimBoolean:
1302 case Primitive::kPrimByte:
1303 case Primitive::kPrimChar:
1304 case Primitive::kPrimShort:
1305 case Primitive::kPrimInt:
1306 case Primitive::kPrimNot:
1307 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001309 break;
1310
1311 case Primitive::kPrimVoid:
1312 break;
1313
1314 case Primitive::kPrimDouble:
1315 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001316 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001317 break;
1318 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319}
1320
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001321void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001322 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001323 if (intrinsic.TryDispatch(invoke)) {
1324 return;
1325 }
1326
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 HandleInvoke(invoke);
1328}
1329
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001330void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001331 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1332 return;
1333 }
1334
Roland Levillain271ab9c2014-11-27 15:23:57 +00001335 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001336 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1337 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1338 LocationSummary* locations = invoke->GetLocations();
1339 Location receiver = locations->InAt(0);
1340 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1341 // temp = object->GetClass();
1342 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1344 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001345 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001346 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001347 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001348 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001349 // temp = temp->GetMethodAt(method_offset);
1350 __ movl(temp, Address(temp, method_offset));
1351 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001352 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001353 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001354
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001355 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001356 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001357}
1358
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001359void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1360 HandleInvoke(invoke);
1361 // Add the hidden argument.
1362 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1363}
1364
1365void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1366 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001367 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001368 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1369 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1370 LocationSummary* locations = invoke->GetLocations();
1371 Location receiver = locations->InAt(0);
1372 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1373
1374 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001375 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001376 Immediate(invoke->GetDexMethodIndex()));
1377
1378 // temp = object->GetClass();
1379 if (receiver.IsStackSlot()) {
1380 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1381 __ movl(temp, Address(temp, class_offset));
1382 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001383 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001384 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001385 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001386 // temp = temp->GetImtEntryAt(method_offset);
1387 __ movl(temp, Address(temp, method_offset));
1388 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001389 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001390 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001391
1392 DCHECK(!codegen_->IsLeafMethod());
1393 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1394}
1395
Roland Levillain88cb1752014-10-20 16:36:47 +01001396void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1397 LocationSummary* locations =
1398 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1399 switch (neg->GetResultType()) {
1400 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001401 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001402 locations->SetInAt(0, Location::RequiresRegister());
1403 locations->SetOut(Location::SameAsFirstInput());
1404 break;
1405
Roland Levillain88cb1752014-10-20 16:36:47 +01001406 case Primitive::kPrimFloat:
1407 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001408 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001409 locations->SetOut(Location::SameAsFirstInput());
1410 locations->AddTemp(Location::RequiresRegister());
1411 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001412 break;
1413
1414 default:
1415 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1416 }
1417}
1418
1419void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1420 LocationSummary* locations = neg->GetLocations();
1421 Location out = locations->Out();
1422 Location in = locations->InAt(0);
1423 switch (neg->GetResultType()) {
1424 case Primitive::kPrimInt:
1425 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001426 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001427 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001428 break;
1429
1430 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001431 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001432 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001433 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001434 break;
1435
Roland Levillain5368c212014-11-27 15:03:41 +00001436 case Primitive::kPrimFloat: {
1437 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001438 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1439 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001440 // Implement float negation with an exclusive or with value
1441 // 0x80000000 (mask for bit 31, representing the sign of a
1442 // single-precision floating-point number).
1443 __ movq(constant, Immediate(INT64_C(0x80000000)));
1444 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001445 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001446 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001447 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001448
Roland Levillain5368c212014-11-27 15:03:41 +00001449 case Primitive::kPrimDouble: {
1450 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001451 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1452 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001453 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001454 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001455 // a double-precision floating-point number).
1456 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1457 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001458 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001459 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001460 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001461
1462 default:
1463 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1464 }
1465}
1466
Roland Levillaindff1f282014-11-05 14:15:05 +00001467void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1468 LocationSummary* locations =
1469 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1470 Primitive::Type result_type = conversion->GetResultType();
1471 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001472 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001473
David Brazdilb2bd1c52015-03-25 11:17:37 +00001474 // The Java language does not allow treating boolean as an integral type but
1475 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001476
Roland Levillaindff1f282014-11-05 14:15:05 +00001477 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001478 case Primitive::kPrimByte:
1479 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001480 case Primitive::kPrimBoolean:
1481 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001482 case Primitive::kPrimShort:
1483 case Primitive::kPrimInt:
1484 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001485 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001486 locations->SetInAt(0, Location::Any());
1487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1488 break;
1489
1490 default:
1491 LOG(FATAL) << "Unexpected type conversion from " << input_type
1492 << " to " << result_type;
1493 }
1494 break;
1495
Roland Levillain01a8d712014-11-14 16:27:39 +00001496 case Primitive::kPrimShort:
1497 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001498 case Primitive::kPrimBoolean:
1499 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001500 case Primitive::kPrimByte:
1501 case Primitive::kPrimInt:
1502 case Primitive::kPrimChar:
1503 // Processing a Dex `int-to-short' instruction.
1504 locations->SetInAt(0, Location::Any());
1505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1506 break;
1507
1508 default:
1509 LOG(FATAL) << "Unexpected type conversion from " << input_type
1510 << " to " << result_type;
1511 }
1512 break;
1513
Roland Levillain946e1432014-11-11 17:35:19 +00001514 case Primitive::kPrimInt:
1515 switch (input_type) {
1516 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001517 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001518 locations->SetInAt(0, Location::Any());
1519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1520 break;
1521
1522 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001523 // Processing a Dex `float-to-int' instruction.
1524 locations->SetInAt(0, Location::RequiresFpuRegister());
1525 locations->SetOut(Location::RequiresRegister());
1526 locations->AddTemp(Location::RequiresFpuRegister());
1527 break;
1528
Roland Levillain946e1432014-11-11 17:35:19 +00001529 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001530 // Processing a Dex `double-to-int' instruction.
1531 locations->SetInAt(0, Location::RequiresFpuRegister());
1532 locations->SetOut(Location::RequiresRegister());
1533 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001534 break;
1535
1536 default:
1537 LOG(FATAL) << "Unexpected type conversion from " << input_type
1538 << " to " << result_type;
1539 }
1540 break;
1541
Roland Levillaindff1f282014-11-05 14:15:05 +00001542 case Primitive::kPrimLong:
1543 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001544 case Primitive::kPrimBoolean:
1545 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001546 case Primitive::kPrimByte:
1547 case Primitive::kPrimShort:
1548 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001549 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001550 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001551 // TODO: We would benefit from a (to-be-implemented)
1552 // Location::RegisterOrStackSlot requirement for this input.
1553 locations->SetInAt(0, Location::RequiresRegister());
1554 locations->SetOut(Location::RequiresRegister());
1555 break;
1556
1557 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001558 // Processing a Dex `float-to-long' instruction.
1559 locations->SetInAt(0, Location::RequiresFpuRegister());
1560 locations->SetOut(Location::RequiresRegister());
1561 locations->AddTemp(Location::RequiresFpuRegister());
1562 break;
1563
Roland Levillaindff1f282014-11-05 14:15:05 +00001564 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001565 // Processing a Dex `double-to-long' instruction.
1566 locations->SetInAt(0, Location::RequiresFpuRegister());
1567 locations->SetOut(Location::RequiresRegister());
1568 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001569 break;
1570
1571 default:
1572 LOG(FATAL) << "Unexpected type conversion from " << input_type
1573 << " to " << result_type;
1574 }
1575 break;
1576
Roland Levillain981e4542014-11-14 11:47:14 +00001577 case Primitive::kPrimChar:
1578 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001579 case Primitive::kPrimBoolean:
1580 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001581 case Primitive::kPrimByte:
1582 case Primitive::kPrimShort:
1583 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001584 // Processing a Dex `int-to-char' instruction.
1585 locations->SetInAt(0, Location::Any());
1586 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1587 break;
1588
1589 default:
1590 LOG(FATAL) << "Unexpected type conversion from " << input_type
1591 << " to " << result_type;
1592 }
1593 break;
1594
Roland Levillaindff1f282014-11-05 14:15:05 +00001595 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001596 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001597 case Primitive::kPrimBoolean:
1598 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001599 case Primitive::kPrimByte:
1600 case Primitive::kPrimShort:
1601 case Primitive::kPrimInt:
1602 case Primitive::kPrimChar:
1603 // Processing a Dex `int-to-float' instruction.
1604 locations->SetInAt(0, Location::RequiresRegister());
1605 locations->SetOut(Location::RequiresFpuRegister());
1606 break;
1607
1608 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001609 // Processing a Dex `long-to-float' instruction.
1610 locations->SetInAt(0, Location::RequiresRegister());
1611 locations->SetOut(Location::RequiresFpuRegister());
1612 break;
1613
Roland Levillaincff13742014-11-17 14:32:17 +00001614 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001615 // Processing a Dex `double-to-float' instruction.
1616 locations->SetInAt(0, Location::RequiresFpuRegister());
1617 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001618 break;
1619
1620 default:
1621 LOG(FATAL) << "Unexpected type conversion from " << input_type
1622 << " to " << result_type;
1623 };
1624 break;
1625
Roland Levillaindff1f282014-11-05 14:15:05 +00001626 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001627 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001628 case Primitive::kPrimBoolean:
1629 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001630 case Primitive::kPrimByte:
1631 case Primitive::kPrimShort:
1632 case Primitive::kPrimInt:
1633 case Primitive::kPrimChar:
1634 // Processing a Dex `int-to-double' instruction.
1635 locations->SetInAt(0, Location::RequiresRegister());
1636 locations->SetOut(Location::RequiresFpuRegister());
1637 break;
1638
1639 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001640 // Processing a Dex `long-to-double' instruction.
1641 locations->SetInAt(0, Location::RequiresRegister());
1642 locations->SetOut(Location::RequiresFpuRegister());
1643 break;
1644
Roland Levillaincff13742014-11-17 14:32:17 +00001645 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001646 // Processing a Dex `float-to-double' instruction.
1647 locations->SetInAt(0, Location::RequiresFpuRegister());
1648 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001649 break;
1650
1651 default:
1652 LOG(FATAL) << "Unexpected type conversion from " << input_type
1653 << " to " << result_type;
1654 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001655 break;
1656
1657 default:
1658 LOG(FATAL) << "Unexpected type conversion from " << input_type
1659 << " to " << result_type;
1660 }
1661}
1662
1663void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1664 LocationSummary* locations = conversion->GetLocations();
1665 Location out = locations->Out();
1666 Location in = locations->InAt(0);
1667 Primitive::Type result_type = conversion->GetResultType();
1668 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001669 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001670 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001671 case Primitive::kPrimByte:
1672 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001673 case Primitive::kPrimBoolean:
1674 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001675 case Primitive::kPrimShort:
1676 case Primitive::kPrimInt:
1677 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001678 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001679 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001681 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001683 Address(CpuRegister(RSP), in.GetStackIndex()));
1684 } else {
1685 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001686 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001687 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1688 }
1689 break;
1690
1691 default:
1692 LOG(FATAL) << "Unexpected type conversion from " << input_type
1693 << " to " << result_type;
1694 }
1695 break;
1696
Roland Levillain01a8d712014-11-14 16:27:39 +00001697 case Primitive::kPrimShort:
1698 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001699 case Primitive::kPrimBoolean:
1700 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001701 case Primitive::kPrimByte:
1702 case Primitive::kPrimInt:
1703 case Primitive::kPrimChar:
1704 // Processing a Dex `int-to-short' instruction.
1705 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001706 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001707 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001708 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001709 Address(CpuRegister(RSP), in.GetStackIndex()));
1710 } else {
1711 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001712 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001713 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1714 }
1715 break;
1716
1717 default:
1718 LOG(FATAL) << "Unexpected type conversion from " << input_type
1719 << " to " << result_type;
1720 }
1721 break;
1722
Roland Levillain946e1432014-11-11 17:35:19 +00001723 case Primitive::kPrimInt:
1724 switch (input_type) {
1725 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001726 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001727 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001729 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001730 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001731 Address(CpuRegister(RSP), in.GetStackIndex()));
1732 } else {
1733 DCHECK(in.IsConstant());
1734 DCHECK(in.GetConstant()->IsLongConstant());
1735 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001736 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001737 }
1738 break;
1739
Roland Levillain3f8f9362014-12-02 17:45:01 +00001740 case Primitive::kPrimFloat: {
1741 // Processing a Dex `float-to-int' instruction.
1742 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1743 CpuRegister output = out.AsRegister<CpuRegister>();
1744 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1745 Label done, nan;
1746
1747 __ movl(output, Immediate(kPrimIntMax));
1748 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001749 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001750 // if input >= temp goto done
1751 __ comiss(input, temp);
1752 __ j(kAboveEqual, &done);
1753 // if input == NaN goto nan
1754 __ j(kUnordered, &nan);
1755 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001756 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001757 __ jmp(&done);
1758 __ Bind(&nan);
1759 // output = 0
1760 __ xorl(output, output);
1761 __ Bind(&done);
1762 break;
1763 }
1764
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001765 case Primitive::kPrimDouble: {
1766 // Processing a Dex `double-to-int' instruction.
1767 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1768 CpuRegister output = out.AsRegister<CpuRegister>();
1769 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1770 Label done, nan;
1771
1772 __ movl(output, Immediate(kPrimIntMax));
1773 // temp = int-to-double(output)
1774 __ cvtsi2sd(temp, output);
1775 // if input >= temp goto done
1776 __ comisd(input, temp);
1777 __ j(kAboveEqual, &done);
1778 // if input == NaN goto nan
1779 __ j(kUnordered, &nan);
1780 // output = double-to-int-truncate(input)
1781 __ cvttsd2si(output, input);
1782 __ jmp(&done);
1783 __ Bind(&nan);
1784 // output = 0
1785 __ xorl(output, output);
1786 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001787 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001788 }
Roland Levillain946e1432014-11-11 17:35:19 +00001789
1790 default:
1791 LOG(FATAL) << "Unexpected type conversion from " << input_type
1792 << " to " << result_type;
1793 }
1794 break;
1795
Roland Levillaindff1f282014-11-05 14:15:05 +00001796 case Primitive::kPrimLong:
1797 switch (input_type) {
1798 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001799 case Primitive::kPrimBoolean:
1800 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001801 case Primitive::kPrimByte:
1802 case Primitive::kPrimShort:
1803 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001804 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001805 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001806 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001807 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 break;
1809
Roland Levillain624279f2014-12-04 11:54:28 +00001810 case Primitive::kPrimFloat: {
1811 // Processing a Dex `float-to-long' instruction.
1812 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1813 CpuRegister output = out.AsRegister<CpuRegister>();
1814 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1815 Label done, nan;
1816
1817 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001818 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001819 __ cvtsi2ss(temp, output, true);
1820 // if input >= temp goto done
1821 __ comiss(input, temp);
1822 __ j(kAboveEqual, &done);
1823 // if input == NaN goto nan
1824 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001825 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001826 __ cvttss2si(output, input, true);
1827 __ jmp(&done);
1828 __ Bind(&nan);
1829 // output = 0
1830 __ xorq(output, output);
1831 __ Bind(&done);
1832 break;
1833 }
1834
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001835 case Primitive::kPrimDouble: {
1836 // Processing a Dex `double-to-long' instruction.
1837 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1838 CpuRegister output = out.AsRegister<CpuRegister>();
1839 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1840 Label done, nan;
1841
1842 __ movq(output, Immediate(kPrimLongMax));
1843 // temp = long-to-double(output)
1844 __ cvtsi2sd(temp, output, true);
1845 // if input >= temp goto done
1846 __ comisd(input, temp);
1847 __ j(kAboveEqual, &done);
1848 // if input == NaN goto nan
1849 __ j(kUnordered, &nan);
1850 // output = double-to-long-truncate(input)
1851 __ cvttsd2si(output, input, true);
1852 __ jmp(&done);
1853 __ Bind(&nan);
1854 // output = 0
1855 __ xorq(output, output);
1856 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001857 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001858 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001859
1860 default:
1861 LOG(FATAL) << "Unexpected type conversion from " << input_type
1862 << " to " << result_type;
1863 }
1864 break;
1865
Roland Levillain981e4542014-11-14 11:47:14 +00001866 case Primitive::kPrimChar:
1867 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001868 case Primitive::kPrimBoolean:
1869 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001870 case Primitive::kPrimByte:
1871 case Primitive::kPrimShort:
1872 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001873 // Processing a Dex `int-to-char' instruction.
1874 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001875 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001876 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001877 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001878 Address(CpuRegister(RSP), in.GetStackIndex()));
1879 } else {
1880 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001881 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001882 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1883 }
1884 break;
1885
1886 default:
1887 LOG(FATAL) << "Unexpected type conversion from " << input_type
1888 << " to " << result_type;
1889 }
1890 break;
1891
Roland Levillaindff1f282014-11-05 14:15:05 +00001892 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001893 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001894 case Primitive::kPrimBoolean:
1895 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001896 case Primitive::kPrimByte:
1897 case Primitive::kPrimShort:
1898 case Primitive::kPrimInt:
1899 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001900 // Processing a Dex `int-to-float' instruction.
1901 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001902 break;
1903
1904 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001905 // Processing a Dex `long-to-float' instruction.
1906 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1907 break;
1908
Roland Levillaincff13742014-11-17 14:32:17 +00001909 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001910 // Processing a Dex `double-to-float' instruction.
1911 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001912 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::kPrimDouble:
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-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001929 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001930 break;
1931
1932 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001933 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001935 break;
1936
Roland Levillaincff13742014-11-17 14:32:17 +00001937 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001938 // Processing a Dex `float-to-double' instruction.
1939 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001940 break;
1941
1942 default:
1943 LOG(FATAL) << "Unexpected type conversion from " << input_type
1944 << " to " << result_type;
1945 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001946 break;
1947
1948 default:
1949 LOG(FATAL) << "Unexpected type conversion from " << input_type
1950 << " to " << result_type;
1951 }
1952}
1953
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001955 LocationSummary* locations =
1956 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001957 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001958 case Primitive::kPrimInt: {
1959 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001960 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001962 break;
1963 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001964
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001965 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001966 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001967 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001968 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001970 break;
1971 }
1972
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001973 case Primitive::kPrimDouble:
1974 case Primitive::kPrimFloat: {
1975 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04001976 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001977 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001978 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001979 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001980
1981 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001982 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001983 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001984}
1985
1986void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1987 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001988 Location first = locations->InAt(0);
1989 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001990 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001991
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001992 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001993 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001994 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001995 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1996 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1997 } else {
1998 __ leal(out.AsRegister<CpuRegister>(), Address(
1999 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2000 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002001 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002002 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2003 __ addl(out.AsRegister<CpuRegister>(),
2004 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2005 } else {
2006 __ leal(out.AsRegister<CpuRegister>(), Address(
2007 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2008 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002009 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002010 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002011 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002012 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002013 break;
2014 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002015
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002017 if (second.IsRegister()) {
2018 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2019 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2020 } else {
2021 __ leaq(out.AsRegister<CpuRegister>(), Address(
2022 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2023 }
2024 } else {
2025 DCHECK(second.IsConstant());
2026 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2027 int32_t int32_value = Low32Bits(value);
2028 DCHECK_EQ(int32_value, value);
2029 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2030 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2031 } else {
2032 __ leaq(out.AsRegister<CpuRegister>(), Address(
2033 first.AsRegister<CpuRegister>(), int32_value));
2034 }
2035 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036 break;
2037 }
2038
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002039 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002040 if (second.IsFpuRegister()) {
2041 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2042 } else if (second.IsConstant()) {
2043 __ addss(first.AsFpuRegister<XmmRegister>(),
2044 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2045 } else {
2046 DCHECK(second.IsStackSlot());
2047 __ addss(first.AsFpuRegister<XmmRegister>(),
2048 Address(CpuRegister(RSP), second.GetStackIndex()));
2049 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002050 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002051 }
2052
2053 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002054 if (second.IsFpuRegister()) {
2055 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2056 } else if (second.IsConstant()) {
2057 __ addsd(first.AsFpuRegister<XmmRegister>(),
2058 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2059 } else {
2060 DCHECK(second.IsDoubleStackSlot());
2061 __ addsd(first.AsFpuRegister<XmmRegister>(),
2062 Address(CpuRegister(RSP), second.GetStackIndex()));
2063 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002064 break;
2065 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002066
2067 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002068 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002069 }
2070}
2071
2072void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002073 LocationSummary* locations =
2074 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002075 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002076 case Primitive::kPrimInt: {
2077 locations->SetInAt(0, Location::RequiresRegister());
2078 locations->SetInAt(1, Location::Any());
2079 locations->SetOut(Location::SameAsFirstInput());
2080 break;
2081 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002082 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002083 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002084 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002085 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002086 break;
2087 }
Calin Juravle11351682014-10-23 15:38:15 +01002088 case Primitive::kPrimFloat:
2089 case Primitive::kPrimDouble: {
2090 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002091 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002093 break;
Calin Juravle11351682014-10-23 15:38:15 +01002094 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002095 default:
Calin Juravle11351682014-10-23 15:38:15 +01002096 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002097 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002098}
2099
2100void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2101 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002102 Location first = locations->InAt(0);
2103 Location second = locations->InAt(1);
2104 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002105 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002106 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002107 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002108 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002109 } else if (second.IsConstant()) {
2110 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002111 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002112 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002114 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002115 break;
2116 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002117 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002118 if (second.IsConstant()) {
2119 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2120 DCHECK(IsInt<32>(value));
2121 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2122 } else {
2123 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2124 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125 break;
2126 }
2127
Calin Juravle11351682014-10-23 15:38:15 +01002128 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002129 if (second.IsFpuRegister()) {
2130 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2131 } else if (second.IsConstant()) {
2132 __ subss(first.AsFpuRegister<XmmRegister>(),
2133 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2134 } else {
2135 DCHECK(second.IsStackSlot());
2136 __ subss(first.AsFpuRegister<XmmRegister>(),
2137 Address(CpuRegister(RSP), second.GetStackIndex()));
2138 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002139 break;
Calin Juravle11351682014-10-23 15:38:15 +01002140 }
2141
2142 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002143 if (second.IsFpuRegister()) {
2144 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2145 } else if (second.IsConstant()) {
2146 __ subsd(first.AsFpuRegister<XmmRegister>(),
2147 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2148 } else {
2149 DCHECK(second.IsDoubleStackSlot());
2150 __ subsd(first.AsFpuRegister<XmmRegister>(),
2151 Address(CpuRegister(RSP), second.GetStackIndex()));
2152 }
Calin Juravle11351682014-10-23 15:38:15 +01002153 break;
2154 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002155
2156 default:
Calin Juravle11351682014-10-23 15:38:15 +01002157 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002158 }
2159}
2160
Calin Juravle34bacdf2014-10-07 20:23:36 +01002161void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2162 LocationSummary* locations =
2163 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2164 switch (mul->GetResultType()) {
2165 case Primitive::kPrimInt: {
2166 locations->SetInAt(0, Location::RequiresRegister());
2167 locations->SetInAt(1, Location::Any());
2168 locations->SetOut(Location::SameAsFirstInput());
2169 break;
2170 }
2171 case Primitive::kPrimLong: {
2172 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002173 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2174 if (locations->InAt(1).IsConstant()) {
2175 // Can use 3 operand multiply.
2176 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2177 } else {
2178 locations->SetOut(Location::SameAsFirstInput());
2179 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002180 break;
2181 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002182 case Primitive::kPrimFloat:
2183 case Primitive::kPrimDouble: {
2184 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002185 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002186 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002187 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002188 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002189
2190 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002191 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002192 }
2193}
2194
2195void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2196 LocationSummary* locations = mul->GetLocations();
2197 Location first = locations->InAt(0);
2198 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002199 switch (mul->GetResultType()) {
2200 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002201 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002202 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002203 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002204 } else if (second.IsConstant()) {
2205 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002206 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002207 } else {
2208 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002209 __ imull(first.AsRegister<CpuRegister>(),
2210 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002211 }
2212 break;
2213 }
2214 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002215 if (second.IsConstant()) {
2216 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2217 DCHECK(IsInt<32>(value));
2218 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2219 first.AsRegister<CpuRegister>(),
2220 Immediate(static_cast<int32_t>(value)));
2221 } else {
2222 DCHECK(first.Equals(locations->Out()));
2223 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2224 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002225 break;
2226 }
2227
Calin Juravleb5bfa962014-10-21 18:02:24 +01002228 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002229 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002230 if (second.IsFpuRegister()) {
2231 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2232 } else if (second.IsConstant()) {
2233 __ mulss(first.AsFpuRegister<XmmRegister>(),
2234 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2235 } else {
2236 DCHECK(second.IsStackSlot());
2237 __ mulss(first.AsFpuRegister<XmmRegister>(),
2238 Address(CpuRegister(RSP), second.GetStackIndex()));
2239 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002240 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002241 }
2242
2243 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002244 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002245 if (second.IsFpuRegister()) {
2246 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2247 } else if (second.IsConstant()) {
2248 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2249 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2250 } else {
2251 DCHECK(second.IsDoubleStackSlot());
2252 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2253 Address(CpuRegister(RSP), second.GetStackIndex()));
2254 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002255 break;
2256 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002257
2258 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002259 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002260 }
2261}
2262
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002263void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2264 uint32_t stack_adjustment, bool is_float) {
2265 if (source.IsStackSlot()) {
2266 DCHECK(is_float);
2267 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2268 } else if (source.IsDoubleStackSlot()) {
2269 DCHECK(!is_float);
2270 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2271 } else {
2272 // Write the value to the temporary location on the stack and load to FP stack.
2273 if (is_float) {
2274 Location stack_temp = Location::StackSlot(temp_offset);
2275 codegen_->Move(stack_temp, source);
2276 __ flds(Address(CpuRegister(RSP), temp_offset));
2277 } else {
2278 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2279 codegen_->Move(stack_temp, source);
2280 __ fldl(Address(CpuRegister(RSP), temp_offset));
2281 }
2282 }
2283}
2284
2285void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2286 Primitive::Type type = rem->GetResultType();
2287 bool is_float = type == Primitive::kPrimFloat;
2288 size_t elem_size = Primitive::ComponentSize(type);
2289 LocationSummary* locations = rem->GetLocations();
2290 Location first = locations->InAt(0);
2291 Location second = locations->InAt(1);
2292 Location out = locations->Out();
2293
2294 // Create stack space for 2 elements.
2295 // TODO: enhance register allocator to ask for stack temporaries.
2296 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2297
2298 // Load the values to the FP stack in reverse order, using temporaries if needed.
2299 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2300 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2301
2302 // Loop doing FPREM until we stabilize.
2303 Label retry;
2304 __ Bind(&retry);
2305 __ fprem();
2306
2307 // Move FP status to AX.
2308 __ fstsw();
2309
2310 // And see if the argument reduction is complete. This is signaled by the
2311 // C2 FPU flag bit set to 0.
2312 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2313 __ j(kNotEqual, &retry);
2314
2315 // We have settled on the final value. Retrieve it into an XMM register.
2316 // Store FP top of stack to real stack.
2317 if (is_float) {
2318 __ fsts(Address(CpuRegister(RSP), 0));
2319 } else {
2320 __ fstl(Address(CpuRegister(RSP), 0));
2321 }
2322
2323 // Pop the 2 items from the FP stack.
2324 __ fucompp();
2325
2326 // Load the value from the stack into an XMM register.
2327 DCHECK(out.IsFpuRegister()) << out;
2328 if (is_float) {
2329 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2330 } else {
2331 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2332 }
2333
2334 // And remove the temporary stack space we allocated.
2335 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2336}
2337
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002338void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2339 DCHECK(instruction->IsDiv() || instruction->IsRem());
2340
2341 LocationSummary* locations = instruction->GetLocations();
2342 Location second = locations->InAt(1);
2343 DCHECK(second.IsConstant());
2344
2345 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2346 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002347 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002348
2349 DCHECK(imm == 1 || imm == -1);
2350
2351 switch (instruction->GetResultType()) {
2352 case Primitive::kPrimInt: {
2353 if (instruction->IsRem()) {
2354 __ xorl(output_register, output_register);
2355 } else {
2356 __ movl(output_register, input_register);
2357 if (imm == -1) {
2358 __ negl(output_register);
2359 }
2360 }
2361 break;
2362 }
2363
2364 case Primitive::kPrimLong: {
2365 if (instruction->IsRem()) {
2366 __ xorq(output_register, output_register);
2367 } else {
2368 __ movq(output_register, input_register);
2369 if (imm == -1) {
2370 __ negq(output_register);
2371 }
2372 }
2373 break;
2374 }
2375
2376 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002377 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002378 }
2379}
2380
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002381void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002382 LocationSummary* locations = instruction->GetLocations();
2383 Location second = locations->InAt(1);
2384
2385 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2386 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2387
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002388 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002389
2390 DCHECK(IsPowerOfTwo(std::abs(imm)));
2391
2392 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2393
2394 if (instruction->GetResultType() == Primitive::kPrimInt) {
2395 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2396 __ testl(numerator, numerator);
2397 __ cmov(kGreaterEqual, tmp, numerator);
2398 int shift = CTZ(imm);
2399 __ sarl(tmp, Immediate(shift));
2400
2401 if (imm < 0) {
2402 __ negl(tmp);
2403 }
2404
2405 __ movl(output_register, tmp);
2406 } else {
2407 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2408 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2409
2410 __ movq(rdx, Immediate(std::abs(imm) - 1));
2411 __ addq(rdx, numerator);
2412 __ testq(numerator, numerator);
2413 __ cmov(kGreaterEqual, rdx, numerator);
2414 int shift = CTZ(imm);
2415 __ sarq(rdx, Immediate(shift));
2416
2417 if (imm < 0) {
2418 __ negq(rdx);
2419 }
2420
2421 __ movq(output_register, rdx);
2422 }
2423}
2424
2425void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2426 DCHECK(instruction->IsDiv() || instruction->IsRem());
2427
2428 LocationSummary* locations = instruction->GetLocations();
2429 Location second = locations->InAt(1);
2430
2431 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2432 : locations->GetTemp(0).AsRegister<CpuRegister>();
2433 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2434 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2435 : locations->Out().AsRegister<CpuRegister>();
2436 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2437
2438 DCHECK_EQ(RAX, eax.AsRegister());
2439 DCHECK_EQ(RDX, edx.AsRegister());
2440 if (instruction->IsDiv()) {
2441 DCHECK_EQ(RAX, out.AsRegister());
2442 } else {
2443 DCHECK_EQ(RDX, out.AsRegister());
2444 }
2445
2446 int64_t magic;
2447 int shift;
2448
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002449 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002450 if (instruction->GetResultType() == Primitive::kPrimInt) {
2451 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2452
2453 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2454
2455 __ movl(numerator, eax);
2456
2457 Label no_div;
2458 Label end;
2459 __ testl(eax, eax);
2460 __ j(kNotEqual, &no_div);
2461
2462 __ xorl(out, out);
2463 __ jmp(&end);
2464
2465 __ Bind(&no_div);
2466
2467 __ movl(eax, Immediate(magic));
2468 __ imull(numerator);
2469
2470 if (imm > 0 && magic < 0) {
2471 __ addl(edx, numerator);
2472 } else if (imm < 0 && magic > 0) {
2473 __ subl(edx, numerator);
2474 }
2475
2476 if (shift != 0) {
2477 __ sarl(edx, Immediate(shift));
2478 }
2479
2480 __ movl(eax, edx);
2481 __ shrl(edx, Immediate(31));
2482 __ addl(edx, eax);
2483
2484 if (instruction->IsRem()) {
2485 __ movl(eax, numerator);
2486 __ imull(edx, Immediate(imm));
2487 __ subl(eax, edx);
2488 __ movl(edx, eax);
2489 } else {
2490 __ movl(eax, edx);
2491 }
2492 __ Bind(&end);
2493 } else {
2494 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2495
2496 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2497
2498 CpuRegister rax = eax;
2499 CpuRegister rdx = edx;
2500
2501 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2502
2503 // Save the numerator.
2504 __ movq(numerator, rax);
2505
2506 // RAX = magic
2507 __ movq(rax, Immediate(magic));
2508
2509 // RDX:RAX = magic * numerator
2510 __ imulq(numerator);
2511
2512 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002513 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002514 __ addq(rdx, numerator);
2515 } else if (imm < 0 && magic > 0) {
2516 // RDX -= numerator
2517 __ subq(rdx, numerator);
2518 }
2519
2520 // Shift if needed.
2521 if (shift != 0) {
2522 __ sarq(rdx, Immediate(shift));
2523 }
2524
2525 // RDX += 1 if RDX < 0
2526 __ movq(rax, rdx);
2527 __ shrq(rdx, Immediate(63));
2528 __ addq(rdx, rax);
2529
2530 if (instruction->IsRem()) {
2531 __ movq(rax, numerator);
2532
2533 if (IsInt<32>(imm)) {
2534 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2535 } else {
2536 __ movq(numerator, Immediate(imm));
2537 __ imulq(rdx, numerator);
2538 }
2539
2540 __ subq(rax, rdx);
2541 __ movq(rdx, rax);
2542 } else {
2543 __ movq(rax, rdx);
2544 }
2545 }
2546}
2547
Calin Juravlebacfec32014-11-14 15:54:36 +00002548void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2549 DCHECK(instruction->IsDiv() || instruction->IsRem());
2550 Primitive::Type type = instruction->GetResultType();
2551 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2552
2553 bool is_div = instruction->IsDiv();
2554 LocationSummary* locations = instruction->GetLocations();
2555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002556 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2557 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002558
Roland Levillain271ab9c2014-11-27 15:23:57 +00002559 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002560 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002561
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002562 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002563 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002564
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002565 if (imm == 0) {
2566 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2567 } else if (imm == 1 || imm == -1) {
2568 DivRemOneOrMinusOne(instruction);
2569 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002570 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002571 } else {
2572 DCHECK(imm <= -2 || imm >= 2);
2573 GenerateDivRemWithAnyConstant(instruction);
2574 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002575 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002576 SlowPathCodeX86_64* slow_path =
2577 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2578 out.AsRegister(), type, is_div);
2579 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002580
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002581 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2582 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2583 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2584 // so it's safe to just use negl instead of more complex comparisons.
2585 if (type == Primitive::kPrimInt) {
2586 __ cmpl(second_reg, Immediate(-1));
2587 __ j(kEqual, slow_path->GetEntryLabel());
2588 // edx:eax <- sign-extended of eax
2589 __ cdq();
2590 // eax = quotient, edx = remainder
2591 __ idivl(second_reg);
2592 } else {
2593 __ cmpq(second_reg, Immediate(-1));
2594 __ j(kEqual, slow_path->GetEntryLabel());
2595 // rdx:rax <- sign-extended of rax
2596 __ cqo();
2597 // rax = quotient, rdx = remainder
2598 __ idivq(second_reg);
2599 }
2600 __ Bind(slow_path->GetExitLabel());
2601 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002602}
2603
Calin Juravle7c4954d2014-10-28 16:57:40 +00002604void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2605 LocationSummary* locations =
2606 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2607 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002608 case Primitive::kPrimInt:
2609 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002610 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002611 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002612 locations->SetOut(Location::SameAsFirstInput());
2613 // Intel uses edx:eax as the dividend.
2614 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002615 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2616 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2617 // output and request another temp.
2618 if (div->InputAt(1)->IsConstant()) {
2619 locations->AddTemp(Location::RequiresRegister());
2620 }
Calin Juravled0d48522014-11-04 16:40:20 +00002621 break;
2622 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002623
Calin Juravle7c4954d2014-10-28 16:57:40 +00002624 case Primitive::kPrimFloat:
2625 case Primitive::kPrimDouble: {
2626 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002627 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002628 locations->SetOut(Location::SameAsFirstInput());
2629 break;
2630 }
2631
2632 default:
2633 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2634 }
2635}
2636
2637void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2638 LocationSummary* locations = div->GetLocations();
2639 Location first = locations->InAt(0);
2640 Location second = locations->InAt(1);
2641 DCHECK(first.Equals(locations->Out()));
2642
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002643 Primitive::Type type = div->GetResultType();
2644 switch (type) {
2645 case Primitive::kPrimInt:
2646 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002647 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002648 break;
2649 }
2650
Calin Juravle7c4954d2014-10-28 16:57:40 +00002651 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002652 if (second.IsFpuRegister()) {
2653 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2654 } else if (second.IsConstant()) {
2655 __ divss(first.AsFpuRegister<XmmRegister>(),
2656 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2657 } else {
2658 DCHECK(second.IsStackSlot());
2659 __ divss(first.AsFpuRegister<XmmRegister>(),
2660 Address(CpuRegister(RSP), second.GetStackIndex()));
2661 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002662 break;
2663 }
2664
2665 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002666 if (second.IsFpuRegister()) {
2667 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2668 } else if (second.IsConstant()) {
2669 __ divsd(first.AsFpuRegister<XmmRegister>(),
2670 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2671 } else {
2672 DCHECK(second.IsDoubleStackSlot());
2673 __ divsd(first.AsFpuRegister<XmmRegister>(),
2674 Address(CpuRegister(RSP), second.GetStackIndex()));
2675 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002676 break;
2677 }
2678
2679 default:
2680 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2681 }
2682}
2683
Calin Juravlebacfec32014-11-14 15:54:36 +00002684void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002685 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002686 LocationSummary* locations =
2687 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002688
2689 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002690 case Primitive::kPrimInt:
2691 case Primitive::kPrimLong: {
2692 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002693 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002694 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2695 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002696 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2697 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2698 // output and request another temp.
2699 if (rem->InputAt(1)->IsConstant()) {
2700 locations->AddTemp(Location::RequiresRegister());
2701 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002702 break;
2703 }
2704
2705 case Primitive::kPrimFloat:
2706 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002707 locations->SetInAt(0, Location::Any());
2708 locations->SetInAt(1, Location::Any());
2709 locations->SetOut(Location::RequiresFpuRegister());
2710 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002711 break;
2712 }
2713
2714 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002715 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002716 }
2717}
2718
2719void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2720 Primitive::Type type = rem->GetResultType();
2721 switch (type) {
2722 case Primitive::kPrimInt:
2723 case Primitive::kPrimLong: {
2724 GenerateDivRemIntegral(rem);
2725 break;
2726 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002727 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002728 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002729 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002730 break;
2731 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002732 default:
2733 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2734 }
2735}
2736
Calin Juravled0d48522014-11-04 16:40:20 +00002737void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2740 locations->SetInAt(0, Location::Any());
2741 if (instruction->HasUses()) {
2742 locations->SetOut(Location::SameAsFirstInput());
2743 }
2744}
2745
2746void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2747 SlowPathCodeX86_64* slow_path =
2748 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2749 codegen_->AddSlowPath(slow_path);
2750
2751 LocationSummary* locations = instruction->GetLocations();
2752 Location value = locations->InAt(0);
2753
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002754 switch (instruction->GetType()) {
2755 case Primitive::kPrimInt: {
2756 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002758 __ j(kEqual, slow_path->GetEntryLabel());
2759 } else if (value.IsStackSlot()) {
2760 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2761 __ j(kEqual, slow_path->GetEntryLabel());
2762 } else {
2763 DCHECK(value.IsConstant()) << value;
2764 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2765 __ jmp(slow_path->GetEntryLabel());
2766 }
2767 }
2768 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002769 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002770 case Primitive::kPrimLong: {
2771 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002773 __ j(kEqual, slow_path->GetEntryLabel());
2774 } else if (value.IsDoubleStackSlot()) {
2775 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2776 __ j(kEqual, slow_path->GetEntryLabel());
2777 } else {
2778 DCHECK(value.IsConstant()) << value;
2779 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2780 __ jmp(slow_path->GetEntryLabel());
2781 }
2782 }
2783 break;
2784 }
2785 default:
2786 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002787 }
Calin Juravled0d48522014-11-04 16:40:20 +00002788}
2789
Calin Juravle9aec02f2014-11-18 23:06:35 +00002790void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2791 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2792
2793 LocationSummary* locations =
2794 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2795
2796 switch (op->GetResultType()) {
2797 case Primitive::kPrimInt:
2798 case Primitive::kPrimLong: {
2799 locations->SetInAt(0, Location::RequiresRegister());
2800 // The shift count needs to be in CL.
2801 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2802 locations->SetOut(Location::SameAsFirstInput());
2803 break;
2804 }
2805 default:
2806 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2807 }
2808}
2809
2810void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2811 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2812
2813 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002814 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002815 Location second = locations->InAt(1);
2816
2817 switch (op->GetResultType()) {
2818 case Primitive::kPrimInt: {
2819 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002820 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002821 if (op->IsShl()) {
2822 __ shll(first_reg, second_reg);
2823 } else if (op->IsShr()) {
2824 __ sarl(first_reg, second_reg);
2825 } else {
2826 __ shrl(first_reg, second_reg);
2827 }
2828 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002829 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002830 if (op->IsShl()) {
2831 __ shll(first_reg, imm);
2832 } else if (op->IsShr()) {
2833 __ sarl(first_reg, imm);
2834 } else {
2835 __ shrl(first_reg, imm);
2836 }
2837 }
2838 break;
2839 }
2840 case Primitive::kPrimLong: {
2841 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002843 if (op->IsShl()) {
2844 __ shlq(first_reg, second_reg);
2845 } else if (op->IsShr()) {
2846 __ sarq(first_reg, second_reg);
2847 } else {
2848 __ shrq(first_reg, second_reg);
2849 }
2850 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002851 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002852 if (op->IsShl()) {
2853 __ shlq(first_reg, imm);
2854 } else if (op->IsShr()) {
2855 __ sarq(first_reg, imm);
2856 } else {
2857 __ shrq(first_reg, imm);
2858 }
2859 }
2860 break;
2861 }
2862 default:
2863 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2864 }
2865}
2866
2867void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2868 HandleShift(shl);
2869}
2870
2871void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2872 HandleShift(shl);
2873}
2874
2875void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2876 HandleShift(shr);
2877}
2878
2879void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2880 HandleShift(shr);
2881}
2882
2883void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2884 HandleShift(ushr);
2885}
2886
2887void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2888 HandleShift(ushr);
2889}
2890
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002891void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002892 LocationSummary* locations =
2893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002894 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002895 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2896 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2897 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002898}
2899
2900void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2901 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002902 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002903 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2904
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002905 __ gs()->call(
2906 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002907
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002908 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002909 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002910}
2911
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002912void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2913 LocationSummary* locations =
2914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2915 InvokeRuntimeCallingConvention calling_convention;
2916 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002917 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002918 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002919 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002920}
2921
2922void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2923 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002924 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002925 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2926
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002927 __ gs()->call(
2928 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002929
2930 DCHECK(!codegen_->IsLeafMethod());
2931 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2932}
2933
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002934void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002935 LocationSummary* locations =
2936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002937 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2938 if (location.IsStackSlot()) {
2939 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2940 } else if (location.IsDoubleStackSlot()) {
2941 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2942 }
2943 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002944}
2945
2946void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2947 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002948 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002949}
2950
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002951void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002952 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002953 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002954 locations->SetInAt(0, Location::RequiresRegister());
2955 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002956}
2957
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002958void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2959 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2961 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002962 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002963 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002964 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002966 break;
2967
2968 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002970 break;
2971
2972 default:
2973 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2974 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002975}
2976
David Brazdil66d126e2015-04-03 16:02:44 +01002977void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
2978 LocationSummary* locations =
2979 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2980 locations->SetInAt(0, Location::RequiresRegister());
2981 locations->SetOut(Location::SameAsFirstInput());
2982}
2983
2984void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
2985 DCHECK_EQ(bool_not->InputAt(0)->GetType(), Primitive::kPrimBoolean);
2986 LocationSummary* locations = bool_not->GetLocations();
2987 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2988 locations->Out().AsRegister<CpuRegister>().AsRegister());
2989 Location out = locations->Out();
2990 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
2991}
2992
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002993void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002994 LocationSummary* locations =
2995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002996 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2997 locations->SetInAt(i, Location::Any());
2998 }
2999 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003000}
3001
3002void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003003 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003004 LOG(FATAL) << "Unimplemented";
3005}
3006
Calin Juravle52c48962014-12-16 17:02:57 +00003007void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3008 /*
3009 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3010 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3011 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3012 */
3013 switch (kind) {
3014 case MemBarrierKind::kAnyAny: {
3015 __ mfence();
3016 break;
3017 }
3018 case MemBarrierKind::kAnyStore:
3019 case MemBarrierKind::kLoadAny:
3020 case MemBarrierKind::kStoreStore: {
3021 // nop
3022 break;
3023 }
3024 default:
3025 LOG(FATAL) << "Unexpected memory barier " << kind;
3026 }
3027}
3028
3029void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3030 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3031
Nicolas Geoffray39468442014-09-02 15:17:15 +01003032 LocationSummary* locations =
3033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003034 locations->SetInAt(0, Location::RequiresRegister());
3035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3036}
3037
3038void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3039 const FieldInfo& field_info) {
3040 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3041
3042 LocationSummary* locations = instruction->GetLocations();
3043 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3044 Location out = locations->Out();
3045 bool is_volatile = field_info.IsVolatile();
3046 Primitive::Type field_type = field_info.GetFieldType();
3047 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3048
3049 switch (field_type) {
3050 case Primitive::kPrimBoolean: {
3051 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3052 break;
3053 }
3054
3055 case Primitive::kPrimByte: {
3056 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3057 break;
3058 }
3059
3060 case Primitive::kPrimShort: {
3061 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3062 break;
3063 }
3064
3065 case Primitive::kPrimChar: {
3066 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3067 break;
3068 }
3069
3070 case Primitive::kPrimInt:
3071 case Primitive::kPrimNot: {
3072 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3073 break;
3074 }
3075
3076 case Primitive::kPrimLong: {
3077 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3078 break;
3079 }
3080
3081 case Primitive::kPrimFloat: {
3082 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3083 break;
3084 }
3085
3086 case Primitive::kPrimDouble: {
3087 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3088 break;
3089 }
3090
3091 case Primitive::kPrimVoid:
3092 LOG(FATAL) << "Unreachable type " << field_type;
3093 UNREACHABLE();
3094 }
3095
Calin Juravle77520bc2015-01-12 18:45:46 +00003096 codegen_->MaybeRecordImplicitNullCheck(instruction);
3097
Calin Juravle52c48962014-12-16 17:02:57 +00003098 if (is_volatile) {
3099 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3100 }
3101}
3102
3103void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3104 const FieldInfo& field_info) {
3105 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3106
3107 LocationSummary* locations =
3108 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003109 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003110 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3111
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003112 locations->SetInAt(0, Location::RequiresRegister());
3113 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003114 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003115 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003116 locations->AddTemp(Location::RequiresRegister());
3117 locations->AddTemp(Location::RequiresRegister());
3118 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003119}
3120
Calin Juravle52c48962014-12-16 17:02:57 +00003121void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3122 const FieldInfo& field_info) {
3123 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3124
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003125 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003126 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3127 Location value = locations->InAt(1);
3128 bool is_volatile = field_info.IsVolatile();
3129 Primitive::Type field_type = field_info.GetFieldType();
3130 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3131
3132 if (is_volatile) {
3133 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3134 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003135
3136 switch (field_type) {
3137 case Primitive::kPrimBoolean:
3138 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003139 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003140 break;
3141 }
3142
3143 case Primitive::kPrimShort:
3144 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003145 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146 break;
3147 }
3148
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003149 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003150 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003151 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003152 break;
3153 }
3154
3155 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003156 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003157 break;
3158 }
3159
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003160 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003161 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003162 break;
3163 }
3164
3165 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003166 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003167 break;
3168 }
3169
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003170 case Primitive::kPrimVoid:
3171 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003172 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003173 }
Calin Juravle52c48962014-12-16 17:02:57 +00003174
Calin Juravle77520bc2015-01-12 18:45:46 +00003175 codegen_->MaybeRecordImplicitNullCheck(instruction);
3176
3177 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3178 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3179 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3180 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3181 }
3182
Calin Juravle52c48962014-12-16 17:02:57 +00003183 if (is_volatile) {
3184 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3185 }
3186}
3187
3188void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3189 HandleFieldSet(instruction, instruction->GetFieldInfo());
3190}
3191
3192void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3193 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194}
3195
3196void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003197 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003198}
3199
3200void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003201 HandleFieldGet(instruction, instruction->GetFieldInfo());
3202}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203
Calin Juravle52c48962014-12-16 17:02:57 +00003204void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3205 HandleFieldGet(instruction);
3206}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003207
Calin Juravle52c48962014-12-16 17:02:57 +00003208void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3209 HandleFieldGet(instruction, instruction->GetFieldInfo());
3210}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003211
Calin Juravle52c48962014-12-16 17:02:57 +00003212void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3213 HandleFieldSet(instruction, instruction->GetFieldInfo());
3214}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003215
Calin Juravle52c48962014-12-16 17:02:57 +00003216void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3217 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003218}
3219
3220void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003221 LocationSummary* locations =
3222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003223 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3224 ? Location::RequiresRegister()
3225 : Location::Any();
3226 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003227 if (instruction->HasUses()) {
3228 locations->SetOut(Location::SameAsFirstInput());
3229 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003230}
3231
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003232void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003233 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3234 return;
3235 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003236 LocationSummary* locations = instruction->GetLocations();
3237 Location obj = locations->InAt(0);
3238
3239 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3240 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3241}
3242
3243void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003244 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003245 codegen_->AddSlowPath(slow_path);
3246
3247 LocationSummary* locations = instruction->GetLocations();
3248 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003249
3250 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003251 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003252 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003253 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003254 } else {
3255 DCHECK(obj.IsConstant()) << obj;
3256 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3257 __ jmp(slow_path->GetEntryLabel());
3258 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003259 }
3260 __ j(kEqual, slow_path->GetEntryLabel());
3261}
3262
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003263void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3264 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3265 GenerateImplicitNullCheck(instruction);
3266 } else {
3267 GenerateExplicitNullCheck(instruction);
3268 }
3269}
3270
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003271void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003272 LocationSummary* locations =
3273 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003274 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003275 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003276 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278}
3279
3280void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3281 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 Location index = locations->InAt(1);
3284
3285 switch (instruction->GetType()) {
3286 case Primitive::kPrimBoolean: {
3287 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003288 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003289 if (index.IsConstant()) {
3290 __ movzxb(out, Address(obj,
3291 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3292 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003294 }
3295 break;
3296 }
3297
3298 case Primitive::kPrimByte: {
3299 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003300 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301 if (index.IsConstant()) {
3302 __ movsxb(out, Address(obj,
3303 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3304 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003305 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003306 }
3307 break;
3308 }
3309
3310 case Primitive::kPrimShort: {
3311 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003312 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003313 if (index.IsConstant()) {
3314 __ movsxw(out, Address(obj,
3315 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3316 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003317 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003318 }
3319 break;
3320 }
3321
3322 case Primitive::kPrimChar: {
3323 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003325 if (index.IsConstant()) {
3326 __ movzxw(out, Address(obj,
3327 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3328 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003329 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003330 }
3331 break;
3332 }
3333
3334 case Primitive::kPrimInt:
3335 case Primitive::kPrimNot: {
3336 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3337 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339 if (index.IsConstant()) {
3340 __ movl(out, Address(obj,
3341 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3342 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003344 }
3345 break;
3346 }
3347
3348 case Primitive::kPrimLong: {
3349 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003350 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003351 if (index.IsConstant()) {
3352 __ movq(out, Address(obj,
3353 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3354 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003356 }
3357 break;
3358 }
3359
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003360 case Primitive::kPrimFloat: {
3361 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003363 if (index.IsConstant()) {
3364 __ movss(out, Address(obj,
3365 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3366 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003367 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003368 }
3369 break;
3370 }
3371
3372 case Primitive::kPrimDouble: {
3373 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003374 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003375 if (index.IsConstant()) {
3376 __ movsd(out, Address(obj,
3377 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3378 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003380 }
3381 break;
3382 }
3383
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003384 case Primitive::kPrimVoid:
3385 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003386 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003387 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003389}
3390
3391void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003392 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003393
3394 bool needs_write_barrier =
3395 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3396 bool needs_runtime_call = instruction->NeedsTypeCheck();
3397
Nicolas Geoffray39468442014-09-02 15:17:15 +01003398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003399 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3400 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003401 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003402 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3403 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3404 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003406 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003407 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003408 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3409 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003410 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003411 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003412 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3413 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003414 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003415 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003416 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003417
3418 if (needs_write_barrier) {
3419 // Temporary registers for the write barrier.
3420 locations->AddTemp(Location::RequiresRegister());
3421 locations->AddTemp(Location::RequiresRegister());
3422 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003424}
3425
3426void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3427 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003428 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003429 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003430 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003431 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003432 bool needs_runtime_call = locations->WillCall();
3433 bool needs_write_barrier =
3434 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003435
3436 switch (value_type) {
3437 case Primitive::kPrimBoolean:
3438 case Primitive::kPrimByte: {
3439 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003440 if (index.IsConstant()) {
3441 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003442 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003443 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003444 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003445 __ movb(Address(obj, offset),
3446 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003447 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003448 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003449 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3451 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003452 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003453 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003454 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3455 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003457 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003458 break;
3459 }
3460
3461 case Primitive::kPrimShort:
3462 case Primitive::kPrimChar: {
3463 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 if (index.IsConstant()) {
3465 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003466 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003467 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003468 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003469 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003470 __ movw(Address(obj, offset),
3471 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003472 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003473 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003474 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003475 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003476 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3477 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003478 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003479 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003480 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003481 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3482 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003483 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003484 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003485 break;
3486 }
3487
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003488 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003489 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003490 if (!needs_runtime_call) {
3491 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3492 if (index.IsConstant()) {
3493 size_t offset =
3494 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3495 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003497 } else {
3498 DCHECK(value.IsConstant()) << value;
3499 __ movl(Address(obj, offset),
3500 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3501 }
3502 } else {
3503 DCHECK(index.IsRegister()) << index;
3504 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003505 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3506 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003507 } else {
3508 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003509 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003510 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3511 }
3512 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003513 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003514 if (needs_write_barrier) {
3515 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003516 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3517 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3518 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003519 }
3520 } else {
3521 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003522 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3523 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003524 DCHECK(!codegen_->IsLeafMethod());
3525 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3526 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003527 break;
3528 }
3529
3530 case Primitive::kPrimLong: {
3531 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003532 if (index.IsConstant()) {
3533 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003534 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003535 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003536 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003537 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003538 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3539 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003540 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003541 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003542 break;
3543 }
3544
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003545 case Primitive::kPrimFloat: {
3546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3547 if (index.IsConstant()) {
3548 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3549 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003551 } else {
3552 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003553 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3554 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003555 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003556 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003557 break;
3558 }
3559
3560 case Primitive::kPrimDouble: {
3561 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3562 if (index.IsConstant()) {
3563 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3564 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003565 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003566 } else {
3567 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003568 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3569 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003570 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003571 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003572 break;
3573 }
3574
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 case Primitive::kPrimVoid:
3576 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003577 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 }
3579}
3580
3581void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003582 LocationSummary* locations =
3583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003584 locations->SetInAt(0, Location::RequiresRegister());
3585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003586}
3587
3588void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3589 LocationSummary* locations = instruction->GetLocations();
3590 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3592 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003594 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003595}
3596
3597void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003598 LocationSummary* locations =
3599 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003600 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003601 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003602 if (instruction->HasUses()) {
3603 locations->SetOut(Location::SameAsFirstInput());
3604 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003605}
3606
3607void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3608 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003609 Location index_loc = locations->InAt(0);
3610 Location length_loc = locations->InAt(1);
3611 SlowPathCodeX86_64* slow_path =
3612 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003613 codegen_->AddSlowPath(slow_path);
3614
Mark Mendellf60c90b2015-03-04 15:12:59 -05003615 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3616 if (index_loc.IsConstant()) {
3617 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3618 __ cmpl(length, Immediate(value));
3619 } else {
3620 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3621 }
3622 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003623}
3624
3625void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3626 CpuRegister card,
3627 CpuRegister object,
3628 CpuRegister value) {
3629 Label is_null;
3630 __ testl(value, value);
3631 __ j(kEqual, &is_null);
3632 __ gs()->movq(card, Address::Absolute(
3633 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3634 __ movq(temp, object);
3635 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3636 __ movb(Address(temp, card, TIMES_1, 0), card);
3637 __ Bind(&is_null);
3638}
3639
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003640void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3641 temp->SetLocations(nullptr);
3642}
3643
3644void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3645 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003646 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003647}
3648
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003649void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003650 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003651 LOG(FATAL) << "Unimplemented";
3652}
3653
3654void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003655 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3656}
3657
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003658void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3659 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3660}
3661
3662void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003663 HBasicBlock* block = instruction->GetBlock();
3664 if (block->GetLoopInformation() != nullptr) {
3665 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3666 // The back edge will generate the suspend check.
3667 return;
3668 }
3669 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3670 // The goto will generate the suspend check.
3671 return;
3672 }
3673 GenerateSuspendCheck(instruction, nullptr);
3674}
3675
3676void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3677 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003678 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003679 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003680 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003681 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003682 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003683 if (successor == nullptr) {
3684 __ j(kNotEqual, slow_path->GetEntryLabel());
3685 __ Bind(slow_path->GetReturnLabel());
3686 } else {
3687 __ j(kEqual, codegen_->GetLabelOf(successor));
3688 __ jmp(slow_path->GetEntryLabel());
3689 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003690}
3691
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003692X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3693 return codegen_->GetAssembler();
3694}
3695
3696void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3697 MoveOperands* move = moves_.Get(index);
3698 Location source = move->GetSource();
3699 Location destination = move->GetDestination();
3700
3701 if (source.IsRegister()) {
3702 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003703 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003704 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003705 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003706 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003707 } else {
3708 DCHECK(destination.IsDoubleStackSlot());
3709 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003710 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003711 }
3712 } else if (source.IsStackSlot()) {
3713 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003714 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003715 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003716 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003717 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003718 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003719 } else {
3720 DCHECK(destination.IsStackSlot());
3721 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3722 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3723 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003724 } else if (source.IsDoubleStackSlot()) {
3725 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003727 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003728 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003729 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3730 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003731 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003732 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003733 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3734 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3735 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003736 } else if (source.IsConstant()) {
3737 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003738 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3739 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003740 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003741 if (value == 0) {
3742 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3743 } else {
3744 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3745 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003746 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003747 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003748 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003749 }
3750 } else if (constant->IsLongConstant()) {
3751 int64_t value = constant->AsLongConstant()->GetValue();
3752 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003754 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003755 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003756 __ movq(CpuRegister(TMP), Immediate(value));
3757 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3758 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003759 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003760 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003761 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003762 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003763 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003764 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3765 if (value == 0) {
3766 // easy FP 0.0.
3767 __ xorps(dest, dest);
3768 } else {
3769 __ movl(CpuRegister(TMP), imm);
3770 __ movd(dest, CpuRegister(TMP));
3771 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003772 } else {
3773 DCHECK(destination.IsStackSlot()) << destination;
3774 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3775 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003776 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003777 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003778 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003779 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003780 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003781 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003782 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3783 if (value == 0) {
3784 __ xorpd(dest, dest);
3785 } else {
3786 __ movq(CpuRegister(TMP), imm);
3787 __ movd(dest, CpuRegister(TMP));
3788 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003789 } else {
3790 DCHECK(destination.IsDoubleStackSlot()) << destination;
3791 __ movq(CpuRegister(TMP), imm);
3792 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3793 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003794 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003795 } else if (source.IsFpuRegister()) {
3796 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003797 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003798 } else if (destination.IsStackSlot()) {
3799 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003800 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003801 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003802 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003803 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003804 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003805 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003806 }
3807}
3808
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003809void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003810 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003811 __ movl(Address(CpuRegister(RSP), mem), reg);
3812 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003813}
3814
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003815void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003816 ScratchRegisterScope ensure_scratch(
3817 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3818
3819 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3820 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3821 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3822 Address(CpuRegister(RSP), mem2 + stack_offset));
3823 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3824 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3825 CpuRegister(ensure_scratch.GetRegister()));
3826}
3827
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003828void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3829 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3830 __ movq(Address(CpuRegister(RSP), mem), reg);
3831 __ movq(reg, CpuRegister(TMP));
3832}
3833
3834void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3835 ScratchRegisterScope ensure_scratch(
Mark Mendella5c19ce2015-04-01 12:51:05 -04003836 this, TMP, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003837
Mark Mendella5c19ce2015-04-01 12:51:05 -04003838 int temp_reg = ensure_scratch.GetRegister();
3839 if (temp_reg == kNoRegister) {
3840 // Use the stack as a temporary.
3841 // Save mem1 on the stack.
3842 __ pushq(Address(CpuRegister(RSP), mem1));
3843
3844 // Copy mem2 into mem1.
3845 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem2 + kX86_64WordSize));
3846 __ movq(Address(CpuRegister(RSP), mem1 + kX86_64WordSize), CpuRegister(TMP));
3847
3848 // Now pop mem1 into mem2.
3849 __ popq(Address(CpuRegister(RSP), mem2));
3850 } else {
3851 CpuRegister temp = CpuRegister(temp_reg);
3852 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1));
3853 __ movq(temp, Address(CpuRegister(RSP), mem2));
3854 __ movq(Address(CpuRegister(RSP), mem2), CpuRegister(TMP));
3855 __ movq(Address(CpuRegister(RSP), mem1), temp);
3856 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003857}
3858
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003859void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3860 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3861 __ movss(Address(CpuRegister(RSP), mem), reg);
3862 __ movd(reg, CpuRegister(TMP));
3863}
3864
Mark Mendella5c19ce2015-04-01 12:51:05 -04003865void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
3866 // Prefer to avoid xchg as it isn't speedy on smaller processors.
3867 __ movq(CpuRegister(TMP), reg1);
3868 __ movq(reg1, reg2);
3869 __ movq(reg2, CpuRegister(TMP));
3870}
3871
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003872void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3873 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3874 __ movsd(Address(CpuRegister(RSP), mem), reg);
3875 __ movd(reg, CpuRegister(TMP));
3876}
3877
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003878void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3879 MoveOperands* move = moves_.Get(index);
3880 Location source = move->GetSource();
3881 Location destination = move->GetDestination();
3882
3883 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04003884 Exchange64(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003885 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003887 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003888 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003889 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003890 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3891 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003893 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003894 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003895 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3896 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003897 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003898 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3899 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3900 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003901 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003903 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003905 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003907 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003908 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003909 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003910 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003911 }
3912}
3913
3914
3915void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3916 __ pushq(CpuRegister(reg));
3917}
3918
3919
3920void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3921 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003922}
3923
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003924void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3925 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3926 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3927 Immediate(mirror::Class::kStatusInitialized));
3928 __ j(kLess, slow_path->GetEntryLabel());
3929 __ Bind(slow_path->GetExitLabel());
3930 // No need for memory fence, thanks to the X86_64 memory model.
3931}
3932
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003933void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003934 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3935 ? LocationSummary::kCallOnSlowPath
3936 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003937 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003938 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003939 locations->SetOut(Location::RequiresRegister());
3940}
3941
3942void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003944 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003945 DCHECK(!cls->CanCallRuntime());
3946 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003947 codegen_->LoadCurrentMethod(out);
3948 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3949 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003950 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003951 codegen_->LoadCurrentMethod(out);
3952 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3953 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003954 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3955 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3956 codegen_->AddSlowPath(slow_path);
3957 __ testl(out, out);
3958 __ j(kEqual, slow_path->GetEntryLabel());
3959 if (cls->MustGenerateClinitCheck()) {
3960 GenerateClassInitializationCheck(slow_path, out);
3961 } else {
3962 __ Bind(slow_path->GetExitLabel());
3963 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003964 }
3965}
3966
3967void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3968 LocationSummary* locations =
3969 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3970 locations->SetInAt(0, Location::RequiresRegister());
3971 if (check->HasUses()) {
3972 locations->SetOut(Location::SameAsFirstInput());
3973 }
3974}
3975
3976void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003977 // We assume the class to not be null.
3978 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3979 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003980 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003981 GenerateClassInitializationCheck(slow_path,
3982 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003983}
3984
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003985void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3986 LocationSummary* locations =
3987 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3988 locations->SetOut(Location::RequiresRegister());
3989}
3990
3991void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3992 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3993 codegen_->AddSlowPath(slow_path);
3994
Roland Levillain271ab9c2014-11-27 15:23:57 +00003995 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003996 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003997 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3998 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003999 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4000 __ testl(out, out);
4001 __ j(kEqual, slow_path->GetEntryLabel());
4002 __ Bind(slow_path->GetExitLabel());
4003}
4004
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004005void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4006 LocationSummary* locations =
4007 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4008 locations->SetOut(Location::RequiresRegister());
4009}
4010
4011void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4012 Address address = Address::Absolute(
4013 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004014 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004015 __ gs()->movl(address, Immediate(0));
4016}
4017
4018void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4019 LocationSummary* locations =
4020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4021 InvokeRuntimeCallingConvention calling_convention;
4022 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4023}
4024
4025void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4026 __ gs()->call(
4027 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4028 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4029}
4030
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004031void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004032 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4033 ? LocationSummary::kNoCall
4034 : LocationSummary::kCallOnSlowPath;
4035 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4036 locations->SetInAt(0, Location::RequiresRegister());
4037 locations->SetInAt(1, Location::Any());
4038 locations->SetOut(Location::RequiresRegister());
4039}
4040
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004041void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004042 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004044 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004045 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004046 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4047 Label done, zero;
4048 SlowPathCodeX86_64* slow_path = nullptr;
4049
4050 // Return 0 if `obj` is null.
4051 // TODO: avoid this check if we know obj is not null.
4052 __ testl(obj, obj);
4053 __ j(kEqual, &zero);
4054 // Compare the class of `obj` with `cls`.
4055 __ movl(out, Address(obj, class_offset));
4056 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004057 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004058 } else {
4059 DCHECK(cls.IsStackSlot()) << cls;
4060 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4061 }
4062 if (instruction->IsClassFinal()) {
4063 // Classes must be equal for the instanceof to succeed.
4064 __ j(kNotEqual, &zero);
4065 __ movl(out, Immediate(1));
4066 __ jmp(&done);
4067 } else {
4068 // If the classes are not equal, we go into a slow path.
4069 DCHECK(locations->OnlyCallsOnSlowPath());
4070 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004071 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004072 codegen_->AddSlowPath(slow_path);
4073 __ j(kNotEqual, slow_path->GetEntryLabel());
4074 __ movl(out, Immediate(1));
4075 __ jmp(&done);
4076 }
4077 __ Bind(&zero);
4078 __ movl(out, Immediate(0));
4079 if (slow_path != nullptr) {
4080 __ Bind(slow_path->GetExitLabel());
4081 }
4082 __ Bind(&done);
4083}
4084
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004085void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4086 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4087 instruction, LocationSummary::kCallOnSlowPath);
4088 locations->SetInAt(0, Location::RequiresRegister());
4089 locations->SetInAt(1, Location::Any());
4090 locations->AddTemp(Location::RequiresRegister());
4091}
4092
4093void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4094 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004096 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004098 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4099 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4100 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4101 codegen_->AddSlowPath(slow_path);
4102
4103 // TODO: avoid this check if we know obj is not null.
4104 __ testl(obj, obj);
4105 __ j(kEqual, slow_path->GetExitLabel());
4106 // Compare the class of `obj` with `cls`.
4107 __ movl(temp, Address(obj, class_offset));
4108 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004110 } else {
4111 DCHECK(cls.IsStackSlot()) << cls;
4112 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4113 }
4114 // Classes must be equal for the checkcast to succeed.
4115 __ j(kNotEqual, slow_path->GetEntryLabel());
4116 __ Bind(slow_path->GetExitLabel());
4117}
4118
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004119void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4120 LocationSummary* locations =
4121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4122 InvokeRuntimeCallingConvention calling_convention;
4123 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4124}
4125
4126void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4127 __ gs()->call(Address::Absolute(instruction->IsEnter()
4128 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4129 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4130 true));
4131 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4132}
4133
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004134void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4135void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4136void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4137
4138void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4139 LocationSummary* locations =
4140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4141 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4142 || instruction->GetResultType() == Primitive::kPrimLong);
4143 locations->SetInAt(0, Location::RequiresRegister());
4144 if (instruction->GetType() == Primitive::kPrimInt) {
4145 locations->SetInAt(1, Location::Any());
4146 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004147 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004148 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004149 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004150 }
4151 locations->SetOut(Location::SameAsFirstInput());
4152}
4153
4154void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4155 HandleBitwiseOperation(instruction);
4156}
4157
4158void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4159 HandleBitwiseOperation(instruction);
4160}
4161
4162void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4163 HandleBitwiseOperation(instruction);
4164}
4165
4166void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4167 LocationSummary* locations = instruction->GetLocations();
4168 Location first = locations->InAt(0);
4169 Location second = locations->InAt(1);
4170 DCHECK(first.Equals(locations->Out()));
4171
4172 if (instruction->GetResultType() == Primitive::kPrimInt) {
4173 if (second.IsRegister()) {
4174 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004175 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004176 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004178 } else {
4179 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004180 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004181 }
4182 } else if (second.IsConstant()) {
4183 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4184 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004185 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004186 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004187 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004188 } else {
4189 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004190 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004191 }
4192 } else {
4193 Address address(CpuRegister(RSP), second.GetStackIndex());
4194 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004195 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004196 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004197 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004198 } else {
4199 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004200 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004201 }
4202 }
4203 } else {
4204 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004205 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4206 bool second_is_constant = false;
4207 int64_t value = 0;
4208 if (second.IsConstant()) {
4209 second_is_constant = true;
4210 value = second.GetConstant()->AsLongConstant()->GetValue();
4211 DCHECK(IsInt<32>(value));
4212 }
4213
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004214 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004215 if (second_is_constant) {
4216 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4217 } else {
4218 __ andq(first_reg, second.AsRegister<CpuRegister>());
4219 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004220 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004221 if (second_is_constant) {
4222 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4223 } else {
4224 __ orq(first_reg, second.AsRegister<CpuRegister>());
4225 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004226 } else {
4227 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004228 if (second_is_constant) {
4229 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4230 } else {
4231 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4232 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004233 }
4234 }
4235}
4236
Calin Juravleb1498f62015-02-16 13:13:29 +00004237void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4238 // Nothing to do, this should be removed during prepare for register allocator.
4239 UNUSED(instruction);
4240 LOG(FATAL) << "Unreachable";
4241}
4242
4243void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4244 // Nothing to do, this should be removed during prepare for register allocator.
4245 UNUSED(instruction);
4246 LOG(FATAL) << "Unreachable";
4247}
4248
Mark Mendellf55c3e02015-03-26 21:07:46 -04004249void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4250 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004251 X86_64Assembler* assembler = GetAssembler();
4252 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004253 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4254 // byte values. If used for vectors at a later time, this will need to be
4255 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004256 assembler->Align(4, 0);
4257 constant_area_start_ = assembler->CodeSize();
4258 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004259 }
4260
4261 // And finish up.
4262 CodeGenerator::Finalize(allocator);
4263}
4264
4265/**
4266 * Class to handle late fixup of offsets into constant area.
4267 */
4268class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4269 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004270 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004271 : codegen_(codegen), offset_into_constant_area_(offset) {}
4272
4273 private:
4274 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4275 // Patch the correct offset for the instruction. We use the address of the
4276 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4277 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4278 int relative_position = constant_offset - pos;
4279
4280 // Patch in the right value.
4281 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4282 }
4283
Mark Mendell39dcf552015-04-09 20:42:42 -04004284 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004285
4286 // Location in constant area that the fixup refers to.
4287 int offset_into_constant_area_;
4288};
4289
4290Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4291 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4292 return Address::RIP(fixup);
4293}
4294
4295Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4296 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4297 return Address::RIP(fixup);
4298}
4299
4300Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4301 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4302 return Address::RIP(fixup);
4303}
4304
4305Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4306 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4307 return Address::RIP(fixup);
4308}
4309
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004310} // namespace x86_64
4311} // namespace art