blob: 1604a7c1075f84dcba063bff408bc6e688a07295 [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
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100486void CodeGeneratorX86_64::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000487 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100488 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700489 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000490 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100491
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000492 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100493 __ testq(CpuRegister(RAX), Address(
494 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100495 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100496 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000497
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000498 if (HasEmptyFrame()) {
499 return;
500 }
501
Nicolas Geoffray98893962015-01-21 12:32:32 +0000502 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000503 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000504 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000505 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000506 }
507 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100508
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000509 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
510 uint32_t xmm_spill_location = GetFpuSpillStart();
511 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100512
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000513 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
514 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
515 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
516 XmmRegister(kFpuCalleeSaves[i]));
517 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100518 }
519
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100520 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
521}
522
523void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000524 if (HasEmptyFrame()) {
525 return;
526 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000527 uint32_t xmm_spill_location = GetFpuSpillStart();
528 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
529 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
530 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
531 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
532 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
533 }
534 }
535
536 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000537
538 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000539 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000540 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000541 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000542 }
543 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544}
545
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100546void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
547 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100548}
549
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100550void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000551 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100552 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
553}
554
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100555Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
556 switch (load->GetType()) {
557 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100559 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560
561 case Primitive::kPrimInt:
562 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100564 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100565
566 case Primitive::kPrimBoolean:
567 case Primitive::kPrimByte:
568 case Primitive::kPrimChar:
569 case Primitive::kPrimShort:
570 case Primitive::kPrimVoid:
571 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700572 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100573 }
574
575 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700576 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100577}
578
579void CodeGeneratorX86_64::Move(Location destination, Location source) {
580 if (source.Equals(destination)) {
581 return;
582 }
583 if (destination.IsRegister()) {
584 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100588 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000589 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 } else {
592 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000593 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100594 Address(CpuRegister(RSP), source.GetStackIndex()));
595 }
596 } else if (destination.IsFpuRegister()) {
597 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000598 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000600 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100601 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000602 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 Address(CpuRegister(RSP), source.GetStackIndex()));
604 } else {
605 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000606 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100607 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100608 }
609 } else if (destination.IsStackSlot()) {
610 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100611 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000612 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100613 } else if (source.IsFpuRegister()) {
614 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000615 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500616 } else if (source.IsConstant()) {
617 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000618 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500619 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100620 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500621 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000622 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
623 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624 }
625 } else {
626 DCHECK(destination.IsDoubleStackSlot());
627 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000629 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100630 } else if (source.IsFpuRegister()) {
631 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000632 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500633 } else if (source.IsConstant()) {
634 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800635 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500636 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000637 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500638 } else {
639 DCHECK(constant->IsLongConstant());
640 value = constant->AsLongConstant()->GetValue();
641 }
642 __ movq(CpuRegister(TMP), Immediate(value));
643 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100644 } else {
645 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000646 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
647 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648 }
649 }
650}
651
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100652void CodeGeneratorX86_64::Move(HInstruction* instruction,
653 Location location,
654 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000655 LocationSummary* locations = instruction->GetLocations();
656 if (locations != nullptr && locations->Out().Equals(location)) {
657 return;
658 }
659
660 if (locations != nullptr && locations->Out().IsConstant()) {
661 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000662 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
663 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000664 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000665 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000666 } else if (location.IsStackSlot()) {
667 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
668 } else {
669 DCHECK(location.IsConstant());
670 DCHECK_EQ(location.GetConstant(), const_to_move);
671 }
672 } else if (const_to_move->IsLongConstant()) {
673 int64_t value = const_to_move->AsLongConstant()->GetValue();
674 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000675 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000676 } else if (location.IsDoubleStackSlot()) {
677 __ movq(CpuRegister(TMP), Immediate(value));
678 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
679 } else {
680 DCHECK(location.IsConstant());
681 DCHECK_EQ(location.GetConstant(), const_to_move);
682 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100683 }
Roland Levillain476df552014-10-09 17:51:36 +0100684 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685 switch (instruction->GetType()) {
686 case Primitive::kPrimBoolean:
687 case Primitive::kPrimByte:
688 case Primitive::kPrimChar:
689 case Primitive::kPrimShort:
690 case Primitive::kPrimInt:
691 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100693 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
694 break;
695
696 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000698 Move(location,
699 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100700 break;
701
702 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000705 } else if (instruction->IsTemporary()) {
706 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
707 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100709 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100710 switch (instruction->GetType()) {
711 case Primitive::kPrimBoolean:
712 case Primitive::kPrimByte:
713 case Primitive::kPrimChar:
714 case Primitive::kPrimShort:
715 case Primitive::kPrimInt:
716 case Primitive::kPrimNot:
717 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 case Primitive::kPrimFloat:
719 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000720 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100721 break;
722
723 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 }
726 }
727}
728
729void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
730 got->SetLocations(nullptr);
731}
732
733void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
734 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100735 DCHECK(!successor->IsExitBlock());
736
737 HBasicBlock* block = got->GetBlock();
738 HInstruction* previous = got->GetPrevious();
739
740 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000741 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100742 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
743 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
744 return;
745 }
746
747 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
748 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
749 }
750 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100751 __ jmp(codegen_->GetLabelOf(successor));
752 }
753}
754
755void LocationsBuilderX86_64::VisitExit(HExit* exit) {
756 exit->SetLocations(nullptr);
757}
758
759void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700760 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100761}
762
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700763void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
764 Label* true_target,
765 Label* false_target,
766 Label* always_true_target) {
767 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100768 if (cond->IsIntConstant()) {
769 // Constant condition, statically compared against 1.
770 int32_t cond_value = cond->AsIntConstant()->GetValue();
771 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700772 if (always_true_target != nullptr) {
773 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100774 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100775 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100776 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100777 DCHECK_EQ(cond_value, 0);
778 }
779 } else {
780 bool materialized =
781 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
782 // Moves do not affect the eflags register, so if the condition is
783 // evaluated just before the if, we don't need to evaluate it
784 // again.
785 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700786 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100787 if (materialized) {
788 if (!eflags_set) {
789 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700790 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100791 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000792 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100793 } else {
794 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
795 Immediate(0));
796 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700797 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100798 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700799 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100800 }
801 } else {
802 Location lhs = cond->GetLocations()->InAt(0);
803 Location rhs = cond->GetLocations()->InAt(1);
804 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000805 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100806 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000807 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000808 if (constant == 0) {
809 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
810 } else {
811 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
812 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100813 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000814 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100815 Address(CpuRegister(RSP), rhs.GetStackIndex()));
816 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700817 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700818 }
Dave Allison20dfc792014-06-16 20:44:29 -0700819 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700820 if (false_target != nullptr) {
821 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100822 }
823}
824
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700825void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
826 LocationSummary* locations =
827 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
828 HInstruction* cond = if_instr->InputAt(0);
829 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
830 locations->SetInAt(0, Location::Any());
831 }
832}
833
834void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
835 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
836 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
837 Label* always_true_target = true_target;
838 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
839 if_instr->IfTrueSuccessor())) {
840 always_true_target = nullptr;
841 }
842 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
843 if_instr->IfFalseSuccessor())) {
844 false_target = nullptr;
845 }
846 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
847}
848
849void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
850 LocationSummary* locations = new (GetGraph()->GetArena())
851 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
852 HInstruction* cond = deoptimize->InputAt(0);
853 DCHECK(cond->IsCondition());
854 if (cond->AsCondition()->NeedsMaterialization()) {
855 locations->SetInAt(0, Location::Any());
856 }
857}
858
859void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
860 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
861 DeoptimizationSlowPathX86_64(deoptimize);
862 codegen_->AddSlowPath(slow_path);
863 Label* slow_path_entry = slow_path->GetEntryLabel();
864 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
865}
866
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100867void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
868 local->SetLocations(nullptr);
869}
870
871void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
872 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
873}
874
875void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
876 local->SetLocations(nullptr);
877}
878
879void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
880 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700881 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100882}
883
884void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100885 LocationSummary* locations =
886 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100887 switch (store->InputAt(1)->GetType()) {
888 case Primitive::kPrimBoolean:
889 case Primitive::kPrimByte:
890 case Primitive::kPrimChar:
891 case Primitive::kPrimShort:
892 case Primitive::kPrimInt:
893 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100894 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100895 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
896 break;
897
898 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100899 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100900 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
901 break;
902
903 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100904 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100905 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100906}
907
908void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700909 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100910}
911
Dave Allison20dfc792014-06-16 20:44:29 -0700912void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100913 LocationSummary* locations =
914 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100915 locations->SetInAt(0, Location::RequiresRegister());
916 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100917 if (comp->NeedsMaterialization()) {
918 locations->SetOut(Location::RequiresRegister());
919 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100920}
921
Dave Allison20dfc792014-06-16 20:44:29 -0700922void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
923 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100924 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000925 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100926 // Clear register: setcc only sets the low byte.
927 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000928 Location lhs = locations->InAt(0);
929 Location rhs = locations->InAt(1);
930 if (rhs.IsRegister()) {
931 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
932 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800933 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000934 if (constant == 0) {
935 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
936 } else {
937 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
938 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100939 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000940 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100941 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100942 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700943 }
944}
945
946void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
947 VisitCondition(comp);
948}
949
950void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
951 VisitCondition(comp);
952}
953
954void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
955 VisitCondition(comp);
956}
957
958void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
959 VisitCondition(comp);
960}
961
962void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
963 VisitCondition(comp);
964}
965
966void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
967 VisitCondition(comp);
968}
969
970void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
971 VisitCondition(comp);
972}
973
974void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
975 VisitCondition(comp);
976}
977
978void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
979 VisitCondition(comp);
980}
981
982void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
983 VisitCondition(comp);
984}
985
986void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
987 VisitCondition(comp);
988}
989
990void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
991 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100992}
993
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100994void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100995 LocationSummary* locations =
996 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000997 switch (compare->InputAt(0)->GetType()) {
998 case Primitive::kPrimLong: {
999 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001000 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00001001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1002 break;
1003 }
1004 case Primitive::kPrimFloat:
1005 case Primitive::kPrimDouble: {
1006 locations->SetInAt(0, Location::RequiresFpuRegister());
1007 locations->SetInAt(1, Location::RequiresFpuRegister());
1008 locations->SetOut(Location::RequiresRegister());
1009 break;
1010 }
1011 default:
1012 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1013 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001014}
1015
1016void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001017 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001018 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001019 Location left = locations->InAt(0);
1020 Location right = locations->InAt(1);
1021
1022 Label less, greater, done;
1023 Primitive::Type type = compare->InputAt(0)->GetType();
1024 switch (type) {
1025 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001026 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1027 if (right.IsConstant()) {
1028 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1029 DCHECK(IsInt<32>(value));
1030 if (value == 0) {
1031 __ testq(left_reg, left_reg);
1032 } else {
1033 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1034 }
1035 } else {
1036 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1037 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001038 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001039 }
1040 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001041 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001042 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1043 break;
1044 }
1045 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001046 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001047 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1048 break;
1049 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001050 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001051 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001052 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001053 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001054 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001055 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001056
Calin Juravle91debbc2014-11-26 19:01:09 +00001057 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001058 __ movl(out, Immediate(1));
1059 __ jmp(&done);
1060
1061 __ Bind(&less);
1062 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001063
1064 __ Bind(&done);
1065}
1066
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001067void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068 LocationSummary* locations =
1069 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001070 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001071}
1072
1073void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001074 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001075 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001076}
1077
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001078void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1079 LocationSummary* locations =
1080 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1081 locations->SetOut(Location::ConstantLocation(constant));
1082}
1083
1084void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1085 // Will be generated at use site.
1086 UNUSED(constant);
1087}
1088
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001089void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001090 LocationSummary* locations =
1091 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001092 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001093}
1094
1095void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001096 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001097 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098}
1099
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001100void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1101 LocationSummary* locations =
1102 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1103 locations->SetOut(Location::ConstantLocation(constant));
1104}
1105
1106void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1107 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001108 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001109}
1110
1111void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1112 LocationSummary* locations =
1113 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1114 locations->SetOut(Location::ConstantLocation(constant));
1115}
1116
1117void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1118 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001119 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001120}
1121
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001122void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1123 ret->SetLocations(nullptr);
1124}
1125
1126void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001127 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001128 codegen_->GenerateFrameExit();
1129 __ ret();
1130}
1131
1132void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135 switch (ret->InputAt(0)->GetType()) {
1136 case Primitive::kPrimBoolean:
1137 case Primitive::kPrimByte:
1138 case Primitive::kPrimChar:
1139 case Primitive::kPrimShort:
1140 case Primitive::kPrimInt:
1141 case Primitive::kPrimNot:
1142 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001144 break;
1145
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 case Primitive::kPrimFloat:
1147 case Primitive::kPrimDouble:
1148 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001150 break;
1151
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001153 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001154 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001155}
1156
1157void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1158 if (kIsDebugBuild) {
1159 switch (ret->InputAt(0)->GetType()) {
1160 case Primitive::kPrimBoolean:
1161 case Primitive::kPrimByte:
1162 case Primitive::kPrimChar:
1163 case Primitive::kPrimShort:
1164 case Primitive::kPrimInt:
1165 case Primitive::kPrimNot:
1166 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001167 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001168 break;
1169
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001170 case Primitive::kPrimFloat:
1171 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001172 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001173 XMM0);
1174 break;
1175
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001176 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001177 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001178 }
1179 }
1180 codegen_->GenerateFrameExit();
1181 __ ret();
1182}
1183
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001184Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1185 switch (type) {
1186 case Primitive::kPrimBoolean:
1187 case Primitive::kPrimByte:
1188 case Primitive::kPrimChar:
1189 case Primitive::kPrimShort:
1190 case Primitive::kPrimInt:
1191 case Primitive::kPrimNot: {
1192 uint32_t index = gp_index_++;
1193 stack_index_++;
1194 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001195 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196 } else {
1197 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1198 }
1199 }
1200
1201 case Primitive::kPrimLong: {
1202 uint32_t index = gp_index_;
1203 stack_index_ += 2;
1204 if (index < calling_convention.GetNumberOfRegisters()) {
1205 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001206 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207 } else {
1208 gp_index_ += 2;
1209 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1210 }
1211 }
1212
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001213 case Primitive::kPrimFloat: {
1214 uint32_t index = fp_index_++;
1215 stack_index_++;
1216 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001217 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 } else {
1219 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1220 }
1221 }
1222
1223 case Primitive::kPrimDouble: {
1224 uint32_t index = fp_index_++;
1225 stack_index_ += 2;
1226 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001227 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 } else {
1229 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1230 }
1231 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001232
1233 case Primitive::kPrimVoid:
1234 LOG(FATAL) << "Unexpected parameter type " << type;
1235 break;
1236 }
1237 return Location();
1238}
1239
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001240void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001241 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001242 if (intrinsic.TryDispatch(invoke)) {
1243 return;
1244 }
1245
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001246 HandleInvoke(invoke);
1247}
1248
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001249static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1250 if (invoke->GetLocations()->Intrinsified()) {
1251 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1252 intrinsic.Dispatch(invoke);
1253 return true;
1254 }
1255 return false;
1256}
1257
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001258void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001259 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1260 return;
1261 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001262
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001263 codegen_->GenerateStaticOrDirectCall(
1264 invoke,
1265 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001266 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001267}
1268
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001269void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001270 LocationSummary* locations =
1271 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001272 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273
1274 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001275 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276 HInstruction* input = invoke->InputAt(i);
1277 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1278 }
1279
1280 switch (invoke->GetType()) {
1281 case Primitive::kPrimBoolean:
1282 case Primitive::kPrimByte:
1283 case Primitive::kPrimChar:
1284 case Primitive::kPrimShort:
1285 case Primitive::kPrimInt:
1286 case Primitive::kPrimNot:
1287 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001288 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001289 break;
1290
1291 case Primitive::kPrimVoid:
1292 break;
1293
1294 case Primitive::kPrimDouble:
1295 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001296 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001297 break;
1298 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001299}
1300
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001301void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001302 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001303 if (intrinsic.TryDispatch(invoke)) {
1304 return;
1305 }
1306
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307 HandleInvoke(invoke);
1308}
1309
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001310void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001311 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1312 return;
1313 }
1314
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001316 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1317 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1318 LocationSummary* locations = invoke->GetLocations();
1319 Location receiver = locations->InAt(0);
1320 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1321 // temp = object->GetClass();
1322 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001323 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1324 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001329 // temp = temp->GetMethodAt(method_offset);
1330 __ movl(temp, Address(temp, method_offset));
1331 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001332 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001333 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001334
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001335 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001337}
1338
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001339void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1340 HandleInvoke(invoke);
1341 // Add the hidden argument.
1342 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1343}
1344
1345void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1346 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001347 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001348 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1349 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1350 LocationSummary* locations = invoke->GetLocations();
1351 Location receiver = locations->InAt(0);
1352 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1353
1354 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001355 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001356 Immediate(invoke->GetDexMethodIndex()));
1357
1358 // temp = object->GetClass();
1359 if (receiver.IsStackSlot()) {
1360 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1361 __ movl(temp, Address(temp, class_offset));
1362 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001363 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001364 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001365 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001366 // temp = temp->GetImtEntryAt(method_offset);
1367 __ movl(temp, Address(temp, method_offset));
1368 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001369 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001370 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001371
1372 DCHECK(!codegen_->IsLeafMethod());
1373 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1374}
1375
Roland Levillain88cb1752014-10-20 16:36:47 +01001376void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1377 LocationSummary* locations =
1378 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1379 switch (neg->GetResultType()) {
1380 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001381 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001382 locations->SetInAt(0, Location::RequiresRegister());
1383 locations->SetOut(Location::SameAsFirstInput());
1384 break;
1385
Roland Levillain88cb1752014-10-20 16:36:47 +01001386 case Primitive::kPrimFloat:
1387 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001388 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001389 locations->SetOut(Location::SameAsFirstInput());
1390 locations->AddTemp(Location::RequiresRegister());
1391 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1396 }
1397}
1398
1399void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1400 LocationSummary* locations = neg->GetLocations();
1401 Location out = locations->Out();
1402 Location in = locations->InAt(0);
1403 switch (neg->GetResultType()) {
1404 case Primitive::kPrimInt:
1405 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001406 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001407 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001408 break;
1409
1410 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001411 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001412 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001413 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001414 break;
1415
Roland Levillain5368c212014-11-27 15:03:41 +00001416 case Primitive::kPrimFloat: {
1417 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001418 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1419 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001420 // Implement float negation with an exclusive or with value
1421 // 0x80000000 (mask for bit 31, representing the sign of a
1422 // single-precision floating-point number).
1423 __ movq(constant, Immediate(INT64_C(0x80000000)));
1424 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001425 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001426 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001427 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001428
Roland Levillain5368c212014-11-27 15:03:41 +00001429 case Primitive::kPrimDouble: {
1430 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001431 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1432 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001433 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001434 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001435 // a double-precision floating-point number).
1436 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1437 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001438 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001439 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001440 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001441
1442 default:
1443 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1444 }
1445}
1446
Roland Levillaindff1f282014-11-05 14:15:05 +00001447void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1448 LocationSummary* locations =
1449 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1450 Primitive::Type result_type = conversion->GetResultType();
1451 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001452 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001453
David Brazdilb2bd1c52015-03-25 11:17:37 +00001454 // The Java language does not allow treating boolean as an integral type but
1455 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001456
Roland Levillaindff1f282014-11-05 14:15:05 +00001457 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001458 case Primitive::kPrimByte:
1459 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001460 case Primitive::kPrimBoolean:
1461 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001462 case Primitive::kPrimShort:
1463 case Primitive::kPrimInt:
1464 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001465 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001466 locations->SetInAt(0, Location::Any());
1467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1468 break;
1469
1470 default:
1471 LOG(FATAL) << "Unexpected type conversion from " << input_type
1472 << " to " << result_type;
1473 }
1474 break;
1475
Roland Levillain01a8d712014-11-14 16:27:39 +00001476 case Primitive::kPrimShort:
1477 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001478 case Primitive::kPrimBoolean:
1479 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001480 case Primitive::kPrimByte:
1481 case Primitive::kPrimInt:
1482 case Primitive::kPrimChar:
1483 // Processing a Dex `int-to-short' instruction.
1484 locations->SetInAt(0, Location::Any());
1485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1486 break;
1487
1488 default:
1489 LOG(FATAL) << "Unexpected type conversion from " << input_type
1490 << " to " << result_type;
1491 }
1492 break;
1493
Roland Levillain946e1432014-11-11 17:35:19 +00001494 case Primitive::kPrimInt:
1495 switch (input_type) {
1496 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001497 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001498 locations->SetInAt(0, Location::Any());
1499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1500 break;
1501
1502 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001503 // Processing a Dex `float-to-int' instruction.
1504 locations->SetInAt(0, Location::RequiresFpuRegister());
1505 locations->SetOut(Location::RequiresRegister());
1506 locations->AddTemp(Location::RequiresFpuRegister());
1507 break;
1508
Roland Levillain946e1432014-11-11 17:35:19 +00001509 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001510 // Processing a Dex `double-to-int' instruction.
1511 locations->SetInAt(0, Location::RequiresFpuRegister());
1512 locations->SetOut(Location::RequiresRegister());
1513 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001514 break;
1515
1516 default:
1517 LOG(FATAL) << "Unexpected type conversion from " << input_type
1518 << " to " << result_type;
1519 }
1520 break;
1521
Roland Levillaindff1f282014-11-05 14:15:05 +00001522 case Primitive::kPrimLong:
1523 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001524 case Primitive::kPrimBoolean:
1525 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001526 case Primitive::kPrimByte:
1527 case Primitive::kPrimShort:
1528 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001529 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001530 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001531 // TODO: We would benefit from a (to-be-implemented)
1532 // Location::RegisterOrStackSlot requirement for this input.
1533 locations->SetInAt(0, Location::RequiresRegister());
1534 locations->SetOut(Location::RequiresRegister());
1535 break;
1536
1537 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001538 // Processing a Dex `float-to-long' instruction.
1539 locations->SetInAt(0, Location::RequiresFpuRegister());
1540 locations->SetOut(Location::RequiresRegister());
1541 locations->AddTemp(Location::RequiresFpuRegister());
1542 break;
1543
Roland Levillaindff1f282014-11-05 14:15:05 +00001544 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001545 // Processing a Dex `double-to-long' instruction.
1546 locations->SetInAt(0, Location::RequiresFpuRegister());
1547 locations->SetOut(Location::RequiresRegister());
1548 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001549 break;
1550
1551 default:
1552 LOG(FATAL) << "Unexpected type conversion from " << input_type
1553 << " to " << result_type;
1554 }
1555 break;
1556
Roland Levillain981e4542014-11-14 11:47:14 +00001557 case Primitive::kPrimChar:
1558 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001559 case Primitive::kPrimBoolean:
1560 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001561 case Primitive::kPrimByte:
1562 case Primitive::kPrimShort:
1563 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001564 // Processing a Dex `int-to-char' instruction.
1565 locations->SetInAt(0, Location::Any());
1566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1567 break;
1568
1569 default:
1570 LOG(FATAL) << "Unexpected type conversion from " << input_type
1571 << " to " << result_type;
1572 }
1573 break;
1574
Roland Levillaindff1f282014-11-05 14:15:05 +00001575 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001576 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001577 case Primitive::kPrimBoolean:
1578 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001579 case Primitive::kPrimByte:
1580 case Primitive::kPrimShort:
1581 case Primitive::kPrimInt:
1582 case Primitive::kPrimChar:
1583 // Processing a Dex `int-to-float' instruction.
1584 locations->SetInAt(0, Location::RequiresRegister());
1585 locations->SetOut(Location::RequiresFpuRegister());
1586 break;
1587
1588 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001589 // Processing a Dex `long-to-float' instruction.
1590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetOut(Location::RequiresFpuRegister());
1592 break;
1593
Roland Levillaincff13742014-11-17 14:32:17 +00001594 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001595 // Processing a Dex `double-to-float' instruction.
1596 locations->SetInAt(0, Location::RequiresFpuRegister());
1597 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001598 break;
1599
1600 default:
1601 LOG(FATAL) << "Unexpected type conversion from " << input_type
1602 << " to " << result_type;
1603 };
1604 break;
1605
Roland Levillaindff1f282014-11-05 14:15:05 +00001606 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001607 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001608 case Primitive::kPrimBoolean:
1609 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001610 case Primitive::kPrimByte:
1611 case Primitive::kPrimShort:
1612 case Primitive::kPrimInt:
1613 case Primitive::kPrimChar:
1614 // Processing a Dex `int-to-double' instruction.
1615 locations->SetInAt(0, Location::RequiresRegister());
1616 locations->SetOut(Location::RequiresFpuRegister());
1617 break;
1618
1619 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001620 // Processing a Dex `long-to-double' instruction.
1621 locations->SetInAt(0, Location::RequiresRegister());
1622 locations->SetOut(Location::RequiresFpuRegister());
1623 break;
1624
Roland Levillaincff13742014-11-17 14:32:17 +00001625 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001626 // Processing a Dex `float-to-double' instruction.
1627 locations->SetInAt(0, Location::RequiresFpuRegister());
1628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001629 break;
1630
1631 default:
1632 LOG(FATAL) << "Unexpected type conversion from " << input_type
1633 << " to " << result_type;
1634 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001635 break;
1636
1637 default:
1638 LOG(FATAL) << "Unexpected type conversion from " << input_type
1639 << " to " << result_type;
1640 }
1641}
1642
1643void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1644 LocationSummary* locations = conversion->GetLocations();
1645 Location out = locations->Out();
1646 Location in = locations->InAt(0);
1647 Primitive::Type result_type = conversion->GetResultType();
1648 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001649 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001650 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001651 case Primitive::kPrimByte:
1652 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001653 case Primitive::kPrimBoolean:
1654 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001655 case Primitive::kPrimShort:
1656 case Primitive::kPrimInt:
1657 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001658 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001659 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001660 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001661 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001663 Address(CpuRegister(RSP), in.GetStackIndex()));
1664 } else {
1665 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001666 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001667 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1668 }
1669 break;
1670
1671 default:
1672 LOG(FATAL) << "Unexpected type conversion from " << input_type
1673 << " to " << result_type;
1674 }
1675 break;
1676
Roland Levillain01a8d712014-11-14 16:27:39 +00001677 case Primitive::kPrimShort:
1678 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001679 case Primitive::kPrimBoolean:
1680 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001681 case Primitive::kPrimByte:
1682 case Primitive::kPrimInt:
1683 case Primitive::kPrimChar:
1684 // Processing a Dex `int-to-short' instruction.
1685 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001686 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001687 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001688 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001689 Address(CpuRegister(RSP), in.GetStackIndex()));
1690 } else {
1691 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001692 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001693 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1694 }
1695 break;
1696
1697 default:
1698 LOG(FATAL) << "Unexpected type conversion from " << input_type
1699 << " to " << result_type;
1700 }
1701 break;
1702
Roland Levillain946e1432014-11-11 17:35:19 +00001703 case Primitive::kPrimInt:
1704 switch (input_type) {
1705 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001706 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001707 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001708 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001709 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001710 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001711 Address(CpuRegister(RSP), in.GetStackIndex()));
1712 } else {
1713 DCHECK(in.IsConstant());
1714 DCHECK(in.GetConstant()->IsLongConstant());
1715 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001716 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001717 }
1718 break;
1719
Roland Levillain3f8f9362014-12-02 17:45:01 +00001720 case Primitive::kPrimFloat: {
1721 // Processing a Dex `float-to-int' instruction.
1722 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1723 CpuRegister output = out.AsRegister<CpuRegister>();
1724 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1725 Label done, nan;
1726
1727 __ movl(output, Immediate(kPrimIntMax));
1728 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001729 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001730 // if input >= temp goto done
1731 __ comiss(input, temp);
1732 __ j(kAboveEqual, &done);
1733 // if input == NaN goto nan
1734 __ j(kUnordered, &nan);
1735 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001736 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001737 __ jmp(&done);
1738 __ Bind(&nan);
1739 // output = 0
1740 __ xorl(output, output);
1741 __ Bind(&done);
1742 break;
1743 }
1744
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001745 case Primitive::kPrimDouble: {
1746 // Processing a Dex `double-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-double(output)
1754 __ cvtsi2sd(temp, output);
1755 // if input >= temp goto done
1756 __ comisd(input, temp);
1757 __ j(kAboveEqual, &done);
1758 // if input == NaN goto nan
1759 __ j(kUnordered, &nan);
1760 // output = double-to-int-truncate(input)
1761 __ cvttsd2si(output, input);
1762 __ jmp(&done);
1763 __ Bind(&nan);
1764 // output = 0
1765 __ xorl(output, output);
1766 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001767 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001768 }
Roland Levillain946e1432014-11-11 17:35:19 +00001769
1770 default:
1771 LOG(FATAL) << "Unexpected type conversion from " << input_type
1772 << " to " << result_type;
1773 }
1774 break;
1775
Roland Levillaindff1f282014-11-05 14:15:05 +00001776 case Primitive::kPrimLong:
1777 switch (input_type) {
1778 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001779 case Primitive::kPrimBoolean:
1780 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001781 case Primitive::kPrimByte:
1782 case Primitive::kPrimShort:
1783 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001784 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001785 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001786 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001787 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001788 break;
1789
Roland Levillain624279f2014-12-04 11:54:28 +00001790 case Primitive::kPrimFloat: {
1791 // Processing a Dex `float-to-long' instruction.
1792 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1793 CpuRegister output = out.AsRegister<CpuRegister>();
1794 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1795 Label done, nan;
1796
1797 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001798 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001799 __ cvtsi2ss(temp, output, true);
1800 // if input >= temp goto done
1801 __ comiss(input, temp);
1802 __ j(kAboveEqual, &done);
1803 // if input == NaN goto nan
1804 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001805 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001806 __ cvttss2si(output, input, true);
1807 __ jmp(&done);
1808 __ Bind(&nan);
1809 // output = 0
1810 __ xorq(output, output);
1811 __ Bind(&done);
1812 break;
1813 }
1814
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001815 case Primitive::kPrimDouble: {
1816 // Processing a Dex `double-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));
1823 // temp = long-to-double(output)
1824 __ cvtsi2sd(temp, output, true);
1825 // if input >= temp goto done
1826 __ comisd(input, temp);
1827 __ j(kAboveEqual, &done);
1828 // if input == NaN goto nan
1829 __ j(kUnordered, &nan);
1830 // output = double-to-long-truncate(input)
1831 __ cvttsd2si(output, input, true);
1832 __ jmp(&done);
1833 __ Bind(&nan);
1834 // output = 0
1835 __ xorq(output, output);
1836 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001837 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001838 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001839
1840 default:
1841 LOG(FATAL) << "Unexpected type conversion from " << input_type
1842 << " to " << result_type;
1843 }
1844 break;
1845
Roland Levillain981e4542014-11-14 11:47:14 +00001846 case Primitive::kPrimChar:
1847 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001848 case Primitive::kPrimBoolean:
1849 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001850 case Primitive::kPrimByte:
1851 case Primitive::kPrimShort:
1852 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001853 // Processing a Dex `int-to-char' instruction.
1854 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001855 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001856 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001857 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001858 Address(CpuRegister(RSP), in.GetStackIndex()));
1859 } else {
1860 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001861 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001862 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1863 }
1864 break;
1865
1866 default:
1867 LOG(FATAL) << "Unexpected type conversion from " << input_type
1868 << " to " << result_type;
1869 }
1870 break;
1871
Roland Levillaindff1f282014-11-05 14:15:05 +00001872 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001873 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001874 case Primitive::kPrimBoolean:
1875 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001876 case Primitive::kPrimByte:
1877 case Primitive::kPrimShort:
1878 case Primitive::kPrimInt:
1879 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001880 // Processing a Dex `int-to-float' instruction.
1881 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001882 break;
1883
1884 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001885 // Processing a Dex `long-to-float' instruction.
1886 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1887 break;
1888
Roland Levillaincff13742014-11-17 14:32:17 +00001889 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001890 // Processing a Dex `double-to-float' instruction.
1891 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unexpected type conversion from " << input_type
1896 << " to " << result_type;
1897 };
1898 break;
1899
Roland Levillaindff1f282014-11-05 14:15:05 +00001900 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001901 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001902 case Primitive::kPrimBoolean:
1903 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001904 case Primitive::kPrimByte:
1905 case Primitive::kPrimShort:
1906 case Primitive::kPrimInt:
1907 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001908 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001909 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001910 break;
1911
1912 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001913 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001914 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001915 break;
1916
Roland Levillaincff13742014-11-17 14:32:17 +00001917 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001918 // Processing a Dex `float-to-double' instruction.
1919 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001920 break;
1921
1922 default:
1923 LOG(FATAL) << "Unexpected type conversion from " << input_type
1924 << " to " << result_type;
1925 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001926 break;
1927
1928 default:
1929 LOG(FATAL) << "Unexpected type conversion from " << input_type
1930 << " to " << result_type;
1931 }
1932}
1933
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001934void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001935 LocationSummary* locations =
1936 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001937 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001938 case Primitive::kPrimInt: {
1939 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001940 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1941 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001942 break;
1943 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001945 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001946 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001947 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001948 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001949 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001950 break;
1951 }
1952
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 case Primitive::kPrimDouble:
1954 case Primitive::kPrimFloat: {
1955 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04001956 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001958 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001960
1961 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001963 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001964}
1965
1966void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1967 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001968 Location first = locations->InAt(0);
1969 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001970 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001971
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001972 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001973 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001974 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001975 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1976 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1977 } else {
1978 __ leal(out.AsRegister<CpuRegister>(), Address(
1979 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
1980 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001981 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001982 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1983 __ addl(out.AsRegister<CpuRegister>(),
1984 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
1985 } else {
1986 __ leal(out.AsRegister<CpuRegister>(), Address(
1987 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
1988 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001989 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001990 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001991 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001992 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001993 break;
1994 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001995
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001996 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05001997 if (second.IsRegister()) {
1998 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1999 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2000 } else {
2001 __ leaq(out.AsRegister<CpuRegister>(), Address(
2002 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2003 }
2004 } else {
2005 DCHECK(second.IsConstant());
2006 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2007 int32_t int32_value = Low32Bits(value);
2008 DCHECK_EQ(int32_value, value);
2009 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2010 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2011 } else {
2012 __ leaq(out.AsRegister<CpuRegister>(), Address(
2013 first.AsRegister<CpuRegister>(), int32_value));
2014 }
2015 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016 break;
2017 }
2018
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002019 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002020 if (second.IsFpuRegister()) {
2021 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2022 } else if (second.IsConstant()) {
2023 __ addss(first.AsFpuRegister<XmmRegister>(),
2024 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2025 } else {
2026 DCHECK(second.IsStackSlot());
2027 __ addss(first.AsFpuRegister<XmmRegister>(),
2028 Address(CpuRegister(RSP), second.GetStackIndex()));
2029 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002031 }
2032
2033 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002034 if (second.IsFpuRegister()) {
2035 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2036 } else if (second.IsConstant()) {
2037 __ addsd(first.AsFpuRegister<XmmRegister>(),
2038 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2039 } else {
2040 DCHECK(second.IsDoubleStackSlot());
2041 __ addsd(first.AsFpuRegister<XmmRegister>(),
2042 Address(CpuRegister(RSP), second.GetStackIndex()));
2043 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002044 break;
2045 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002046
2047 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002048 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002049 }
2050}
2051
2052void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002053 LocationSummary* locations =
2054 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002055 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002056 case Primitive::kPrimInt: {
2057 locations->SetInAt(0, Location::RequiresRegister());
2058 locations->SetInAt(1, Location::Any());
2059 locations->SetOut(Location::SameAsFirstInput());
2060 break;
2061 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002062 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002063 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002064 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002065 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002066 break;
2067 }
Calin Juravle11351682014-10-23 15:38:15 +01002068 case Primitive::kPrimFloat:
2069 case Primitive::kPrimDouble: {
2070 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002071 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002072 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002073 break;
Calin Juravle11351682014-10-23 15:38:15 +01002074 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002075 default:
Calin Juravle11351682014-10-23 15:38:15 +01002076 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002077 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002078}
2079
2080void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2081 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002082 Location first = locations->InAt(0);
2083 Location second = locations->InAt(1);
2084 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002085 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002086 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002087 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002089 } else if (second.IsConstant()) {
2090 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002091 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002092 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002094 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002095 break;
2096 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002097 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002098 if (second.IsConstant()) {
2099 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2100 DCHECK(IsInt<32>(value));
2101 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2102 } else {
2103 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2104 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002105 break;
2106 }
2107
Calin Juravle11351682014-10-23 15:38:15 +01002108 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002109 if (second.IsFpuRegister()) {
2110 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2111 } else if (second.IsConstant()) {
2112 __ subss(first.AsFpuRegister<XmmRegister>(),
2113 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2114 } else {
2115 DCHECK(second.IsStackSlot());
2116 __ subss(first.AsFpuRegister<XmmRegister>(),
2117 Address(CpuRegister(RSP), second.GetStackIndex()));
2118 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002119 break;
Calin Juravle11351682014-10-23 15:38:15 +01002120 }
2121
2122 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002123 if (second.IsFpuRegister()) {
2124 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2125 } else if (second.IsConstant()) {
2126 __ subsd(first.AsFpuRegister<XmmRegister>(),
2127 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2128 } else {
2129 DCHECK(second.IsDoubleStackSlot());
2130 __ subsd(first.AsFpuRegister<XmmRegister>(),
2131 Address(CpuRegister(RSP), second.GetStackIndex()));
2132 }
Calin Juravle11351682014-10-23 15:38:15 +01002133 break;
2134 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002135
2136 default:
Calin Juravle11351682014-10-23 15:38:15 +01002137 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002138 }
2139}
2140
Calin Juravle34bacdf2014-10-07 20:23:36 +01002141void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2142 LocationSummary* locations =
2143 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2144 switch (mul->GetResultType()) {
2145 case Primitive::kPrimInt: {
2146 locations->SetInAt(0, Location::RequiresRegister());
2147 locations->SetInAt(1, Location::Any());
2148 locations->SetOut(Location::SameAsFirstInput());
2149 break;
2150 }
2151 case Primitive::kPrimLong: {
2152 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002153 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2154 if (locations->InAt(1).IsConstant()) {
2155 // Can use 3 operand multiply.
2156 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2157 } else {
2158 locations->SetOut(Location::SameAsFirstInput());
2159 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002160 break;
2161 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002162 case Primitive::kPrimFloat:
2163 case Primitive::kPrimDouble: {
2164 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002165 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002166 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002167 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002168 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002169
2170 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002171 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002172 }
2173}
2174
2175void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2176 LocationSummary* locations = mul->GetLocations();
2177 Location first = locations->InAt(0);
2178 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002179 switch (mul->GetResultType()) {
2180 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002181 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002182 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002183 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002184 } else if (second.IsConstant()) {
2185 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002186 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002187 } else {
2188 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002189 __ imull(first.AsRegister<CpuRegister>(),
2190 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002191 }
2192 break;
2193 }
2194 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002195 if (second.IsConstant()) {
2196 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2197 DCHECK(IsInt<32>(value));
2198 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2199 first.AsRegister<CpuRegister>(),
2200 Immediate(static_cast<int32_t>(value)));
2201 } else {
2202 DCHECK(first.Equals(locations->Out()));
2203 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2204 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002205 break;
2206 }
2207
Calin Juravleb5bfa962014-10-21 18:02:24 +01002208 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002209 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002210 if (second.IsFpuRegister()) {
2211 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2212 } else if (second.IsConstant()) {
2213 __ mulss(first.AsFpuRegister<XmmRegister>(),
2214 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2215 } else {
2216 DCHECK(second.IsStackSlot());
2217 __ mulss(first.AsFpuRegister<XmmRegister>(),
2218 Address(CpuRegister(RSP), second.GetStackIndex()));
2219 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002220 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002221 }
2222
2223 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002224 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002225 if (second.IsFpuRegister()) {
2226 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2227 } else if (second.IsConstant()) {
2228 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2229 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2230 } else {
2231 DCHECK(second.IsDoubleStackSlot());
2232 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2233 Address(CpuRegister(RSP), second.GetStackIndex()));
2234 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002235 break;
2236 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002237
2238 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002239 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002240 }
2241}
2242
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002243void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2244 uint32_t stack_adjustment, bool is_float) {
2245 if (source.IsStackSlot()) {
2246 DCHECK(is_float);
2247 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2248 } else if (source.IsDoubleStackSlot()) {
2249 DCHECK(!is_float);
2250 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2251 } else {
2252 // Write the value to the temporary location on the stack and load to FP stack.
2253 if (is_float) {
2254 Location stack_temp = Location::StackSlot(temp_offset);
2255 codegen_->Move(stack_temp, source);
2256 __ flds(Address(CpuRegister(RSP), temp_offset));
2257 } else {
2258 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2259 codegen_->Move(stack_temp, source);
2260 __ fldl(Address(CpuRegister(RSP), temp_offset));
2261 }
2262 }
2263}
2264
2265void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2266 Primitive::Type type = rem->GetResultType();
2267 bool is_float = type == Primitive::kPrimFloat;
2268 size_t elem_size = Primitive::ComponentSize(type);
2269 LocationSummary* locations = rem->GetLocations();
2270 Location first = locations->InAt(0);
2271 Location second = locations->InAt(1);
2272 Location out = locations->Out();
2273
2274 // Create stack space for 2 elements.
2275 // TODO: enhance register allocator to ask for stack temporaries.
2276 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2277
2278 // Load the values to the FP stack in reverse order, using temporaries if needed.
2279 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2280 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2281
2282 // Loop doing FPREM until we stabilize.
2283 Label retry;
2284 __ Bind(&retry);
2285 __ fprem();
2286
2287 // Move FP status to AX.
2288 __ fstsw();
2289
2290 // And see if the argument reduction is complete. This is signaled by the
2291 // C2 FPU flag bit set to 0.
2292 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2293 __ j(kNotEqual, &retry);
2294
2295 // We have settled on the final value. Retrieve it into an XMM register.
2296 // Store FP top of stack to real stack.
2297 if (is_float) {
2298 __ fsts(Address(CpuRegister(RSP), 0));
2299 } else {
2300 __ fstl(Address(CpuRegister(RSP), 0));
2301 }
2302
2303 // Pop the 2 items from the FP stack.
2304 __ fucompp();
2305
2306 // Load the value from the stack into an XMM register.
2307 DCHECK(out.IsFpuRegister()) << out;
2308 if (is_float) {
2309 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2310 } else {
2311 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2312 }
2313
2314 // And remove the temporary stack space we allocated.
2315 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2316}
2317
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002318void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2319 DCHECK(instruction->IsDiv() || instruction->IsRem());
2320
2321 LocationSummary* locations = instruction->GetLocations();
2322 Location second = locations->InAt(1);
2323 DCHECK(second.IsConstant());
2324
2325 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2326 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
2327 int64_t imm;
2328 if (second.GetConstant()->IsLongConstant()) {
2329 imm = second.GetConstant()->AsLongConstant()->GetValue();
2330 } else {
2331 imm = second.GetConstant()->AsIntConstant()->GetValue();
2332 }
2333
2334 DCHECK(imm == 1 || imm == -1);
2335
2336 switch (instruction->GetResultType()) {
2337 case Primitive::kPrimInt: {
2338 if (instruction->IsRem()) {
2339 __ xorl(output_register, output_register);
2340 } else {
2341 __ movl(output_register, input_register);
2342 if (imm == -1) {
2343 __ negl(output_register);
2344 }
2345 }
2346 break;
2347 }
2348
2349 case Primitive::kPrimLong: {
2350 if (instruction->IsRem()) {
2351 __ xorq(output_register, output_register);
2352 } else {
2353 __ movq(output_register, input_register);
2354 if (imm == -1) {
2355 __ negq(output_register);
2356 }
2357 }
2358 break;
2359 }
2360
2361 default:
2362 LOG(FATAL) << "Unreachable";
2363 }
2364}
2365
2366void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HBinaryOperation* instruction) {
2367 DCHECK(instruction->IsDiv());
2368
2369 LocationSummary* locations = instruction->GetLocations();
2370 Location second = locations->InAt(1);
2371
2372 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2373 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2374
2375 int64_t imm;
2376 if (instruction->GetResultType() == Primitive::kPrimLong) {
2377 imm = second.GetConstant()->AsLongConstant()->GetValue();
2378 } else {
2379 imm = second.GetConstant()->AsIntConstant()->GetValue();
2380 }
2381
2382 DCHECK(IsPowerOfTwo(std::abs(imm)));
2383
2384 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2385
2386 if (instruction->GetResultType() == Primitive::kPrimInt) {
2387 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2388 __ testl(numerator, numerator);
2389 __ cmov(kGreaterEqual, tmp, numerator);
2390 int shift = CTZ(imm);
2391 __ sarl(tmp, Immediate(shift));
2392
2393 if (imm < 0) {
2394 __ negl(tmp);
2395 }
2396
2397 __ movl(output_register, tmp);
2398 } else {
2399 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2400 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2401
2402 __ movq(rdx, Immediate(std::abs(imm) - 1));
2403 __ addq(rdx, numerator);
2404 __ testq(numerator, numerator);
2405 __ cmov(kGreaterEqual, rdx, numerator);
2406 int shift = CTZ(imm);
2407 __ sarq(rdx, Immediate(shift));
2408
2409 if (imm < 0) {
2410 __ negq(rdx);
2411 }
2412
2413 __ movq(output_register, rdx);
2414 }
2415}
2416
2417void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2418 DCHECK(instruction->IsDiv() || instruction->IsRem());
2419
2420 LocationSummary* locations = instruction->GetLocations();
2421 Location second = locations->InAt(1);
2422
2423 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2424 : locations->GetTemp(0).AsRegister<CpuRegister>();
2425 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2426 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2427 : locations->Out().AsRegister<CpuRegister>();
2428 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2429
2430 DCHECK_EQ(RAX, eax.AsRegister());
2431 DCHECK_EQ(RDX, edx.AsRegister());
2432 if (instruction->IsDiv()) {
2433 DCHECK_EQ(RAX, out.AsRegister());
2434 } else {
2435 DCHECK_EQ(RDX, out.AsRegister());
2436 }
2437
2438 int64_t magic;
2439 int shift;
2440
2441 // TODO: can these branch be written as one?
2442 if (instruction->GetResultType() == Primitive::kPrimInt) {
2443 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2444
2445 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2446
2447 __ movl(numerator, eax);
2448
2449 Label no_div;
2450 Label end;
2451 __ testl(eax, eax);
2452 __ j(kNotEqual, &no_div);
2453
2454 __ xorl(out, out);
2455 __ jmp(&end);
2456
2457 __ Bind(&no_div);
2458
2459 __ movl(eax, Immediate(magic));
2460 __ imull(numerator);
2461
2462 if (imm > 0 && magic < 0) {
2463 __ addl(edx, numerator);
2464 } else if (imm < 0 && magic > 0) {
2465 __ subl(edx, numerator);
2466 }
2467
2468 if (shift != 0) {
2469 __ sarl(edx, Immediate(shift));
2470 }
2471
2472 __ movl(eax, edx);
2473 __ shrl(edx, Immediate(31));
2474 __ addl(edx, eax);
2475
2476 if (instruction->IsRem()) {
2477 __ movl(eax, numerator);
2478 __ imull(edx, Immediate(imm));
2479 __ subl(eax, edx);
2480 __ movl(edx, eax);
2481 } else {
2482 __ movl(eax, edx);
2483 }
2484 __ Bind(&end);
2485 } else {
2486 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2487
2488 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2489
2490 CpuRegister rax = eax;
2491 CpuRegister rdx = edx;
2492
2493 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2494
2495 // Save the numerator.
2496 __ movq(numerator, rax);
2497
2498 // RAX = magic
2499 __ movq(rax, Immediate(magic));
2500
2501 // RDX:RAX = magic * numerator
2502 __ imulq(numerator);
2503
2504 if (imm > 0 && magic < 0) {
2505 // RDX += numeratorerator
2506 __ addq(rdx, numerator);
2507 } else if (imm < 0 && magic > 0) {
2508 // RDX -= numerator
2509 __ subq(rdx, numerator);
2510 }
2511
2512 // Shift if needed.
2513 if (shift != 0) {
2514 __ sarq(rdx, Immediate(shift));
2515 }
2516
2517 // RDX += 1 if RDX < 0
2518 __ movq(rax, rdx);
2519 __ shrq(rdx, Immediate(63));
2520 __ addq(rdx, rax);
2521
2522 if (instruction->IsRem()) {
2523 __ movq(rax, numerator);
2524
2525 if (IsInt<32>(imm)) {
2526 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2527 } else {
2528 __ movq(numerator, Immediate(imm));
2529 __ imulq(rdx, numerator);
2530 }
2531
2532 __ subq(rax, rdx);
2533 __ movq(rdx, rax);
2534 } else {
2535 __ movq(rax, rdx);
2536 }
2537 }
2538}
2539
Calin Juravlebacfec32014-11-14 15:54:36 +00002540void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2541 DCHECK(instruction->IsDiv() || instruction->IsRem());
2542 Primitive::Type type = instruction->GetResultType();
2543 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2544
2545 bool is_div = instruction->IsDiv();
2546 LocationSummary* locations = instruction->GetLocations();
2547
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002548 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2549 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002550
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002552 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002553
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002554 if (second.IsConstant()) {
2555 int64_t imm;
2556 if (second.GetConstant()->AsLongConstant()) {
2557 imm = second.GetConstant()->AsLongConstant()->GetValue();
2558 } else {
2559 imm = second.GetConstant()->AsIntConstant()->GetValue();
2560 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002561
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002562 if (imm == 0) {
2563 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2564 } else if (imm == 1 || imm == -1) {
2565 DivRemOneOrMinusOne(instruction);
2566 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
2567 DivByPowerOfTwo(instruction);
2568 } else {
2569 DCHECK(imm <= -2 || imm >= 2);
2570 GenerateDivRemWithAnyConstant(instruction);
2571 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002572 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002573 SlowPathCodeX86_64* slow_path =
2574 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2575 out.AsRegister(), type, is_div);
2576 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002577
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002578 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2579 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2580 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2581 // so it's safe to just use negl instead of more complex comparisons.
2582 if (type == Primitive::kPrimInt) {
2583 __ cmpl(second_reg, Immediate(-1));
2584 __ j(kEqual, slow_path->GetEntryLabel());
2585 // edx:eax <- sign-extended of eax
2586 __ cdq();
2587 // eax = quotient, edx = remainder
2588 __ idivl(second_reg);
2589 } else {
2590 __ cmpq(second_reg, Immediate(-1));
2591 __ j(kEqual, slow_path->GetEntryLabel());
2592 // rdx:rax <- sign-extended of rax
2593 __ cqo();
2594 // rax = quotient, rdx = remainder
2595 __ idivq(second_reg);
2596 }
2597 __ Bind(slow_path->GetExitLabel());
2598 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002599}
2600
Calin Juravle7c4954d2014-10-28 16:57:40 +00002601void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2602 LocationSummary* locations =
2603 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2604 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002605 case Primitive::kPrimInt:
2606 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002607 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002608 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002609 locations->SetOut(Location::SameAsFirstInput());
2610 // Intel uses edx:eax as the dividend.
2611 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002612 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2613 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2614 // output and request another temp.
2615 if (div->InputAt(1)->IsConstant()) {
2616 locations->AddTemp(Location::RequiresRegister());
2617 }
Calin Juravled0d48522014-11-04 16:40:20 +00002618 break;
2619 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002620
Calin Juravle7c4954d2014-10-28 16:57:40 +00002621 case Primitive::kPrimFloat:
2622 case Primitive::kPrimDouble: {
2623 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002624 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002625 locations->SetOut(Location::SameAsFirstInput());
2626 break;
2627 }
2628
2629 default:
2630 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2631 }
2632}
2633
2634void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2635 LocationSummary* locations = div->GetLocations();
2636 Location first = locations->InAt(0);
2637 Location second = locations->InAt(1);
2638 DCHECK(first.Equals(locations->Out()));
2639
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002640 Primitive::Type type = div->GetResultType();
2641 switch (type) {
2642 case Primitive::kPrimInt:
2643 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002644 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002645 break;
2646 }
2647
Calin Juravle7c4954d2014-10-28 16:57:40 +00002648 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002649 if (second.IsFpuRegister()) {
2650 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2651 } else if (second.IsConstant()) {
2652 __ divss(first.AsFpuRegister<XmmRegister>(),
2653 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2654 } else {
2655 DCHECK(second.IsStackSlot());
2656 __ divss(first.AsFpuRegister<XmmRegister>(),
2657 Address(CpuRegister(RSP), second.GetStackIndex()));
2658 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002659 break;
2660 }
2661
2662 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002663 if (second.IsFpuRegister()) {
2664 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2665 } else if (second.IsConstant()) {
2666 __ divsd(first.AsFpuRegister<XmmRegister>(),
2667 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2668 } else {
2669 DCHECK(second.IsDoubleStackSlot());
2670 __ divsd(first.AsFpuRegister<XmmRegister>(),
2671 Address(CpuRegister(RSP), second.GetStackIndex()));
2672 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002673 break;
2674 }
2675
2676 default:
2677 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2678 }
2679}
2680
Calin Juravlebacfec32014-11-14 15:54:36 +00002681void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002682 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002683 LocationSummary* locations =
2684 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002685
2686 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002687 case Primitive::kPrimInt:
2688 case Primitive::kPrimLong: {
2689 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002690 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002691 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2692 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002693 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2694 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2695 // output and request another temp.
2696 if (rem->InputAt(1)->IsConstant()) {
2697 locations->AddTemp(Location::RequiresRegister());
2698 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002699 break;
2700 }
2701
2702 case Primitive::kPrimFloat:
2703 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002704 locations->SetInAt(0, Location::Any());
2705 locations->SetInAt(1, Location::Any());
2706 locations->SetOut(Location::RequiresFpuRegister());
2707 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002708 break;
2709 }
2710
2711 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002712 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002713 }
2714}
2715
2716void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2717 Primitive::Type type = rem->GetResultType();
2718 switch (type) {
2719 case Primitive::kPrimInt:
2720 case Primitive::kPrimLong: {
2721 GenerateDivRemIntegral(rem);
2722 break;
2723 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002724 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002725 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002726 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002727 break;
2728 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002729 default:
2730 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2731 }
2732}
2733
Calin Juravled0d48522014-11-04 16:40:20 +00002734void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2735 LocationSummary* locations =
2736 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2737 locations->SetInAt(0, Location::Any());
2738 if (instruction->HasUses()) {
2739 locations->SetOut(Location::SameAsFirstInput());
2740 }
2741}
2742
2743void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2744 SlowPathCodeX86_64* slow_path =
2745 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2746 codegen_->AddSlowPath(slow_path);
2747
2748 LocationSummary* locations = instruction->GetLocations();
2749 Location value = locations->InAt(0);
2750
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002751 switch (instruction->GetType()) {
2752 case Primitive::kPrimInt: {
2753 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002754 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002755 __ j(kEqual, slow_path->GetEntryLabel());
2756 } else if (value.IsStackSlot()) {
2757 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2758 __ j(kEqual, slow_path->GetEntryLabel());
2759 } else {
2760 DCHECK(value.IsConstant()) << value;
2761 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2762 __ jmp(slow_path->GetEntryLabel());
2763 }
2764 }
2765 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002766 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002767 case Primitive::kPrimLong: {
2768 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002770 __ j(kEqual, slow_path->GetEntryLabel());
2771 } else if (value.IsDoubleStackSlot()) {
2772 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2773 __ j(kEqual, slow_path->GetEntryLabel());
2774 } else {
2775 DCHECK(value.IsConstant()) << value;
2776 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2777 __ jmp(slow_path->GetEntryLabel());
2778 }
2779 }
2780 break;
2781 }
2782 default:
2783 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002784 }
Calin Juravled0d48522014-11-04 16:40:20 +00002785}
2786
Calin Juravle9aec02f2014-11-18 23:06:35 +00002787void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2788 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2789
2790 LocationSummary* locations =
2791 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2792
2793 switch (op->GetResultType()) {
2794 case Primitive::kPrimInt:
2795 case Primitive::kPrimLong: {
2796 locations->SetInAt(0, Location::RequiresRegister());
2797 // The shift count needs to be in CL.
2798 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2799 locations->SetOut(Location::SameAsFirstInput());
2800 break;
2801 }
2802 default:
2803 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2804 }
2805}
2806
2807void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2808 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2809
2810 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002812 Location second = locations->InAt(1);
2813
2814 switch (op->GetResultType()) {
2815 case Primitive::kPrimInt: {
2816 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002817 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002818 if (op->IsShl()) {
2819 __ shll(first_reg, second_reg);
2820 } else if (op->IsShr()) {
2821 __ sarl(first_reg, second_reg);
2822 } else {
2823 __ shrl(first_reg, second_reg);
2824 }
2825 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002826 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002827 if (op->IsShl()) {
2828 __ shll(first_reg, imm);
2829 } else if (op->IsShr()) {
2830 __ sarl(first_reg, imm);
2831 } else {
2832 __ shrl(first_reg, imm);
2833 }
2834 }
2835 break;
2836 }
2837 case Primitive::kPrimLong: {
2838 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002839 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002840 if (op->IsShl()) {
2841 __ shlq(first_reg, second_reg);
2842 } else if (op->IsShr()) {
2843 __ sarq(first_reg, second_reg);
2844 } else {
2845 __ shrq(first_reg, second_reg);
2846 }
2847 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002848 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002849 if (op->IsShl()) {
2850 __ shlq(first_reg, imm);
2851 } else if (op->IsShr()) {
2852 __ sarq(first_reg, imm);
2853 } else {
2854 __ shrq(first_reg, imm);
2855 }
2856 }
2857 break;
2858 }
2859 default:
2860 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2861 }
2862}
2863
2864void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2865 HandleShift(shl);
2866}
2867
2868void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2869 HandleShift(shl);
2870}
2871
2872void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2873 HandleShift(shr);
2874}
2875
2876void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2877 HandleShift(shr);
2878}
2879
2880void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2881 HandleShift(ushr);
2882}
2883
2884void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2885 HandleShift(ushr);
2886}
2887
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002888void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002889 LocationSummary* locations =
2890 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002891 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002892 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2893 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2894 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002895}
2896
2897void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2898 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002899 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002900 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2901
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002902 __ gs()->call(
2903 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002905 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002906 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002907}
2908
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002909void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2910 LocationSummary* locations =
2911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2912 InvokeRuntimeCallingConvention calling_convention;
2913 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002914 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002915 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002916 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002917}
2918
2919void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2920 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002921 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002922 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2923
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002924 __ gs()->call(
2925 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002926
2927 DCHECK(!codegen_->IsLeafMethod());
2928 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2929}
2930
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002931void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002932 LocationSummary* locations =
2933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002934 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2935 if (location.IsStackSlot()) {
2936 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2937 } else if (location.IsDoubleStackSlot()) {
2938 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2939 }
2940 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002941}
2942
2943void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2944 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002945 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002946}
2947
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002948void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002949 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002950 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002951 locations->SetInAt(0, Location::RequiresRegister());
2952 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002953}
2954
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002955void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2956 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2958 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002959 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002960 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002961 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002962 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002963 break;
2964
2965 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002966 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002967 break;
2968
2969 default:
2970 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2971 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002972}
2973
2974void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002975 LocationSummary* locations =
2976 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002977 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2978 locations->SetInAt(i, Location::Any());
2979 }
2980 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002981}
2982
2983void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002984 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002985 LOG(FATAL) << "Unimplemented";
2986}
2987
Calin Juravle52c48962014-12-16 17:02:57 +00002988void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2989 /*
2990 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2991 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2992 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2993 */
2994 switch (kind) {
2995 case MemBarrierKind::kAnyAny: {
2996 __ mfence();
2997 break;
2998 }
2999 case MemBarrierKind::kAnyStore:
3000 case MemBarrierKind::kLoadAny:
3001 case MemBarrierKind::kStoreStore: {
3002 // nop
3003 break;
3004 }
3005 default:
3006 LOG(FATAL) << "Unexpected memory barier " << kind;
3007 }
3008}
3009
3010void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3011 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3012
Nicolas Geoffray39468442014-09-02 15:17:15 +01003013 LocationSummary* locations =
3014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003015 locations->SetInAt(0, Location::RequiresRegister());
3016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3017}
3018
3019void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3020 const FieldInfo& field_info) {
3021 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3022
3023 LocationSummary* locations = instruction->GetLocations();
3024 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3025 Location out = locations->Out();
3026 bool is_volatile = field_info.IsVolatile();
3027 Primitive::Type field_type = field_info.GetFieldType();
3028 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3029
3030 switch (field_type) {
3031 case Primitive::kPrimBoolean: {
3032 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3033 break;
3034 }
3035
3036 case Primitive::kPrimByte: {
3037 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3038 break;
3039 }
3040
3041 case Primitive::kPrimShort: {
3042 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3043 break;
3044 }
3045
3046 case Primitive::kPrimChar: {
3047 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3048 break;
3049 }
3050
3051 case Primitive::kPrimInt:
3052 case Primitive::kPrimNot: {
3053 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3054 break;
3055 }
3056
3057 case Primitive::kPrimLong: {
3058 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3059 break;
3060 }
3061
3062 case Primitive::kPrimFloat: {
3063 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3064 break;
3065 }
3066
3067 case Primitive::kPrimDouble: {
3068 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3069 break;
3070 }
3071
3072 case Primitive::kPrimVoid:
3073 LOG(FATAL) << "Unreachable type " << field_type;
3074 UNREACHABLE();
3075 }
3076
Calin Juravle77520bc2015-01-12 18:45:46 +00003077 codegen_->MaybeRecordImplicitNullCheck(instruction);
3078
Calin Juravle52c48962014-12-16 17:02:57 +00003079 if (is_volatile) {
3080 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3081 }
3082}
3083
3084void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3085 const FieldInfo& field_info) {
3086 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3087
3088 LocationSummary* locations =
3089 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003090 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003091 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3092
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003093 locations->SetInAt(0, Location::RequiresRegister());
3094 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003095 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003096 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003097 locations->AddTemp(Location::RequiresRegister());
3098 locations->AddTemp(Location::RequiresRegister());
3099 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003100}
3101
Calin Juravle52c48962014-12-16 17:02:57 +00003102void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3103 const FieldInfo& field_info) {
3104 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3105
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003106 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003107 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3108 Location value = locations->InAt(1);
3109 bool is_volatile = field_info.IsVolatile();
3110 Primitive::Type field_type = field_info.GetFieldType();
3111 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3112
3113 if (is_volatile) {
3114 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3115 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003116
3117 switch (field_type) {
3118 case Primitive::kPrimBoolean:
3119 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003120 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003121 break;
3122 }
3123
3124 case Primitive::kPrimShort:
3125 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003126 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003127 break;
3128 }
3129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003131 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003132 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003133 break;
3134 }
3135
3136 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003137 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003138 break;
3139 }
3140
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003141 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003142 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003143 break;
3144 }
3145
3146 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003147 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003148 break;
3149 }
3150
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003151 case Primitive::kPrimVoid:
3152 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003153 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003154 }
Calin Juravle52c48962014-12-16 17:02:57 +00003155
Calin Juravle77520bc2015-01-12 18:45:46 +00003156 codegen_->MaybeRecordImplicitNullCheck(instruction);
3157
3158 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3159 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3160 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3161 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3162 }
3163
Calin Juravle52c48962014-12-16 17:02:57 +00003164 if (is_volatile) {
3165 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3166 }
3167}
3168
3169void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3170 HandleFieldSet(instruction, instruction->GetFieldInfo());
3171}
3172
3173void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3174 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003175}
3176
3177void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003178 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003179}
3180
3181void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003182 HandleFieldGet(instruction, instruction->GetFieldInfo());
3183}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184
Calin Juravle52c48962014-12-16 17:02:57 +00003185void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3186 HandleFieldGet(instruction);
3187}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003188
Calin Juravle52c48962014-12-16 17:02:57 +00003189void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3190 HandleFieldGet(instruction, instruction->GetFieldInfo());
3191}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003192
Calin Juravle52c48962014-12-16 17:02:57 +00003193void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3194 HandleFieldSet(instruction, instruction->GetFieldInfo());
3195}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003196
Calin Juravle52c48962014-12-16 17:02:57 +00003197void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3198 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199}
3200
3201void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003202 LocationSummary* locations =
3203 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003204 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3205 ? Location::RequiresRegister()
3206 : Location::Any();
3207 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003208 if (instruction->HasUses()) {
3209 locations->SetOut(Location::SameAsFirstInput());
3210 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003211}
3212
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003213void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003214 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3215 return;
3216 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003217 LocationSummary* locations = instruction->GetLocations();
3218 Location obj = locations->InAt(0);
3219
3220 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3221 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3222}
3223
3224void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003225 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003226 codegen_->AddSlowPath(slow_path);
3227
3228 LocationSummary* locations = instruction->GetLocations();
3229 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003230
3231 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003232 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003233 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003234 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003235 } else {
3236 DCHECK(obj.IsConstant()) << obj;
3237 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3238 __ jmp(slow_path->GetEntryLabel());
3239 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003240 }
3241 __ j(kEqual, slow_path->GetEntryLabel());
3242}
3243
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003244void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3245 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3246 GenerateImplicitNullCheck(instruction);
3247 } else {
3248 GenerateExplicitNullCheck(instruction);
3249 }
3250}
3251
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003252void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003253 LocationSummary* locations =
3254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003255 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003256 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003257 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003259}
3260
3261void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3262 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003263 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 Location index = locations->InAt(1);
3265
3266 switch (instruction->GetType()) {
3267 case Primitive::kPrimBoolean: {
3268 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003269 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003270 if (index.IsConstant()) {
3271 __ movzxb(out, Address(obj,
3272 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3273 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003274 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003275 }
3276 break;
3277 }
3278
3279 case Primitive::kPrimByte: {
3280 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003281 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282 if (index.IsConstant()) {
3283 __ movsxb(out, Address(obj,
3284 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3285 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003286 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003287 }
3288 break;
3289 }
3290
3291 case Primitive::kPrimShort: {
3292 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003294 if (index.IsConstant()) {
3295 __ movsxw(out, Address(obj,
3296 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3297 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003298 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003299 }
3300 break;
3301 }
3302
3303 case Primitive::kPrimChar: {
3304 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003305 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003306 if (index.IsConstant()) {
3307 __ movzxw(out, Address(obj,
3308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3309 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003310 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003311 }
3312 break;
3313 }
3314
3315 case Primitive::kPrimInt:
3316 case Primitive::kPrimNot: {
3317 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003320 if (index.IsConstant()) {
3321 __ movl(out, Address(obj,
3322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3323 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003325 }
3326 break;
3327 }
3328
3329 case Primitive::kPrimLong: {
3330 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003332 if (index.IsConstant()) {
3333 __ movq(out, Address(obj,
3334 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3335 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 }
3338 break;
3339 }
3340
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003341 case Primitive::kPrimFloat: {
3342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003344 if (index.IsConstant()) {
3345 __ movss(out, Address(obj,
3346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3347 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003348 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003349 }
3350 break;
3351 }
3352
3353 case Primitive::kPrimDouble: {
3354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003356 if (index.IsConstant()) {
3357 __ movsd(out, Address(obj,
3358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3359 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003360 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003361 }
3362 break;
3363 }
3364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003365 case Primitive::kPrimVoid:
3366 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003367 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003368 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003369 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370}
3371
3372void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003373 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003374
3375 bool needs_write_barrier =
3376 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3377 bool needs_runtime_call = instruction->NeedsTypeCheck();
3378
Nicolas Geoffray39468442014-09-02 15:17:15 +01003379 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003380 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3381 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003382 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003383 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3384 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3385 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003386 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003387 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003388 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003389 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3390 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003391 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003392 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003393 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3394 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003395 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003396 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003397 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003398
3399 if (needs_write_barrier) {
3400 // Temporary registers for the write barrier.
3401 locations->AddTemp(Location::RequiresRegister());
3402 locations->AddTemp(Location::RequiresRegister());
3403 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003404 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405}
3406
3407void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3408 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003409 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003410 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003411 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003412 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003413 bool needs_runtime_call = locations->WillCall();
3414 bool needs_write_barrier =
3415 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003416
3417 switch (value_type) {
3418 case Primitive::kPrimBoolean:
3419 case Primitive::kPrimByte: {
3420 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003421 if (index.IsConstant()) {
3422 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003423 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003424 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003425 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003426 __ movb(Address(obj, offset),
3427 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003428 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003429 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003430 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003431 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3432 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003433 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003434 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003435 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3436 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003437 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003438 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003439 break;
3440 }
3441
3442 case Primitive::kPrimShort:
3443 case Primitive::kPrimChar: {
3444 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445 if (index.IsConstant()) {
3446 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003447 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003449 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003450 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003451 __ movw(Address(obj, offset),
3452 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003453 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003454 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003455 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003456 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3458 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003459 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003460 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003461 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003462 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3463 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003465 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466 break;
3467 }
3468
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003469 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003470 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003471 if (!needs_runtime_call) {
3472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3473 if (index.IsConstant()) {
3474 size_t offset =
3475 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3476 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003477 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003478 } else {
3479 DCHECK(value.IsConstant()) << value;
3480 __ movl(Address(obj, offset),
3481 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3482 }
3483 } else {
3484 DCHECK(index.IsRegister()) << index;
3485 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3487 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003488 } else {
3489 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003490 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003491 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3492 }
3493 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003494 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003495 if (needs_write_barrier) {
3496 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003497 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3498 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3499 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003500 }
3501 } else {
3502 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003503 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3504 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003505 DCHECK(!codegen_->IsLeafMethod());
3506 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3507 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003508 break;
3509 }
3510
3511 case Primitive::kPrimLong: {
3512 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003513 if (index.IsConstant()) {
3514 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003515 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003516 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003517 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003518 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003519 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3520 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003521 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003522 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003523 break;
3524 }
3525
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003526 case Primitive::kPrimFloat: {
3527 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3528 if (index.IsConstant()) {
3529 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3530 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003531 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003532 } else {
3533 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3535 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003536 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003537 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003538 break;
3539 }
3540
3541 case Primitive::kPrimDouble: {
3542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3543 if (index.IsConstant()) {
3544 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3545 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003546 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003547 } else {
3548 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3550 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003551 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003552 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003553 break;
3554 }
3555
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003556 case Primitive::kPrimVoid:
3557 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003558 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003559 }
3560}
3561
3562void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003563 LocationSummary* locations =
3564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003565 locations->SetInAt(0, Location::RequiresRegister());
3566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567}
3568
3569void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3570 LocationSummary* locations = instruction->GetLocations();
3571 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3573 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003574 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003575 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003576}
3577
3578void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003579 LocationSummary* locations =
3580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003581 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003582 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003583 if (instruction->HasUses()) {
3584 locations->SetOut(Location::SameAsFirstInput());
3585 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003586}
3587
3588void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3589 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003590 Location index_loc = locations->InAt(0);
3591 Location length_loc = locations->InAt(1);
3592 SlowPathCodeX86_64* slow_path =
3593 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003594 codegen_->AddSlowPath(slow_path);
3595
Mark Mendellf60c90b2015-03-04 15:12:59 -05003596 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3597 if (index_loc.IsConstant()) {
3598 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3599 __ cmpl(length, Immediate(value));
3600 } else {
3601 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3602 }
3603 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604}
3605
3606void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3607 CpuRegister card,
3608 CpuRegister object,
3609 CpuRegister value) {
3610 Label is_null;
3611 __ testl(value, value);
3612 __ j(kEqual, &is_null);
3613 __ gs()->movq(card, Address::Absolute(
3614 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3615 __ movq(temp, object);
3616 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3617 __ movb(Address(temp, card, TIMES_1, 0), card);
3618 __ Bind(&is_null);
3619}
3620
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003621void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3622 temp->SetLocations(nullptr);
3623}
3624
3625void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3626 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003627 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003628}
3629
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003630void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003631 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003632 LOG(FATAL) << "Unimplemented";
3633}
3634
3635void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003636 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3637}
3638
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003639void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3641}
3642
3643void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003644 HBasicBlock* block = instruction->GetBlock();
3645 if (block->GetLoopInformation() != nullptr) {
3646 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3647 // The back edge will generate the suspend check.
3648 return;
3649 }
3650 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3651 // The goto will generate the suspend check.
3652 return;
3653 }
3654 GenerateSuspendCheck(instruction, nullptr);
3655}
3656
3657void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3658 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003659 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003660 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003661 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003662 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003663 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003664 if (successor == nullptr) {
3665 __ j(kNotEqual, slow_path->GetEntryLabel());
3666 __ Bind(slow_path->GetReturnLabel());
3667 } else {
3668 __ j(kEqual, codegen_->GetLabelOf(successor));
3669 __ jmp(slow_path->GetEntryLabel());
3670 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003671}
3672
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003673X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3674 return codegen_->GetAssembler();
3675}
3676
3677void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3678 MoveOperands* move = moves_.Get(index);
3679 Location source = move->GetSource();
3680 Location destination = move->GetDestination();
3681
3682 if (source.IsRegister()) {
3683 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003685 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003686 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003688 } else {
3689 DCHECK(destination.IsDoubleStackSlot());
3690 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003692 }
3693 } else if (source.IsStackSlot()) {
3694 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003696 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003697 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003698 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003699 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003700 } else {
3701 DCHECK(destination.IsStackSlot());
3702 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3703 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3704 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003705 } else if (source.IsDoubleStackSlot()) {
3706 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003707 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003708 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003709 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003710 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3711 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003712 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003713 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003714 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3715 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3716 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003717 } else if (source.IsConstant()) {
3718 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003719 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3720 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003721 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003722 if (value == 0) {
3723 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3724 } else {
3725 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3726 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003727 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003728 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003729 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003730 }
3731 } else if (constant->IsLongConstant()) {
3732 int64_t value = constant->AsLongConstant()->GetValue();
3733 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003734 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003735 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003736 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003737 __ movq(CpuRegister(TMP), Immediate(value));
3738 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3739 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003740 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003741 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003742 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003743 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003744 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003745 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3746 if (value == 0) {
3747 // easy FP 0.0.
3748 __ xorps(dest, dest);
3749 } else {
3750 __ movl(CpuRegister(TMP), imm);
3751 __ movd(dest, CpuRegister(TMP));
3752 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003753 } else {
3754 DCHECK(destination.IsStackSlot()) << destination;
3755 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3756 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003757 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003758 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003759 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003760 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003761 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003762 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003763 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3764 if (value == 0) {
3765 __ xorpd(dest, dest);
3766 } else {
3767 __ movq(CpuRegister(TMP), imm);
3768 __ movd(dest, CpuRegister(TMP));
3769 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003770 } else {
3771 DCHECK(destination.IsDoubleStackSlot()) << destination;
3772 __ movq(CpuRegister(TMP), imm);
3773 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3774 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003775 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003776 } else if (source.IsFpuRegister()) {
3777 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003778 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003779 } else if (destination.IsStackSlot()) {
3780 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003781 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003782 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003783 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003784 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003785 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003786 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003787 }
3788}
3789
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003790void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003791 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003792 __ movl(Address(CpuRegister(RSP), mem), reg);
3793 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003794}
3795
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003796void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003797 ScratchRegisterScope ensure_scratch(
3798 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3799
3800 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3801 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3802 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3803 Address(CpuRegister(RSP), mem2 + stack_offset));
3804 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3805 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3806 CpuRegister(ensure_scratch.GetRegister()));
3807}
3808
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003809void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3810 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3811 __ movq(Address(CpuRegister(RSP), mem), reg);
3812 __ movq(reg, CpuRegister(TMP));
3813}
3814
3815void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3816 ScratchRegisterScope ensure_scratch(
3817 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3818
3819 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3820 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3821 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3822 Address(CpuRegister(RSP), mem2 + stack_offset));
3823 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3824 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3825 CpuRegister(ensure_scratch.GetRegister()));
3826}
3827
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003828void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3829 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3830 __ movss(Address(CpuRegister(RSP), mem), reg);
3831 __ movd(reg, CpuRegister(TMP));
3832}
3833
3834void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3835 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3836 __ movsd(Address(CpuRegister(RSP), mem), reg);
3837 __ movd(reg, CpuRegister(TMP));
3838}
3839
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003840void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3841 MoveOperands* move = moves_.Get(index);
3842 Location source = move->GetSource();
3843 Location destination = move->GetDestination();
3844
3845 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003846 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003847 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003848 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003849 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003851 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003852 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3853 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003854 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003855 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003856 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003857 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3858 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003859 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003860 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3861 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3862 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003863 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003865 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003867 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003869 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003870 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003871 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003872 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003873 }
3874}
3875
3876
3877void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3878 __ pushq(CpuRegister(reg));
3879}
3880
3881
3882void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3883 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003884}
3885
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003886void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3887 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3888 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3889 Immediate(mirror::Class::kStatusInitialized));
3890 __ j(kLess, slow_path->GetEntryLabel());
3891 __ Bind(slow_path->GetExitLabel());
3892 // No need for memory fence, thanks to the X86_64 memory model.
3893}
3894
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003895void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003896 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3897 ? LocationSummary::kCallOnSlowPath
3898 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003899 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003900 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003901 locations->SetOut(Location::RequiresRegister());
3902}
3903
3904void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003905 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003906 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003907 DCHECK(!cls->CanCallRuntime());
3908 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003909 codegen_->LoadCurrentMethod(out);
3910 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3911 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003912 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003913 codegen_->LoadCurrentMethod(out);
3914 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3915 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003916 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3917 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3918 codegen_->AddSlowPath(slow_path);
3919 __ testl(out, out);
3920 __ j(kEqual, slow_path->GetEntryLabel());
3921 if (cls->MustGenerateClinitCheck()) {
3922 GenerateClassInitializationCheck(slow_path, out);
3923 } else {
3924 __ Bind(slow_path->GetExitLabel());
3925 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003926 }
3927}
3928
3929void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3930 LocationSummary* locations =
3931 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3932 locations->SetInAt(0, Location::RequiresRegister());
3933 if (check->HasUses()) {
3934 locations->SetOut(Location::SameAsFirstInput());
3935 }
3936}
3937
3938void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003939 // We assume the class to not be null.
3940 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3941 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003942 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003943 GenerateClassInitializationCheck(slow_path,
3944 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003945}
3946
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003947void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3950 locations->SetOut(Location::RequiresRegister());
3951}
3952
3953void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3954 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3955 codegen_->AddSlowPath(slow_path);
3956
Roland Levillain271ab9c2014-11-27 15:23:57 +00003957 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003958 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003959 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3960 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003961 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3962 __ testl(out, out);
3963 __ j(kEqual, slow_path->GetEntryLabel());
3964 __ Bind(slow_path->GetExitLabel());
3965}
3966
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003967void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3968 LocationSummary* locations =
3969 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3970 locations->SetOut(Location::RequiresRegister());
3971}
3972
3973void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3974 Address address = Address::Absolute(
3975 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003976 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003977 __ gs()->movl(address, Immediate(0));
3978}
3979
3980void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3981 LocationSummary* locations =
3982 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3983 InvokeRuntimeCallingConvention calling_convention;
3984 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3985}
3986
3987void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3988 __ gs()->call(
3989 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3990 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3991}
3992
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003993void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003994 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3995 ? LocationSummary::kNoCall
3996 : LocationSummary::kCallOnSlowPath;
3997 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3998 locations->SetInAt(0, Location::RequiresRegister());
3999 locations->SetInAt(1, Location::Any());
4000 locations->SetOut(Location::RequiresRegister());
4001}
4002
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004003void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004004 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004005 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004006 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004007 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004008 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4009 Label done, zero;
4010 SlowPathCodeX86_64* slow_path = nullptr;
4011
4012 // Return 0 if `obj` is null.
4013 // TODO: avoid this check if we know obj is not null.
4014 __ testl(obj, obj);
4015 __ j(kEqual, &zero);
4016 // Compare the class of `obj` with `cls`.
4017 __ movl(out, Address(obj, class_offset));
4018 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004019 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004020 } else {
4021 DCHECK(cls.IsStackSlot()) << cls;
4022 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4023 }
4024 if (instruction->IsClassFinal()) {
4025 // Classes must be equal for the instanceof to succeed.
4026 __ j(kNotEqual, &zero);
4027 __ movl(out, Immediate(1));
4028 __ jmp(&done);
4029 } else {
4030 // If the classes are not equal, we go into a slow path.
4031 DCHECK(locations->OnlyCallsOnSlowPath());
4032 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004033 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004034 codegen_->AddSlowPath(slow_path);
4035 __ j(kNotEqual, slow_path->GetEntryLabel());
4036 __ movl(out, Immediate(1));
4037 __ jmp(&done);
4038 }
4039 __ Bind(&zero);
4040 __ movl(out, Immediate(0));
4041 if (slow_path != nullptr) {
4042 __ Bind(slow_path->GetExitLabel());
4043 }
4044 __ Bind(&done);
4045}
4046
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004047void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4049 instruction, LocationSummary::kCallOnSlowPath);
4050 locations->SetInAt(0, Location::RequiresRegister());
4051 locations->SetInAt(1, Location::Any());
4052 locations->AddTemp(Location::RequiresRegister());
4053}
4054
4055void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4056 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004057 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004058 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004059 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004060 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4061 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4062 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4063 codegen_->AddSlowPath(slow_path);
4064
4065 // TODO: avoid this check if we know obj is not null.
4066 __ testl(obj, obj);
4067 __ j(kEqual, slow_path->GetExitLabel());
4068 // Compare the class of `obj` with `cls`.
4069 __ movl(temp, Address(obj, class_offset));
4070 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004072 } else {
4073 DCHECK(cls.IsStackSlot()) << cls;
4074 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4075 }
4076 // Classes must be equal for the checkcast to succeed.
4077 __ j(kNotEqual, slow_path->GetEntryLabel());
4078 __ Bind(slow_path->GetExitLabel());
4079}
4080
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004081void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4082 LocationSummary* locations =
4083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4084 InvokeRuntimeCallingConvention calling_convention;
4085 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4086}
4087
4088void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4089 __ gs()->call(Address::Absolute(instruction->IsEnter()
4090 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4091 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4092 true));
4093 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4094}
4095
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004096void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4097void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4098void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4099
4100void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4101 LocationSummary* locations =
4102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4103 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4104 || instruction->GetResultType() == Primitive::kPrimLong);
4105 locations->SetInAt(0, Location::RequiresRegister());
4106 if (instruction->GetType() == Primitive::kPrimInt) {
4107 locations->SetInAt(1, Location::Any());
4108 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004109 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004110 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004111 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004112 }
4113 locations->SetOut(Location::SameAsFirstInput());
4114}
4115
4116void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4117 HandleBitwiseOperation(instruction);
4118}
4119
4120void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4121 HandleBitwiseOperation(instruction);
4122}
4123
4124void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4125 HandleBitwiseOperation(instruction);
4126}
4127
4128void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4129 LocationSummary* locations = instruction->GetLocations();
4130 Location first = locations->InAt(0);
4131 Location second = locations->InAt(1);
4132 DCHECK(first.Equals(locations->Out()));
4133
4134 if (instruction->GetResultType() == Primitive::kPrimInt) {
4135 if (second.IsRegister()) {
4136 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004137 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004138 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004139 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004140 } else {
4141 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004142 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004143 }
4144 } else if (second.IsConstant()) {
4145 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4146 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004147 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004148 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004149 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004150 } else {
4151 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004152 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004153 }
4154 } else {
4155 Address address(CpuRegister(RSP), second.GetStackIndex());
4156 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004157 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004158 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004159 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004160 } else {
4161 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004162 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004163 }
4164 }
4165 } else {
4166 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004167 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4168 bool second_is_constant = false;
4169 int64_t value = 0;
4170 if (second.IsConstant()) {
4171 second_is_constant = true;
4172 value = second.GetConstant()->AsLongConstant()->GetValue();
4173 DCHECK(IsInt<32>(value));
4174 }
4175
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004176 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004177 if (second_is_constant) {
4178 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4179 } else {
4180 __ andq(first_reg, second.AsRegister<CpuRegister>());
4181 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004182 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004183 if (second_is_constant) {
4184 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4185 } else {
4186 __ orq(first_reg, second.AsRegister<CpuRegister>());
4187 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004188 } else {
4189 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004190 if (second_is_constant) {
4191 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4192 } else {
4193 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4194 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004195 }
4196 }
4197}
4198
Calin Juravleb1498f62015-02-16 13:13:29 +00004199void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4200 // Nothing to do, this should be removed during prepare for register allocator.
4201 UNUSED(instruction);
4202 LOG(FATAL) << "Unreachable";
4203}
4204
4205void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4206 // Nothing to do, this should be removed during prepare for register allocator.
4207 UNUSED(instruction);
4208 LOG(FATAL) << "Unreachable";
4209}
4210
Mark Mendellf55c3e02015-03-26 21:07:46 -04004211void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4212 // Generate the constant area if needed.
4213 if (!__ IsConstantAreaEmpty()) {
4214 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4215 // byte values. If used for vectors at a later time, this will need to be
4216 // updated to 16 bytes with the appropriate offset.
4217 __ Align(4, 0);
4218 constant_area_start_ = __ CodeSize();
4219 __ AddConstantArea();
4220 }
4221
4222 // And finish up.
4223 CodeGenerator::Finalize(allocator);
4224}
4225
4226/**
4227 * Class to handle late fixup of offsets into constant area.
4228 */
4229class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4230 public:
4231 RIPFixup(CodeGeneratorX86_64& codegen, int offset)
4232 : codegen_(codegen), offset_into_constant_area_(offset) {}
4233
4234 private:
4235 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4236 // Patch the correct offset for the instruction. We use the address of the
4237 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4238 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4239 int relative_position = constant_offset - pos;
4240
4241 // Patch in the right value.
4242 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4243 }
4244
4245 CodeGeneratorX86_64& codegen_;
4246
4247 // Location in constant area that the fixup refers to.
4248 int offset_into_constant_area_;
4249};
4250
4251Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4252 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4253 return Address::RIP(fixup);
4254}
4255
4256Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4257 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4258 return Address::RIP(fixup);
4259}
4260
4261Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4262 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4263 return Address::RIP(fixup);
4264}
4265
4266Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4267 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4268 return Address::RIP(fixup);
4269}
4270
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004271} // namespace x86_64
4272} // namespace art