blob: b4020cf27b31edaacab60e51e60319d30b58a960 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080022#include "intrinsics.h"
23#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010027#include "mirror/object_reference.h"
28#include "thread.h"
29#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/managed_register_x86_64.h"
33
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010034namespace art {
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036namespace x86_64 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038// Some x86_64 instructions require a register to be available as temp.
39static constexpr Register TMP = R11;
40
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000043static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
50
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010051class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Alexandre Rames2ed20af2015-03-06 13:55:35 +000055 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010057 __ gs()->call(
58 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000059 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 }
61
62 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010063 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
65};
66
Calin Juravled0d48522014-11-04 16:40:20 +000067class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
68 public:
69 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
70
Alexandre Rames2ed20af2015-03-06 13:55:35 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000072 __ Bind(GetEntryLabel());
73 __ gs()->call(
74 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000075 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000076 }
77
78 private:
79 HDivZeroCheck* const instruction_;
80 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
81};
82
Calin Juravlebacfec32014-11-14 15:54:36 +000083class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000085 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
86 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +000090 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +000091 if (is_div_) {
92 __ negl(cpu_reg_);
93 } else {
94 __ movl(cpu_reg_, Immediate(0));
95 }
96
Calin Juravled6fb6cf2014-11-11 19:07:44 +000097 } else {
98 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +000099 if (is_div_) {
100 __ negq(cpu_reg_);
101 } else {
102 __ movq(cpu_reg_, Immediate(0));
103 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000104 }
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ jmp(GetExitLabel());
106 }
107
108 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000110 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000111 const bool is_div_;
112 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000113};
114
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100117 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000124 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000125 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ jmp(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
139 private:
140 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100141 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 Label return_label_;
143
144 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
145};
146
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100149 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
150 Location index_location,
151 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 : instruction_(instruction),
153 index_location_(index_location),
154 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000158 // We're moving two locations to locations that could overlap, so we need a parallel
159 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 codegen->EmitParallelMoves(
162 index_location_,
163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
164 length_location_,
165 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 __ gs()->call(Address::Absolute(
167 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000168 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100169 }
170
171 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 const Location index_location_;
174 const Location length_location_;
175
176 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
177};
178
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000179class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100180 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000181 LoadClassSlowPathX86_64(HLoadClass* cls,
182 HInstruction* at,
183 uint32_t dex_pc,
184 bool do_clinit)
185 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
186 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
187 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100188
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000190 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100191 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
192 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000199 __ gs()->call(Address::Absolute((do_clinit_
200 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
201 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000202 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000204 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000205 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 }
210
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 __ jmp(GetExitLabel());
213 }
214
215 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216 // The class this slow path will load.
217 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219 // The instruction where this slow path is happening.
220 // (Might be the load class or an initialization check).
221 HInstruction* const at_;
222
223 // The dex PC of `at_`.
224 const uint32_t dex_pc_;
225
226 // Whether to initialize the class.
227 const bool do_clinit_;
228
229 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230};
231
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
233 public:
234 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
235
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237 LocationSummary* locations = instruction_->GetLocations();
238 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
239
240 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
241 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000242 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243
244 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800245 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
246 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 Immediate(instruction_->GetStringIndex()));
248 __ gs()->call(Address::Absolute(
249 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000251 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 __ jmp(GetExitLabel());
254 }
255
256 private:
257 HLoadString* const instruction_;
258
259 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
260};
261
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
263 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000264 TypeCheckSlowPathX86_64(HInstruction* instruction,
265 Location class_to_check,
266 Location object_class,
267 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000269 class_to_check_(class_to_check),
270 object_class_(object_class),
271 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 DCHECK(instruction_->IsCheckCast()
276 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
278 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 // We're moving two locations to locations that could overlap, so we need a parallel
283 // move resolver.
284 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000285 codegen->EmitParallelMoves(
286 class_to_check_,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
288 object_class_,
289 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 if (instruction_->IsInstanceOf()) {
292 __ gs()->call(
293 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
294 } else {
295 DCHECK(instruction_->IsCheckCast());
296 __ gs()->call(
297 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
298 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000299 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300
301 if (instruction_->IsInstanceOf()) {
302 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
303 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000305 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 __ jmp(GetExitLabel());
307 }
308
309 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 HInstruction* const instruction_;
311 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
315 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
316};
317
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700318class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
319 public:
320 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
321 : instruction_(instruction) {}
322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 __ Bind(GetEntryLabel());
325 SaveLiveRegisters(codegen, instruction_->GetLocations());
326 __ gs()->call(
327 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeoptimize), true));
328 DCHECK(instruction_->IsDeoptimize());
329 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
330 uint32_t dex_pc = deoptimize->GetDexPc();
331 codegen->RecordPcInfo(instruction_, dex_pc, this);
332 }
333
334 private:
335 HInstruction* const instruction_;
336 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
337};
338
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100339#undef __
340#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
341
Dave Allison20dfc792014-06-16 20:44:29 -0700342inline Condition X86_64Condition(IfCondition cond) {
343 switch (cond) {
344 case kCondEQ: return kEqual;
345 case kCondNE: return kNotEqual;
346 case kCondLT: return kLess;
347 case kCondLE: return kLessEqual;
348 case kCondGT: return kGreater;
349 case kCondGE: return kGreaterEqual;
350 default:
351 LOG(FATAL) << "Unknown if condition";
352 }
353 return kEqual;
354}
355
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800356void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
357 CpuRegister temp) {
358 // All registers are assumed to be correctly set up.
359
360 // TODO: Implement all kinds of calls:
361 // 1) boot -> boot
362 // 2) app -> boot
363 // 3) app -> app
364 //
365 // Currently we implement the app -> app logic, which looks up in the resolve cache.
366
367 // temp = method;
368 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000369 if (!invoke->IsRecursive()) {
370 // temp = temp->dex_cache_resolved_methods_;
371 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
372 // temp = temp[index_in_cache]
373 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
374 // (temp + offset_of_quick_compiled_code)()
375 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
376 kX86_64WordSize).SizeValue()));
377 } else {
378 __ call(&frame_entry_label_);
379 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800380
381 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800382}
383
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100384void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
385 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
386}
387
388void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
389 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
390}
391
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100392size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
393 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
394 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100395}
396
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100397size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
398 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
399 return kX86_64WordSize;
400}
401
402size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
403 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
404 return kX86_64WordSize;
405}
406
407size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
408 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
409 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100410}
411
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000412static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000413// Use a fake return address register to mimic Quick.
414static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400415CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
416 const X86_64InstructionSetFeatures& isa_features,
417 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000418 : CodeGenerator(graph,
419 kNumberOfCpuRegisters,
420 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000421 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000422 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
423 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000424 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000425 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
426 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000427 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100428 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100429 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000430 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400431 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400432 isa_features_(isa_features),
433 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000434 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
435}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100437InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
438 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100439 : HGraphVisitor(graph),
440 assembler_(codegen->GetAssembler()),
441 codegen_(codegen) {}
442
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100444 switch (type) {
445 case Primitive::kPrimLong:
446 case Primitive::kPrimByte:
447 case Primitive::kPrimBoolean:
448 case Primitive::kPrimChar:
449 case Primitive::kPrimShort:
450 case Primitive::kPrimInt:
451 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100452 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100453 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454 }
455
456 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100457 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100459 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100460 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100461
462 case Primitive::kPrimVoid:
463 LOG(FATAL) << "Unreachable type " << type;
464 }
465
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100466 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100467}
468
Nicolas Geoffray98893962015-01-21 12:32:32 +0000469void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100470 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100471 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100472
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000473 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100474 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000475
Nicolas Geoffray98893962015-01-21 12:32:32 +0000476 if (is_baseline) {
477 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
478 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
479 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000480 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
481 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
482 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000483 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100484}
485
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100486static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100487 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100488}
David Srbecky9d8606d2015-04-12 09:35:32 +0100489
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100490static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100491 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100492}
493
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100494void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100495 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000496 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100497 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700498 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000499 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100500
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100502 __ testq(CpuRegister(RAX), Address(
503 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100504 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100505 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000506
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000507 if (HasEmptyFrame()) {
508 return;
509 }
510
Nicolas Geoffray98893962015-01-21 12:32:32 +0000511 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000512 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000513 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000514 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100515 __ cfi().AdjustCFAOffset(kX86_64WordSize);
516 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000517 }
518 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100519
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100520 int adjust = GetFrameSize() - GetCoreSpillSize();
521 __ subq(CpuRegister(RSP), Immediate(adjust));
522 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000523 uint32_t xmm_spill_location = GetFpuSpillStart();
524 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100525
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000526 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
527 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100528 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
529 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
530 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100532 }
533
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100534 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
535}
536
537void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000538 if (HasEmptyFrame()) {
539 return;
540 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000541 uint32_t xmm_spill_location = GetFpuSpillStart();
542 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
543 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
544 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100545 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
546 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
547 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000548 }
549 }
550
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100551 int adjust = GetFrameSize() - GetCoreSpillSize();
552 __ addq(CpuRegister(RSP), Immediate(adjust));
553 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000554
555 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000556 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000557 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000558 __ popq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100559 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
560 __ cfi().Restore(DWARFReg(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000561 }
562 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563}
564
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100565void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
566 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100567}
568
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100569void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000570 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100571 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
572}
573
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
575 switch (load->GetType()) {
576 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100577 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100578 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579
580 case Primitive::kPrimInt:
581 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100582 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584
585 case Primitive::kPrimBoolean:
586 case Primitive::kPrimByte:
587 case Primitive::kPrimChar:
588 case Primitive::kPrimShort:
589 case Primitive::kPrimVoid:
590 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700591 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100592 }
593
594 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700595 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100596}
597
598void CodeGeneratorX86_64::Move(Location destination, Location source) {
599 if (source.Equals(destination)) {
600 return;
601 }
602 if (destination.IsRegister()) {
603 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000604 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000606 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100607 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000608 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100609 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100610 } else {
611 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000612 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100613 Address(CpuRegister(RSP), source.GetStackIndex()));
614 }
615 } else if (destination.IsFpuRegister()) {
616 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 Address(CpuRegister(RSP), source.GetStackIndex()));
623 } else {
624 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000625 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100627 }
628 } else if (destination.IsStackSlot()) {
629 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100630 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (source.IsFpuRegister()) {
633 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000634 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500635 } else if (source.IsConstant()) {
636 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000637 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500638 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100639 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500640 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000641 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
642 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100643 }
644 } else {
645 DCHECK(destination.IsDoubleStackSlot());
646 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100647 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000648 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100649 } else if (source.IsFpuRegister()) {
650 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000651 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500652 } else if (source.IsConstant()) {
653 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800654 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500655 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000656 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500657 } else {
658 DCHECK(constant->IsLongConstant());
659 value = constant->AsLongConstant()->GetValue();
660 }
661 __ movq(CpuRegister(TMP), Immediate(value));
662 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663 } else {
664 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000665 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
666 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100667 }
668 }
669}
670
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100671void CodeGeneratorX86_64::Move(HInstruction* instruction,
672 Location location,
673 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000674 LocationSummary* locations = instruction->GetLocations();
675 if (locations != nullptr && locations->Out().Equals(location)) {
676 return;
677 }
678
679 if (locations != nullptr && locations->Out().IsConstant()) {
680 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000681 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
682 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000683 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000684 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000685 } else if (location.IsStackSlot()) {
686 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
687 } else {
688 DCHECK(location.IsConstant());
689 DCHECK_EQ(location.GetConstant(), const_to_move);
690 }
691 } else if (const_to_move->IsLongConstant()) {
692 int64_t value = const_to_move->AsLongConstant()->GetValue();
693 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000695 } else if (location.IsDoubleStackSlot()) {
696 __ movq(CpuRegister(TMP), Immediate(value));
697 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
698 } else {
699 DCHECK(location.IsConstant());
700 DCHECK_EQ(location.GetConstant(), const_to_move);
701 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702 }
Roland Levillain476df552014-10-09 17:51:36 +0100703 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704 switch (instruction->GetType()) {
705 case Primitive::kPrimBoolean:
706 case Primitive::kPrimByte:
707 case Primitive::kPrimChar:
708 case Primitive::kPrimShort:
709 case Primitive::kPrimInt:
710 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100712 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
713 break;
714
715 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000717 Move(location,
718 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100719 break;
720
721 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100723 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000724 } else if (instruction->IsTemporary()) {
725 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
726 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100727 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100728 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100729 switch (instruction->GetType()) {
730 case Primitive::kPrimBoolean:
731 case Primitive::kPrimByte:
732 case Primitive::kPrimChar:
733 case Primitive::kPrimShort:
734 case Primitive::kPrimInt:
735 case Primitive::kPrimNot:
736 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimFloat:
738 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000739 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100740 break;
741
742 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100744 }
745 }
746}
747
748void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
749 got->SetLocations(nullptr);
750}
751
752void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
753 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100754 DCHECK(!successor->IsExitBlock());
755
756 HBasicBlock* block = got->GetBlock();
757 HInstruction* previous = got->GetPrevious();
758
759 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000760 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100761 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
762 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
763 return;
764 }
765
766 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
767 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
768 }
769 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100770 __ jmp(codegen_->GetLabelOf(successor));
771 }
772}
773
774void LocationsBuilderX86_64::VisitExit(HExit* exit) {
775 exit->SetLocations(nullptr);
776}
777
778void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700779 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100780}
781
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700782void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
783 Label* true_target,
784 Label* false_target,
785 Label* always_true_target) {
786 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100787 if (cond->IsIntConstant()) {
788 // Constant condition, statically compared against 1.
789 int32_t cond_value = cond->AsIntConstant()->GetValue();
790 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700791 if (always_true_target != nullptr) {
792 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100793 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100794 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100795 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 DCHECK_EQ(cond_value, 0);
797 }
798 } else {
799 bool materialized =
800 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
801 // Moves do not affect the eflags register, so if the condition is
802 // evaluated just before the if, we don't need to evaluate it
803 // again.
804 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700805 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100806 if (materialized) {
807 if (!eflags_set) {
808 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700809 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100810 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000811 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100812 } else {
813 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
814 Immediate(0));
815 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700816 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700818 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 }
820 } else {
821 Location lhs = cond->GetLocations()->InAt(0);
822 Location rhs = cond->GetLocations()->InAt(1);
823 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000824 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000826 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000827 if (constant == 0) {
828 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
829 } else {
830 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
831 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000833 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100834 Address(CpuRegister(RSP), rhs.GetStackIndex()));
835 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700837 }
Dave Allison20dfc792014-06-16 20:44:29 -0700838 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700839 if (false_target != nullptr) {
840 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100841 }
842}
843
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700844void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
845 LocationSummary* locations =
846 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
847 HInstruction* cond = if_instr->InputAt(0);
848 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
849 locations->SetInAt(0, Location::Any());
850 }
851}
852
853void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
854 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
855 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
856 Label* always_true_target = true_target;
857 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
858 if_instr->IfTrueSuccessor())) {
859 always_true_target = nullptr;
860 }
861 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
862 if_instr->IfFalseSuccessor())) {
863 false_target = nullptr;
864 }
865 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
866}
867
868void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
869 LocationSummary* locations = new (GetGraph()->GetArena())
870 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
871 HInstruction* cond = deoptimize->InputAt(0);
872 DCHECK(cond->IsCondition());
873 if (cond->AsCondition()->NeedsMaterialization()) {
874 locations->SetInAt(0, Location::Any());
875 }
876}
877
878void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
879 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
880 DeoptimizationSlowPathX86_64(deoptimize);
881 codegen_->AddSlowPath(slow_path);
882 Label* slow_path_entry = slow_path->GetEntryLabel();
883 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
884}
885
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100886void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
887 local->SetLocations(nullptr);
888}
889
890void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
891 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
892}
893
894void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
895 local->SetLocations(nullptr);
896}
897
898void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
899 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700900 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100901}
902
903void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100904 LocationSummary* locations =
905 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100906 switch (store->InputAt(1)->GetType()) {
907 case Primitive::kPrimBoolean:
908 case Primitive::kPrimByte:
909 case Primitive::kPrimChar:
910 case Primitive::kPrimShort:
911 case Primitive::kPrimInt:
912 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100913 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100914 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
915 break;
916
917 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100918 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100919 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
920 break;
921
922 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100923 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100925}
926
927void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700928 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100929}
930
Dave Allison20dfc792014-06-16 20:44:29 -0700931void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100932 LocationSummary* locations =
933 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100934 locations->SetInAt(0, Location::RequiresRegister());
935 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100936 if (comp->NeedsMaterialization()) {
937 locations->SetOut(Location::RequiresRegister());
938 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100939}
940
Dave Allison20dfc792014-06-16 20:44:29 -0700941void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
942 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100943 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000944 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100945 // Clear register: setcc only sets the low byte.
946 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000947 Location lhs = locations->InAt(0);
948 Location rhs = locations->InAt(1);
949 if (rhs.IsRegister()) {
950 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
951 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800952 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000953 if (constant == 0) {
954 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
955 } else {
956 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
957 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100958 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000959 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100960 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100961 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700962 }
963}
964
965void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
966 VisitCondition(comp);
967}
968
969void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
970 VisitCondition(comp);
971}
972
973void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
974 VisitCondition(comp);
975}
976
977void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
978 VisitCondition(comp);
979}
980
981void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
982 VisitCondition(comp);
983}
984
985void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
986 VisitCondition(comp);
987}
988
989void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
990 VisitCondition(comp);
991}
992
993void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
994 VisitCondition(comp);
995}
996
997void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
998 VisitCondition(comp);
999}
1000
1001void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1002 VisitCondition(comp);
1003}
1004
1005void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1006 VisitCondition(comp);
1007}
1008
1009void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1010 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001011}
1012
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001013void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001014 LocationSummary* locations =
1015 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001016 switch (compare->InputAt(0)->GetType()) {
1017 case Primitive::kPrimLong: {
1018 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001019 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00001020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1021 break;
1022 }
1023 case Primitive::kPrimFloat:
1024 case Primitive::kPrimDouble: {
1025 locations->SetInAt(0, Location::RequiresFpuRegister());
1026 locations->SetInAt(1, Location::RequiresFpuRegister());
1027 locations->SetOut(Location::RequiresRegister());
1028 break;
1029 }
1030 default:
1031 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1032 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001033}
1034
1035void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001036 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001037 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001038 Location left = locations->InAt(0);
1039 Location right = locations->InAt(1);
1040
1041 Label less, greater, done;
1042 Primitive::Type type = compare->InputAt(0)->GetType();
1043 switch (type) {
1044 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001045 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1046 if (right.IsConstant()) {
1047 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1048 DCHECK(IsInt<32>(value));
1049 if (value == 0) {
1050 __ testq(left_reg, left_reg);
1051 } else {
1052 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1053 }
1054 } else {
1055 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1056 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001057 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001058 }
1059 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001060 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001061 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1062 break;
1063 }
1064 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001065 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001066 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1067 break;
1068 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001069 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001070 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001071 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001072 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001073 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001074 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001075
Calin Juravle91debbc2014-11-26 19:01:09 +00001076 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001077 __ movl(out, Immediate(1));
1078 __ jmp(&done);
1079
1080 __ Bind(&less);
1081 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001082
1083 __ Bind(&done);
1084}
1085
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001086void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001087 LocationSummary* locations =
1088 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001089 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001090}
1091
1092void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001093 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001094 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095}
1096
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001097void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1098 LocationSummary* locations =
1099 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1100 locations->SetOut(Location::ConstantLocation(constant));
1101}
1102
1103void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1104 // Will be generated at use site.
1105 UNUSED(constant);
1106}
1107
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001109 LocationSummary* locations =
1110 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001111 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001112}
1113
1114void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001115 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001116 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117}
1118
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001119void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1120 LocationSummary* locations =
1121 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1122 locations->SetOut(Location::ConstantLocation(constant));
1123}
1124
1125void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1126 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001127 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001128}
1129
1130void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1131 LocationSummary* locations =
1132 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1133 locations->SetOut(Location::ConstantLocation(constant));
1134}
1135
1136void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1137 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001139}
1140
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1142 ret->SetLocations(nullptr);
1143}
1144
1145void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001146 UNUSED(ret);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001147 __ cfi().RememberState();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148 codegen_->GenerateFrameExit();
1149 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001150 __ cfi().RestoreState();
1151 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152}
1153
1154void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001155 LocationSummary* locations =
1156 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001157 switch (ret->InputAt(0)->GetType()) {
1158 case Primitive::kPrimBoolean:
1159 case Primitive::kPrimByte:
1160 case Primitive::kPrimChar:
1161 case Primitive::kPrimShort:
1162 case Primitive::kPrimInt:
1163 case Primitive::kPrimNot:
1164 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001166 break;
1167
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 case Primitive::kPrimFloat:
1169 case Primitive::kPrimDouble:
1170 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 break;
1173
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001174 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001175 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001176 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001177}
1178
1179void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1180 if (kIsDebugBuild) {
1181 switch (ret->InputAt(0)->GetType()) {
1182 case Primitive::kPrimBoolean:
1183 case Primitive::kPrimByte:
1184 case Primitive::kPrimChar:
1185 case Primitive::kPrimShort:
1186 case Primitive::kPrimInt:
1187 case Primitive::kPrimNot:
1188 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190 break;
1191
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 case Primitive::kPrimFloat:
1193 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 XMM0);
1196 break;
1197
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001198 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001199 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001200 }
1201 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001202 __ cfi().RememberState();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001203 codegen_->GenerateFrameExit();
1204 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001205 __ cfi().RestoreState();
1206 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207}
1208
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001209Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1210 switch (type) {
1211 case Primitive::kPrimBoolean:
1212 case Primitive::kPrimByte:
1213 case Primitive::kPrimChar:
1214 case Primitive::kPrimShort:
1215 case Primitive::kPrimInt:
1216 case Primitive::kPrimNot: {
1217 uint32_t index = gp_index_++;
1218 stack_index_++;
1219 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001220 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221 } else {
1222 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1223 }
1224 }
1225
1226 case Primitive::kPrimLong: {
1227 uint32_t index = gp_index_;
1228 stack_index_ += 2;
1229 if (index < calling_convention.GetNumberOfRegisters()) {
1230 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001231 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001232 } else {
1233 gp_index_ += 2;
1234 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1235 }
1236 }
1237
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 case Primitive::kPrimFloat: {
1239 uint32_t index = fp_index_++;
1240 stack_index_++;
1241 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001242 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else {
1244 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1245 }
1246 }
1247
1248 case Primitive::kPrimDouble: {
1249 uint32_t index = fp_index_++;
1250 stack_index_ += 2;
1251 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001252 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 } else {
1254 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1255 }
1256 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001257
1258 case Primitive::kPrimVoid:
1259 LOG(FATAL) << "Unexpected parameter type " << type;
1260 break;
1261 }
1262 return Location();
1263}
1264
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001265void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001266 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001267 if (intrinsic.TryDispatch(invoke)) {
1268 return;
1269 }
1270
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001271 HandleInvoke(invoke);
1272}
1273
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001274static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1275 if (invoke->GetLocations()->Intrinsified()) {
1276 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1277 intrinsic.Dispatch(invoke);
1278 return true;
1279 }
1280 return false;
1281}
1282
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001283void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001284 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1285 return;
1286 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001287
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001288 codegen_->GenerateStaticOrDirectCall(
1289 invoke,
1290 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001291 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001292}
1293
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001294void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001295 LocationSummary* locations =
1296 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001298
1299 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001300 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001301 HInstruction* input = invoke->InputAt(i);
1302 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1303 }
1304
1305 switch (invoke->GetType()) {
1306 case Primitive::kPrimBoolean:
1307 case Primitive::kPrimByte:
1308 case Primitive::kPrimChar:
1309 case Primitive::kPrimShort:
1310 case Primitive::kPrimInt:
1311 case Primitive::kPrimNot:
1312 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001313 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001314 break;
1315
1316 case Primitive::kPrimVoid:
1317 break;
1318
1319 case Primitive::kPrimDouble:
1320 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001321 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001322 break;
1323 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001324}
1325
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001326void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001327 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001328 if (intrinsic.TryDispatch(invoke)) {
1329 return;
1330 }
1331
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001332 HandleInvoke(invoke);
1333}
1334
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001335void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001336 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1337 return;
1338 }
1339
Roland Levillain271ab9c2014-11-27 15:23:57 +00001340 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001341 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1342 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1343 LocationSummary* locations = invoke->GetLocations();
1344 Location receiver = locations->InAt(0);
1345 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1346 // temp = object->GetClass();
1347 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001348 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1349 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001350 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001351 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001352 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001353 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001354 // temp = temp->GetMethodAt(method_offset);
1355 __ movl(temp, Address(temp, method_offset));
1356 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001357 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001358 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001359
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001360 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001361 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001362}
1363
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001364void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1365 HandleInvoke(invoke);
1366 // Add the hidden argument.
1367 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1368}
1369
1370void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1371 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001372 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001373 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1374 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1375 LocationSummary* locations = invoke->GetLocations();
1376 Location receiver = locations->InAt(0);
1377 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1378
1379 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001380 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001381 Immediate(invoke->GetDexMethodIndex()));
1382
1383 // temp = object->GetClass();
1384 if (receiver.IsStackSlot()) {
1385 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1386 __ movl(temp, Address(temp, class_offset));
1387 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001388 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001389 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001390 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001391 // temp = temp->GetImtEntryAt(method_offset);
1392 __ movl(temp, Address(temp, method_offset));
1393 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001394 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001395 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001396
1397 DCHECK(!codegen_->IsLeafMethod());
1398 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1399}
1400
Roland Levillain88cb1752014-10-20 16:36:47 +01001401void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1402 LocationSummary* locations =
1403 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1404 switch (neg->GetResultType()) {
1405 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001406 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001407 locations->SetInAt(0, Location::RequiresRegister());
1408 locations->SetOut(Location::SameAsFirstInput());
1409 break;
1410
Roland Levillain88cb1752014-10-20 16:36:47 +01001411 case Primitive::kPrimFloat:
1412 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001413 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001414 locations->SetOut(Location::SameAsFirstInput());
1415 locations->AddTemp(Location::RequiresRegister());
1416 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001417 break;
1418
1419 default:
1420 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1421 }
1422}
1423
1424void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1425 LocationSummary* locations = neg->GetLocations();
1426 Location out = locations->Out();
1427 Location in = locations->InAt(0);
1428 switch (neg->GetResultType()) {
1429 case Primitive::kPrimInt:
1430 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001431 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001432 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001433 break;
1434
1435 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001436 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001437 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001438 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001439 break;
1440
Roland Levillain5368c212014-11-27 15:03:41 +00001441 case Primitive::kPrimFloat: {
1442 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001443 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1444 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001445 // Implement float negation with an exclusive or with value
1446 // 0x80000000 (mask for bit 31, representing the sign of a
1447 // single-precision floating-point number).
1448 __ movq(constant, Immediate(INT64_C(0x80000000)));
1449 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001450 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001451 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001452 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001453
Roland Levillain5368c212014-11-27 15:03:41 +00001454 case Primitive::kPrimDouble: {
1455 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001456 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1457 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001458 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001459 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001460 // a double-precision floating-point number).
1461 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1462 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001463 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001464 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001465 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001466
1467 default:
1468 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1469 }
1470}
1471
Roland Levillaindff1f282014-11-05 14:15:05 +00001472void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1473 LocationSummary* locations =
1474 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1475 Primitive::Type result_type = conversion->GetResultType();
1476 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001477 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001478
David Brazdilb2bd1c52015-03-25 11:17:37 +00001479 // The Java language does not allow treating boolean as an integral type but
1480 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001481
Roland Levillaindff1f282014-11-05 14:15:05 +00001482 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001483 case Primitive::kPrimByte:
1484 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001485 case Primitive::kPrimBoolean:
1486 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001487 case Primitive::kPrimShort:
1488 case Primitive::kPrimInt:
1489 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001490 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001491 locations->SetInAt(0, Location::Any());
1492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1493 break;
1494
1495 default:
1496 LOG(FATAL) << "Unexpected type conversion from " << input_type
1497 << " to " << result_type;
1498 }
1499 break;
1500
Roland Levillain01a8d712014-11-14 16:27:39 +00001501 case Primitive::kPrimShort:
1502 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001503 case Primitive::kPrimBoolean:
1504 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001505 case Primitive::kPrimByte:
1506 case Primitive::kPrimInt:
1507 case Primitive::kPrimChar:
1508 // Processing a Dex `int-to-short' instruction.
1509 locations->SetInAt(0, Location::Any());
1510 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1511 break;
1512
1513 default:
1514 LOG(FATAL) << "Unexpected type conversion from " << input_type
1515 << " to " << result_type;
1516 }
1517 break;
1518
Roland Levillain946e1432014-11-11 17:35:19 +00001519 case Primitive::kPrimInt:
1520 switch (input_type) {
1521 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001522 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001523 locations->SetInAt(0, Location::Any());
1524 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1525 break;
1526
1527 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001528 // Processing a Dex `float-to-int' instruction.
1529 locations->SetInAt(0, Location::RequiresFpuRegister());
1530 locations->SetOut(Location::RequiresRegister());
1531 locations->AddTemp(Location::RequiresFpuRegister());
1532 break;
1533
Roland Levillain946e1432014-11-11 17:35:19 +00001534 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001535 // Processing a Dex `double-to-int' instruction.
1536 locations->SetInAt(0, Location::RequiresFpuRegister());
1537 locations->SetOut(Location::RequiresRegister());
1538 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001539 break;
1540
1541 default:
1542 LOG(FATAL) << "Unexpected type conversion from " << input_type
1543 << " to " << result_type;
1544 }
1545 break;
1546
Roland Levillaindff1f282014-11-05 14:15:05 +00001547 case Primitive::kPrimLong:
1548 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001549 case Primitive::kPrimBoolean:
1550 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001551 case Primitive::kPrimByte:
1552 case Primitive::kPrimShort:
1553 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001554 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001555 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001556 // TODO: We would benefit from a (to-be-implemented)
1557 // Location::RegisterOrStackSlot requirement for this input.
1558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetOut(Location::RequiresRegister());
1560 break;
1561
1562 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001563 // Processing a Dex `float-to-long' instruction.
1564 locations->SetInAt(0, Location::RequiresFpuRegister());
1565 locations->SetOut(Location::RequiresRegister());
1566 locations->AddTemp(Location::RequiresFpuRegister());
1567 break;
1568
Roland Levillaindff1f282014-11-05 14:15:05 +00001569 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001570 // Processing a Dex `double-to-long' instruction.
1571 locations->SetInAt(0, Location::RequiresFpuRegister());
1572 locations->SetOut(Location::RequiresRegister());
1573 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001574 break;
1575
1576 default:
1577 LOG(FATAL) << "Unexpected type conversion from " << input_type
1578 << " to " << result_type;
1579 }
1580 break;
1581
Roland Levillain981e4542014-11-14 11:47:14 +00001582 case Primitive::kPrimChar:
1583 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001584 case Primitive::kPrimBoolean:
1585 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001586 case Primitive::kPrimByte:
1587 case Primitive::kPrimShort:
1588 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001589 // Processing a Dex `int-to-char' instruction.
1590 locations->SetInAt(0, Location::Any());
1591 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1592 break;
1593
1594 default:
1595 LOG(FATAL) << "Unexpected type conversion from " << input_type
1596 << " to " << result_type;
1597 }
1598 break;
1599
Roland Levillaindff1f282014-11-05 14:15:05 +00001600 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001601 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001602 case Primitive::kPrimBoolean:
1603 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001604 case Primitive::kPrimByte:
1605 case Primitive::kPrimShort:
1606 case Primitive::kPrimInt:
1607 case Primitive::kPrimChar:
1608 // Processing a Dex `int-to-float' instruction.
1609 locations->SetInAt(0, Location::RequiresRegister());
1610 locations->SetOut(Location::RequiresFpuRegister());
1611 break;
1612
1613 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001614 // Processing a Dex `long-to-float' instruction.
1615 locations->SetInAt(0, Location::RequiresRegister());
1616 locations->SetOut(Location::RequiresFpuRegister());
1617 break;
1618
Roland Levillaincff13742014-11-17 14:32:17 +00001619 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001620 // Processing a Dex `double-to-float' instruction.
1621 locations->SetInAt(0, Location::RequiresFpuRegister());
1622 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001623 break;
1624
1625 default:
1626 LOG(FATAL) << "Unexpected type conversion from " << input_type
1627 << " to " << result_type;
1628 };
1629 break;
1630
Roland Levillaindff1f282014-11-05 14:15:05 +00001631 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001632 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001633 case Primitive::kPrimBoolean:
1634 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001635 case Primitive::kPrimByte:
1636 case Primitive::kPrimShort:
1637 case Primitive::kPrimInt:
1638 case Primitive::kPrimChar:
1639 // Processing a Dex `int-to-double' instruction.
1640 locations->SetInAt(0, Location::RequiresRegister());
1641 locations->SetOut(Location::RequiresFpuRegister());
1642 break;
1643
1644 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001645 // Processing a Dex `long-to-double' instruction.
1646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetOut(Location::RequiresFpuRegister());
1648 break;
1649
Roland Levillaincff13742014-11-17 14:32:17 +00001650 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001651 // Processing a Dex `float-to-double' instruction.
1652 locations->SetInAt(0, Location::RequiresFpuRegister());
1653 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001654 break;
1655
1656 default:
1657 LOG(FATAL) << "Unexpected type conversion from " << input_type
1658 << " to " << result_type;
1659 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001660 break;
1661
1662 default:
1663 LOG(FATAL) << "Unexpected type conversion from " << input_type
1664 << " to " << result_type;
1665 }
1666}
1667
1668void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1669 LocationSummary* locations = conversion->GetLocations();
1670 Location out = locations->Out();
1671 Location in = locations->InAt(0);
1672 Primitive::Type result_type = conversion->GetResultType();
1673 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001674 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001675 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001676 case Primitive::kPrimByte:
1677 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001678 case Primitive::kPrimBoolean:
1679 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001680 case Primitive::kPrimShort:
1681 case Primitive::kPrimInt:
1682 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001683 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001684 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001686 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001688 Address(CpuRegister(RSP), in.GetStackIndex()));
1689 } else {
1690 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001691 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001692 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1693 }
1694 break;
1695
1696 default:
1697 LOG(FATAL) << "Unexpected type conversion from " << input_type
1698 << " to " << result_type;
1699 }
1700 break;
1701
Roland Levillain01a8d712014-11-14 16:27:39 +00001702 case Primitive::kPrimShort:
1703 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001704 case Primitive::kPrimBoolean:
1705 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001706 case Primitive::kPrimByte:
1707 case Primitive::kPrimInt:
1708 case Primitive::kPrimChar:
1709 // Processing a Dex `int-to-short' instruction.
1710 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001711 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001712 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001713 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001714 Address(CpuRegister(RSP), in.GetStackIndex()));
1715 } else {
1716 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001717 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001718 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1719 }
1720 break;
1721
1722 default:
1723 LOG(FATAL) << "Unexpected type conversion from " << input_type
1724 << " to " << result_type;
1725 }
1726 break;
1727
Roland Levillain946e1432014-11-11 17:35:19 +00001728 case Primitive::kPrimInt:
1729 switch (input_type) {
1730 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001731 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001732 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001733 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001734 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001735 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001736 Address(CpuRegister(RSP), in.GetStackIndex()));
1737 } else {
1738 DCHECK(in.IsConstant());
1739 DCHECK(in.GetConstant()->IsLongConstant());
1740 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001741 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001742 }
1743 break;
1744
Roland Levillain3f8f9362014-12-02 17:45:01 +00001745 case Primitive::kPrimFloat: {
1746 // Processing a Dex `float-to-int' instruction.
1747 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1748 CpuRegister output = out.AsRegister<CpuRegister>();
1749 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1750 Label done, nan;
1751
1752 __ movl(output, Immediate(kPrimIntMax));
1753 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001754 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001755 // if input >= temp goto done
1756 __ comiss(input, temp);
1757 __ j(kAboveEqual, &done);
1758 // if input == NaN goto nan
1759 __ j(kUnordered, &nan);
1760 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001761 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001762 __ jmp(&done);
1763 __ Bind(&nan);
1764 // output = 0
1765 __ xorl(output, output);
1766 __ Bind(&done);
1767 break;
1768 }
1769
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001770 case Primitive::kPrimDouble: {
1771 // Processing a Dex `double-to-int' instruction.
1772 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1773 CpuRegister output = out.AsRegister<CpuRegister>();
1774 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1775 Label done, nan;
1776
1777 __ movl(output, Immediate(kPrimIntMax));
1778 // temp = int-to-double(output)
1779 __ cvtsi2sd(temp, output);
1780 // if input >= temp goto done
1781 __ comisd(input, temp);
1782 __ j(kAboveEqual, &done);
1783 // if input == NaN goto nan
1784 __ j(kUnordered, &nan);
1785 // output = double-to-int-truncate(input)
1786 __ cvttsd2si(output, input);
1787 __ jmp(&done);
1788 __ Bind(&nan);
1789 // output = 0
1790 __ xorl(output, output);
1791 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001792 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001793 }
Roland Levillain946e1432014-11-11 17:35:19 +00001794
1795 default:
1796 LOG(FATAL) << "Unexpected type conversion from " << input_type
1797 << " to " << result_type;
1798 }
1799 break;
1800
Roland Levillaindff1f282014-11-05 14:15:05 +00001801 case Primitive::kPrimLong:
1802 switch (input_type) {
1803 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001804 case Primitive::kPrimBoolean:
1805 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001806 case Primitive::kPrimByte:
1807 case Primitive::kPrimShort:
1808 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001809 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001810 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001811 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001812 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001813 break;
1814
Roland Levillain624279f2014-12-04 11:54:28 +00001815 case Primitive::kPrimFloat: {
1816 // Processing a Dex `float-to-long' instruction.
1817 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1818 CpuRegister output = out.AsRegister<CpuRegister>();
1819 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1820 Label done, nan;
1821
1822 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001823 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001824 __ cvtsi2ss(temp, output, true);
1825 // if input >= temp goto done
1826 __ comiss(input, temp);
1827 __ j(kAboveEqual, &done);
1828 // if input == NaN goto nan
1829 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001830 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001831 __ cvttss2si(output, input, true);
1832 __ jmp(&done);
1833 __ Bind(&nan);
1834 // output = 0
1835 __ xorq(output, output);
1836 __ Bind(&done);
1837 break;
1838 }
1839
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001840 case Primitive::kPrimDouble: {
1841 // Processing a Dex `double-to-long' instruction.
1842 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1843 CpuRegister output = out.AsRegister<CpuRegister>();
1844 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1845 Label done, nan;
1846
1847 __ movq(output, Immediate(kPrimLongMax));
1848 // temp = long-to-double(output)
1849 __ cvtsi2sd(temp, output, true);
1850 // if input >= temp goto done
1851 __ comisd(input, temp);
1852 __ j(kAboveEqual, &done);
1853 // if input == NaN goto nan
1854 __ j(kUnordered, &nan);
1855 // output = double-to-long-truncate(input)
1856 __ cvttsd2si(output, input, true);
1857 __ jmp(&done);
1858 __ Bind(&nan);
1859 // output = 0
1860 __ xorq(output, output);
1861 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001862 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001863 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001864
1865 default:
1866 LOG(FATAL) << "Unexpected type conversion from " << input_type
1867 << " to " << result_type;
1868 }
1869 break;
1870
Roland Levillain981e4542014-11-14 11:47:14 +00001871 case Primitive::kPrimChar:
1872 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001873 case Primitive::kPrimBoolean:
1874 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001875 case Primitive::kPrimByte:
1876 case Primitive::kPrimShort:
1877 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001878 // Processing a Dex `int-to-char' instruction.
1879 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001880 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001881 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001882 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001883 Address(CpuRegister(RSP), in.GetStackIndex()));
1884 } else {
1885 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001886 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001887 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1888 }
1889 break;
1890
1891 default:
1892 LOG(FATAL) << "Unexpected type conversion from " << input_type
1893 << " to " << result_type;
1894 }
1895 break;
1896
Roland Levillaindff1f282014-11-05 14:15:05 +00001897 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001898 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001899 case Primitive::kPrimBoolean:
1900 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001901 case Primitive::kPrimByte:
1902 case Primitive::kPrimShort:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001905 // Processing a Dex `int-to-float' instruction.
1906 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001907 break;
1908
1909 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001910 // Processing a Dex `long-to-float' instruction.
1911 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1912 break;
1913
Roland Levillaincff13742014-11-17 14:32:17 +00001914 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001915 // Processing a Dex `double-to-float' instruction.
1916 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001917 break;
1918
1919 default:
1920 LOG(FATAL) << "Unexpected type conversion from " << input_type
1921 << " to " << result_type;
1922 };
1923 break;
1924
Roland Levillaindff1f282014-11-05 14:15:05 +00001925 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001926 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001927 case Primitive::kPrimBoolean:
1928 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001929 case Primitive::kPrimByte:
1930 case Primitive::kPrimShort:
1931 case Primitive::kPrimInt:
1932 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001933 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001935 break;
1936
1937 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001938 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001940 break;
1941
Roland Levillaincff13742014-11-17 14:32:17 +00001942 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001943 // Processing a Dex `float-to-double' instruction.
1944 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001945 break;
1946
1947 default:
1948 LOG(FATAL) << "Unexpected type conversion from " << input_type
1949 << " to " << result_type;
1950 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001951 break;
1952
1953 default:
1954 LOG(FATAL) << "Unexpected type conversion from " << input_type
1955 << " to " << result_type;
1956 }
1957}
1958
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001959void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001960 LocationSummary* locations =
1961 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001962 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001963 case Primitive::kPrimInt: {
1964 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001965 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001967 break;
1968 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001970 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001971 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001972 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001973 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001975 break;
1976 }
1977
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001978 case Primitive::kPrimDouble:
1979 case Primitive::kPrimFloat: {
1980 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04001981 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001982 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001983 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001985
1986 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001987 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001988 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001989}
1990
1991void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1992 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001993 Location first = locations->InAt(0);
1994 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001995 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001996
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001997 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001998 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002000 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2001 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2002 } else {
2003 __ leal(out.AsRegister<CpuRegister>(), Address(
2004 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2005 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002006 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002007 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2008 __ addl(out.AsRegister<CpuRegister>(),
2009 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2010 } else {
2011 __ leal(out.AsRegister<CpuRegister>(), Address(
2012 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2013 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002014 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002015 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002016 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002017 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002018 break;
2019 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002020
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002021 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002022 if (second.IsRegister()) {
2023 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2024 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2025 } else {
2026 __ leaq(out.AsRegister<CpuRegister>(), Address(
2027 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2028 }
2029 } else {
2030 DCHECK(second.IsConstant());
2031 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2032 int32_t int32_value = Low32Bits(value);
2033 DCHECK_EQ(int32_value, value);
2034 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2035 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2036 } else {
2037 __ leaq(out.AsRegister<CpuRegister>(), Address(
2038 first.AsRegister<CpuRegister>(), int32_value));
2039 }
2040 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002041 break;
2042 }
2043
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002044 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002045 if (second.IsFpuRegister()) {
2046 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2047 } else if (second.IsConstant()) {
2048 __ addss(first.AsFpuRegister<XmmRegister>(),
2049 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2050 } else {
2051 DCHECK(second.IsStackSlot());
2052 __ addss(first.AsFpuRegister<XmmRegister>(),
2053 Address(CpuRegister(RSP), second.GetStackIndex()));
2054 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002055 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002056 }
2057
2058 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002059 if (second.IsFpuRegister()) {
2060 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2061 } else if (second.IsConstant()) {
2062 __ addsd(first.AsFpuRegister<XmmRegister>(),
2063 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2064 } else {
2065 DCHECK(second.IsDoubleStackSlot());
2066 __ addsd(first.AsFpuRegister<XmmRegister>(),
2067 Address(CpuRegister(RSP), second.GetStackIndex()));
2068 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002069 break;
2070 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002071
2072 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002073 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002074 }
2075}
2076
2077void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002078 LocationSummary* locations =
2079 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002080 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002081 case Primitive::kPrimInt: {
2082 locations->SetInAt(0, Location::RequiresRegister());
2083 locations->SetInAt(1, Location::Any());
2084 locations->SetOut(Location::SameAsFirstInput());
2085 break;
2086 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002087 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002088 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002089 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002090 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002091 break;
2092 }
Calin Juravle11351682014-10-23 15:38:15 +01002093 case Primitive::kPrimFloat:
2094 case Primitive::kPrimDouble: {
2095 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002096 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002097 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002098 break;
Calin Juravle11351682014-10-23 15:38:15 +01002099 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002100 default:
Calin Juravle11351682014-10-23 15:38:15 +01002101 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002102 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103}
2104
2105void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2106 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002107 Location first = locations->InAt(0);
2108 Location second = locations->InAt(1);
2109 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002110 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002111 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002112 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002114 } else if (second.IsConstant()) {
2115 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002116 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002117 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002119 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002120 break;
2121 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002122 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002123 if (second.IsConstant()) {
2124 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2125 DCHECK(IsInt<32>(value));
2126 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2127 } else {
2128 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2129 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002130 break;
2131 }
2132
Calin Juravle11351682014-10-23 15:38:15 +01002133 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002134 if (second.IsFpuRegister()) {
2135 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2136 } else if (second.IsConstant()) {
2137 __ subss(first.AsFpuRegister<XmmRegister>(),
2138 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2139 } else {
2140 DCHECK(second.IsStackSlot());
2141 __ subss(first.AsFpuRegister<XmmRegister>(),
2142 Address(CpuRegister(RSP), second.GetStackIndex()));
2143 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002144 break;
Calin Juravle11351682014-10-23 15:38:15 +01002145 }
2146
2147 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002148 if (second.IsFpuRegister()) {
2149 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2150 } else if (second.IsConstant()) {
2151 __ subsd(first.AsFpuRegister<XmmRegister>(),
2152 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2153 } else {
2154 DCHECK(second.IsDoubleStackSlot());
2155 __ subsd(first.AsFpuRegister<XmmRegister>(),
2156 Address(CpuRegister(RSP), second.GetStackIndex()));
2157 }
Calin Juravle11351682014-10-23 15:38:15 +01002158 break;
2159 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002160
2161 default:
Calin Juravle11351682014-10-23 15:38:15 +01002162 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002163 }
2164}
2165
Calin Juravle34bacdf2014-10-07 20:23:36 +01002166void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2167 LocationSummary* locations =
2168 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2169 switch (mul->GetResultType()) {
2170 case Primitive::kPrimInt: {
2171 locations->SetInAt(0, Location::RequiresRegister());
2172 locations->SetInAt(1, Location::Any());
2173 locations->SetOut(Location::SameAsFirstInput());
2174 break;
2175 }
2176 case Primitive::kPrimLong: {
2177 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002178 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2179 if (locations->InAt(1).IsConstant()) {
2180 // Can use 3 operand multiply.
2181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2182 } else {
2183 locations->SetOut(Location::SameAsFirstInput());
2184 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002185 break;
2186 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002187 case Primitive::kPrimFloat:
2188 case Primitive::kPrimDouble: {
2189 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002190 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002191 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002192 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002193 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002194
2195 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002196 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002197 }
2198}
2199
2200void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2201 LocationSummary* locations = mul->GetLocations();
2202 Location first = locations->InAt(0);
2203 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002204 switch (mul->GetResultType()) {
2205 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002206 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002207 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002208 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002209 } else if (second.IsConstant()) {
2210 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002212 } else {
2213 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002214 __ imull(first.AsRegister<CpuRegister>(),
2215 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002216 }
2217 break;
2218 }
2219 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002220 if (second.IsConstant()) {
2221 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2222 DCHECK(IsInt<32>(value));
2223 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2224 first.AsRegister<CpuRegister>(),
2225 Immediate(static_cast<int32_t>(value)));
2226 } else {
2227 DCHECK(first.Equals(locations->Out()));
2228 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2229 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002230 break;
2231 }
2232
Calin Juravleb5bfa962014-10-21 18:02:24 +01002233 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002234 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002235 if (second.IsFpuRegister()) {
2236 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2237 } else if (second.IsConstant()) {
2238 __ mulss(first.AsFpuRegister<XmmRegister>(),
2239 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2240 } else {
2241 DCHECK(second.IsStackSlot());
2242 __ mulss(first.AsFpuRegister<XmmRegister>(),
2243 Address(CpuRegister(RSP), second.GetStackIndex()));
2244 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002245 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002246 }
2247
2248 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002249 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002250 if (second.IsFpuRegister()) {
2251 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2252 } else if (second.IsConstant()) {
2253 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2254 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2255 } else {
2256 DCHECK(second.IsDoubleStackSlot());
2257 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2258 Address(CpuRegister(RSP), second.GetStackIndex()));
2259 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002260 break;
2261 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002262
2263 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002264 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002265 }
2266}
2267
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002268void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2269 uint32_t stack_adjustment, bool is_float) {
2270 if (source.IsStackSlot()) {
2271 DCHECK(is_float);
2272 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2273 } else if (source.IsDoubleStackSlot()) {
2274 DCHECK(!is_float);
2275 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2276 } else {
2277 // Write the value to the temporary location on the stack and load to FP stack.
2278 if (is_float) {
2279 Location stack_temp = Location::StackSlot(temp_offset);
2280 codegen_->Move(stack_temp, source);
2281 __ flds(Address(CpuRegister(RSP), temp_offset));
2282 } else {
2283 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2284 codegen_->Move(stack_temp, source);
2285 __ fldl(Address(CpuRegister(RSP), temp_offset));
2286 }
2287 }
2288}
2289
2290void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2291 Primitive::Type type = rem->GetResultType();
2292 bool is_float = type == Primitive::kPrimFloat;
2293 size_t elem_size = Primitive::ComponentSize(type);
2294 LocationSummary* locations = rem->GetLocations();
2295 Location first = locations->InAt(0);
2296 Location second = locations->InAt(1);
2297 Location out = locations->Out();
2298
2299 // Create stack space for 2 elements.
2300 // TODO: enhance register allocator to ask for stack temporaries.
2301 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2302
2303 // Load the values to the FP stack in reverse order, using temporaries if needed.
2304 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2305 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2306
2307 // Loop doing FPREM until we stabilize.
2308 Label retry;
2309 __ Bind(&retry);
2310 __ fprem();
2311
2312 // Move FP status to AX.
2313 __ fstsw();
2314
2315 // And see if the argument reduction is complete. This is signaled by the
2316 // C2 FPU flag bit set to 0.
2317 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2318 __ j(kNotEqual, &retry);
2319
2320 // We have settled on the final value. Retrieve it into an XMM register.
2321 // Store FP top of stack to real stack.
2322 if (is_float) {
2323 __ fsts(Address(CpuRegister(RSP), 0));
2324 } else {
2325 __ fstl(Address(CpuRegister(RSP), 0));
2326 }
2327
2328 // Pop the 2 items from the FP stack.
2329 __ fucompp();
2330
2331 // Load the value from the stack into an XMM register.
2332 DCHECK(out.IsFpuRegister()) << out;
2333 if (is_float) {
2334 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2335 } else {
2336 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2337 }
2338
2339 // And remove the temporary stack space we allocated.
2340 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2341}
2342
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002343void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2344 DCHECK(instruction->IsDiv() || instruction->IsRem());
2345
2346 LocationSummary* locations = instruction->GetLocations();
2347 Location second = locations->InAt(1);
2348 DCHECK(second.IsConstant());
2349
2350 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2351 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002352 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002353
2354 DCHECK(imm == 1 || imm == -1);
2355
2356 switch (instruction->GetResultType()) {
2357 case Primitive::kPrimInt: {
2358 if (instruction->IsRem()) {
2359 __ xorl(output_register, output_register);
2360 } else {
2361 __ movl(output_register, input_register);
2362 if (imm == -1) {
2363 __ negl(output_register);
2364 }
2365 }
2366 break;
2367 }
2368
2369 case Primitive::kPrimLong: {
2370 if (instruction->IsRem()) {
2371 __ xorq(output_register, output_register);
2372 } else {
2373 __ movq(output_register, input_register);
2374 if (imm == -1) {
2375 __ negq(output_register);
2376 }
2377 }
2378 break;
2379 }
2380
2381 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002382 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002383 }
2384}
2385
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002386void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002387 LocationSummary* locations = instruction->GetLocations();
2388 Location second = locations->InAt(1);
2389
2390 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2391 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2392
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002393 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002394
2395 DCHECK(IsPowerOfTwo(std::abs(imm)));
2396
2397 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2398
2399 if (instruction->GetResultType() == Primitive::kPrimInt) {
2400 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2401 __ testl(numerator, numerator);
2402 __ cmov(kGreaterEqual, tmp, numerator);
2403 int shift = CTZ(imm);
2404 __ sarl(tmp, Immediate(shift));
2405
2406 if (imm < 0) {
2407 __ negl(tmp);
2408 }
2409
2410 __ movl(output_register, tmp);
2411 } else {
2412 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2413 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2414
2415 __ movq(rdx, Immediate(std::abs(imm) - 1));
2416 __ addq(rdx, numerator);
2417 __ testq(numerator, numerator);
2418 __ cmov(kGreaterEqual, rdx, numerator);
2419 int shift = CTZ(imm);
2420 __ sarq(rdx, Immediate(shift));
2421
2422 if (imm < 0) {
2423 __ negq(rdx);
2424 }
2425
2426 __ movq(output_register, rdx);
2427 }
2428}
2429
2430void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2431 DCHECK(instruction->IsDiv() || instruction->IsRem());
2432
2433 LocationSummary* locations = instruction->GetLocations();
2434 Location second = locations->InAt(1);
2435
2436 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2437 : locations->GetTemp(0).AsRegister<CpuRegister>();
2438 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2439 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2440 : locations->Out().AsRegister<CpuRegister>();
2441 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2442
2443 DCHECK_EQ(RAX, eax.AsRegister());
2444 DCHECK_EQ(RDX, edx.AsRegister());
2445 if (instruction->IsDiv()) {
2446 DCHECK_EQ(RAX, out.AsRegister());
2447 } else {
2448 DCHECK_EQ(RDX, out.AsRegister());
2449 }
2450
2451 int64_t magic;
2452 int shift;
2453
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002454 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002455 if (instruction->GetResultType() == Primitive::kPrimInt) {
2456 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2457
2458 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2459
2460 __ movl(numerator, eax);
2461
2462 Label no_div;
2463 Label end;
2464 __ testl(eax, eax);
2465 __ j(kNotEqual, &no_div);
2466
2467 __ xorl(out, out);
2468 __ jmp(&end);
2469
2470 __ Bind(&no_div);
2471
2472 __ movl(eax, Immediate(magic));
2473 __ imull(numerator);
2474
2475 if (imm > 0 && magic < 0) {
2476 __ addl(edx, numerator);
2477 } else if (imm < 0 && magic > 0) {
2478 __ subl(edx, numerator);
2479 }
2480
2481 if (shift != 0) {
2482 __ sarl(edx, Immediate(shift));
2483 }
2484
2485 __ movl(eax, edx);
2486 __ shrl(edx, Immediate(31));
2487 __ addl(edx, eax);
2488
2489 if (instruction->IsRem()) {
2490 __ movl(eax, numerator);
2491 __ imull(edx, Immediate(imm));
2492 __ subl(eax, edx);
2493 __ movl(edx, eax);
2494 } else {
2495 __ movl(eax, edx);
2496 }
2497 __ Bind(&end);
2498 } else {
2499 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2500
2501 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2502
2503 CpuRegister rax = eax;
2504 CpuRegister rdx = edx;
2505
2506 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2507
2508 // Save the numerator.
2509 __ movq(numerator, rax);
2510
2511 // RAX = magic
2512 __ movq(rax, Immediate(magic));
2513
2514 // RDX:RAX = magic * numerator
2515 __ imulq(numerator);
2516
2517 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002518 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002519 __ addq(rdx, numerator);
2520 } else if (imm < 0 && magic > 0) {
2521 // RDX -= numerator
2522 __ subq(rdx, numerator);
2523 }
2524
2525 // Shift if needed.
2526 if (shift != 0) {
2527 __ sarq(rdx, Immediate(shift));
2528 }
2529
2530 // RDX += 1 if RDX < 0
2531 __ movq(rax, rdx);
2532 __ shrq(rdx, Immediate(63));
2533 __ addq(rdx, rax);
2534
2535 if (instruction->IsRem()) {
2536 __ movq(rax, numerator);
2537
2538 if (IsInt<32>(imm)) {
2539 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2540 } else {
2541 __ movq(numerator, Immediate(imm));
2542 __ imulq(rdx, numerator);
2543 }
2544
2545 __ subq(rax, rdx);
2546 __ movq(rdx, rax);
2547 } else {
2548 __ movq(rax, rdx);
2549 }
2550 }
2551}
2552
Calin Juravlebacfec32014-11-14 15:54:36 +00002553void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2554 DCHECK(instruction->IsDiv() || instruction->IsRem());
2555 Primitive::Type type = instruction->GetResultType();
2556 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2557
2558 bool is_div = instruction->IsDiv();
2559 LocationSummary* locations = instruction->GetLocations();
2560
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002561 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2562 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002563
Roland Levillain271ab9c2014-11-27 15:23:57 +00002564 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002565 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002566
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002567 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002568 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002569
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002570 if (imm == 0) {
2571 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2572 } else if (imm == 1 || imm == -1) {
2573 DivRemOneOrMinusOne(instruction);
2574 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002575 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002576 } else {
2577 DCHECK(imm <= -2 || imm >= 2);
2578 GenerateDivRemWithAnyConstant(instruction);
2579 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002580 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002581 SlowPathCodeX86_64* slow_path =
2582 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2583 out.AsRegister(), type, is_div);
2584 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002585
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002586 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2587 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2588 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2589 // so it's safe to just use negl instead of more complex comparisons.
2590 if (type == Primitive::kPrimInt) {
2591 __ cmpl(second_reg, Immediate(-1));
2592 __ j(kEqual, slow_path->GetEntryLabel());
2593 // edx:eax <- sign-extended of eax
2594 __ cdq();
2595 // eax = quotient, edx = remainder
2596 __ idivl(second_reg);
2597 } else {
2598 __ cmpq(second_reg, Immediate(-1));
2599 __ j(kEqual, slow_path->GetEntryLabel());
2600 // rdx:rax <- sign-extended of rax
2601 __ cqo();
2602 // rax = quotient, rdx = remainder
2603 __ idivq(second_reg);
2604 }
2605 __ Bind(slow_path->GetExitLabel());
2606 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002607}
2608
Calin Juravle7c4954d2014-10-28 16:57:40 +00002609void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2610 LocationSummary* locations =
2611 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2612 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002613 case Primitive::kPrimInt:
2614 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002615 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002616 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002617 locations->SetOut(Location::SameAsFirstInput());
2618 // Intel uses edx:eax as the dividend.
2619 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002620 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2621 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2622 // output and request another temp.
2623 if (div->InputAt(1)->IsConstant()) {
2624 locations->AddTemp(Location::RequiresRegister());
2625 }
Calin Juravled0d48522014-11-04 16:40:20 +00002626 break;
2627 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002628
Calin Juravle7c4954d2014-10-28 16:57:40 +00002629 case Primitive::kPrimFloat:
2630 case Primitive::kPrimDouble: {
2631 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002632 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002633 locations->SetOut(Location::SameAsFirstInput());
2634 break;
2635 }
2636
2637 default:
2638 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2639 }
2640}
2641
2642void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2643 LocationSummary* locations = div->GetLocations();
2644 Location first = locations->InAt(0);
2645 Location second = locations->InAt(1);
2646 DCHECK(first.Equals(locations->Out()));
2647
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002648 Primitive::Type type = div->GetResultType();
2649 switch (type) {
2650 case Primitive::kPrimInt:
2651 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002652 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002653 break;
2654 }
2655
Calin Juravle7c4954d2014-10-28 16:57:40 +00002656 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002657 if (second.IsFpuRegister()) {
2658 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2659 } else if (second.IsConstant()) {
2660 __ divss(first.AsFpuRegister<XmmRegister>(),
2661 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2662 } else {
2663 DCHECK(second.IsStackSlot());
2664 __ divss(first.AsFpuRegister<XmmRegister>(),
2665 Address(CpuRegister(RSP), second.GetStackIndex()));
2666 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002667 break;
2668 }
2669
2670 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002671 if (second.IsFpuRegister()) {
2672 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2673 } else if (second.IsConstant()) {
2674 __ divsd(first.AsFpuRegister<XmmRegister>(),
2675 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2676 } else {
2677 DCHECK(second.IsDoubleStackSlot());
2678 __ divsd(first.AsFpuRegister<XmmRegister>(),
2679 Address(CpuRegister(RSP), second.GetStackIndex()));
2680 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002681 break;
2682 }
2683
2684 default:
2685 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2686 }
2687}
2688
Calin Juravlebacfec32014-11-14 15:54:36 +00002689void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002690 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002691 LocationSummary* locations =
2692 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002693
2694 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002695 case Primitive::kPrimInt:
2696 case Primitive::kPrimLong: {
2697 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002698 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002699 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2700 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002701 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2702 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2703 // output and request another temp.
2704 if (rem->InputAt(1)->IsConstant()) {
2705 locations->AddTemp(Location::RequiresRegister());
2706 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002707 break;
2708 }
2709
2710 case Primitive::kPrimFloat:
2711 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002712 locations->SetInAt(0, Location::Any());
2713 locations->SetInAt(1, Location::Any());
2714 locations->SetOut(Location::RequiresFpuRegister());
2715 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002716 break;
2717 }
2718
2719 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002720 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002721 }
2722}
2723
2724void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2725 Primitive::Type type = rem->GetResultType();
2726 switch (type) {
2727 case Primitive::kPrimInt:
2728 case Primitive::kPrimLong: {
2729 GenerateDivRemIntegral(rem);
2730 break;
2731 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002732 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002733 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002734 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002735 break;
2736 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002737 default:
2738 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2739 }
2740}
2741
Calin Juravled0d48522014-11-04 16:40:20 +00002742void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2745 locations->SetInAt(0, Location::Any());
2746 if (instruction->HasUses()) {
2747 locations->SetOut(Location::SameAsFirstInput());
2748 }
2749}
2750
2751void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2752 SlowPathCodeX86_64* slow_path =
2753 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2754 codegen_->AddSlowPath(slow_path);
2755
2756 LocationSummary* locations = instruction->GetLocations();
2757 Location value = locations->InAt(0);
2758
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002759 switch (instruction->GetType()) {
2760 case Primitive::kPrimInt: {
2761 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002762 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002763 __ j(kEqual, slow_path->GetEntryLabel());
2764 } else if (value.IsStackSlot()) {
2765 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2766 __ j(kEqual, slow_path->GetEntryLabel());
2767 } else {
2768 DCHECK(value.IsConstant()) << value;
2769 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2770 __ jmp(slow_path->GetEntryLabel());
2771 }
2772 }
2773 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002774 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002775 case Primitive::kPrimLong: {
2776 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002777 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002778 __ j(kEqual, slow_path->GetEntryLabel());
2779 } else if (value.IsDoubleStackSlot()) {
2780 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2781 __ j(kEqual, slow_path->GetEntryLabel());
2782 } else {
2783 DCHECK(value.IsConstant()) << value;
2784 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2785 __ jmp(slow_path->GetEntryLabel());
2786 }
2787 }
2788 break;
2789 }
2790 default:
2791 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002792 }
Calin Juravled0d48522014-11-04 16:40:20 +00002793}
2794
Calin Juravle9aec02f2014-11-18 23:06:35 +00002795void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2796 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2797
2798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2800
2801 switch (op->GetResultType()) {
2802 case Primitive::kPrimInt:
2803 case Primitive::kPrimLong: {
2804 locations->SetInAt(0, Location::RequiresRegister());
2805 // The shift count needs to be in CL.
2806 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2807 locations->SetOut(Location::SameAsFirstInput());
2808 break;
2809 }
2810 default:
2811 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2812 }
2813}
2814
2815void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2816 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2817
2818 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002819 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002820 Location second = locations->InAt(1);
2821
2822 switch (op->GetResultType()) {
2823 case Primitive::kPrimInt: {
2824 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002826 if (op->IsShl()) {
2827 __ shll(first_reg, second_reg);
2828 } else if (op->IsShr()) {
2829 __ sarl(first_reg, second_reg);
2830 } else {
2831 __ shrl(first_reg, second_reg);
2832 }
2833 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002834 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002835 if (op->IsShl()) {
2836 __ shll(first_reg, imm);
2837 } else if (op->IsShr()) {
2838 __ sarl(first_reg, imm);
2839 } else {
2840 __ shrl(first_reg, imm);
2841 }
2842 }
2843 break;
2844 }
2845 case Primitive::kPrimLong: {
2846 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002847 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002848 if (op->IsShl()) {
2849 __ shlq(first_reg, second_reg);
2850 } else if (op->IsShr()) {
2851 __ sarq(first_reg, second_reg);
2852 } else {
2853 __ shrq(first_reg, second_reg);
2854 }
2855 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002856 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002857 if (op->IsShl()) {
2858 __ shlq(first_reg, imm);
2859 } else if (op->IsShr()) {
2860 __ sarq(first_reg, imm);
2861 } else {
2862 __ shrq(first_reg, imm);
2863 }
2864 }
2865 break;
2866 }
2867 default:
2868 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2869 }
2870}
2871
2872void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2873 HandleShift(shl);
2874}
2875
2876void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2877 HandleShift(shl);
2878}
2879
2880void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2881 HandleShift(shr);
2882}
2883
2884void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2885 HandleShift(shr);
2886}
2887
2888void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2889 HandleShift(ushr);
2890}
2891
2892void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2893 HandleShift(ushr);
2894}
2895
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002896void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002897 LocationSummary* locations =
2898 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002899 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002900 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2901 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2902 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002903}
2904
2905void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2906 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002907 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002908 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2909
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002910 __ gs()->call(
2911 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002912
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002913 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002914 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002915}
2916
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002917void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2918 LocationSummary* locations =
2919 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2920 InvokeRuntimeCallingConvention calling_convention;
2921 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002922 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002923 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002924 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002925}
2926
2927void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2928 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002929 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002930 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2931
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002932 __ gs()->call(
2933 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002934
2935 DCHECK(!codegen_->IsLeafMethod());
2936 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2937}
2938
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002939void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002940 LocationSummary* locations =
2941 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002942 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2943 if (location.IsStackSlot()) {
2944 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2945 } else if (location.IsDoubleStackSlot()) {
2946 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2947 }
2948 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002949}
2950
2951void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2952 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002953 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002954}
2955
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002956void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002958 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002959 locations->SetInAt(0, Location::RequiresRegister());
2960 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002961}
2962
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002963void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2964 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2966 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002967 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002968 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002969 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002971 break;
2972
2973 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002975 break;
2976
2977 default:
2978 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2979 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002980}
2981
2982void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002985 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2986 locations->SetInAt(i, Location::Any());
2987 }
2988 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002989}
2990
2991void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002992 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002993 LOG(FATAL) << "Unimplemented";
2994}
2995
Calin Juravle52c48962014-12-16 17:02:57 +00002996void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2997 /*
2998 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2999 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3000 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3001 */
3002 switch (kind) {
3003 case MemBarrierKind::kAnyAny: {
3004 __ mfence();
3005 break;
3006 }
3007 case MemBarrierKind::kAnyStore:
3008 case MemBarrierKind::kLoadAny:
3009 case MemBarrierKind::kStoreStore: {
3010 // nop
3011 break;
3012 }
3013 default:
3014 LOG(FATAL) << "Unexpected memory barier " << kind;
3015 }
3016}
3017
3018void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3019 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3020
Nicolas Geoffray39468442014-09-02 15:17:15 +01003021 LocationSummary* locations =
3022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003023 locations->SetInAt(0, Location::RequiresRegister());
3024 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3025}
3026
3027void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3028 const FieldInfo& field_info) {
3029 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3030
3031 LocationSummary* locations = instruction->GetLocations();
3032 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3033 Location out = locations->Out();
3034 bool is_volatile = field_info.IsVolatile();
3035 Primitive::Type field_type = field_info.GetFieldType();
3036 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3037
3038 switch (field_type) {
3039 case Primitive::kPrimBoolean: {
3040 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3041 break;
3042 }
3043
3044 case Primitive::kPrimByte: {
3045 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3046 break;
3047 }
3048
3049 case Primitive::kPrimShort: {
3050 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3051 break;
3052 }
3053
3054 case Primitive::kPrimChar: {
3055 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3056 break;
3057 }
3058
3059 case Primitive::kPrimInt:
3060 case Primitive::kPrimNot: {
3061 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3062 break;
3063 }
3064
3065 case Primitive::kPrimLong: {
3066 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3067 break;
3068 }
3069
3070 case Primitive::kPrimFloat: {
3071 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3072 break;
3073 }
3074
3075 case Primitive::kPrimDouble: {
3076 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3077 break;
3078 }
3079
3080 case Primitive::kPrimVoid:
3081 LOG(FATAL) << "Unreachable type " << field_type;
3082 UNREACHABLE();
3083 }
3084
Calin Juravle77520bc2015-01-12 18:45:46 +00003085 codegen_->MaybeRecordImplicitNullCheck(instruction);
3086
Calin Juravle52c48962014-12-16 17:02:57 +00003087 if (is_volatile) {
3088 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3089 }
3090}
3091
3092void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3093 const FieldInfo& field_info) {
3094 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3095
3096 LocationSummary* locations =
3097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003098 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003099 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3100
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003101 locations->SetInAt(0, Location::RequiresRegister());
3102 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003103 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003104 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003105 locations->AddTemp(Location::RequiresRegister());
3106 locations->AddTemp(Location::RequiresRegister());
3107 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003108}
3109
Calin Juravle52c48962014-12-16 17:02:57 +00003110void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3111 const FieldInfo& field_info) {
3112 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3113
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003114 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003115 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3116 Location value = locations->InAt(1);
3117 bool is_volatile = field_info.IsVolatile();
3118 Primitive::Type field_type = field_info.GetFieldType();
3119 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3120
3121 if (is_volatile) {
3122 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3123 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003124
3125 switch (field_type) {
3126 case Primitive::kPrimBoolean:
3127 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003128 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003129 break;
3130 }
3131
3132 case Primitive::kPrimShort:
3133 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003134 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003135 break;
3136 }
3137
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003138 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003139 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003140 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003141 break;
3142 }
3143
3144 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003145 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146 break;
3147 }
3148
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003149 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003150 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003151 break;
3152 }
3153
3154 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003155 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003156 break;
3157 }
3158
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003159 case Primitive::kPrimVoid:
3160 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003161 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003162 }
Calin Juravle52c48962014-12-16 17:02:57 +00003163
Calin Juravle77520bc2015-01-12 18:45:46 +00003164 codegen_->MaybeRecordImplicitNullCheck(instruction);
3165
3166 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3167 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3168 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3169 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3170 }
3171
Calin Juravle52c48962014-12-16 17:02:57 +00003172 if (is_volatile) {
3173 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3174 }
3175}
3176
3177void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3178 HandleFieldSet(instruction, instruction->GetFieldInfo());
3179}
3180
3181void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3182 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003183}
3184
3185void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003186 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003187}
3188
3189void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003190 HandleFieldGet(instruction, instruction->GetFieldInfo());
3191}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003192
Calin Juravle52c48962014-12-16 17:02:57 +00003193void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3194 HandleFieldGet(instruction);
3195}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003196
Calin Juravle52c48962014-12-16 17:02:57 +00003197void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3198 HandleFieldGet(instruction, instruction->GetFieldInfo());
3199}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003200
Calin Juravle52c48962014-12-16 17:02:57 +00003201void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3202 HandleFieldSet(instruction, instruction->GetFieldInfo());
3203}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003204
Calin Juravle52c48962014-12-16 17:02:57 +00003205void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3206 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003207}
3208
3209void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003210 LocationSummary* locations =
3211 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003212 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3213 ? Location::RequiresRegister()
3214 : Location::Any();
3215 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003216 if (instruction->HasUses()) {
3217 locations->SetOut(Location::SameAsFirstInput());
3218 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003219}
3220
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003221void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003222 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3223 return;
3224 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003225 LocationSummary* locations = instruction->GetLocations();
3226 Location obj = locations->InAt(0);
3227
3228 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3229 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3230}
3231
3232void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003233 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003234 codegen_->AddSlowPath(slow_path);
3235
3236 LocationSummary* locations = instruction->GetLocations();
3237 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238
3239 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003240 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003241 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003242 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003243 } else {
3244 DCHECK(obj.IsConstant()) << obj;
3245 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3246 __ jmp(slow_path->GetEntryLabel());
3247 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003248 }
3249 __ j(kEqual, slow_path->GetEntryLabel());
3250}
3251
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003252void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3253 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3254 GenerateImplicitNullCheck(instruction);
3255 } else {
3256 GenerateExplicitNullCheck(instruction);
3257 }
3258}
3259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003260void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003261 LocationSummary* locations =
3262 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003263 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003264 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003265 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003267}
3268
3269void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3270 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003271 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272 Location index = locations->InAt(1);
3273
3274 switch (instruction->GetType()) {
3275 case Primitive::kPrimBoolean: {
3276 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 if (index.IsConstant()) {
3279 __ movzxb(out, Address(obj,
3280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3281 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 }
3284 break;
3285 }
3286
3287 case Primitive::kPrimByte: {
3288 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003289 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003290 if (index.IsConstant()) {
3291 __ movsxb(out, Address(obj,
3292 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3293 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003294 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003295 }
3296 break;
3297 }
3298
3299 case Primitive::kPrimShort: {
3300 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003301 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003302 if (index.IsConstant()) {
3303 __ movsxw(out, Address(obj,
3304 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3305 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003306 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003307 }
3308 break;
3309 }
3310
3311 case Primitive::kPrimChar: {
3312 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003313 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003314 if (index.IsConstant()) {
3315 __ movzxw(out, Address(obj,
3316 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3317 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003318 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003319 }
3320 break;
3321 }
3322
3323 case Primitive::kPrimInt:
3324 case Primitive::kPrimNot: {
3325 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3326 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003327 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003328 if (index.IsConstant()) {
3329 __ movl(out, Address(obj,
3330 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3331 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003332 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003333 }
3334 break;
3335 }
3336
3337 case Primitive::kPrimLong: {
3338 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003339 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003340 if (index.IsConstant()) {
3341 __ movq(out, Address(obj,
3342 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3343 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003345 }
3346 break;
3347 }
3348
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003349 case Primitive::kPrimFloat: {
3350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003352 if (index.IsConstant()) {
3353 __ movss(out, Address(obj,
3354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3355 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003356 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003357 }
3358 break;
3359 }
3360
3361 case Primitive::kPrimDouble: {
3362 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003363 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003364 if (index.IsConstant()) {
3365 __ movsd(out, Address(obj,
3366 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3367 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003368 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003369 }
3370 break;
3371 }
3372
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003373 case Primitive::kPrimVoid:
3374 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003375 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003376 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003377 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003378}
3379
3380void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003381 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003382
3383 bool needs_write_barrier =
3384 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3385 bool needs_runtime_call = instruction->NeedsTypeCheck();
3386
Nicolas Geoffray39468442014-09-02 15:17:15 +01003387 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003388 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3389 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003390 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003391 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3392 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3393 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003394 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003395 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003396 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003397 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3398 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003399 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003400 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003401 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3402 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003403 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003404 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003405 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003406
3407 if (needs_write_barrier) {
3408 // Temporary registers for the write barrier.
3409 locations->AddTemp(Location::RequiresRegister());
3410 locations->AddTemp(Location::RequiresRegister());
3411 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003412 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003413}
3414
3415void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3416 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003417 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003418 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003419 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003420 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003421 bool needs_runtime_call = locations->WillCall();
3422 bool needs_write_barrier =
3423 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003424
3425 switch (value_type) {
3426 case Primitive::kPrimBoolean:
3427 case Primitive::kPrimByte: {
3428 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003429 if (index.IsConstant()) {
3430 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003431 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003432 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003433 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003434 __ movb(Address(obj, offset),
3435 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003436 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003437 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003438 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3440 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003441 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003442 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003443 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3444 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003446 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003447 break;
3448 }
3449
3450 case Primitive::kPrimShort:
3451 case Primitive::kPrimChar: {
3452 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 if (index.IsConstant()) {
3454 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003455 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003457 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003458 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003459 __ movw(Address(obj, offset),
3460 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003461 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003462 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003463 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003464 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003465 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3466 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003467 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003468 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003470 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3471 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003472 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003473 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003474 break;
3475 }
3476
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003477 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003478 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003479 if (!needs_runtime_call) {
3480 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3481 if (index.IsConstant()) {
3482 size_t offset =
3483 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3484 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003486 } else {
3487 DCHECK(value.IsConstant()) << value;
3488 __ movl(Address(obj, offset),
3489 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3490 }
3491 } else {
3492 DCHECK(index.IsRegister()) << index;
3493 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3495 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003496 } else {
3497 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003498 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003499 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3500 }
3501 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003502 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003503 if (needs_write_barrier) {
3504 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003505 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3506 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3507 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003508 }
3509 } else {
3510 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003511 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3512 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003513 DCHECK(!codegen_->IsLeafMethod());
3514 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3515 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003516 break;
3517 }
3518
3519 case Primitive::kPrimLong: {
3520 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003521 if (index.IsConstant()) {
3522 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003523 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003524 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003525 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003526 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003527 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3528 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003529 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003530 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003531 break;
3532 }
3533
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003534 case Primitive::kPrimFloat: {
3535 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3536 if (index.IsConstant()) {
3537 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3538 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003539 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003540 } else {
3541 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003542 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3543 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003544 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003545 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003546 break;
3547 }
3548
3549 case Primitive::kPrimDouble: {
3550 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3551 if (index.IsConstant()) {
3552 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3553 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003554 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003555 } else {
3556 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003557 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3558 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003559 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003560 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003561 break;
3562 }
3563
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003564 case Primitive::kPrimVoid:
3565 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003566 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 }
3568}
3569
3570void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003571 LocationSummary* locations =
3572 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003573 locations->SetInAt(0, Location::RequiresRegister());
3574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575}
3576
3577void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3578 LocationSummary* locations = instruction->GetLocations();
3579 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3581 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003582 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003583 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584}
3585
3586void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003587 LocationSummary* locations =
3588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003589 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003590 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003591 if (instruction->HasUses()) {
3592 locations->SetOut(Location::SameAsFirstInput());
3593 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003594}
3595
3596void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3597 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003598 Location index_loc = locations->InAt(0);
3599 Location length_loc = locations->InAt(1);
3600 SlowPathCodeX86_64* slow_path =
3601 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 codegen_->AddSlowPath(slow_path);
3603
Mark Mendellf60c90b2015-03-04 15:12:59 -05003604 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3605 if (index_loc.IsConstant()) {
3606 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3607 __ cmpl(length, Immediate(value));
3608 } else {
3609 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3610 }
3611 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003612}
3613
3614void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3615 CpuRegister card,
3616 CpuRegister object,
3617 CpuRegister value) {
3618 Label is_null;
3619 __ testl(value, value);
3620 __ j(kEqual, &is_null);
3621 __ gs()->movq(card, Address::Absolute(
3622 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3623 __ movq(temp, object);
3624 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3625 __ movb(Address(temp, card, TIMES_1, 0), card);
3626 __ Bind(&is_null);
3627}
3628
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003629void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3630 temp->SetLocations(nullptr);
3631}
3632
3633void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3634 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003635 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003636}
3637
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003638void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003639 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003640 LOG(FATAL) << "Unimplemented";
3641}
3642
3643void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003644 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3645}
3646
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003647void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3649}
3650
3651void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003652 HBasicBlock* block = instruction->GetBlock();
3653 if (block->GetLoopInformation() != nullptr) {
3654 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3655 // The back edge will generate the suspend check.
3656 return;
3657 }
3658 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3659 // The goto will generate the suspend check.
3660 return;
3661 }
3662 GenerateSuspendCheck(instruction, nullptr);
3663}
3664
3665void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3666 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003667 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003668 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003669 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003670 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003671 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003672 if (successor == nullptr) {
3673 __ j(kNotEqual, slow_path->GetEntryLabel());
3674 __ Bind(slow_path->GetReturnLabel());
3675 } else {
3676 __ j(kEqual, codegen_->GetLabelOf(successor));
3677 __ jmp(slow_path->GetEntryLabel());
3678 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003679}
3680
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003681X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3682 return codegen_->GetAssembler();
3683}
3684
3685void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3686 MoveOperands* move = moves_.Get(index);
3687 Location source = move->GetSource();
3688 Location destination = move->GetDestination();
3689
3690 if (source.IsRegister()) {
3691 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003693 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003694 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003696 } else {
3697 DCHECK(destination.IsDoubleStackSlot());
3698 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003700 }
3701 } else if (source.IsStackSlot()) {
3702 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003703 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003704 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003705 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003706 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003707 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003708 } else {
3709 DCHECK(destination.IsStackSlot());
3710 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3711 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3712 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713 } else if (source.IsDoubleStackSlot()) {
3714 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003715 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003716 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003717 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003718 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3719 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003720 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003721 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003722 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3723 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3724 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003725 } else if (source.IsConstant()) {
3726 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003727 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3728 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003729 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003730 if (value == 0) {
3731 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3732 } else {
3733 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3734 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003735 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003736 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003737 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003738 }
3739 } else if (constant->IsLongConstant()) {
3740 int64_t value = constant->AsLongConstant()->GetValue();
3741 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003743 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003744 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003745 __ movq(CpuRegister(TMP), Immediate(value));
3746 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3747 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003748 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003749 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003750 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003751 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003752 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003753 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3754 if (value == 0) {
3755 // easy FP 0.0.
3756 __ xorps(dest, dest);
3757 } else {
3758 __ movl(CpuRegister(TMP), imm);
3759 __ movd(dest, CpuRegister(TMP));
3760 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003761 } else {
3762 DCHECK(destination.IsStackSlot()) << destination;
3763 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3764 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003765 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003766 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003767 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003768 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003769 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003770 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003771 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3772 if (value == 0) {
3773 __ xorpd(dest, dest);
3774 } else {
3775 __ movq(CpuRegister(TMP), imm);
3776 __ movd(dest, CpuRegister(TMP));
3777 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003778 } else {
3779 DCHECK(destination.IsDoubleStackSlot()) << destination;
3780 __ movq(CpuRegister(TMP), imm);
3781 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3782 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003783 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003784 } else if (source.IsFpuRegister()) {
3785 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003786 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003787 } else if (destination.IsStackSlot()) {
3788 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003789 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003790 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003791 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003792 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003793 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003794 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003795 }
3796}
3797
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003798void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003799 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003800 __ movl(Address(CpuRegister(RSP), mem), reg);
3801 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003802}
3803
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003804void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003805 ScratchRegisterScope ensure_scratch(
3806 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3807
3808 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3809 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3810 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3811 Address(CpuRegister(RSP), mem2 + stack_offset));
3812 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3813 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3814 CpuRegister(ensure_scratch.GetRegister()));
3815}
3816
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003817void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3818 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3819 __ movq(Address(CpuRegister(RSP), mem), reg);
3820 __ movq(reg, CpuRegister(TMP));
3821}
3822
3823void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3824 ScratchRegisterScope ensure_scratch(
Mark Mendella5c19ce2015-04-01 12:51:05 -04003825 this, TMP, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003826
Mark Mendella5c19ce2015-04-01 12:51:05 -04003827 int temp_reg = ensure_scratch.GetRegister();
3828 if (temp_reg == kNoRegister) {
3829 // Use the stack as a temporary.
3830 // Save mem1 on the stack.
3831 __ pushq(Address(CpuRegister(RSP), mem1));
3832
3833 // Copy mem2 into mem1.
3834 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem2 + kX86_64WordSize));
3835 __ movq(Address(CpuRegister(RSP), mem1 + kX86_64WordSize), CpuRegister(TMP));
3836
3837 // Now pop mem1 into mem2.
3838 __ popq(Address(CpuRegister(RSP), mem2));
3839 } else {
3840 CpuRegister temp = CpuRegister(temp_reg);
3841 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1));
3842 __ movq(temp, Address(CpuRegister(RSP), mem2));
3843 __ movq(Address(CpuRegister(RSP), mem2), CpuRegister(TMP));
3844 __ movq(Address(CpuRegister(RSP), mem1), temp);
3845 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003846}
3847
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003848void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3849 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3850 __ movss(Address(CpuRegister(RSP), mem), reg);
3851 __ movd(reg, CpuRegister(TMP));
3852}
3853
Mark Mendella5c19ce2015-04-01 12:51:05 -04003854void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
3855 // Prefer to avoid xchg as it isn't speedy on smaller processors.
3856 __ movq(CpuRegister(TMP), reg1);
3857 __ movq(reg1, reg2);
3858 __ movq(reg2, CpuRegister(TMP));
3859}
3860
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003861void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3862 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3863 __ movsd(Address(CpuRegister(RSP), mem), reg);
3864 __ movd(reg, CpuRegister(TMP));
3865}
3866
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003867void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3868 MoveOperands* move = moves_.Get(index);
3869 Location source = move->GetSource();
3870 Location destination = move->GetDestination();
3871
3872 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04003873 Exchange64(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003874 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003875 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003876 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003877 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003878 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003879 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3880 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003882 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003883 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003884 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3885 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003886 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003887 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3888 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3889 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003890 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003891 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003892 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003894 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003895 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003896 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003897 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003898 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003899 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003900 }
3901}
3902
3903
3904void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3905 __ pushq(CpuRegister(reg));
3906}
3907
3908
3909void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3910 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003911}
3912
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003913void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3914 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3915 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3916 Immediate(mirror::Class::kStatusInitialized));
3917 __ j(kLess, slow_path->GetEntryLabel());
3918 __ Bind(slow_path->GetExitLabel());
3919 // No need for memory fence, thanks to the X86_64 memory model.
3920}
3921
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003922void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003923 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3924 ? LocationSummary::kCallOnSlowPath
3925 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003926 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003927 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003928 locations->SetOut(Location::RequiresRegister());
3929}
3930
3931void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003933 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003934 DCHECK(!cls->CanCallRuntime());
3935 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003936 codegen_->LoadCurrentMethod(out);
3937 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3938 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003939 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003940 codegen_->LoadCurrentMethod(out);
3941 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3942 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003943 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3944 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3945 codegen_->AddSlowPath(slow_path);
3946 __ testl(out, out);
3947 __ j(kEqual, slow_path->GetEntryLabel());
3948 if (cls->MustGenerateClinitCheck()) {
3949 GenerateClassInitializationCheck(slow_path, out);
3950 } else {
3951 __ Bind(slow_path->GetExitLabel());
3952 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003953 }
3954}
3955
3956void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3957 LocationSummary* locations =
3958 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3959 locations->SetInAt(0, Location::RequiresRegister());
3960 if (check->HasUses()) {
3961 locations->SetOut(Location::SameAsFirstInput());
3962 }
3963}
3964
3965void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003966 // We assume the class to not be null.
3967 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3968 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003969 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003970 GenerateClassInitializationCheck(slow_path,
3971 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003972}
3973
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003974void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3975 LocationSummary* locations =
3976 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3977 locations->SetOut(Location::RequiresRegister());
3978}
3979
3980void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3981 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3982 codegen_->AddSlowPath(slow_path);
3983
Roland Levillain271ab9c2014-11-27 15:23:57 +00003984 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003985 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003986 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3987 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003988 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3989 __ testl(out, out);
3990 __ j(kEqual, slow_path->GetEntryLabel());
3991 __ Bind(slow_path->GetExitLabel());
3992}
3993
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003994void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3995 LocationSummary* locations =
3996 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3997 locations->SetOut(Location::RequiresRegister());
3998}
3999
4000void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4001 Address address = Address::Absolute(
4002 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004003 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004004 __ gs()->movl(address, Immediate(0));
4005}
4006
4007void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4008 LocationSummary* locations =
4009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4010 InvokeRuntimeCallingConvention calling_convention;
4011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4012}
4013
4014void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4015 __ gs()->call(
4016 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4017 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4018}
4019
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004020void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004021 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4022 ? LocationSummary::kNoCall
4023 : LocationSummary::kCallOnSlowPath;
4024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4025 locations->SetInAt(0, Location::RequiresRegister());
4026 locations->SetInAt(1, Location::Any());
4027 locations->SetOut(Location::RequiresRegister());
4028}
4029
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004030void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004031 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004032 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004033 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004034 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004035 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4036 Label done, zero;
4037 SlowPathCodeX86_64* slow_path = nullptr;
4038
4039 // Return 0 if `obj` is null.
4040 // TODO: avoid this check if we know obj is not null.
4041 __ testl(obj, obj);
4042 __ j(kEqual, &zero);
4043 // Compare the class of `obj` with `cls`.
4044 __ movl(out, Address(obj, class_offset));
4045 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004046 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004047 } else {
4048 DCHECK(cls.IsStackSlot()) << cls;
4049 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4050 }
4051 if (instruction->IsClassFinal()) {
4052 // Classes must be equal for the instanceof to succeed.
4053 __ j(kNotEqual, &zero);
4054 __ movl(out, Immediate(1));
4055 __ jmp(&done);
4056 } else {
4057 // If the classes are not equal, we go into a slow path.
4058 DCHECK(locations->OnlyCallsOnSlowPath());
4059 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004060 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004061 codegen_->AddSlowPath(slow_path);
4062 __ j(kNotEqual, slow_path->GetEntryLabel());
4063 __ movl(out, Immediate(1));
4064 __ jmp(&done);
4065 }
4066 __ Bind(&zero);
4067 __ movl(out, Immediate(0));
4068 if (slow_path != nullptr) {
4069 __ Bind(slow_path->GetExitLabel());
4070 }
4071 __ Bind(&done);
4072}
4073
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004074void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4076 instruction, LocationSummary::kCallOnSlowPath);
4077 locations->SetInAt(0, Location::RequiresRegister());
4078 locations->SetInAt(1, Location::Any());
4079 locations->AddTemp(Location::RequiresRegister());
4080}
4081
4082void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4083 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004084 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004085 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004086 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004087 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4088 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4089 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4090 codegen_->AddSlowPath(slow_path);
4091
4092 // TODO: avoid this check if we know obj is not null.
4093 __ testl(obj, obj);
4094 __ j(kEqual, slow_path->GetExitLabel());
4095 // Compare the class of `obj` with `cls`.
4096 __ movl(temp, Address(obj, class_offset));
4097 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004099 } else {
4100 DCHECK(cls.IsStackSlot()) << cls;
4101 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4102 }
4103 // Classes must be equal for the checkcast to succeed.
4104 __ j(kNotEqual, slow_path->GetEntryLabel());
4105 __ Bind(slow_path->GetExitLabel());
4106}
4107
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004108void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4109 LocationSummary* locations =
4110 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4111 InvokeRuntimeCallingConvention calling_convention;
4112 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4113}
4114
4115void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4116 __ gs()->call(Address::Absolute(instruction->IsEnter()
4117 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4118 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4119 true));
4120 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4121}
4122
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004123void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4124void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4125void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4126
4127void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4128 LocationSummary* locations =
4129 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4130 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4131 || instruction->GetResultType() == Primitive::kPrimLong);
4132 locations->SetInAt(0, Location::RequiresRegister());
4133 if (instruction->GetType() == Primitive::kPrimInt) {
4134 locations->SetInAt(1, Location::Any());
4135 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004136 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004137 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004138 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004139 }
4140 locations->SetOut(Location::SameAsFirstInput());
4141}
4142
4143void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4144 HandleBitwiseOperation(instruction);
4145}
4146
4147void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4148 HandleBitwiseOperation(instruction);
4149}
4150
4151void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4152 HandleBitwiseOperation(instruction);
4153}
4154
4155void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4156 LocationSummary* locations = instruction->GetLocations();
4157 Location first = locations->InAt(0);
4158 Location second = locations->InAt(1);
4159 DCHECK(first.Equals(locations->Out()));
4160
4161 if (instruction->GetResultType() == Primitive::kPrimInt) {
4162 if (second.IsRegister()) {
4163 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004164 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004165 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004166 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004167 } else {
4168 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004169 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004170 }
4171 } else if (second.IsConstant()) {
4172 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4173 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004175 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004176 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004177 } else {
4178 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004179 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004180 }
4181 } else {
4182 Address address(CpuRegister(RSP), second.GetStackIndex());
4183 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004184 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004185 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004186 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004187 } else {
4188 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004190 }
4191 }
4192 } else {
4193 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004194 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4195 bool second_is_constant = false;
4196 int64_t value = 0;
4197 if (second.IsConstant()) {
4198 second_is_constant = true;
4199 value = second.GetConstant()->AsLongConstant()->GetValue();
4200 DCHECK(IsInt<32>(value));
4201 }
4202
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004203 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004204 if (second_is_constant) {
4205 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4206 } else {
4207 __ andq(first_reg, second.AsRegister<CpuRegister>());
4208 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004209 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004210 if (second_is_constant) {
4211 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4212 } else {
4213 __ orq(first_reg, second.AsRegister<CpuRegister>());
4214 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004215 } else {
4216 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004217 if (second_is_constant) {
4218 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4219 } else {
4220 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4221 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004222 }
4223 }
4224}
4225
Calin Juravleb1498f62015-02-16 13:13:29 +00004226void LocationsBuilderX86_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
4232void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4233 // Nothing to do, this should be removed during prepare for register allocator.
4234 UNUSED(instruction);
4235 LOG(FATAL) << "Unreachable";
4236}
4237
Mark Mendellf55c3e02015-03-26 21:07:46 -04004238void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4239 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004240 X86_64Assembler* assembler = GetAssembler();
4241 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004242 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4243 // byte values. If used for vectors at a later time, this will need to be
4244 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004245 assembler->Align(4, 0);
4246 constant_area_start_ = assembler->CodeSize();
4247 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004248 }
4249
4250 // And finish up.
4251 CodeGenerator::Finalize(allocator);
4252}
4253
4254/**
4255 * Class to handle late fixup of offsets into constant area.
4256 */
4257class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4258 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004259 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004260 : codegen_(codegen), offset_into_constant_area_(offset) {}
4261
4262 private:
4263 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4264 // Patch the correct offset for the instruction. We use the address of the
4265 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4266 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4267 int relative_position = constant_offset - pos;
4268
4269 // Patch in the right value.
4270 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4271 }
4272
Mark Mendell39dcf552015-04-09 20:42:42 -04004273 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004274
4275 // Location in constant area that the fixup refers to.
4276 int offset_into_constant_area_;
4277};
4278
4279Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4280 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4281 return Address::RIP(fixup);
4282}
4283
4284Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4285 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4286 return Address::RIP(fixup);
4287}
4288
4289Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4290 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4291 return Address::RIP(fixup);
4292}
4293
4294Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4295 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4296 return Address::RIP(fixup);
4297}
4298
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004299} // namespace x86_64
4300} // namespace art