blob: 0cb35d2b88aff31cc1514b562e09daaa303dfc43 [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) {
487 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
488}
489static dwarf::Reg DWARFReg(FloatRegister reg) {
490 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
491}
492
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100493void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000495 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100496 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700497 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000498 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100499
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000500 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100501 __ testq(CpuRegister(RAX), Address(
502 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100503 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100504 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000505
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000506 if (HasEmptyFrame()) {
507 return;
508 }
509
Nicolas Geoffray98893962015-01-21 12:32:32 +0000510 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000511 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000512 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000513 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100514 __ cfi().AdjustCFAOffset(kX86_64WordSize);
515 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000516 }
517 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100518
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100519 int adjust = GetFrameSize() - GetCoreSpillSize();
520 __ subq(CpuRegister(RSP), Immediate(adjust));
521 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000522 uint32_t xmm_spill_location = GetFpuSpillStart();
523 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100524
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000525 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
526 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100527 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
528 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
529 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000530 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100531 }
532
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100533 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
534}
535
536void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100537 __ cfi().RememberState();
538 if (!HasEmptyFrame()) {
539 uint32_t xmm_spill_location = GetFpuSpillStart();
540 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
541 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
542 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
543 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
544 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
545 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
546 }
547 }
548
549 int adjust = GetFrameSize() - GetCoreSpillSize();
550 __ addq(CpuRegister(RSP), Immediate(adjust));
551 __ cfi().AdjustCFAOffset(-adjust);
552
553 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
554 Register reg = kCoreCalleeSaves[i];
555 if (allocated_registers_.ContainsCoreRegister(reg)) {
556 __ popq(CpuRegister(reg));
557 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
558 __ cfi().Restore(DWARFReg(reg));
559 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000560 }
561 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100562 __ ret();
563 __ cfi().RestoreState();
564 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100565}
566
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100567void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
568 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100569}
570
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100571void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000572 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100573 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
574}
575
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
577 switch (load->GetType()) {
578 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100579 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100581
582 case Primitive::kPrimInt:
583 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586
587 case Primitive::kPrimBoolean:
588 case Primitive::kPrimByte:
589 case Primitive::kPrimChar:
590 case Primitive::kPrimShort:
591 case Primitive::kPrimVoid:
592 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700593 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100594 }
595
596 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700597 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100598}
599
600void CodeGeneratorX86_64::Move(Location destination, Location source) {
601 if (source.Equals(destination)) {
602 return;
603 }
604 if (destination.IsRegister()) {
605 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000606 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100607 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000608 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000610 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100611 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100612 } else {
613 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000614 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100615 Address(CpuRegister(RSP), source.GetStackIndex()));
616 }
617 } else if (destination.IsFpuRegister()) {
618 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000623 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 Address(CpuRegister(RSP), source.GetStackIndex()));
625 } else {
626 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100629 }
630 } else if (destination.IsStackSlot()) {
631 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 } else if (source.IsFpuRegister()) {
635 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500637 } else if (source.IsConstant()) {
638 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000639 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500640 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500642 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000643 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
644 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100645 }
646 } else {
647 DCHECK(destination.IsDoubleStackSlot());
648 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100649 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (source.IsFpuRegister()) {
652 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500654 } else if (source.IsConstant()) {
655 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800656 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500657 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000658 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500659 } else {
660 DCHECK(constant->IsLongConstant());
661 value = constant->AsLongConstant()->GetValue();
662 }
663 __ movq(CpuRegister(TMP), Immediate(value));
664 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100665 } else {
666 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000667 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
668 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100669 }
670 }
671}
672
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100673void CodeGeneratorX86_64::Move(HInstruction* instruction,
674 Location location,
675 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000676 LocationSummary* locations = instruction->GetLocations();
677 if (locations != nullptr && locations->Out().Equals(location)) {
678 return;
679 }
680
681 if (locations != nullptr && locations->Out().IsConstant()) {
682 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000683 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
684 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000685 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000686 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000687 } else if (location.IsStackSlot()) {
688 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
689 } else {
690 DCHECK(location.IsConstant());
691 DCHECK_EQ(location.GetConstant(), const_to_move);
692 }
693 } else if (const_to_move->IsLongConstant()) {
694 int64_t value = const_to_move->AsLongConstant()->GetValue();
695 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000696 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000697 } else if (location.IsDoubleStackSlot()) {
698 __ movq(CpuRegister(TMP), Immediate(value));
699 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
700 } else {
701 DCHECK(location.IsConstant());
702 DCHECK_EQ(location.GetConstant(), const_to_move);
703 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704 }
Roland Levillain476df552014-10-09 17:51:36 +0100705 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100706 switch (instruction->GetType()) {
707 case Primitive::kPrimBoolean:
708 case Primitive::kPrimByte:
709 case Primitive::kPrimChar:
710 case Primitive::kPrimShort:
711 case Primitive::kPrimInt:
712 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100714 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
715 break;
716
717 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000719 Move(location,
720 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100721 break;
722
723 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000726 } else if (instruction->IsTemporary()) {
727 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
728 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100729 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100730 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100731 switch (instruction->GetType()) {
732 case Primitive::kPrimBoolean:
733 case Primitive::kPrimByte:
734 case Primitive::kPrimChar:
735 case Primitive::kPrimShort:
736 case Primitive::kPrimInt:
737 case Primitive::kPrimNot:
738 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100739 case Primitive::kPrimFloat:
740 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000741 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742 break;
743
744 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100745 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 }
747 }
748}
749
750void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
751 got->SetLocations(nullptr);
752}
753
754void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
755 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100756 DCHECK(!successor->IsExitBlock());
757
758 HBasicBlock* block = got->GetBlock();
759 HInstruction* previous = got->GetPrevious();
760
761 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000762 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100763 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
764 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
765 return;
766 }
767
768 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
769 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
770 }
771 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100772 __ jmp(codegen_->GetLabelOf(successor));
773 }
774}
775
776void LocationsBuilderX86_64::VisitExit(HExit* exit) {
777 exit->SetLocations(nullptr);
778}
779
780void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700781 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100782}
783
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700784void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
785 Label* true_target,
786 Label* false_target,
787 Label* always_true_target) {
788 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100789 if (cond->IsIntConstant()) {
790 // Constant condition, statically compared against 1.
791 int32_t cond_value = cond->AsIntConstant()->GetValue();
792 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700793 if (always_true_target != nullptr) {
794 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100795 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100797 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100798 DCHECK_EQ(cond_value, 0);
799 }
800 } else {
801 bool materialized =
802 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
803 // Moves do not affect the eflags register, so if the condition is
804 // evaluated just before the if, we don't need to evaluate it
805 // again.
806 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700807 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100808 if (materialized) {
809 if (!eflags_set) {
810 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700811 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100812 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000813 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 } else {
815 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
816 Immediate(0));
817 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700818 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700820 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100821 }
822 } else {
823 Location lhs = cond->GetLocations()->InAt(0);
824 Location rhs = cond->GetLocations()->InAt(1);
825 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000826 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100827 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000828 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000829 if (constant == 0) {
830 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
831 } else {
832 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
833 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100834 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000835 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100836 Address(CpuRegister(RSP), rhs.GetStackIndex()));
837 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700838 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700839 }
Dave Allison20dfc792014-06-16 20:44:29 -0700840 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700841 if (false_target != nullptr) {
842 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100843 }
844}
845
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700846void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
847 LocationSummary* locations =
848 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
849 HInstruction* cond = if_instr->InputAt(0);
850 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
851 locations->SetInAt(0, Location::Any());
852 }
853}
854
855void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
856 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
857 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
858 Label* always_true_target = true_target;
859 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
860 if_instr->IfTrueSuccessor())) {
861 always_true_target = nullptr;
862 }
863 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
864 if_instr->IfFalseSuccessor())) {
865 false_target = nullptr;
866 }
867 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
868}
869
870void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
871 LocationSummary* locations = new (GetGraph()->GetArena())
872 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
873 HInstruction* cond = deoptimize->InputAt(0);
874 DCHECK(cond->IsCondition());
875 if (cond->AsCondition()->NeedsMaterialization()) {
876 locations->SetInAt(0, Location::Any());
877 }
878}
879
880void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
881 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
882 DeoptimizationSlowPathX86_64(deoptimize);
883 codegen_->AddSlowPath(slow_path);
884 Label* slow_path_entry = slow_path->GetEntryLabel();
885 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
886}
887
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100888void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
889 local->SetLocations(nullptr);
890}
891
892void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
893 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
894}
895
896void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
897 local->SetLocations(nullptr);
898}
899
900void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
901 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700902 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100903}
904
905void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100906 LocationSummary* locations =
907 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908 switch (store->InputAt(1)->GetType()) {
909 case Primitive::kPrimBoolean:
910 case Primitive::kPrimByte:
911 case Primitive::kPrimChar:
912 case Primitive::kPrimShort:
913 case Primitive::kPrimInt:
914 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100916 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
917 break;
918
919 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
922 break;
923
924 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100925 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927}
928
929void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700930 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931}
932
Dave Allison20dfc792014-06-16 20:44:29 -0700933void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 LocationSummary* locations =
935 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100936 locations->SetInAt(0, Location::RequiresRegister());
937 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100938 if (comp->NeedsMaterialization()) {
939 locations->SetOut(Location::RequiresRegister());
940 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100941}
942
Dave Allison20dfc792014-06-16 20:44:29 -0700943void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
944 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100945 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000946 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100947 // Clear register: setcc only sets the low byte.
948 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000949 Location lhs = locations->InAt(0);
950 Location rhs = locations->InAt(1);
951 if (rhs.IsRegister()) {
952 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
953 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800954 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000955 if (constant == 0) {
956 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
957 } else {
958 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
959 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100960 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000961 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100962 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100963 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700964 }
965}
966
967void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
968 VisitCondition(comp);
969}
970
971void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
972 VisitCondition(comp);
973}
974
975void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
976 VisitCondition(comp);
977}
978
979void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
980 VisitCondition(comp);
981}
982
983void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
984 VisitCondition(comp);
985}
986
987void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
988 VisitCondition(comp);
989}
990
991void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
992 VisitCondition(comp);
993}
994
995void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
996 VisitCondition(comp);
997}
998
999void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1000 VisitCondition(comp);
1001}
1002
1003void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1004 VisitCondition(comp);
1005}
1006
1007void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1008 VisitCondition(comp);
1009}
1010
1011void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1012 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013}
1014
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001015void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001016 LocationSummary* locations =
1017 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001018 switch (compare->InputAt(0)->GetType()) {
1019 case Primitive::kPrimLong: {
1020 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001021 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00001022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1023 break;
1024 }
1025 case Primitive::kPrimFloat:
1026 case Primitive::kPrimDouble: {
1027 locations->SetInAt(0, Location::RequiresFpuRegister());
1028 locations->SetInAt(1, Location::RequiresFpuRegister());
1029 locations->SetOut(Location::RequiresRegister());
1030 break;
1031 }
1032 default:
1033 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1034 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001035}
1036
1037void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001038 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001039 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001040 Location left = locations->InAt(0);
1041 Location right = locations->InAt(1);
1042
1043 Label less, greater, done;
1044 Primitive::Type type = compare->InputAt(0)->GetType();
1045 switch (type) {
1046 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001047 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1048 if (right.IsConstant()) {
1049 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1050 DCHECK(IsInt<32>(value));
1051 if (value == 0) {
1052 __ testq(left_reg, left_reg);
1053 } else {
1054 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1055 }
1056 } else {
1057 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1058 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001059 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001060 }
1061 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001062 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001063 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1064 break;
1065 }
1066 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001067 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001068 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1069 break;
1070 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001071 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001072 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001073 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001074 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001075 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001076 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001077
Calin Juravle91debbc2014-11-26 19:01:09 +00001078 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001079 __ movl(out, Immediate(1));
1080 __ jmp(&done);
1081
1082 __ Bind(&less);
1083 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001084
1085 __ Bind(&done);
1086}
1087
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001089 LocationSummary* locations =
1090 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001091 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092}
1093
1094void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001095 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001096 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097}
1098
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001099void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1100 LocationSummary* locations =
1101 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1102 locations->SetOut(Location::ConstantLocation(constant));
1103}
1104
1105void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1106 // Will be generated at use site.
1107 UNUSED(constant);
1108}
1109
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001110void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001111 LocationSummary* locations =
1112 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001113 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114}
1115
1116void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001117 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001118 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001119}
1120
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001121void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1122 LocationSummary* locations =
1123 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1124 locations->SetOut(Location::ConstantLocation(constant));
1125}
1126
1127void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1128 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001129 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001130}
1131
1132void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1135 locations->SetOut(Location::ConstantLocation(constant));
1136}
1137
1138void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1139 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001140 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001141}
1142
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001143void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1144 ret->SetLocations(nullptr);
1145}
1146
1147void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001148 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150}
1151
1152void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001153 LocationSummary* locations =
1154 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001155 switch (ret->InputAt(0)->GetType()) {
1156 case Primitive::kPrimBoolean:
1157 case Primitive::kPrimByte:
1158 case Primitive::kPrimChar:
1159 case Primitive::kPrimShort:
1160 case Primitive::kPrimInt:
1161 case Primitive::kPrimNot:
1162 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001164 break;
1165
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 case Primitive::kPrimFloat:
1167 case Primitive::kPrimDouble:
1168 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001169 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001170 break;
1171
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001172 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001173 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001174 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001175}
1176
1177void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1178 if (kIsDebugBuild) {
1179 switch (ret->InputAt(0)->GetType()) {
1180 case Primitive::kPrimBoolean:
1181 case Primitive::kPrimByte:
1182 case Primitive::kPrimChar:
1183 case Primitive::kPrimShort:
1184 case Primitive::kPrimInt:
1185 case Primitive::kPrimNot:
1186 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001187 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188 break;
1189
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 case Primitive::kPrimFloat:
1191 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001192 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001193 XMM0);
1194 break;
1195
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001198 }
1199 }
1200 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201}
1202
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001203Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1204 switch (type) {
1205 case Primitive::kPrimBoolean:
1206 case Primitive::kPrimByte:
1207 case Primitive::kPrimChar:
1208 case Primitive::kPrimShort:
1209 case Primitive::kPrimInt:
1210 case Primitive::kPrimNot: {
1211 uint32_t index = gp_index_++;
1212 stack_index_++;
1213 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001214 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215 } else {
1216 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1217 }
1218 }
1219
1220 case Primitive::kPrimLong: {
1221 uint32_t index = gp_index_;
1222 stack_index_ += 2;
1223 if (index < calling_convention.GetNumberOfRegisters()) {
1224 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001225 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001226 } else {
1227 gp_index_ += 2;
1228 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1229 }
1230 }
1231
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 case Primitive::kPrimFloat: {
1233 uint32_t index = fp_index_++;
1234 stack_index_++;
1235 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001236 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 } else {
1238 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1239 }
1240 }
1241
1242 case Primitive::kPrimDouble: {
1243 uint32_t index = fp_index_++;
1244 stack_index_ += 2;
1245 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001246 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001247 } else {
1248 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1249 }
1250 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251
1252 case Primitive::kPrimVoid:
1253 LOG(FATAL) << "Unexpected parameter type " << type;
1254 break;
1255 }
1256 return Location();
1257}
1258
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001259void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001260 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001261 if (intrinsic.TryDispatch(invoke)) {
1262 return;
1263 }
1264
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001265 HandleInvoke(invoke);
1266}
1267
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001268static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1269 if (invoke->GetLocations()->Intrinsified()) {
1270 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1271 intrinsic.Dispatch(invoke);
1272 return true;
1273 }
1274 return false;
1275}
1276
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001277void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001278 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1279 return;
1280 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001281
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001282 codegen_->GenerateStaticOrDirectCall(
1283 invoke,
1284 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001285 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001286}
1287
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001289 LocationSummary* locations =
1290 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001291 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001292
1293 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001294 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001295 HInstruction* input = invoke->InputAt(i);
1296 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1297 }
1298
1299 switch (invoke->GetType()) {
1300 case Primitive::kPrimBoolean:
1301 case Primitive::kPrimByte:
1302 case Primitive::kPrimChar:
1303 case Primitive::kPrimShort:
1304 case Primitive::kPrimInt:
1305 case Primitive::kPrimNot:
1306 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001307 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001308 break;
1309
1310 case Primitive::kPrimVoid:
1311 break;
1312
1313 case Primitive::kPrimDouble:
1314 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001315 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316 break;
1317 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001318}
1319
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001320void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001321 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001322 if (intrinsic.TryDispatch(invoke)) {
1323 return;
1324 }
1325
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001326 HandleInvoke(invoke);
1327}
1328
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001329void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001330 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1331 return;
1332 }
1333
Roland Levillain271ab9c2014-11-27 15:23:57 +00001334 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001335 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1336 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1337 LocationSummary* locations = invoke->GetLocations();
1338 Location receiver = locations->InAt(0);
1339 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1340 // temp = object->GetClass();
1341 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1343 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001344 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001345 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001346 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001347 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001348 // temp = temp->GetMethodAt(method_offset);
1349 __ movl(temp, Address(temp, method_offset));
1350 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001351 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001352 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001353
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001354 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001355 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001356}
1357
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001358void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1359 HandleInvoke(invoke);
1360 // Add the hidden argument.
1361 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1362}
1363
1364void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1365 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001366 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001367 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1368 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1369 LocationSummary* locations = invoke->GetLocations();
1370 Location receiver = locations->InAt(0);
1371 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1372
1373 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001374 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001375 Immediate(invoke->GetDexMethodIndex()));
1376
1377 // temp = object->GetClass();
1378 if (receiver.IsStackSlot()) {
1379 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1380 __ movl(temp, Address(temp, class_offset));
1381 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001382 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001383 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001384 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001385 // temp = temp->GetImtEntryAt(method_offset);
1386 __ movl(temp, Address(temp, method_offset));
1387 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001388 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001389 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001390
1391 DCHECK(!codegen_->IsLeafMethod());
1392 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1393}
1394
Roland Levillain88cb1752014-10-20 16:36:47 +01001395void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1396 LocationSummary* locations =
1397 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1398 switch (neg->GetResultType()) {
1399 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001400 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001401 locations->SetInAt(0, Location::RequiresRegister());
1402 locations->SetOut(Location::SameAsFirstInput());
1403 break;
1404
Roland Levillain88cb1752014-10-20 16:36:47 +01001405 case Primitive::kPrimFloat:
1406 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001407 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001408 locations->SetOut(Location::SameAsFirstInput());
1409 locations->AddTemp(Location::RequiresRegister());
1410 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001411 break;
1412
1413 default:
1414 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1415 }
1416}
1417
1418void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1419 LocationSummary* locations = neg->GetLocations();
1420 Location out = locations->Out();
1421 Location in = locations->InAt(0);
1422 switch (neg->GetResultType()) {
1423 case Primitive::kPrimInt:
1424 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001425 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001426 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001427 break;
1428
1429 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001430 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001431 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001432 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001433 break;
1434
Roland Levillain5368c212014-11-27 15:03:41 +00001435 case Primitive::kPrimFloat: {
1436 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001437 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1438 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001439 // Implement float negation with an exclusive or with value
1440 // 0x80000000 (mask for bit 31, representing the sign of a
1441 // single-precision floating-point number).
1442 __ movq(constant, Immediate(INT64_C(0x80000000)));
1443 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001444 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001445 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001446 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001447
Roland Levillain5368c212014-11-27 15:03:41 +00001448 case Primitive::kPrimDouble: {
1449 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001450 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1451 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001452 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001453 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001454 // a double-precision floating-point number).
1455 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1456 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001457 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001458 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001459 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001460
1461 default:
1462 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1463 }
1464}
1465
Roland Levillaindff1f282014-11-05 14:15:05 +00001466void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1467 LocationSummary* locations =
1468 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1469 Primitive::Type result_type = conversion->GetResultType();
1470 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001471 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001472
David Brazdilb2bd1c52015-03-25 11:17:37 +00001473 // The Java language does not allow treating boolean as an integral type but
1474 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001475
Roland Levillaindff1f282014-11-05 14:15:05 +00001476 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001477 case Primitive::kPrimByte:
1478 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001479 case Primitive::kPrimBoolean:
1480 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001481 case Primitive::kPrimShort:
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001484 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001485 locations->SetInAt(0, Location::Any());
1486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1487 break;
1488
1489 default:
1490 LOG(FATAL) << "Unexpected type conversion from " << input_type
1491 << " to " << result_type;
1492 }
1493 break;
1494
Roland Levillain01a8d712014-11-14 16:27:39 +00001495 case Primitive::kPrimShort:
1496 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001497 case Primitive::kPrimBoolean:
1498 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001499 case Primitive::kPrimByte:
1500 case Primitive::kPrimInt:
1501 case Primitive::kPrimChar:
1502 // Processing a Dex `int-to-short' instruction.
1503 locations->SetInAt(0, Location::Any());
1504 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1505 break;
1506
1507 default:
1508 LOG(FATAL) << "Unexpected type conversion from " << input_type
1509 << " to " << result_type;
1510 }
1511 break;
1512
Roland Levillain946e1432014-11-11 17:35:19 +00001513 case Primitive::kPrimInt:
1514 switch (input_type) {
1515 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001516 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001517 locations->SetInAt(0, Location::Any());
1518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1519 break;
1520
1521 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001522 // Processing a Dex `float-to-int' instruction.
1523 locations->SetInAt(0, Location::RequiresFpuRegister());
1524 locations->SetOut(Location::RequiresRegister());
1525 locations->AddTemp(Location::RequiresFpuRegister());
1526 break;
1527
Roland Levillain946e1432014-11-11 17:35:19 +00001528 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001529 // Processing a Dex `double-to-int' instruction.
1530 locations->SetInAt(0, Location::RequiresFpuRegister());
1531 locations->SetOut(Location::RequiresRegister());
1532 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001533 break;
1534
1535 default:
1536 LOG(FATAL) << "Unexpected type conversion from " << input_type
1537 << " to " << result_type;
1538 }
1539 break;
1540
Roland Levillaindff1f282014-11-05 14:15:05 +00001541 case Primitive::kPrimLong:
1542 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001543 case Primitive::kPrimBoolean:
1544 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001545 case Primitive::kPrimByte:
1546 case Primitive::kPrimShort:
1547 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001548 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001549 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001550 // TODO: We would benefit from a (to-be-implemented)
1551 // Location::RegisterOrStackSlot requirement for this input.
1552 locations->SetInAt(0, Location::RequiresRegister());
1553 locations->SetOut(Location::RequiresRegister());
1554 break;
1555
1556 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001557 // Processing a Dex `float-to-long' instruction.
1558 locations->SetInAt(0, Location::RequiresFpuRegister());
1559 locations->SetOut(Location::RequiresRegister());
1560 locations->AddTemp(Location::RequiresFpuRegister());
1561 break;
1562
Roland Levillaindff1f282014-11-05 14:15:05 +00001563 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001564 // Processing a Dex `double-to-long' instruction.
1565 locations->SetInAt(0, Location::RequiresFpuRegister());
1566 locations->SetOut(Location::RequiresRegister());
1567 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001568 break;
1569
1570 default:
1571 LOG(FATAL) << "Unexpected type conversion from " << input_type
1572 << " to " << result_type;
1573 }
1574 break;
1575
Roland Levillain981e4542014-11-14 11:47:14 +00001576 case Primitive::kPrimChar:
1577 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001578 case Primitive::kPrimBoolean:
1579 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001580 case Primitive::kPrimByte:
1581 case Primitive::kPrimShort:
1582 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001583 // Processing a Dex `int-to-char' instruction.
1584 locations->SetInAt(0, Location::Any());
1585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1586 break;
1587
1588 default:
1589 LOG(FATAL) << "Unexpected type conversion from " << input_type
1590 << " to " << result_type;
1591 }
1592 break;
1593
Roland Levillaindff1f282014-11-05 14:15:05 +00001594 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001595 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001596 case Primitive::kPrimBoolean:
1597 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001598 case Primitive::kPrimByte:
1599 case Primitive::kPrimShort:
1600 case Primitive::kPrimInt:
1601 case Primitive::kPrimChar:
1602 // Processing a Dex `int-to-float' instruction.
1603 locations->SetInAt(0, Location::RequiresRegister());
1604 locations->SetOut(Location::RequiresFpuRegister());
1605 break;
1606
1607 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001608 // Processing a Dex `long-to-float' instruction.
1609 locations->SetInAt(0, Location::RequiresRegister());
1610 locations->SetOut(Location::RequiresFpuRegister());
1611 break;
1612
Roland Levillaincff13742014-11-17 14:32:17 +00001613 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001614 // Processing a Dex `double-to-float' instruction.
1615 locations->SetInAt(0, Location::RequiresFpuRegister());
1616 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001617 break;
1618
1619 default:
1620 LOG(FATAL) << "Unexpected type conversion from " << input_type
1621 << " to " << result_type;
1622 };
1623 break;
1624
Roland Levillaindff1f282014-11-05 14:15:05 +00001625 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001626 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001627 case Primitive::kPrimBoolean:
1628 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001629 case Primitive::kPrimByte:
1630 case Primitive::kPrimShort:
1631 case Primitive::kPrimInt:
1632 case Primitive::kPrimChar:
1633 // Processing a Dex `int-to-double' instruction.
1634 locations->SetInAt(0, Location::RequiresRegister());
1635 locations->SetOut(Location::RequiresFpuRegister());
1636 break;
1637
1638 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001639 // Processing a Dex `long-to-double' instruction.
1640 locations->SetInAt(0, Location::RequiresRegister());
1641 locations->SetOut(Location::RequiresFpuRegister());
1642 break;
1643
Roland Levillaincff13742014-11-17 14:32:17 +00001644 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001645 // Processing a Dex `float-to-double' instruction.
1646 locations->SetInAt(0, Location::RequiresFpuRegister());
1647 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001648 break;
1649
1650 default:
1651 LOG(FATAL) << "Unexpected type conversion from " << input_type
1652 << " to " << result_type;
1653 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001654 break;
1655
1656 default:
1657 LOG(FATAL) << "Unexpected type conversion from " << input_type
1658 << " to " << result_type;
1659 }
1660}
1661
1662void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1663 LocationSummary* locations = conversion->GetLocations();
1664 Location out = locations->Out();
1665 Location in = locations->InAt(0);
1666 Primitive::Type result_type = conversion->GetResultType();
1667 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001668 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001669 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001670 case Primitive::kPrimByte:
1671 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001672 case Primitive::kPrimBoolean:
1673 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001674 case Primitive::kPrimShort:
1675 case Primitive::kPrimInt:
1676 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001677 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001678 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001680 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001681 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001682 Address(CpuRegister(RSP), in.GetStackIndex()));
1683 } else {
1684 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001686 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1687 }
1688 break;
1689
1690 default:
1691 LOG(FATAL) << "Unexpected type conversion from " << input_type
1692 << " to " << result_type;
1693 }
1694 break;
1695
Roland Levillain01a8d712014-11-14 16:27:39 +00001696 case Primitive::kPrimShort:
1697 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001698 case Primitive::kPrimBoolean:
1699 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001700 case Primitive::kPrimByte:
1701 case Primitive::kPrimInt:
1702 case Primitive::kPrimChar:
1703 // Processing a Dex `int-to-short' instruction.
1704 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001705 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001706 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001707 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001708 Address(CpuRegister(RSP), in.GetStackIndex()));
1709 } else {
1710 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001711 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001712 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1713 }
1714 break;
1715
1716 default:
1717 LOG(FATAL) << "Unexpected type conversion from " << input_type
1718 << " to " << result_type;
1719 }
1720 break;
1721
Roland Levillain946e1432014-11-11 17:35:19 +00001722 case Primitive::kPrimInt:
1723 switch (input_type) {
1724 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001725 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001726 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001727 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001728 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001729 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001730 Address(CpuRegister(RSP), in.GetStackIndex()));
1731 } else {
1732 DCHECK(in.IsConstant());
1733 DCHECK(in.GetConstant()->IsLongConstant());
1734 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001735 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001736 }
1737 break;
1738
Roland Levillain3f8f9362014-12-02 17:45:01 +00001739 case Primitive::kPrimFloat: {
1740 // Processing a Dex `float-to-int' instruction.
1741 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1742 CpuRegister output = out.AsRegister<CpuRegister>();
1743 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1744 Label done, nan;
1745
1746 __ movl(output, Immediate(kPrimIntMax));
1747 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001748 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001749 // if input >= temp goto done
1750 __ comiss(input, temp);
1751 __ j(kAboveEqual, &done);
1752 // if input == NaN goto nan
1753 __ j(kUnordered, &nan);
1754 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001755 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001756 __ jmp(&done);
1757 __ Bind(&nan);
1758 // output = 0
1759 __ xorl(output, output);
1760 __ Bind(&done);
1761 break;
1762 }
1763
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001764 case Primitive::kPrimDouble: {
1765 // Processing a Dex `double-to-int' instruction.
1766 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1767 CpuRegister output = out.AsRegister<CpuRegister>();
1768 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1769 Label done, nan;
1770
1771 __ movl(output, Immediate(kPrimIntMax));
1772 // temp = int-to-double(output)
1773 __ cvtsi2sd(temp, output);
1774 // if input >= temp goto done
1775 __ comisd(input, temp);
1776 __ j(kAboveEqual, &done);
1777 // if input == NaN goto nan
1778 __ j(kUnordered, &nan);
1779 // output = double-to-int-truncate(input)
1780 __ cvttsd2si(output, input);
1781 __ jmp(&done);
1782 __ Bind(&nan);
1783 // output = 0
1784 __ xorl(output, output);
1785 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001786 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001787 }
Roland Levillain946e1432014-11-11 17:35:19 +00001788
1789 default:
1790 LOG(FATAL) << "Unexpected type conversion from " << input_type
1791 << " to " << result_type;
1792 }
1793 break;
1794
Roland Levillaindff1f282014-11-05 14:15:05 +00001795 case Primitive::kPrimLong:
1796 switch (input_type) {
1797 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001798 case Primitive::kPrimBoolean:
1799 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001800 case Primitive::kPrimByte:
1801 case Primitive::kPrimShort:
1802 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001803 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001804 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001805 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001806 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001807 break;
1808
Roland Levillain624279f2014-12-04 11:54:28 +00001809 case Primitive::kPrimFloat: {
1810 // Processing a Dex `float-to-long' instruction.
1811 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1812 CpuRegister output = out.AsRegister<CpuRegister>();
1813 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1814 Label done, nan;
1815
1816 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001817 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001818 __ cvtsi2ss(temp, output, true);
1819 // if input >= temp goto done
1820 __ comiss(input, temp);
1821 __ j(kAboveEqual, &done);
1822 // if input == NaN goto nan
1823 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001824 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001825 __ cvttss2si(output, input, true);
1826 __ jmp(&done);
1827 __ Bind(&nan);
1828 // output = 0
1829 __ xorq(output, output);
1830 __ Bind(&done);
1831 break;
1832 }
1833
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001834 case Primitive::kPrimDouble: {
1835 // Processing a Dex `double-to-long' instruction.
1836 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1837 CpuRegister output = out.AsRegister<CpuRegister>();
1838 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1839 Label done, nan;
1840
1841 __ movq(output, Immediate(kPrimLongMax));
1842 // temp = long-to-double(output)
1843 __ cvtsi2sd(temp, output, true);
1844 // if input >= temp goto done
1845 __ comisd(input, temp);
1846 __ j(kAboveEqual, &done);
1847 // if input == NaN goto nan
1848 __ j(kUnordered, &nan);
1849 // output = double-to-long-truncate(input)
1850 __ cvttsd2si(output, input, true);
1851 __ jmp(&done);
1852 __ Bind(&nan);
1853 // output = 0
1854 __ xorq(output, output);
1855 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001856 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001857 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001858
1859 default:
1860 LOG(FATAL) << "Unexpected type conversion from " << input_type
1861 << " to " << result_type;
1862 }
1863 break;
1864
Roland Levillain981e4542014-11-14 11:47:14 +00001865 case Primitive::kPrimChar:
1866 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001867 case Primitive::kPrimBoolean:
1868 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001869 case Primitive::kPrimByte:
1870 case Primitive::kPrimShort:
1871 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001872 // Processing a Dex `int-to-char' instruction.
1873 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001874 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001875 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001877 Address(CpuRegister(RSP), in.GetStackIndex()));
1878 } else {
1879 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001880 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001881 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1882 }
1883 break;
1884
1885 default:
1886 LOG(FATAL) << "Unexpected type conversion from " << input_type
1887 << " to " << result_type;
1888 }
1889 break;
1890
Roland Levillaindff1f282014-11-05 14:15:05 +00001891 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001892 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001893 case Primitive::kPrimBoolean:
1894 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001895 case Primitive::kPrimByte:
1896 case Primitive::kPrimShort:
1897 case Primitive::kPrimInt:
1898 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001899 // Processing a Dex `int-to-float' instruction.
1900 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001901 break;
1902
1903 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001904 // Processing a Dex `long-to-float' instruction.
1905 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1906 break;
1907
Roland Levillaincff13742014-11-17 14:32:17 +00001908 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001909 // Processing a Dex `double-to-float' instruction.
1910 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001911 break;
1912
1913 default:
1914 LOG(FATAL) << "Unexpected type conversion from " << input_type
1915 << " to " << result_type;
1916 };
1917 break;
1918
Roland Levillaindff1f282014-11-05 14:15:05 +00001919 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001920 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001921 case Primitive::kPrimBoolean:
1922 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001923 case Primitive::kPrimByte:
1924 case Primitive::kPrimShort:
1925 case Primitive::kPrimInt:
1926 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001927 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001928 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001929 break;
1930
1931 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001932 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001933 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001934 break;
1935
Roland Levillaincff13742014-11-17 14:32:17 +00001936 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001937 // Processing a Dex `float-to-double' instruction.
1938 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001939 break;
1940
1941 default:
1942 LOG(FATAL) << "Unexpected type conversion from " << input_type
1943 << " to " << result_type;
1944 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001945 break;
1946
1947 default:
1948 LOG(FATAL) << "Unexpected type conversion from " << input_type
1949 << " to " << result_type;
1950 }
1951}
1952
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001953void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001956 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001957 case Primitive::kPrimInt: {
1958 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001959 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001961 break;
1962 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001963
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001964 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001965 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001966 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001967 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001969 break;
1970 }
1971
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001972 case Primitive::kPrimDouble:
1973 case Primitive::kPrimFloat: {
1974 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04001975 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001976 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001978 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001979
1980 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001981 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001982 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001983}
1984
1985void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1986 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001987 Location first = locations->InAt(0);
1988 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001989 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001990
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001991 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001992 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001993 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001994 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1995 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1996 } else {
1997 __ leal(out.AsRegister<CpuRegister>(), Address(
1998 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1999 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002001 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2002 __ addl(out.AsRegister<CpuRegister>(),
2003 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2004 } else {
2005 __ leal(out.AsRegister<CpuRegister>(), Address(
2006 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2007 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002008 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002009 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002011 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002012 break;
2013 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002014
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002015 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002016 if (second.IsRegister()) {
2017 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2018 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2019 } else {
2020 __ leaq(out.AsRegister<CpuRegister>(), Address(
2021 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2022 }
2023 } else {
2024 DCHECK(second.IsConstant());
2025 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2026 int32_t int32_value = Low32Bits(value);
2027 DCHECK_EQ(int32_value, value);
2028 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2029 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2030 } else {
2031 __ leaq(out.AsRegister<CpuRegister>(), Address(
2032 first.AsRegister<CpuRegister>(), int32_value));
2033 }
2034 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002035 break;
2036 }
2037
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002038 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002039 if (second.IsFpuRegister()) {
2040 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2041 } else if (second.IsConstant()) {
2042 __ addss(first.AsFpuRegister<XmmRegister>(),
2043 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2044 } else {
2045 DCHECK(second.IsStackSlot());
2046 __ addss(first.AsFpuRegister<XmmRegister>(),
2047 Address(CpuRegister(RSP), second.GetStackIndex()));
2048 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002049 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002050 }
2051
2052 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002053 if (second.IsFpuRegister()) {
2054 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2055 } else if (second.IsConstant()) {
2056 __ addsd(first.AsFpuRegister<XmmRegister>(),
2057 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2058 } else {
2059 DCHECK(second.IsDoubleStackSlot());
2060 __ addsd(first.AsFpuRegister<XmmRegister>(),
2061 Address(CpuRegister(RSP), second.GetStackIndex()));
2062 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002063 break;
2064 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002065
2066 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002067 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002068 }
2069}
2070
2071void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002072 LocationSummary* locations =
2073 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002074 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002075 case Primitive::kPrimInt: {
2076 locations->SetInAt(0, Location::RequiresRegister());
2077 locations->SetInAt(1, Location::Any());
2078 locations->SetOut(Location::SameAsFirstInput());
2079 break;
2080 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002081 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002082 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002083 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002084 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002085 break;
2086 }
Calin Juravle11351682014-10-23 15:38:15 +01002087 case Primitive::kPrimFloat:
2088 case Primitive::kPrimDouble: {
2089 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002090 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002091 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002092 break;
Calin Juravle11351682014-10-23 15:38:15 +01002093 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002094 default:
Calin Juravle11351682014-10-23 15:38:15 +01002095 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002096 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002097}
2098
2099void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2100 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002101 Location first = locations->InAt(0);
2102 Location second = locations->InAt(1);
2103 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002105 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002106 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002108 } else if (second.IsConstant()) {
2109 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002110 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002111 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002113 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002114 break;
2115 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002116 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002117 if (second.IsConstant()) {
2118 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2119 DCHECK(IsInt<32>(value));
2120 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2121 } else {
2122 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2123 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002124 break;
2125 }
2126
Calin Juravle11351682014-10-23 15:38:15 +01002127 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002128 if (second.IsFpuRegister()) {
2129 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2130 } else if (second.IsConstant()) {
2131 __ subss(first.AsFpuRegister<XmmRegister>(),
2132 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2133 } else {
2134 DCHECK(second.IsStackSlot());
2135 __ subss(first.AsFpuRegister<XmmRegister>(),
2136 Address(CpuRegister(RSP), second.GetStackIndex()));
2137 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002138 break;
Calin Juravle11351682014-10-23 15:38:15 +01002139 }
2140
2141 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002142 if (second.IsFpuRegister()) {
2143 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2144 } else if (second.IsConstant()) {
2145 __ subsd(first.AsFpuRegister<XmmRegister>(),
2146 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2147 } else {
2148 DCHECK(second.IsDoubleStackSlot());
2149 __ subsd(first.AsFpuRegister<XmmRegister>(),
2150 Address(CpuRegister(RSP), second.GetStackIndex()));
2151 }
Calin Juravle11351682014-10-23 15:38:15 +01002152 break;
2153 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002154
2155 default:
Calin Juravle11351682014-10-23 15:38:15 +01002156 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002157 }
2158}
2159
Calin Juravle34bacdf2014-10-07 20:23:36 +01002160void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2161 LocationSummary* locations =
2162 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2163 switch (mul->GetResultType()) {
2164 case Primitive::kPrimInt: {
2165 locations->SetInAt(0, Location::RequiresRegister());
2166 locations->SetInAt(1, Location::Any());
2167 locations->SetOut(Location::SameAsFirstInput());
2168 break;
2169 }
2170 case Primitive::kPrimLong: {
2171 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002172 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2173 if (locations->InAt(1).IsConstant()) {
2174 // Can use 3 operand multiply.
2175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2176 } else {
2177 locations->SetOut(Location::SameAsFirstInput());
2178 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002179 break;
2180 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002181 case Primitive::kPrimFloat:
2182 case Primitive::kPrimDouble: {
2183 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002184 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002185 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002186 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002187 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002188
2189 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002190 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002191 }
2192}
2193
2194void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2195 LocationSummary* locations = mul->GetLocations();
2196 Location first = locations->InAt(0);
2197 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002198 switch (mul->GetResultType()) {
2199 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002200 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002201 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002202 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002203 } else if (second.IsConstant()) {
2204 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002205 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002206 } else {
2207 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002208 __ imull(first.AsRegister<CpuRegister>(),
2209 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002210 }
2211 break;
2212 }
2213 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002214 if (second.IsConstant()) {
2215 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2216 DCHECK(IsInt<32>(value));
2217 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2218 first.AsRegister<CpuRegister>(),
2219 Immediate(static_cast<int32_t>(value)));
2220 } else {
2221 DCHECK(first.Equals(locations->Out()));
2222 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2223 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002224 break;
2225 }
2226
Calin Juravleb5bfa962014-10-21 18:02:24 +01002227 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002228 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002229 if (second.IsFpuRegister()) {
2230 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2231 } else if (second.IsConstant()) {
2232 __ mulss(first.AsFpuRegister<XmmRegister>(),
2233 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2234 } else {
2235 DCHECK(second.IsStackSlot());
2236 __ mulss(first.AsFpuRegister<XmmRegister>(),
2237 Address(CpuRegister(RSP), second.GetStackIndex()));
2238 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002239 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002240 }
2241
2242 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002243 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002244 if (second.IsFpuRegister()) {
2245 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2246 } else if (second.IsConstant()) {
2247 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2248 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2249 } else {
2250 DCHECK(second.IsDoubleStackSlot());
2251 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2252 Address(CpuRegister(RSP), second.GetStackIndex()));
2253 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002254 break;
2255 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002256
2257 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002258 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002259 }
2260}
2261
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002262void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2263 uint32_t stack_adjustment, bool is_float) {
2264 if (source.IsStackSlot()) {
2265 DCHECK(is_float);
2266 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2267 } else if (source.IsDoubleStackSlot()) {
2268 DCHECK(!is_float);
2269 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2270 } else {
2271 // Write the value to the temporary location on the stack and load to FP stack.
2272 if (is_float) {
2273 Location stack_temp = Location::StackSlot(temp_offset);
2274 codegen_->Move(stack_temp, source);
2275 __ flds(Address(CpuRegister(RSP), temp_offset));
2276 } else {
2277 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2278 codegen_->Move(stack_temp, source);
2279 __ fldl(Address(CpuRegister(RSP), temp_offset));
2280 }
2281 }
2282}
2283
2284void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2285 Primitive::Type type = rem->GetResultType();
2286 bool is_float = type == Primitive::kPrimFloat;
2287 size_t elem_size = Primitive::ComponentSize(type);
2288 LocationSummary* locations = rem->GetLocations();
2289 Location first = locations->InAt(0);
2290 Location second = locations->InAt(1);
2291 Location out = locations->Out();
2292
2293 // Create stack space for 2 elements.
2294 // TODO: enhance register allocator to ask for stack temporaries.
2295 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2296
2297 // Load the values to the FP stack in reverse order, using temporaries if needed.
2298 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2299 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2300
2301 // Loop doing FPREM until we stabilize.
2302 Label retry;
2303 __ Bind(&retry);
2304 __ fprem();
2305
2306 // Move FP status to AX.
2307 __ fstsw();
2308
2309 // And see if the argument reduction is complete. This is signaled by the
2310 // C2 FPU flag bit set to 0.
2311 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2312 __ j(kNotEqual, &retry);
2313
2314 // We have settled on the final value. Retrieve it into an XMM register.
2315 // Store FP top of stack to real stack.
2316 if (is_float) {
2317 __ fsts(Address(CpuRegister(RSP), 0));
2318 } else {
2319 __ fstl(Address(CpuRegister(RSP), 0));
2320 }
2321
2322 // Pop the 2 items from the FP stack.
2323 __ fucompp();
2324
2325 // Load the value from the stack into an XMM register.
2326 DCHECK(out.IsFpuRegister()) << out;
2327 if (is_float) {
2328 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2329 } else {
2330 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2331 }
2332
2333 // And remove the temporary stack space we allocated.
2334 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2335}
2336
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002337void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2338 DCHECK(instruction->IsDiv() || instruction->IsRem());
2339
2340 LocationSummary* locations = instruction->GetLocations();
2341 Location second = locations->InAt(1);
2342 DCHECK(second.IsConstant());
2343
2344 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2345 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002346 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002347
2348 DCHECK(imm == 1 || imm == -1);
2349
2350 switch (instruction->GetResultType()) {
2351 case Primitive::kPrimInt: {
2352 if (instruction->IsRem()) {
2353 __ xorl(output_register, output_register);
2354 } else {
2355 __ movl(output_register, input_register);
2356 if (imm == -1) {
2357 __ negl(output_register);
2358 }
2359 }
2360 break;
2361 }
2362
2363 case Primitive::kPrimLong: {
2364 if (instruction->IsRem()) {
2365 __ xorq(output_register, output_register);
2366 } else {
2367 __ movq(output_register, input_register);
2368 if (imm == -1) {
2369 __ negq(output_register);
2370 }
2371 }
2372 break;
2373 }
2374
2375 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002376 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002377 }
2378}
2379
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002380void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002381 LocationSummary* locations = instruction->GetLocations();
2382 Location second = locations->InAt(1);
2383
2384 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2385 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2386
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002387 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002388
2389 DCHECK(IsPowerOfTwo(std::abs(imm)));
2390
2391 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2392
2393 if (instruction->GetResultType() == Primitive::kPrimInt) {
2394 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2395 __ testl(numerator, numerator);
2396 __ cmov(kGreaterEqual, tmp, numerator);
2397 int shift = CTZ(imm);
2398 __ sarl(tmp, Immediate(shift));
2399
2400 if (imm < 0) {
2401 __ negl(tmp);
2402 }
2403
2404 __ movl(output_register, tmp);
2405 } else {
2406 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2407 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2408
2409 __ movq(rdx, Immediate(std::abs(imm) - 1));
2410 __ addq(rdx, numerator);
2411 __ testq(numerator, numerator);
2412 __ cmov(kGreaterEqual, rdx, numerator);
2413 int shift = CTZ(imm);
2414 __ sarq(rdx, Immediate(shift));
2415
2416 if (imm < 0) {
2417 __ negq(rdx);
2418 }
2419
2420 __ movq(output_register, rdx);
2421 }
2422}
2423
2424void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2425 DCHECK(instruction->IsDiv() || instruction->IsRem());
2426
2427 LocationSummary* locations = instruction->GetLocations();
2428 Location second = locations->InAt(1);
2429
2430 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2431 : locations->GetTemp(0).AsRegister<CpuRegister>();
2432 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2433 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2434 : locations->Out().AsRegister<CpuRegister>();
2435 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2436
2437 DCHECK_EQ(RAX, eax.AsRegister());
2438 DCHECK_EQ(RDX, edx.AsRegister());
2439 if (instruction->IsDiv()) {
2440 DCHECK_EQ(RAX, out.AsRegister());
2441 } else {
2442 DCHECK_EQ(RDX, out.AsRegister());
2443 }
2444
2445 int64_t magic;
2446 int shift;
2447
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002448 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002449 if (instruction->GetResultType() == Primitive::kPrimInt) {
2450 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2451
2452 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2453
2454 __ movl(numerator, eax);
2455
2456 Label no_div;
2457 Label end;
2458 __ testl(eax, eax);
2459 __ j(kNotEqual, &no_div);
2460
2461 __ xorl(out, out);
2462 __ jmp(&end);
2463
2464 __ Bind(&no_div);
2465
2466 __ movl(eax, Immediate(magic));
2467 __ imull(numerator);
2468
2469 if (imm > 0 && magic < 0) {
2470 __ addl(edx, numerator);
2471 } else if (imm < 0 && magic > 0) {
2472 __ subl(edx, numerator);
2473 }
2474
2475 if (shift != 0) {
2476 __ sarl(edx, Immediate(shift));
2477 }
2478
2479 __ movl(eax, edx);
2480 __ shrl(edx, Immediate(31));
2481 __ addl(edx, eax);
2482
2483 if (instruction->IsRem()) {
2484 __ movl(eax, numerator);
2485 __ imull(edx, Immediate(imm));
2486 __ subl(eax, edx);
2487 __ movl(edx, eax);
2488 } else {
2489 __ movl(eax, edx);
2490 }
2491 __ Bind(&end);
2492 } else {
2493 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2494
2495 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2496
2497 CpuRegister rax = eax;
2498 CpuRegister rdx = edx;
2499
2500 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2501
2502 // Save the numerator.
2503 __ movq(numerator, rax);
2504
2505 // RAX = magic
2506 __ movq(rax, Immediate(magic));
2507
2508 // RDX:RAX = magic * numerator
2509 __ imulq(numerator);
2510
2511 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002512 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002513 __ addq(rdx, numerator);
2514 } else if (imm < 0 && magic > 0) {
2515 // RDX -= numerator
2516 __ subq(rdx, numerator);
2517 }
2518
2519 // Shift if needed.
2520 if (shift != 0) {
2521 __ sarq(rdx, Immediate(shift));
2522 }
2523
2524 // RDX += 1 if RDX < 0
2525 __ movq(rax, rdx);
2526 __ shrq(rdx, Immediate(63));
2527 __ addq(rdx, rax);
2528
2529 if (instruction->IsRem()) {
2530 __ movq(rax, numerator);
2531
2532 if (IsInt<32>(imm)) {
2533 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2534 } else {
2535 __ movq(numerator, Immediate(imm));
2536 __ imulq(rdx, numerator);
2537 }
2538
2539 __ subq(rax, rdx);
2540 __ movq(rdx, rax);
2541 } else {
2542 __ movq(rax, rdx);
2543 }
2544 }
2545}
2546
Calin Juravlebacfec32014-11-14 15:54:36 +00002547void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2548 DCHECK(instruction->IsDiv() || instruction->IsRem());
2549 Primitive::Type type = instruction->GetResultType();
2550 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2551
2552 bool is_div = instruction->IsDiv();
2553 LocationSummary* locations = instruction->GetLocations();
2554
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002555 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2556 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002557
Roland Levillain271ab9c2014-11-27 15:23:57 +00002558 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002559 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002560
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002561 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002562 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002563
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002564 if (imm == 0) {
2565 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2566 } else if (imm == 1 || imm == -1) {
2567 DivRemOneOrMinusOne(instruction);
2568 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002569 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002570 } else {
2571 DCHECK(imm <= -2 || imm >= 2);
2572 GenerateDivRemWithAnyConstant(instruction);
2573 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002574 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002575 SlowPathCodeX86_64* slow_path =
2576 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2577 out.AsRegister(), type, is_div);
2578 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002579
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002580 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2581 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2582 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2583 // so it's safe to just use negl instead of more complex comparisons.
2584 if (type == Primitive::kPrimInt) {
2585 __ cmpl(second_reg, Immediate(-1));
2586 __ j(kEqual, slow_path->GetEntryLabel());
2587 // edx:eax <- sign-extended of eax
2588 __ cdq();
2589 // eax = quotient, edx = remainder
2590 __ idivl(second_reg);
2591 } else {
2592 __ cmpq(second_reg, Immediate(-1));
2593 __ j(kEqual, slow_path->GetEntryLabel());
2594 // rdx:rax <- sign-extended of rax
2595 __ cqo();
2596 // rax = quotient, rdx = remainder
2597 __ idivq(second_reg);
2598 }
2599 __ Bind(slow_path->GetExitLabel());
2600 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002601}
2602
Calin Juravle7c4954d2014-10-28 16:57:40 +00002603void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2604 LocationSummary* locations =
2605 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2606 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002607 case Primitive::kPrimInt:
2608 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002609 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002610 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002611 locations->SetOut(Location::SameAsFirstInput());
2612 // Intel uses edx:eax as the dividend.
2613 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002614 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2615 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2616 // output and request another temp.
2617 if (div->InputAt(1)->IsConstant()) {
2618 locations->AddTemp(Location::RequiresRegister());
2619 }
Calin Juravled0d48522014-11-04 16:40:20 +00002620 break;
2621 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002622
Calin Juravle7c4954d2014-10-28 16:57:40 +00002623 case Primitive::kPrimFloat:
2624 case Primitive::kPrimDouble: {
2625 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002626 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002627 locations->SetOut(Location::SameAsFirstInput());
2628 break;
2629 }
2630
2631 default:
2632 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2633 }
2634}
2635
2636void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2637 LocationSummary* locations = div->GetLocations();
2638 Location first = locations->InAt(0);
2639 Location second = locations->InAt(1);
2640 DCHECK(first.Equals(locations->Out()));
2641
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002642 Primitive::Type type = div->GetResultType();
2643 switch (type) {
2644 case Primitive::kPrimInt:
2645 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002646 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002647 break;
2648 }
2649
Calin Juravle7c4954d2014-10-28 16:57:40 +00002650 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002651 if (second.IsFpuRegister()) {
2652 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2653 } else if (second.IsConstant()) {
2654 __ divss(first.AsFpuRegister<XmmRegister>(),
2655 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2656 } else {
2657 DCHECK(second.IsStackSlot());
2658 __ divss(first.AsFpuRegister<XmmRegister>(),
2659 Address(CpuRegister(RSP), second.GetStackIndex()));
2660 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002661 break;
2662 }
2663
2664 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002665 if (second.IsFpuRegister()) {
2666 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2667 } else if (second.IsConstant()) {
2668 __ divsd(first.AsFpuRegister<XmmRegister>(),
2669 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2670 } else {
2671 DCHECK(second.IsDoubleStackSlot());
2672 __ divsd(first.AsFpuRegister<XmmRegister>(),
2673 Address(CpuRegister(RSP), second.GetStackIndex()));
2674 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002675 break;
2676 }
2677
2678 default:
2679 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2680 }
2681}
2682
Calin Juravlebacfec32014-11-14 15:54:36 +00002683void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002684 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002685 LocationSummary* locations =
2686 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002687
2688 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002689 case Primitive::kPrimInt:
2690 case Primitive::kPrimLong: {
2691 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002692 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002693 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2694 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002695 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2696 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2697 // output and request another temp.
2698 if (rem->InputAt(1)->IsConstant()) {
2699 locations->AddTemp(Location::RequiresRegister());
2700 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002701 break;
2702 }
2703
2704 case Primitive::kPrimFloat:
2705 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002706 locations->SetInAt(0, Location::Any());
2707 locations->SetInAt(1, Location::Any());
2708 locations->SetOut(Location::RequiresFpuRegister());
2709 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002710 break;
2711 }
2712
2713 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002714 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002715 }
2716}
2717
2718void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2719 Primitive::Type type = rem->GetResultType();
2720 switch (type) {
2721 case Primitive::kPrimInt:
2722 case Primitive::kPrimLong: {
2723 GenerateDivRemIntegral(rem);
2724 break;
2725 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002726 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002727 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002728 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002729 break;
2730 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002731 default:
2732 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2733 }
2734}
2735
Calin Juravled0d48522014-11-04 16:40:20 +00002736void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2737 LocationSummary* locations =
2738 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2739 locations->SetInAt(0, Location::Any());
2740 if (instruction->HasUses()) {
2741 locations->SetOut(Location::SameAsFirstInput());
2742 }
2743}
2744
2745void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2746 SlowPathCodeX86_64* slow_path =
2747 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2748 codegen_->AddSlowPath(slow_path);
2749
2750 LocationSummary* locations = instruction->GetLocations();
2751 Location value = locations->InAt(0);
2752
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002753 switch (instruction->GetType()) {
2754 case Primitive::kPrimInt: {
2755 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002757 __ j(kEqual, slow_path->GetEntryLabel());
2758 } else if (value.IsStackSlot()) {
2759 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2760 __ j(kEqual, slow_path->GetEntryLabel());
2761 } else {
2762 DCHECK(value.IsConstant()) << value;
2763 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2764 __ jmp(slow_path->GetEntryLabel());
2765 }
2766 }
2767 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002768 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002769 case Primitive::kPrimLong: {
2770 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002772 __ j(kEqual, slow_path->GetEntryLabel());
2773 } else if (value.IsDoubleStackSlot()) {
2774 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2775 __ j(kEqual, slow_path->GetEntryLabel());
2776 } else {
2777 DCHECK(value.IsConstant()) << value;
2778 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2779 __ jmp(slow_path->GetEntryLabel());
2780 }
2781 }
2782 break;
2783 }
2784 default:
2785 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002786 }
Calin Juravled0d48522014-11-04 16:40:20 +00002787}
2788
Calin Juravle9aec02f2014-11-18 23:06:35 +00002789void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2790 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2791
2792 LocationSummary* locations =
2793 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2794
2795 switch (op->GetResultType()) {
2796 case Primitive::kPrimInt:
2797 case Primitive::kPrimLong: {
2798 locations->SetInAt(0, Location::RequiresRegister());
2799 // The shift count needs to be in CL.
2800 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2801 locations->SetOut(Location::SameAsFirstInput());
2802 break;
2803 }
2804 default:
2805 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2806 }
2807}
2808
2809void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2810 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2811
2812 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002814 Location second = locations->InAt(1);
2815
2816 switch (op->GetResultType()) {
2817 case Primitive::kPrimInt: {
2818 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002819 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002820 if (op->IsShl()) {
2821 __ shll(first_reg, second_reg);
2822 } else if (op->IsShr()) {
2823 __ sarl(first_reg, second_reg);
2824 } else {
2825 __ shrl(first_reg, second_reg);
2826 }
2827 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002828 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002829 if (op->IsShl()) {
2830 __ shll(first_reg, imm);
2831 } else if (op->IsShr()) {
2832 __ sarl(first_reg, imm);
2833 } else {
2834 __ shrl(first_reg, imm);
2835 }
2836 }
2837 break;
2838 }
2839 case Primitive::kPrimLong: {
2840 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002841 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002842 if (op->IsShl()) {
2843 __ shlq(first_reg, second_reg);
2844 } else if (op->IsShr()) {
2845 __ sarq(first_reg, second_reg);
2846 } else {
2847 __ shrq(first_reg, second_reg);
2848 }
2849 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002850 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002851 if (op->IsShl()) {
2852 __ shlq(first_reg, imm);
2853 } else if (op->IsShr()) {
2854 __ sarq(first_reg, imm);
2855 } else {
2856 __ shrq(first_reg, imm);
2857 }
2858 }
2859 break;
2860 }
2861 default:
2862 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2863 }
2864}
2865
2866void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2867 HandleShift(shl);
2868}
2869
2870void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2871 HandleShift(shl);
2872}
2873
2874void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2875 HandleShift(shr);
2876}
2877
2878void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2879 HandleShift(shr);
2880}
2881
2882void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2883 HandleShift(ushr);
2884}
2885
2886void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2887 HandleShift(ushr);
2888}
2889
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002890void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002891 LocationSummary* locations =
2892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002893 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002894 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2895 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2896 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002897}
2898
2899void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2900 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002901 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002902 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2903
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002904 __ gs()->call(
2905 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002906
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002907 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002908 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002909}
2910
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002911void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2912 LocationSummary* locations =
2913 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2914 InvokeRuntimeCallingConvention calling_convention;
2915 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002916 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002917 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002918 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002919}
2920
2921void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2922 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002923 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002924 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2925
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002926 __ gs()->call(
2927 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002928
2929 DCHECK(!codegen_->IsLeafMethod());
2930 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2931}
2932
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002933void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002934 LocationSummary* locations =
2935 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002936 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2937 if (location.IsStackSlot()) {
2938 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2939 } else if (location.IsDoubleStackSlot()) {
2940 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2941 }
2942 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002943}
2944
2945void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2946 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002947 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002948}
2949
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002950void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002951 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002952 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002953 locations->SetInAt(0, Location::RequiresRegister());
2954 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002955}
2956
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002957void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2958 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002959 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2960 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002961 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002962 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002963 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002964 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002965 break;
2966
2967 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002969 break;
2970
2971 default:
2972 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2973 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002974}
2975
2976void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002977 LocationSummary* locations =
2978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002979 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2980 locations->SetInAt(i, Location::Any());
2981 }
2982 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002983}
2984
2985void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002986 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002987 LOG(FATAL) << "Unimplemented";
2988}
2989
Calin Juravle52c48962014-12-16 17:02:57 +00002990void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2991 /*
2992 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2993 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2994 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2995 */
2996 switch (kind) {
2997 case MemBarrierKind::kAnyAny: {
2998 __ mfence();
2999 break;
3000 }
3001 case MemBarrierKind::kAnyStore:
3002 case MemBarrierKind::kLoadAny:
3003 case MemBarrierKind::kStoreStore: {
3004 // nop
3005 break;
3006 }
3007 default:
3008 LOG(FATAL) << "Unexpected memory barier " << kind;
3009 }
3010}
3011
3012void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3013 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3014
Nicolas Geoffray39468442014-09-02 15:17:15 +01003015 LocationSummary* locations =
3016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003017 locations->SetInAt(0, Location::RequiresRegister());
3018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3019}
3020
3021void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3022 const FieldInfo& field_info) {
3023 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3024
3025 LocationSummary* locations = instruction->GetLocations();
3026 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3027 Location out = locations->Out();
3028 bool is_volatile = field_info.IsVolatile();
3029 Primitive::Type field_type = field_info.GetFieldType();
3030 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3031
3032 switch (field_type) {
3033 case Primitive::kPrimBoolean: {
3034 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3035 break;
3036 }
3037
3038 case Primitive::kPrimByte: {
3039 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3040 break;
3041 }
3042
3043 case Primitive::kPrimShort: {
3044 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3045 break;
3046 }
3047
3048 case Primitive::kPrimChar: {
3049 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3050 break;
3051 }
3052
3053 case Primitive::kPrimInt:
3054 case Primitive::kPrimNot: {
3055 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3056 break;
3057 }
3058
3059 case Primitive::kPrimLong: {
3060 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3061 break;
3062 }
3063
3064 case Primitive::kPrimFloat: {
3065 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3066 break;
3067 }
3068
3069 case Primitive::kPrimDouble: {
3070 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3071 break;
3072 }
3073
3074 case Primitive::kPrimVoid:
3075 LOG(FATAL) << "Unreachable type " << field_type;
3076 UNREACHABLE();
3077 }
3078
Calin Juravle77520bc2015-01-12 18:45:46 +00003079 codegen_->MaybeRecordImplicitNullCheck(instruction);
3080
Calin Juravle52c48962014-12-16 17:02:57 +00003081 if (is_volatile) {
3082 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3083 }
3084}
3085
3086void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3087 const FieldInfo& field_info) {
3088 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3089
3090 LocationSummary* locations =
3091 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003092 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003093 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3094
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003095 locations->SetInAt(0, Location::RequiresRegister());
3096 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003097 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003098 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003099 locations->AddTemp(Location::RequiresRegister());
3100 locations->AddTemp(Location::RequiresRegister());
3101 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003102}
3103
Calin Juravle52c48962014-12-16 17:02:57 +00003104void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3105 const FieldInfo& field_info) {
3106 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3107
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003108 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003109 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3110 Location value = locations->InAt(1);
3111 bool is_volatile = field_info.IsVolatile();
3112 Primitive::Type field_type = field_info.GetFieldType();
3113 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3114
3115 if (is_volatile) {
3116 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3117 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003118
3119 switch (field_type) {
3120 case Primitive::kPrimBoolean:
3121 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003122 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003123 break;
3124 }
3125
3126 case Primitive::kPrimShort:
3127 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003128 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003129 break;
3130 }
3131
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003133 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003134 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003135 break;
3136 }
3137
3138 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003139 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003140 break;
3141 }
3142
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003143 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003144 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003145 break;
3146 }
3147
3148 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003149 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003150 break;
3151 }
3152
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003153 case Primitive::kPrimVoid:
3154 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003155 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003156 }
Calin Juravle52c48962014-12-16 17:02:57 +00003157
Calin Juravle77520bc2015-01-12 18:45:46 +00003158 codegen_->MaybeRecordImplicitNullCheck(instruction);
3159
3160 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3161 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3162 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3163 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3164 }
3165
Calin Juravle52c48962014-12-16 17:02:57 +00003166 if (is_volatile) {
3167 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3168 }
3169}
3170
3171void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3172 HandleFieldSet(instruction, instruction->GetFieldInfo());
3173}
3174
3175void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3176 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003177}
3178
3179void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003180 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003181}
3182
3183void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003184 HandleFieldGet(instruction, instruction->GetFieldInfo());
3185}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003186
Calin Juravle52c48962014-12-16 17:02:57 +00003187void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3188 HandleFieldGet(instruction);
3189}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003190
Calin Juravle52c48962014-12-16 17:02:57 +00003191void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3192 HandleFieldGet(instruction, instruction->GetFieldInfo());
3193}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194
Calin Juravle52c48962014-12-16 17:02:57 +00003195void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3196 HandleFieldSet(instruction, instruction->GetFieldInfo());
3197}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003198
Calin Juravle52c48962014-12-16 17:02:57 +00003199void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3200 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003201}
3202
3203void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003204 LocationSummary* locations =
3205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003206 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3207 ? Location::RequiresRegister()
3208 : Location::Any();
3209 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003210 if (instruction->HasUses()) {
3211 locations->SetOut(Location::SameAsFirstInput());
3212 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003213}
3214
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003215void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003216 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3217 return;
3218 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003219 LocationSummary* locations = instruction->GetLocations();
3220 Location obj = locations->InAt(0);
3221
3222 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3223 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3224}
3225
3226void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003227 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003228 codegen_->AddSlowPath(slow_path);
3229
3230 LocationSummary* locations = instruction->GetLocations();
3231 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003232
3233 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003234 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003235 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003236 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003237 } else {
3238 DCHECK(obj.IsConstant()) << obj;
3239 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3240 __ jmp(slow_path->GetEntryLabel());
3241 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003242 }
3243 __ j(kEqual, slow_path->GetEntryLabel());
3244}
3245
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003246void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3247 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3248 GenerateImplicitNullCheck(instruction);
3249 } else {
3250 GenerateExplicitNullCheck(instruction);
3251 }
3252}
3253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003254void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003255 LocationSummary* locations =
3256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003257 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003258 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003259 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003261}
3262
3263void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3264 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003266 Location index = locations->InAt(1);
3267
3268 switch (instruction->GetType()) {
3269 case Primitive::kPrimBoolean: {
3270 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003271 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272 if (index.IsConstant()) {
3273 __ movzxb(out, Address(obj,
3274 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3275 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003276 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003277 }
3278 break;
3279 }
3280
3281 case Primitive::kPrimByte: {
3282 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003283 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003284 if (index.IsConstant()) {
3285 __ movsxb(out, Address(obj,
3286 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3287 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003288 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003289 }
3290 break;
3291 }
3292
3293 case Primitive::kPrimShort: {
3294 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003296 if (index.IsConstant()) {
3297 __ movsxw(out, Address(obj,
3298 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3299 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003300 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301 }
3302 break;
3303 }
3304
3305 case Primitive::kPrimChar: {
3306 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003307 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003308 if (index.IsConstant()) {
3309 __ movzxw(out, Address(obj,
3310 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3311 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003312 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003313 }
3314 break;
3315 }
3316
3317 case Primitive::kPrimInt:
3318 case Primitive::kPrimNot: {
3319 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3320 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003321 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003322 if (index.IsConstant()) {
3323 __ movl(out, Address(obj,
3324 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003327 }
3328 break;
3329 }
3330
3331 case Primitive::kPrimLong: {
3332 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003333 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003334 if (index.IsConstant()) {
3335 __ movq(out, Address(obj,
3336 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3337 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339 }
3340 break;
3341 }
3342
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003343 case Primitive::kPrimFloat: {
3344 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003345 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003346 if (index.IsConstant()) {
3347 __ movss(out, Address(obj,
3348 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3349 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003350 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003351 }
3352 break;
3353 }
3354
3355 case Primitive::kPrimDouble: {
3356 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003357 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003358 if (index.IsConstant()) {
3359 __ movsd(out, Address(obj,
3360 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3361 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003363 }
3364 break;
3365 }
3366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367 case Primitive::kPrimVoid:
3368 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003369 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003371 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003372}
3373
3374void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003375 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003376
3377 bool needs_write_barrier =
3378 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3379 bool needs_runtime_call = instruction->NeedsTypeCheck();
3380
Nicolas Geoffray39468442014-09-02 15:17:15 +01003381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003382 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3383 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003384 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003385 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3386 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3387 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003388 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003389 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003390 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003391 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3392 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003393 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003394 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003395 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3396 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003397 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003398 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003399 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003400
3401 if (needs_write_barrier) {
3402 // Temporary registers for the write barrier.
3403 locations->AddTemp(Location::RequiresRegister());
3404 locations->AddTemp(Location::RequiresRegister());
3405 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003406 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003407}
3408
3409void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3410 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003411 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003412 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003413 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003414 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003415 bool needs_runtime_call = locations->WillCall();
3416 bool needs_write_barrier =
3417 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003418
3419 switch (value_type) {
3420 case Primitive::kPrimBoolean:
3421 case Primitive::kPrimByte: {
3422 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003423 if (index.IsConstant()) {
3424 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003425 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003427 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003428 __ movb(Address(obj, offset),
3429 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003430 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003431 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003432 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003433 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3434 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003435 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003436 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003437 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3438 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003439 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003440 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003441 break;
3442 }
3443
3444 case Primitive::kPrimShort:
3445 case Primitive::kPrimChar: {
3446 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003447 if (index.IsConstant()) {
3448 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003449 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003451 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003452 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003453 __ movw(Address(obj, offset),
3454 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003455 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003457 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003458 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003459 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3460 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003461 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003462 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003464 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3465 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003467 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003468 break;
3469 }
3470
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003471 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003472 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003473 if (!needs_runtime_call) {
3474 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3475 if (index.IsConstant()) {
3476 size_t offset =
3477 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3478 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003480 } else {
3481 DCHECK(value.IsConstant()) << value;
3482 __ movl(Address(obj, offset),
3483 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3484 }
3485 } else {
3486 DCHECK(index.IsRegister()) << index;
3487 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003488 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3489 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003490 } else {
3491 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003493 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3494 }
3495 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003496 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003497 if (needs_write_barrier) {
3498 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3500 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3501 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003502 }
3503 } else {
3504 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003505 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3506 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003507 DCHECK(!codegen_->IsLeafMethod());
3508 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3509 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003510 break;
3511 }
3512
3513 case Primitive::kPrimLong: {
3514 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003515 if (index.IsConstant()) {
3516 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003517 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003518 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003519 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003520 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003521 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3522 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003523 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003524 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003525 break;
3526 }
3527
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003528 case Primitive::kPrimFloat: {
3529 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3530 if (index.IsConstant()) {
3531 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3532 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003533 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003534 } else {
3535 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3537 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003538 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003539 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003540 break;
3541 }
3542
3543 case Primitive::kPrimDouble: {
3544 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3545 if (index.IsConstant()) {
3546 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3547 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003548 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003549 } else {
3550 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003551 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3552 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003553 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003554 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003555 break;
3556 }
3557
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003558 case Primitive::kPrimVoid:
3559 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003560 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003561 }
3562}
3563
3564void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003565 LocationSummary* locations =
3566 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003567 locations->SetInAt(0, Location::RequiresRegister());
3568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003569}
3570
3571void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3572 LocationSummary* locations = instruction->GetLocations();
3573 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003574 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3575 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003576 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003577 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578}
3579
3580void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003581 LocationSummary* locations =
3582 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003583 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003585 if (instruction->HasUses()) {
3586 locations->SetOut(Location::SameAsFirstInput());
3587 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003588}
3589
3590void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3591 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003592 Location index_loc = locations->InAt(0);
3593 Location length_loc = locations->InAt(1);
3594 SlowPathCodeX86_64* slow_path =
3595 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 codegen_->AddSlowPath(slow_path);
3597
Mark Mendellf60c90b2015-03-04 15:12:59 -05003598 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3599 if (index_loc.IsConstant()) {
3600 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3601 __ cmpl(length, Immediate(value));
3602 } else {
3603 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3604 }
3605 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606}
3607
3608void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3609 CpuRegister card,
3610 CpuRegister object,
3611 CpuRegister value) {
3612 Label is_null;
3613 __ testl(value, value);
3614 __ j(kEqual, &is_null);
3615 __ gs()->movq(card, Address::Absolute(
3616 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3617 __ movq(temp, object);
3618 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3619 __ movb(Address(temp, card, TIMES_1, 0), card);
3620 __ Bind(&is_null);
3621}
3622
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003623void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3624 temp->SetLocations(nullptr);
3625}
3626
3627void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3628 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003629 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003630}
3631
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003632void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003633 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003634 LOG(FATAL) << "Unimplemented";
3635}
3636
3637void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003638 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3639}
3640
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003641void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3642 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3643}
3644
3645void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003646 HBasicBlock* block = instruction->GetBlock();
3647 if (block->GetLoopInformation() != nullptr) {
3648 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3649 // The back edge will generate the suspend check.
3650 return;
3651 }
3652 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3653 // The goto will generate the suspend check.
3654 return;
3655 }
3656 GenerateSuspendCheck(instruction, nullptr);
3657}
3658
3659void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3660 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003661 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003662 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003663 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003664 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003665 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003666 if (successor == nullptr) {
3667 __ j(kNotEqual, slow_path->GetEntryLabel());
3668 __ Bind(slow_path->GetReturnLabel());
3669 } else {
3670 __ j(kEqual, codegen_->GetLabelOf(successor));
3671 __ jmp(slow_path->GetEntryLabel());
3672 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003673}
3674
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003675X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3676 return codegen_->GetAssembler();
3677}
3678
3679void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3680 MoveOperands* move = moves_.Get(index);
3681 Location source = move->GetSource();
3682 Location destination = move->GetDestination();
3683
3684 if (source.IsRegister()) {
3685 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003686 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003687 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003688 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003689 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003690 } else {
3691 DCHECK(destination.IsDoubleStackSlot());
3692 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003694 }
3695 } else if (source.IsStackSlot()) {
3696 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003697 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003698 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003699 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003701 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003702 } else {
3703 DCHECK(destination.IsStackSlot());
3704 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3705 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3706 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003707 } else if (source.IsDoubleStackSlot()) {
3708 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003710 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003711 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003712 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3713 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003714 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003715 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003716 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3717 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3718 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003719 } else if (source.IsConstant()) {
3720 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003721 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3722 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003723 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003724 if (value == 0) {
3725 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3726 } else {
3727 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3728 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003729 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003730 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003731 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003732 }
3733 } else if (constant->IsLongConstant()) {
3734 int64_t value = constant->AsLongConstant()->GetValue();
3735 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003736 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003737 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003738 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003739 __ movq(CpuRegister(TMP), Immediate(value));
3740 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3741 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003742 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003743 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003744 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003745 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003746 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003747 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3748 if (value == 0) {
3749 // easy FP 0.0.
3750 __ xorps(dest, dest);
3751 } else {
3752 __ movl(CpuRegister(TMP), imm);
3753 __ movd(dest, CpuRegister(TMP));
3754 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003755 } else {
3756 DCHECK(destination.IsStackSlot()) << destination;
3757 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3758 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003759 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003760 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003761 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003762 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003763 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003764 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003765 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3766 if (value == 0) {
3767 __ xorpd(dest, dest);
3768 } else {
3769 __ movq(CpuRegister(TMP), imm);
3770 __ movd(dest, CpuRegister(TMP));
3771 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003772 } else {
3773 DCHECK(destination.IsDoubleStackSlot()) << destination;
3774 __ movq(CpuRegister(TMP), imm);
3775 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3776 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003777 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003778 } else if (source.IsFpuRegister()) {
3779 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003781 } else if (destination.IsStackSlot()) {
3782 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003784 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003785 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003786 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003788 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003789 }
3790}
3791
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003792void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003793 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003794 __ movl(Address(CpuRegister(RSP), mem), reg);
3795 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003796}
3797
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003798void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003799 ScratchRegisterScope ensure_scratch(
3800 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3801
3802 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3803 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3804 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3805 Address(CpuRegister(RSP), mem2 + stack_offset));
3806 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3807 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3808 CpuRegister(ensure_scratch.GetRegister()));
3809}
3810
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003811void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3812 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3813 __ movq(Address(CpuRegister(RSP), mem), reg);
3814 __ movq(reg, CpuRegister(TMP));
3815}
3816
3817void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3818 ScratchRegisterScope ensure_scratch(
Mark Mendella5c19ce2015-04-01 12:51:05 -04003819 this, TMP, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003820
Mark Mendella5c19ce2015-04-01 12:51:05 -04003821 int temp_reg = ensure_scratch.GetRegister();
3822 if (temp_reg == kNoRegister) {
3823 // Use the stack as a temporary.
3824 // Save mem1 on the stack.
3825 __ pushq(Address(CpuRegister(RSP), mem1));
3826
3827 // Copy mem2 into mem1.
3828 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem2 + kX86_64WordSize));
3829 __ movq(Address(CpuRegister(RSP), mem1 + kX86_64WordSize), CpuRegister(TMP));
3830
3831 // Now pop mem1 into mem2.
3832 __ popq(Address(CpuRegister(RSP), mem2));
3833 } else {
3834 CpuRegister temp = CpuRegister(temp_reg);
3835 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1));
3836 __ movq(temp, Address(CpuRegister(RSP), mem2));
3837 __ movq(Address(CpuRegister(RSP), mem2), CpuRegister(TMP));
3838 __ movq(Address(CpuRegister(RSP), mem1), temp);
3839 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003840}
3841
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003842void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3843 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3844 __ movss(Address(CpuRegister(RSP), mem), reg);
3845 __ movd(reg, CpuRegister(TMP));
3846}
3847
Mark Mendella5c19ce2015-04-01 12:51:05 -04003848void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
3849 // Prefer to avoid xchg as it isn't speedy on smaller processors.
3850 __ movq(CpuRegister(TMP), reg1);
3851 __ movq(reg1, reg2);
3852 __ movq(reg2, CpuRegister(TMP));
3853}
3854
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003855void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3856 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3857 __ movsd(Address(CpuRegister(RSP), mem), reg);
3858 __ movd(reg, CpuRegister(TMP));
3859}
3860
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003861void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3862 MoveOperands* move = moves_.Get(index);
3863 Location source = move->GetSource();
3864 Location destination = move->GetDestination();
3865
3866 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04003867 Exchange64(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003868 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003870 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003871 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003872 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003873 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3874 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003875 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003876 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003877 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003878 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3879 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003880 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3882 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3883 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003884 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003885 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003886 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003887 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003888 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003889 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003890 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003891 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003892 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003893 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003894 }
3895}
3896
3897
3898void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3899 __ pushq(CpuRegister(reg));
3900}
3901
3902
3903void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3904 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003905}
3906
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003907void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3908 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3909 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3910 Immediate(mirror::Class::kStatusInitialized));
3911 __ j(kLess, slow_path->GetEntryLabel());
3912 __ Bind(slow_path->GetExitLabel());
3913 // No need for memory fence, thanks to the X86_64 memory model.
3914}
3915
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003916void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003917 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3918 ? LocationSummary::kCallOnSlowPath
3919 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003920 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003921 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003922 locations->SetOut(Location::RequiresRegister());
3923}
3924
3925void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003926 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003927 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003928 DCHECK(!cls->CanCallRuntime());
3929 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003930 codegen_->LoadCurrentMethod(out);
3931 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3932 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003933 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003934 codegen_->LoadCurrentMethod(out);
3935 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3936 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003937 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3938 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3939 codegen_->AddSlowPath(slow_path);
3940 __ testl(out, out);
3941 __ j(kEqual, slow_path->GetEntryLabel());
3942 if (cls->MustGenerateClinitCheck()) {
3943 GenerateClassInitializationCheck(slow_path, out);
3944 } else {
3945 __ Bind(slow_path->GetExitLabel());
3946 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003947 }
3948}
3949
3950void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3951 LocationSummary* locations =
3952 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3953 locations->SetInAt(0, Location::RequiresRegister());
3954 if (check->HasUses()) {
3955 locations->SetOut(Location::SameAsFirstInput());
3956 }
3957}
3958
3959void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003960 // We assume the class to not be null.
3961 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3962 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003963 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003964 GenerateClassInitializationCheck(slow_path,
3965 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003966}
3967
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003968void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3969 LocationSummary* locations =
3970 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3971 locations->SetOut(Location::RequiresRegister());
3972}
3973
3974void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3975 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3976 codegen_->AddSlowPath(slow_path);
3977
Roland Levillain271ab9c2014-11-27 15:23:57 +00003978 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003979 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003980 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3981 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003982 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3983 __ testl(out, out);
3984 __ j(kEqual, slow_path->GetEntryLabel());
3985 __ Bind(slow_path->GetExitLabel());
3986}
3987
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003988void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3989 LocationSummary* locations =
3990 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3991 locations->SetOut(Location::RequiresRegister());
3992}
3993
3994void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3995 Address address = Address::Absolute(
3996 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003997 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003998 __ gs()->movl(address, Immediate(0));
3999}
4000
4001void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4002 LocationSummary* locations =
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4004 InvokeRuntimeCallingConvention calling_convention;
4005 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4006}
4007
4008void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4009 __ gs()->call(
4010 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4011 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4012}
4013
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004014void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004015 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4016 ? LocationSummary::kNoCall
4017 : LocationSummary::kCallOnSlowPath;
4018 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4019 locations->SetInAt(0, Location::RequiresRegister());
4020 locations->SetInAt(1, Location::Any());
4021 locations->SetOut(Location::RequiresRegister());
4022}
4023
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004024void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004025 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004026 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004027 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004028 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004029 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4030 Label done, zero;
4031 SlowPathCodeX86_64* slow_path = nullptr;
4032
4033 // Return 0 if `obj` is null.
4034 // TODO: avoid this check if we know obj is not null.
4035 __ testl(obj, obj);
4036 __ j(kEqual, &zero);
4037 // Compare the class of `obj` with `cls`.
4038 __ movl(out, Address(obj, class_offset));
4039 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004040 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004041 } else {
4042 DCHECK(cls.IsStackSlot()) << cls;
4043 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4044 }
4045 if (instruction->IsClassFinal()) {
4046 // Classes must be equal for the instanceof to succeed.
4047 __ j(kNotEqual, &zero);
4048 __ movl(out, Immediate(1));
4049 __ jmp(&done);
4050 } else {
4051 // If the classes are not equal, we go into a slow path.
4052 DCHECK(locations->OnlyCallsOnSlowPath());
4053 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004054 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004055 codegen_->AddSlowPath(slow_path);
4056 __ j(kNotEqual, slow_path->GetEntryLabel());
4057 __ movl(out, Immediate(1));
4058 __ jmp(&done);
4059 }
4060 __ Bind(&zero);
4061 __ movl(out, Immediate(0));
4062 if (slow_path != nullptr) {
4063 __ Bind(slow_path->GetExitLabel());
4064 }
4065 __ Bind(&done);
4066}
4067
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004068void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4069 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4070 instruction, LocationSummary::kCallOnSlowPath);
4071 locations->SetInAt(0, Location::RequiresRegister());
4072 locations->SetInAt(1, Location::Any());
4073 locations->AddTemp(Location::RequiresRegister());
4074}
4075
4076void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4077 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004079 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004080 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004081 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4082 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4083 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4084 codegen_->AddSlowPath(slow_path);
4085
4086 // TODO: avoid this check if we know obj is not null.
4087 __ testl(obj, obj);
4088 __ j(kEqual, slow_path->GetExitLabel());
4089 // Compare the class of `obj` with `cls`.
4090 __ movl(temp, Address(obj, class_offset));
4091 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004093 } else {
4094 DCHECK(cls.IsStackSlot()) << cls;
4095 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4096 }
4097 // Classes must be equal for the checkcast to succeed.
4098 __ j(kNotEqual, slow_path->GetEntryLabel());
4099 __ Bind(slow_path->GetExitLabel());
4100}
4101
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004102void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4103 LocationSummary* locations =
4104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4105 InvokeRuntimeCallingConvention calling_convention;
4106 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4107}
4108
4109void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4110 __ gs()->call(Address::Absolute(instruction->IsEnter()
4111 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4112 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4113 true));
4114 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4115}
4116
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004117void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4118void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4119void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4120
4121void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4122 LocationSummary* locations =
4123 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4124 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4125 || instruction->GetResultType() == Primitive::kPrimLong);
4126 locations->SetInAt(0, Location::RequiresRegister());
4127 if (instruction->GetType() == Primitive::kPrimInt) {
4128 locations->SetInAt(1, Location::Any());
4129 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004130 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004131 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004132 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004133 }
4134 locations->SetOut(Location::SameAsFirstInput());
4135}
4136
4137void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4138 HandleBitwiseOperation(instruction);
4139}
4140
4141void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4142 HandleBitwiseOperation(instruction);
4143}
4144
4145void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4146 HandleBitwiseOperation(instruction);
4147}
4148
4149void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4150 LocationSummary* locations = instruction->GetLocations();
4151 Location first = locations->InAt(0);
4152 Location second = locations->InAt(1);
4153 DCHECK(first.Equals(locations->Out()));
4154
4155 if (instruction->GetResultType() == Primitive::kPrimInt) {
4156 if (second.IsRegister()) {
4157 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004159 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004160 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004161 } else {
4162 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004163 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004164 }
4165 } else if (second.IsConstant()) {
4166 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4167 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004168 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004169 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004170 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004171 } else {
4172 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004173 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004174 }
4175 } else {
4176 Address address(CpuRegister(RSP), second.GetStackIndex());
4177 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004178 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004179 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004180 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004181 } else {
4182 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004183 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004184 }
4185 }
4186 } else {
4187 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004188 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4189 bool second_is_constant = false;
4190 int64_t value = 0;
4191 if (second.IsConstant()) {
4192 second_is_constant = true;
4193 value = second.GetConstant()->AsLongConstant()->GetValue();
4194 DCHECK(IsInt<32>(value));
4195 }
4196
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004197 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004198 if (second_is_constant) {
4199 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4200 } else {
4201 __ andq(first_reg, second.AsRegister<CpuRegister>());
4202 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004203 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004204 if (second_is_constant) {
4205 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4206 } else {
4207 __ orq(first_reg, second.AsRegister<CpuRegister>());
4208 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004209 } else {
4210 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004211 if (second_is_constant) {
4212 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4213 } else {
4214 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4215 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004216 }
4217 }
4218}
4219
Calin Juravleb1498f62015-02-16 13:13:29 +00004220void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4221 // Nothing to do, this should be removed during prepare for register allocator.
4222 UNUSED(instruction);
4223 LOG(FATAL) << "Unreachable";
4224}
4225
4226void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4227 // Nothing to do, this should be removed during prepare for register allocator.
4228 UNUSED(instruction);
4229 LOG(FATAL) << "Unreachable";
4230}
4231
Mark Mendellf55c3e02015-03-26 21:07:46 -04004232void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4233 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004234 X86_64Assembler* assembler = GetAssembler();
4235 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004236 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4237 // byte values. If used for vectors at a later time, this will need to be
4238 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004239 assembler->Align(4, 0);
4240 constant_area_start_ = assembler->CodeSize();
4241 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004242 }
4243
4244 // And finish up.
4245 CodeGenerator::Finalize(allocator);
4246}
4247
4248/**
4249 * Class to handle late fixup of offsets into constant area.
4250 */
4251class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4252 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004253 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004254 : codegen_(codegen), offset_into_constant_area_(offset) {}
4255
4256 private:
4257 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4258 // Patch the correct offset for the instruction. We use the address of the
4259 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4260 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4261 int relative_position = constant_offset - pos;
4262
4263 // Patch in the right value.
4264 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4265 }
4266
Mark Mendell39dcf552015-04-09 20:42:42 -04004267 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004268
4269 // Location in constant area that the fixup refers to.
4270 int offset_into_constant_area_;
4271};
4272
4273Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4274 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4275 return Address::RIP(fixup);
4276}
4277
4278Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4279 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4280 return Address::RIP(fixup);
4281}
4282
4283Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4284 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4285 return Address::RIP(fixup);
4286}
4287
4288Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4289 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4290 return Address::RIP(fixup);
4291}
4292
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004293} // namespace x86_64
4294} // namespace art