blob: 5ac68668ba18567e56c85e356a418cd32f8b30da [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080022#include "intrinsics.h"
23#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010025#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010027#include "mirror/object_reference.h"
28#include "thread.h"
29#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/managed_register_x86_64.h"
33
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010034namespace art {
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036namespace x86_64 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038// Some x86_64 instructions require a register to be available as temp.
39static constexpr Register TMP = R11;
40
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000043static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
50
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010051class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Alexandre Rames2ed20af2015-03-06 13:55:35 +000055 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010057 __ gs()->call(
58 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000059 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 }
61
62 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010063 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
65};
66
Calin Juravled0d48522014-11-04 16:40:20 +000067class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
68 public:
69 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
70
Alexandre Rames2ed20af2015-03-06 13:55:35 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000072 __ Bind(GetEntryLabel());
73 __ gs()->call(
74 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000075 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000076 }
77
78 private:
79 HDivZeroCheck* const instruction_;
80 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
81};
82
Calin Juravlebacfec32014-11-14 15:54:36 +000083class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000085 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
86 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +000090 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +000091 if (is_div_) {
92 __ negl(cpu_reg_);
93 } else {
94 __ movl(cpu_reg_, Immediate(0));
95 }
96
Calin Juravled6fb6cf2014-11-11 19:07:44 +000097 } else {
98 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +000099 if (is_div_) {
100 __ negq(cpu_reg_);
101 } else {
102 __ movq(cpu_reg_, Immediate(0));
103 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000104 }
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ jmp(GetExitLabel());
106 }
107
108 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000110 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000111 const bool is_div_;
112 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000113};
114
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100117 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000124 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000125 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ jmp(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
139 private:
140 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100141 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 Label return_label_;
143
144 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
145};
146
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100149 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
150 Location index_location,
151 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 : instruction_(instruction),
153 index_location_(index_location),
154 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000158 // We're moving two locations to locations that could overlap, so we need a parallel
159 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 codegen->EmitParallelMoves(
162 index_location_,
163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100164 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 __ gs()->call(Address::Absolute(
169 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000170 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 }
172
173 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100174 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175 const Location index_location_;
176 const Location length_location_;
177
178 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
179};
180
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000181class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100182 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000183 LoadClassSlowPathX86_64(HLoadClass* cls,
184 HInstruction* at,
185 uint32_t dex_pc,
186 bool do_clinit)
187 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
188 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
189 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000191 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000192 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
194 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100195
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000196 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000199 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 __ gs()->call(Address::Absolute((do_clinit_
201 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
202 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000203 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000205 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000207 if (out.IsValid()) {
208 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
209 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 }
211
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000212 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213 __ jmp(GetExitLabel());
214 }
215
216 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 // The class this slow path will load.
218 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 // The instruction where this slow path is happening.
221 // (Might be the load class or an initialization check).
222 HInstruction* const at_;
223
224 // The dex PC of `at_`.
225 const uint32_t dex_pc_;
226
227 // Whether to initialize the class.
228 const bool do_clinit_;
229
230 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231};
232
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000233class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
234 public:
235 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
236
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000237 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238 LocationSummary* locations = instruction_->GetLocations();
239 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
240
241 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
242 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000243 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000244
245 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 __ 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)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100288 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000289 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100290 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
291 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 if (instruction_->IsInstanceOf()) {
294 __ gs()->call(
295 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
296 } else {
297 DCHECK(instruction_->IsCheckCast());
298 __ gs()->call(
299 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
300 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000301 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302
303 if (instruction_->IsInstanceOf()) {
304 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
305 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000307 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308 __ jmp(GetExitLabel());
309 }
310
311 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000312 HInstruction* const instruction_;
313 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316
317 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
318};
319
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700320class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
321 public:
322 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
323 : instruction_(instruction) {}
324
325 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
326 __ Bind(GetEntryLabel());
327 SaveLiveRegisters(codegen, instruction_->GetLocations());
328 __ gs()->call(
329 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeoptimize), true));
330 DCHECK(instruction_->IsDeoptimize());
331 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
332 uint32_t dex_pc = deoptimize->GetDexPc();
333 codegen->RecordPcInfo(instruction_, dex_pc, this);
334 }
335
336 private:
337 HInstruction* const instruction_;
338 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
339};
340
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100341#undef __
342#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
343
Dave Allison20dfc792014-06-16 20:44:29 -0700344inline Condition X86_64Condition(IfCondition cond) {
345 switch (cond) {
346 case kCondEQ: return kEqual;
347 case kCondNE: return kNotEqual;
348 case kCondLT: return kLess;
349 case kCondLE: return kLessEqual;
350 case kCondGT: return kGreater;
351 case kCondGE: return kGreaterEqual;
352 default:
353 LOG(FATAL) << "Unknown if condition";
354 }
355 return kEqual;
356}
357
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800358void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
359 CpuRegister temp) {
360 // All registers are assumed to be correctly set up.
361
362 // TODO: Implement all kinds of calls:
363 // 1) boot -> boot
364 // 2) app -> boot
365 // 3) app -> app
366 //
367 // Currently we implement the app -> app logic, which looks up in the resolve cache.
368
Jeff Hao848f70a2014-01-15 13:49:50 -0800369 if (invoke->IsStringInit()) {
370 // temp = thread->string_init_entrypoint
371 __ gs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000372 // (temp + offset_of_quick_compiled_code)()
373 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
374 kX86_64WordSize).SizeValue()));
375 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -0800376 // temp = method;
377 LoadCurrentMethod(temp);
378 if (!invoke->IsRecursive()) {
379 // temp = temp->dex_cache_resolved_methods_;
380 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
381 // temp = temp[index_in_cache]
382 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
383 // (temp + offset_of_quick_compiled_code)()
384 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
385 kX86_64WordSize).SizeValue()));
386 } else {
387 __ call(&frame_entry_label_);
388 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000389 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800390
391 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800392}
393
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100394void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
395 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
396}
397
398void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
399 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
400}
401
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100402size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
403 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
404 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100405}
406
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100407size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
408 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
409 return kX86_64WordSize;
410}
411
412size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
413 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
414 return kX86_64WordSize;
415}
416
417size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
418 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
419 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100420}
421
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000422static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000423// Use a fake return address register to mimic Quick.
424static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400425CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
426 const X86_64InstructionSetFeatures& isa_features,
427 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000428 : CodeGenerator(graph,
429 kNumberOfCpuRegisters,
430 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000431 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000432 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
433 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000434 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000435 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
436 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000437 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100438 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100439 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000440 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400441 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400442 isa_features_(isa_features),
443 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000444 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
445}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100446
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100447InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
448 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100449 : HGraphVisitor(graph),
450 assembler_(codegen->GetAssembler()),
451 codegen_(codegen) {}
452
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100453Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454 switch (type) {
455 case Primitive::kPrimLong:
456 case Primitive::kPrimByte:
457 case Primitive::kPrimBoolean:
458 case Primitive::kPrimChar:
459 case Primitive::kPrimShort:
460 case Primitive::kPrimInt:
461 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100462 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100463 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100464 }
465
466 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100467 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100468 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100469 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100470 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100471
472 case Primitive::kPrimVoid:
473 LOG(FATAL) << "Unreachable type " << type;
474 }
475
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100476 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100477}
478
Nicolas Geoffray98893962015-01-21 12:32:32 +0000479void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100480 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100481 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100482
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000483 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100484 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000485
Nicolas Geoffray98893962015-01-21 12:32:32 +0000486 if (is_baseline) {
487 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
488 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
489 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000490 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
491 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
492 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000493 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100494}
495
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100496static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100497 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100498}
David Srbecky9d8606d2015-04-12 09:35:32 +0100499
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100500static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100501 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100502}
503
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100504void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100505 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000506 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100507 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700508 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000509 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100510
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000511 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100512 __ testq(CpuRegister(RAX), Address(
513 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100514 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100515 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000516
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000517 if (HasEmptyFrame()) {
518 return;
519 }
520
Nicolas Geoffray98893962015-01-21 12:32:32 +0000521 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000522 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000523 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000524 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100525 __ cfi().AdjustCFAOffset(kX86_64WordSize);
526 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000527 }
528 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100529
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100530 int adjust = GetFrameSize() - GetCoreSpillSize();
531 __ subq(CpuRegister(RSP), Immediate(adjust));
532 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533 uint32_t xmm_spill_location = GetFpuSpillStart();
534 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100535
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000536 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
537 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100538 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
539 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
540 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000541 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100542 }
543
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
545}
546
547void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100548 __ cfi().RememberState();
549 if (!HasEmptyFrame()) {
550 uint32_t xmm_spill_location = GetFpuSpillStart();
551 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
552 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
553 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
554 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
555 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
556 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
557 }
558 }
559
560 int adjust = GetFrameSize() - GetCoreSpillSize();
561 __ addq(CpuRegister(RSP), Immediate(adjust));
562 __ cfi().AdjustCFAOffset(-adjust);
563
564 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
565 Register reg = kCoreCalleeSaves[i];
566 if (allocated_registers_.ContainsCoreRegister(reg)) {
567 __ popq(CpuRegister(reg));
568 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
569 __ cfi().Restore(DWARFReg(reg));
570 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000571 }
572 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100573 __ ret();
574 __ cfi().RestoreState();
575 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576}
577
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100578void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
579 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580}
581
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100582void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000583 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
585}
586
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100587Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
588 switch (load->GetType()) {
589 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100592
593 case Primitive::kPrimInt:
594 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100595 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100597
598 case Primitive::kPrimBoolean:
599 case Primitive::kPrimByte:
600 case Primitive::kPrimChar:
601 case Primitive::kPrimShort:
602 case Primitive::kPrimVoid:
603 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700604 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605 }
606
607 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700608 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609}
610
611void CodeGeneratorX86_64::Move(Location destination, Location source) {
612 if (source.Equals(destination)) {
613 return;
614 }
615 if (destination.IsRegister()) {
616 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100620 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100623 } else {
624 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000625 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 Address(CpuRegister(RSP), source.GetStackIndex()));
627 }
628 } else if (destination.IsFpuRegister()) {
629 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000632 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100633 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000634 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 Address(CpuRegister(RSP), source.GetStackIndex()));
636 } else {
637 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100640 }
641 } else if (destination.IsStackSlot()) {
642 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100643 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000644 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100645 } else if (source.IsFpuRegister()) {
646 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000647 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500648 } else if (source.IsConstant()) {
649 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000650 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500651 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100652 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500653 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000654 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
655 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100656 }
657 } else {
658 DCHECK(destination.IsDoubleStackSlot());
659 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000661 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100662 } else if (source.IsFpuRegister()) {
663 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000664 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500665 } else if (source.IsConstant()) {
666 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800667 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500668 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000669 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500670 } else {
671 DCHECK(constant->IsLongConstant());
672 value = constant->AsLongConstant()->GetValue();
673 }
674 __ movq(CpuRegister(TMP), Immediate(value));
675 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100676 } else {
677 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000678 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
679 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100680 }
681 }
682}
683
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100684void CodeGeneratorX86_64::Move(HInstruction* instruction,
685 Location location,
686 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000687 LocationSummary* locations = instruction->GetLocations();
688 if (locations != nullptr && locations->Out().Equals(location)) {
689 return;
690 }
691
692 if (locations != nullptr && locations->Out().IsConstant()) {
693 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000694 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
695 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000696 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000697 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000698 } else if (location.IsStackSlot()) {
699 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
700 } else {
701 DCHECK(location.IsConstant());
702 DCHECK_EQ(location.GetConstant(), const_to_move);
703 }
704 } else if (const_to_move->IsLongConstant()) {
705 int64_t value = const_to_move->AsLongConstant()->GetValue();
706 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000707 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000708 } else if (location.IsDoubleStackSlot()) {
709 __ movq(CpuRegister(TMP), Immediate(value));
710 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
711 } else {
712 DCHECK(location.IsConstant());
713 DCHECK_EQ(location.GetConstant(), const_to_move);
714 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100715 }
Roland Levillain476df552014-10-09 17:51:36 +0100716 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 switch (instruction->GetType()) {
718 case Primitive::kPrimBoolean:
719 case Primitive::kPrimByte:
720 case Primitive::kPrimChar:
721 case Primitive::kPrimShort:
722 case Primitive::kPrimInt:
723 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
726 break;
727
728 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000730 Move(location,
731 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732 break;
733
734 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000737 } else if (instruction->IsTemporary()) {
738 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
739 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100740 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100741 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742 switch (instruction->GetType()) {
743 case Primitive::kPrimBoolean:
744 case Primitive::kPrimByte:
745 case Primitive::kPrimChar:
746 case Primitive::kPrimShort:
747 case Primitive::kPrimInt:
748 case Primitive::kPrimNot:
749 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100750 case Primitive::kPrimFloat:
751 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000752 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100753 break;
754
755 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100756 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100757 }
758 }
759}
760
761void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
762 got->SetLocations(nullptr);
763}
764
765void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
766 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100767 DCHECK(!successor->IsExitBlock());
768
769 HBasicBlock* block = got->GetBlock();
770 HInstruction* previous = got->GetPrevious();
771
772 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000773 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100774 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
775 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
776 return;
777 }
778
779 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
780 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
781 }
782 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100783 __ jmp(codegen_->GetLabelOf(successor));
784 }
785}
786
787void LocationsBuilderX86_64::VisitExit(HExit* exit) {
788 exit->SetLocations(nullptr);
789}
790
791void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700792 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100793}
794
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700795void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
796 Label* true_target,
797 Label* false_target,
798 Label* always_true_target) {
799 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100800 if (cond->IsIntConstant()) {
801 // Constant condition, statically compared against 1.
802 int32_t cond_value = cond->AsIntConstant()->GetValue();
803 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700804 if (always_true_target != nullptr) {
805 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100806 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100808 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100809 DCHECK_EQ(cond_value, 0);
810 }
811 } else {
812 bool materialized =
813 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
814 // Moves do not affect the eflags register, so if the condition is
815 // evaluated just before the if, we don't need to evaluate it
816 // again.
817 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700818 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 if (materialized) {
820 if (!eflags_set) {
821 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700822 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100823 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000824 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 } else {
826 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
827 Immediate(0));
828 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700829 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700831 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 }
833 } else {
834 Location lhs = cond->GetLocations()->InAt(0);
835 Location rhs = cond->GetLocations()->InAt(1);
836 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000837 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000839 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000840 if (constant == 0) {
841 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
842 } else {
843 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
844 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100845 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000846 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 Address(CpuRegister(RSP), rhs.GetStackIndex()));
848 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700849 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700850 }
Dave Allison20dfc792014-06-16 20:44:29 -0700851 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700852 if (false_target != nullptr) {
853 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100854 }
855}
856
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700857void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
858 LocationSummary* locations =
859 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
860 HInstruction* cond = if_instr->InputAt(0);
861 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
862 locations->SetInAt(0, Location::Any());
863 }
864}
865
866void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
867 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
868 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
869 Label* always_true_target = true_target;
870 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
871 if_instr->IfTrueSuccessor())) {
872 always_true_target = nullptr;
873 }
874 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
875 if_instr->IfFalseSuccessor())) {
876 false_target = nullptr;
877 }
878 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
879}
880
881void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
882 LocationSummary* locations = new (GetGraph()->GetArena())
883 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
884 HInstruction* cond = deoptimize->InputAt(0);
885 DCHECK(cond->IsCondition());
886 if (cond->AsCondition()->NeedsMaterialization()) {
887 locations->SetInAt(0, Location::Any());
888 }
889}
890
891void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
892 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
893 DeoptimizationSlowPathX86_64(deoptimize);
894 codegen_->AddSlowPath(slow_path);
895 Label* slow_path_entry = slow_path->GetEntryLabel();
896 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
897}
898
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100899void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
900 local->SetLocations(nullptr);
901}
902
903void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
904 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
905}
906
907void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
908 local->SetLocations(nullptr);
909}
910
911void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
912 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700913 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100914}
915
916void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100917 LocationSummary* locations =
918 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100919 switch (store->InputAt(1)->GetType()) {
920 case Primitive::kPrimBoolean:
921 case Primitive::kPrimByte:
922 case Primitive::kPrimChar:
923 case Primitive::kPrimShort:
924 case Primitive::kPrimInt:
925 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100926 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
928 break;
929
930 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100931 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100932 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
933 break;
934
935 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100936 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100937 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100938}
939
940void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700941 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942}
943
Dave Allison20dfc792014-06-16 20:44:29 -0700944void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100945 LocationSummary* locations =
946 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100947 locations->SetInAt(0, Location::RequiresRegister());
948 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100949 if (comp->NeedsMaterialization()) {
950 locations->SetOut(Location::RequiresRegister());
951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952}
953
Dave Allison20dfc792014-06-16 20:44:29 -0700954void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
955 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100956 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000957 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100958 // Clear register: setcc only sets the low byte.
959 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000960 Location lhs = locations->InAt(0);
961 Location rhs = locations->InAt(1);
962 if (rhs.IsRegister()) {
963 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
964 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800965 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000966 if (constant == 0) {
967 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
968 } else {
969 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
970 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100971 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000972 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100973 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100974 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700975 }
976}
977
978void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
979 VisitCondition(comp);
980}
981
982void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
983 VisitCondition(comp);
984}
985
986void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
987 VisitCondition(comp);
988}
989
990void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
991 VisitCondition(comp);
992}
993
994void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
995 VisitCondition(comp);
996}
997
998void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
999 VisitCondition(comp);
1000}
1001
1002void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1003 VisitCondition(comp);
1004}
1005
1006void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1007 VisitCondition(comp);
1008}
1009
1010void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1011 VisitCondition(comp);
1012}
1013
1014void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1015 VisitCondition(comp);
1016}
1017
1018void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1019 VisitCondition(comp);
1020}
1021
1022void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1023 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001024}
1025
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001026void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001027 LocationSummary* locations =
1028 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001029 switch (compare->InputAt(0)->GetType()) {
1030 case Primitive::kPrimLong: {
1031 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001032 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1034 break;
1035 }
1036 case Primitive::kPrimFloat:
1037 case Primitive::kPrimDouble: {
1038 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001039 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001040 locations->SetOut(Location::RequiresRegister());
1041 break;
1042 }
1043 default:
1044 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1045 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001046}
1047
1048void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001049 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001050 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001051 Location left = locations->InAt(0);
1052 Location right = locations->InAt(1);
1053
1054 Label less, greater, done;
1055 Primitive::Type type = compare->InputAt(0)->GetType();
1056 switch (type) {
1057 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001058 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1059 if (right.IsConstant()) {
1060 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001061 if (IsInt<32>(value)) {
1062 if (value == 0) {
1063 __ testq(left_reg, left_reg);
1064 } else {
1065 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1066 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001067 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001068 // Value won't fit in an int.
1069 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001070 }
Mark Mendell40741f32015-04-20 22:10:34 -04001071 } else if (right.IsDoubleStackSlot()) {
1072 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001073 } else {
1074 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1075 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001076 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001077 }
1078 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001079 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1080 if (right.IsConstant()) {
1081 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1082 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1083 } else if (right.IsStackSlot()) {
1084 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1085 } else {
1086 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1087 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001088 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1089 break;
1090 }
1091 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001092 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1093 if (right.IsConstant()) {
1094 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1095 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1096 } else if (right.IsDoubleStackSlot()) {
1097 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1098 } else {
1099 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1100 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001101 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1102 break;
1103 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001104 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001105 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001106 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001107 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001108 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001109 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001110
Calin Juravle91debbc2014-11-26 19:01:09 +00001111 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001112 __ movl(out, Immediate(1));
1113 __ jmp(&done);
1114
1115 __ Bind(&less);
1116 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001117
1118 __ Bind(&done);
1119}
1120
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001121void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001122 LocationSummary* locations =
1123 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001124 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125}
1126
1127void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001128 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001129 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130}
1131
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001132void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1135 locations->SetOut(Location::ConstantLocation(constant));
1136}
1137
1138void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1139 // Will be generated at use site.
1140 UNUSED(constant);
1141}
1142
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001143void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001144 LocationSummary* locations =
1145 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001146 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147}
1148
1149void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001150 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001151 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152}
1153
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001154void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1155 LocationSummary* locations =
1156 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1157 locations->SetOut(Location::ConstantLocation(constant));
1158}
1159
1160void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1161 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001162 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001163}
1164
1165void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1166 LocationSummary* locations =
1167 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1168 locations->SetOut(Location::ConstantLocation(constant));
1169}
1170
1171void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1172 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001173 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001174}
1175
Calin Juravle27df7582015-04-17 19:12:31 +01001176void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1177 memory_barrier->SetLocations(nullptr);
1178}
1179
1180void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1181 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1182}
1183
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001184void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1185 ret->SetLocations(nullptr);
1186}
1187
1188void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001189 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191}
1192
1193void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001194 LocationSummary* locations =
1195 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196 switch (ret->InputAt(0)->GetType()) {
1197 case Primitive::kPrimBoolean:
1198 case Primitive::kPrimByte:
1199 case Primitive::kPrimChar:
1200 case Primitive::kPrimShort:
1201 case Primitive::kPrimInt:
1202 case Primitive::kPrimNot:
1203 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001204 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001205 break;
1206
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 case Primitive::kPrimFloat:
1208 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001209 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 break;
1211
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001212 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001213 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001214 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215}
1216
1217void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1218 if (kIsDebugBuild) {
1219 switch (ret->InputAt(0)->GetType()) {
1220 case Primitive::kPrimBoolean:
1221 case Primitive::kPrimByte:
1222 case Primitive::kPrimChar:
1223 case Primitive::kPrimShort:
1224 case Primitive::kPrimInt:
1225 case Primitive::kPrimNot:
1226 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 break;
1229
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 case Primitive::kPrimFloat:
1231 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 XMM0);
1234 break;
1235
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001236 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001238 }
1239 }
1240 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241}
1242
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001243Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001244 switch (type) {
1245 case Primitive::kPrimBoolean:
1246 case Primitive::kPrimByte:
1247 case Primitive::kPrimChar:
1248 case Primitive::kPrimShort:
1249 case Primitive::kPrimInt:
1250 case Primitive::kPrimNot: {
1251 uint32_t index = gp_index_++;
1252 stack_index_++;
1253 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001254 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001255 } else {
1256 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1257 }
1258 }
1259
1260 case Primitive::kPrimLong: {
1261 uint32_t index = gp_index_;
1262 stack_index_ += 2;
1263 if (index < calling_convention.GetNumberOfRegisters()) {
1264 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001265 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266 } else {
1267 gp_index_ += 2;
1268 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1269 }
1270 }
1271
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001273 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001274 stack_index_++;
1275 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001276 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 } else {
1278 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1279 }
1280 }
1281
1282 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001283 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001284 stack_index_ += 2;
1285 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001286 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001287 } else {
1288 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1289 }
1290 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001291
1292 case Primitive::kPrimVoid:
1293 LOG(FATAL) << "Unexpected parameter type " << type;
1294 break;
1295 }
1296 return Location();
1297}
1298
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001299void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001300 // When we do not run baseline, explicit clinit checks triggered by static
1301 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1302 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001303
Mark Mendellfb8d2792015-03-31 22:16:59 -04001304 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001305 if (intrinsic.TryDispatch(invoke)) {
1306 return;
1307 }
1308
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001309 HandleInvoke(invoke);
1310}
1311
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001312static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1313 if (invoke->GetLocations()->Intrinsified()) {
1314 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1315 intrinsic.Dispatch(invoke);
1316 return true;
1317 }
1318 return false;
1319}
1320
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001321void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001322 // When we do not run baseline, explicit clinit checks triggered by static
1323 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1324 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001325
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001326 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1327 return;
1328 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001329
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001330 codegen_->GenerateStaticOrDirectCall(
1331 invoke,
1332 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001333 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001334}
1335
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001336void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001337 LocationSummary* locations =
1338 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001339 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001340
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001341 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001342 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001343 HInstruction* input = invoke->InputAt(i);
1344 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1345 }
1346
1347 switch (invoke->GetType()) {
1348 case Primitive::kPrimBoolean:
1349 case Primitive::kPrimByte:
1350 case Primitive::kPrimChar:
1351 case Primitive::kPrimShort:
1352 case Primitive::kPrimInt:
1353 case Primitive::kPrimNot:
1354 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001355 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001356 break;
1357
1358 case Primitive::kPrimVoid:
1359 break;
1360
1361 case Primitive::kPrimDouble:
1362 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001363 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001364 break;
1365 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001366}
1367
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001368void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001369 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001370 if (intrinsic.TryDispatch(invoke)) {
1371 return;
1372 }
1373
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001374 HandleInvoke(invoke);
1375}
1376
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001377void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001378 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1379 return;
1380 }
1381
Roland Levillain271ab9c2014-11-27 15:23:57 +00001382 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001383 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1384 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1385 LocationSummary* locations = invoke->GetLocations();
1386 Location receiver = locations->InAt(0);
1387 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1388 // temp = object->GetClass();
1389 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001390 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1391 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001392 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001393 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001394 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001395 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001396 // temp = temp->GetMethodAt(method_offset);
1397 __ movl(temp, Address(temp, method_offset));
1398 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001399 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001400 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001401
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001402 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001403 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001404}
1405
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001406void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1407 HandleInvoke(invoke);
1408 // Add the hidden argument.
1409 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1410}
1411
1412void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1413 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001414 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001415 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1416 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1417 LocationSummary* locations = invoke->GetLocations();
1418 Location receiver = locations->InAt(0);
1419 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1420
1421 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001422 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001423 Immediate(invoke->GetDexMethodIndex()));
1424
1425 // temp = object->GetClass();
1426 if (receiver.IsStackSlot()) {
1427 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1428 __ movl(temp, Address(temp, class_offset));
1429 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001430 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001431 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001432 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001433 // temp = temp->GetImtEntryAt(method_offset);
1434 __ movl(temp, Address(temp, method_offset));
1435 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001436 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001437 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001438
1439 DCHECK(!codegen_->IsLeafMethod());
1440 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1441}
1442
Roland Levillain88cb1752014-10-20 16:36:47 +01001443void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1444 LocationSummary* locations =
1445 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1446 switch (neg->GetResultType()) {
1447 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001448 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001449 locations->SetInAt(0, Location::RequiresRegister());
1450 locations->SetOut(Location::SameAsFirstInput());
1451 break;
1452
Roland Levillain88cb1752014-10-20 16:36:47 +01001453 case Primitive::kPrimFloat:
1454 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001455 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001456 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001457 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001458 break;
1459
1460 default:
1461 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1462 }
1463}
1464
1465void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1466 LocationSummary* locations = neg->GetLocations();
1467 Location out = locations->Out();
1468 Location in = locations->InAt(0);
1469 switch (neg->GetResultType()) {
1470 case Primitive::kPrimInt:
1471 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001472 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001473 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001474 break;
1475
1476 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001477 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001478 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001479 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001480 break;
1481
Roland Levillain5368c212014-11-27 15:03:41 +00001482 case Primitive::kPrimFloat: {
1483 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001484 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001485 // Implement float negation with an exclusive or with value
1486 // 0x80000000 (mask for bit 31, representing the sign of a
1487 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001488 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001489 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001490 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001491 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001492
Roland Levillain5368c212014-11-27 15:03:41 +00001493 case Primitive::kPrimDouble: {
1494 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001495 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001496 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001497 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001498 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001499 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001500 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001501 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001502 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001503
1504 default:
1505 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1506 }
1507}
1508
Roland Levillaindff1f282014-11-05 14:15:05 +00001509void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1510 LocationSummary* locations =
1511 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1512 Primitive::Type result_type = conversion->GetResultType();
1513 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001514 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001515
David Brazdilb2bd1c52015-03-25 11:17:37 +00001516 // The Java language does not allow treating boolean as an integral type but
1517 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001518
Roland Levillaindff1f282014-11-05 14:15:05 +00001519 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001520 case Primitive::kPrimByte:
1521 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001522 case Primitive::kPrimBoolean:
1523 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001524 case Primitive::kPrimShort:
1525 case Primitive::kPrimInt:
1526 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001527 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001528 locations->SetInAt(0, Location::Any());
1529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1530 break;
1531
1532 default:
1533 LOG(FATAL) << "Unexpected type conversion from " << input_type
1534 << " to " << result_type;
1535 }
1536 break;
1537
Roland Levillain01a8d712014-11-14 16:27:39 +00001538 case Primitive::kPrimShort:
1539 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001540 case Primitive::kPrimBoolean:
1541 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001542 case Primitive::kPrimByte:
1543 case Primitive::kPrimInt:
1544 case Primitive::kPrimChar:
1545 // Processing a Dex `int-to-short' instruction.
1546 locations->SetInAt(0, Location::Any());
1547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1548 break;
1549
1550 default:
1551 LOG(FATAL) << "Unexpected type conversion from " << input_type
1552 << " to " << result_type;
1553 }
1554 break;
1555
Roland Levillain946e1432014-11-11 17:35:19 +00001556 case Primitive::kPrimInt:
1557 switch (input_type) {
1558 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001559 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001560 locations->SetInAt(0, Location::Any());
1561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1562 break;
1563
1564 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001565 // Processing a Dex `float-to-int' instruction.
1566 locations->SetInAt(0, Location::RequiresFpuRegister());
1567 locations->SetOut(Location::RequiresRegister());
1568 locations->AddTemp(Location::RequiresFpuRegister());
1569 break;
1570
Roland Levillain946e1432014-11-11 17:35:19 +00001571 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001572 // Processing a Dex `double-to-int' instruction.
1573 locations->SetInAt(0, Location::RequiresFpuRegister());
1574 locations->SetOut(Location::RequiresRegister());
1575 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001576 break;
1577
1578 default:
1579 LOG(FATAL) << "Unexpected type conversion from " << input_type
1580 << " to " << result_type;
1581 }
1582 break;
1583
Roland Levillaindff1f282014-11-05 14:15:05 +00001584 case Primitive::kPrimLong:
1585 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001586 case Primitive::kPrimBoolean:
1587 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001588 case Primitive::kPrimByte:
1589 case Primitive::kPrimShort:
1590 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001591 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001592 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001593 // TODO: We would benefit from a (to-be-implemented)
1594 // Location::RegisterOrStackSlot requirement for this input.
1595 locations->SetInAt(0, Location::RequiresRegister());
1596 locations->SetOut(Location::RequiresRegister());
1597 break;
1598
1599 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001600 // Processing a Dex `float-to-long' instruction.
1601 locations->SetInAt(0, Location::RequiresFpuRegister());
1602 locations->SetOut(Location::RequiresRegister());
1603 locations->AddTemp(Location::RequiresFpuRegister());
1604 break;
1605
Roland Levillaindff1f282014-11-05 14:15:05 +00001606 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001607 // Processing a Dex `double-to-long' instruction.
1608 locations->SetInAt(0, Location::RequiresFpuRegister());
1609 locations->SetOut(Location::RequiresRegister());
1610 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001611 break;
1612
1613 default:
1614 LOG(FATAL) << "Unexpected type conversion from " << input_type
1615 << " to " << result_type;
1616 }
1617 break;
1618
Roland Levillain981e4542014-11-14 11:47:14 +00001619 case Primitive::kPrimChar:
1620 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001621 case Primitive::kPrimBoolean:
1622 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001623 case Primitive::kPrimByte:
1624 case Primitive::kPrimShort:
1625 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001626 // Processing a Dex `int-to-char' instruction.
1627 locations->SetInAt(0, Location::Any());
1628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1629 break;
1630
1631 default:
1632 LOG(FATAL) << "Unexpected type conversion from " << input_type
1633 << " to " << result_type;
1634 }
1635 break;
1636
Roland Levillaindff1f282014-11-05 14:15:05 +00001637 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001638 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001639 case Primitive::kPrimBoolean:
1640 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001641 case Primitive::kPrimByte:
1642 case Primitive::kPrimShort:
1643 case Primitive::kPrimInt:
1644 case Primitive::kPrimChar:
1645 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001646 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001647 locations->SetOut(Location::RequiresFpuRegister());
1648 break;
1649
1650 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001651 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001652 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001653 locations->SetOut(Location::RequiresFpuRegister());
1654 break;
1655
Roland Levillaincff13742014-11-17 14:32:17 +00001656 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001657 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001658 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001659 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001660 break;
1661
1662 default:
1663 LOG(FATAL) << "Unexpected type conversion from " << input_type
1664 << " to " << result_type;
1665 };
1666 break;
1667
Roland Levillaindff1f282014-11-05 14:15:05 +00001668 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001669 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001670 case Primitive::kPrimBoolean:
1671 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001672 case Primitive::kPrimByte:
1673 case Primitive::kPrimShort:
1674 case Primitive::kPrimInt:
1675 case Primitive::kPrimChar:
1676 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001677 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001678 locations->SetOut(Location::RequiresFpuRegister());
1679 break;
1680
1681 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001682 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001683 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001684 locations->SetOut(Location::RequiresFpuRegister());
1685 break;
1686
Roland Levillaincff13742014-11-17 14:32:17 +00001687 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001688 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001689 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001690 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001691 break;
1692
1693 default:
1694 LOG(FATAL) << "Unexpected type conversion from " << input_type
1695 << " to " << result_type;
1696 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001697 break;
1698
1699 default:
1700 LOG(FATAL) << "Unexpected type conversion from " << input_type
1701 << " to " << result_type;
1702 }
1703}
1704
1705void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1706 LocationSummary* locations = conversion->GetLocations();
1707 Location out = locations->Out();
1708 Location in = locations->InAt(0);
1709 Primitive::Type result_type = conversion->GetResultType();
1710 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001711 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001712 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001713 case Primitive::kPrimByte:
1714 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001715 case Primitive::kPrimBoolean:
1716 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001717 case Primitive::kPrimShort:
1718 case Primitive::kPrimInt:
1719 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001720 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001721 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001722 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001723 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001724 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001725 Address(CpuRegister(RSP), in.GetStackIndex()));
1726 } else {
1727 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001729 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1730 }
1731 break;
1732
1733 default:
1734 LOG(FATAL) << "Unexpected type conversion from " << input_type
1735 << " to " << result_type;
1736 }
1737 break;
1738
Roland Levillain01a8d712014-11-14 16:27:39 +00001739 case Primitive::kPrimShort:
1740 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001741 case Primitive::kPrimBoolean:
1742 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001743 case Primitive::kPrimByte:
1744 case Primitive::kPrimInt:
1745 case Primitive::kPrimChar:
1746 // Processing a Dex `int-to-short' instruction.
1747 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001749 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001751 Address(CpuRegister(RSP), in.GetStackIndex()));
1752 } else {
1753 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001754 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001755 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1756 }
1757 break;
1758
1759 default:
1760 LOG(FATAL) << "Unexpected type conversion from " << input_type
1761 << " to " << result_type;
1762 }
1763 break;
1764
Roland Levillain946e1432014-11-11 17:35:19 +00001765 case Primitive::kPrimInt:
1766 switch (input_type) {
1767 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001768 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001769 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001770 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001771 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001772 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001773 Address(CpuRegister(RSP), in.GetStackIndex()));
1774 } else {
1775 DCHECK(in.IsConstant());
1776 DCHECK(in.GetConstant()->IsLongConstant());
1777 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001778 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001779 }
1780 break;
1781
Roland Levillain3f8f9362014-12-02 17:45:01 +00001782 case Primitive::kPrimFloat: {
1783 // Processing a Dex `float-to-int' instruction.
1784 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1785 CpuRegister output = out.AsRegister<CpuRegister>();
1786 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1787 Label done, nan;
1788
1789 __ movl(output, Immediate(kPrimIntMax));
1790 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001791 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001792 // if input >= temp goto done
1793 __ comiss(input, temp);
1794 __ j(kAboveEqual, &done);
1795 // if input == NaN goto nan
1796 __ j(kUnordered, &nan);
1797 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001798 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001799 __ jmp(&done);
1800 __ Bind(&nan);
1801 // output = 0
1802 __ xorl(output, output);
1803 __ Bind(&done);
1804 break;
1805 }
1806
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001807 case Primitive::kPrimDouble: {
1808 // Processing a Dex `double-to-int' instruction.
1809 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1810 CpuRegister output = out.AsRegister<CpuRegister>();
1811 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1812 Label done, nan;
1813
1814 __ movl(output, Immediate(kPrimIntMax));
1815 // temp = int-to-double(output)
1816 __ cvtsi2sd(temp, output);
1817 // if input >= temp goto done
1818 __ comisd(input, temp);
1819 __ j(kAboveEqual, &done);
1820 // if input == NaN goto nan
1821 __ j(kUnordered, &nan);
1822 // output = double-to-int-truncate(input)
1823 __ cvttsd2si(output, input);
1824 __ jmp(&done);
1825 __ Bind(&nan);
1826 // output = 0
1827 __ xorl(output, output);
1828 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001829 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001830 }
Roland Levillain946e1432014-11-11 17:35:19 +00001831
1832 default:
1833 LOG(FATAL) << "Unexpected type conversion from " << input_type
1834 << " to " << result_type;
1835 }
1836 break;
1837
Roland Levillaindff1f282014-11-05 14:15:05 +00001838 case Primitive::kPrimLong:
1839 switch (input_type) {
1840 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001841 case Primitive::kPrimBoolean:
1842 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001843 case Primitive::kPrimByte:
1844 case Primitive::kPrimShort:
1845 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001846 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001847 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001848 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001849 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001850 break;
1851
Roland Levillain624279f2014-12-04 11:54:28 +00001852 case Primitive::kPrimFloat: {
1853 // Processing a Dex `float-to-long' instruction.
1854 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1855 CpuRegister output = out.AsRegister<CpuRegister>();
1856 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1857 Label done, nan;
1858
1859 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001860 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001861 __ cvtsi2ss(temp, output, true);
1862 // if input >= temp goto done
1863 __ comiss(input, temp);
1864 __ j(kAboveEqual, &done);
1865 // if input == NaN goto nan
1866 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001867 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001868 __ cvttss2si(output, input, true);
1869 __ jmp(&done);
1870 __ Bind(&nan);
1871 // output = 0
1872 __ xorq(output, output);
1873 __ Bind(&done);
1874 break;
1875 }
1876
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001877 case Primitive::kPrimDouble: {
1878 // Processing a Dex `double-to-long' instruction.
1879 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1880 CpuRegister output = out.AsRegister<CpuRegister>();
1881 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1882 Label done, nan;
1883
1884 __ movq(output, Immediate(kPrimLongMax));
1885 // temp = long-to-double(output)
1886 __ cvtsi2sd(temp, output, true);
1887 // if input >= temp goto done
1888 __ comisd(input, temp);
1889 __ j(kAboveEqual, &done);
1890 // if input == NaN goto nan
1891 __ j(kUnordered, &nan);
1892 // output = double-to-long-truncate(input)
1893 __ cvttsd2si(output, input, true);
1894 __ jmp(&done);
1895 __ Bind(&nan);
1896 // output = 0
1897 __ xorq(output, output);
1898 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001899 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001900 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001901
1902 default:
1903 LOG(FATAL) << "Unexpected type conversion from " << input_type
1904 << " to " << result_type;
1905 }
1906 break;
1907
Roland Levillain981e4542014-11-14 11:47:14 +00001908 case Primitive::kPrimChar:
1909 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001910 case Primitive::kPrimBoolean:
1911 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001912 case Primitive::kPrimByte:
1913 case Primitive::kPrimShort:
1914 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001915 // Processing a Dex `int-to-char' instruction.
1916 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001917 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001918 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001919 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001920 Address(CpuRegister(RSP), in.GetStackIndex()));
1921 } else {
1922 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001924 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1925 }
1926 break;
1927
1928 default:
1929 LOG(FATAL) << "Unexpected type conversion from " << input_type
1930 << " to " << result_type;
1931 }
1932 break;
1933
Roland Levillaindff1f282014-11-05 14:15:05 +00001934 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001935 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001936 case Primitive::kPrimBoolean:
1937 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001938 case Primitive::kPrimByte:
1939 case Primitive::kPrimShort:
1940 case Primitive::kPrimInt:
1941 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001942 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001943 if (in.IsRegister()) {
1944 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1945 } else if (in.IsConstant()) {
1946 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
1947 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1948 if (v == 0) {
1949 __ xorps(dest, dest);
1950 } else {
1951 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1952 }
1953 } else {
1954 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1955 Address(CpuRegister(RSP), in.GetStackIndex()), false);
1956 }
Roland Levillaincff13742014-11-17 14:32:17 +00001957 break;
1958
1959 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001960 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001961 if (in.IsRegister()) {
1962 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1963 } else if (in.IsConstant()) {
1964 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
1965 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1966 if (v == 0) {
1967 __ xorps(dest, dest);
1968 } else {
1969 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1970 }
1971 } else {
1972 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1973 Address(CpuRegister(RSP), in.GetStackIndex()), true);
1974 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001975 break;
1976
Roland Levillaincff13742014-11-17 14:32:17 +00001977 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001978 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001979 if (in.IsFpuRegister()) {
1980 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
1981 } else if (in.IsConstant()) {
1982 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
1983 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1984 if (bit_cast<int64_t, double>(v) == 0) {
1985 __ xorps(dest, dest);
1986 } else {
1987 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1988 }
1989 } else {
1990 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
1991 Address(CpuRegister(RSP), in.GetStackIndex()));
1992 }
Roland Levillaincff13742014-11-17 14:32:17 +00001993 break;
1994
1995 default:
1996 LOG(FATAL) << "Unexpected type conversion from " << input_type
1997 << " to " << result_type;
1998 };
1999 break;
2000
Roland Levillaindff1f282014-11-05 14:15:05 +00002001 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002002 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002003 case Primitive::kPrimBoolean:
2004 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002005 case Primitive::kPrimByte:
2006 case Primitive::kPrimShort:
2007 case Primitive::kPrimInt:
2008 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002009 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002010 if (in.IsRegister()) {
2011 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2012 } else if (in.IsConstant()) {
2013 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2014 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2015 if (v == 0) {
2016 __ xorpd(dest, dest);
2017 } else {
2018 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2019 }
2020 } else {
2021 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2022 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2023 }
Roland Levillaincff13742014-11-17 14:32:17 +00002024 break;
2025
2026 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002027 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002028 if (in.IsRegister()) {
2029 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2030 } else if (in.IsConstant()) {
2031 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2032 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2033 if (v == 0) {
2034 __ xorpd(dest, dest);
2035 } else {
2036 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2037 }
2038 } else {
2039 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2040 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2041 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002042 break;
2043
Roland Levillaincff13742014-11-17 14:32:17 +00002044 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002045 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002046 if (in.IsFpuRegister()) {
2047 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2048 } else if (in.IsConstant()) {
2049 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2050 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2051 if (bit_cast<int32_t, float>(v) == 0) {
2052 __ xorpd(dest, dest);
2053 } else {
2054 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2055 }
2056 } else {
2057 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2058 Address(CpuRegister(RSP), in.GetStackIndex()));
2059 }
Roland Levillaincff13742014-11-17 14:32:17 +00002060 break;
2061
2062 default:
2063 LOG(FATAL) << "Unexpected type conversion from " << input_type
2064 << " to " << result_type;
2065 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002066 break;
2067
2068 default:
2069 LOG(FATAL) << "Unexpected type conversion from " << input_type
2070 << " to " << result_type;
2071 }
2072}
2073
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002074void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002075 LocationSummary* locations =
2076 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002077 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002078 case Primitive::kPrimInt: {
2079 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002080 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002082 break;
2083 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002084
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002085 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002086 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002087 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002088 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002090 break;
2091 }
2092
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002093 case Primitive::kPrimDouble:
2094 case Primitive::kPrimFloat: {
2095 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002096 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002097 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002098 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002099 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002100
2101 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002102 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104}
2105
2106void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2107 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002108 Location first = locations->InAt(0);
2109 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002110 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002111
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002112 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002113 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002114 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002115 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2116 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2117 } else {
2118 __ leal(out.AsRegister<CpuRegister>(), Address(
2119 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2120 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002122 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2123 __ addl(out.AsRegister<CpuRegister>(),
2124 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2125 } else {
2126 __ leal(out.AsRegister<CpuRegister>(), Address(
2127 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2128 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002129 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002130 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002132 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002133 break;
2134 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002135
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002136 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002137 if (second.IsRegister()) {
2138 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2139 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2140 } else {
2141 __ leaq(out.AsRegister<CpuRegister>(), Address(
2142 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2143 }
2144 } else {
2145 DCHECK(second.IsConstant());
2146 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2147 int32_t int32_value = Low32Bits(value);
2148 DCHECK_EQ(int32_value, value);
2149 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2150 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2151 } else {
2152 __ leaq(out.AsRegister<CpuRegister>(), Address(
2153 first.AsRegister<CpuRegister>(), int32_value));
2154 }
2155 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002156 break;
2157 }
2158
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002159 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002160 if (second.IsFpuRegister()) {
2161 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2162 } else if (second.IsConstant()) {
2163 __ addss(first.AsFpuRegister<XmmRegister>(),
2164 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2165 } else {
2166 DCHECK(second.IsStackSlot());
2167 __ addss(first.AsFpuRegister<XmmRegister>(),
2168 Address(CpuRegister(RSP), second.GetStackIndex()));
2169 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002170 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002171 }
2172
2173 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002174 if (second.IsFpuRegister()) {
2175 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2176 } else if (second.IsConstant()) {
2177 __ addsd(first.AsFpuRegister<XmmRegister>(),
2178 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2179 } else {
2180 DCHECK(second.IsDoubleStackSlot());
2181 __ addsd(first.AsFpuRegister<XmmRegister>(),
2182 Address(CpuRegister(RSP), second.GetStackIndex()));
2183 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002184 break;
2185 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002186
2187 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002188 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002189 }
2190}
2191
2192void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002193 LocationSummary* locations =
2194 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002195 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002196 case Primitive::kPrimInt: {
2197 locations->SetInAt(0, Location::RequiresRegister());
2198 locations->SetInAt(1, Location::Any());
2199 locations->SetOut(Location::SameAsFirstInput());
2200 break;
2201 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002202 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002203 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002204 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002205 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002206 break;
2207 }
Calin Juravle11351682014-10-23 15:38:15 +01002208 case Primitive::kPrimFloat:
2209 case Primitive::kPrimDouble: {
2210 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002211 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002212 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213 break;
Calin Juravle11351682014-10-23 15:38:15 +01002214 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002215 default:
Calin Juravle11351682014-10-23 15:38:15 +01002216 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002217 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002218}
2219
2220void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2221 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002222 Location first = locations->InAt(0);
2223 Location second = locations->InAt(1);
2224 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002225 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002226 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002227 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002228 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002229 } else if (second.IsConstant()) {
2230 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002231 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002232 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002233 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002234 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002235 break;
2236 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002238 if (second.IsConstant()) {
2239 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2240 DCHECK(IsInt<32>(value));
2241 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2242 } else {
2243 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2244 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002245 break;
2246 }
2247
Calin Juravle11351682014-10-23 15:38:15 +01002248 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002249 if (second.IsFpuRegister()) {
2250 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2251 } else if (second.IsConstant()) {
2252 __ subss(first.AsFpuRegister<XmmRegister>(),
2253 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2254 } else {
2255 DCHECK(second.IsStackSlot());
2256 __ subss(first.AsFpuRegister<XmmRegister>(),
2257 Address(CpuRegister(RSP), second.GetStackIndex()));
2258 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002259 break;
Calin Juravle11351682014-10-23 15:38:15 +01002260 }
2261
2262 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002263 if (second.IsFpuRegister()) {
2264 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2265 } else if (second.IsConstant()) {
2266 __ subsd(first.AsFpuRegister<XmmRegister>(),
2267 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2268 } else {
2269 DCHECK(second.IsDoubleStackSlot());
2270 __ subsd(first.AsFpuRegister<XmmRegister>(),
2271 Address(CpuRegister(RSP), second.GetStackIndex()));
2272 }
Calin Juravle11351682014-10-23 15:38:15 +01002273 break;
2274 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002275
2276 default:
Calin Juravle11351682014-10-23 15:38:15 +01002277 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002278 }
2279}
2280
Calin Juravle34bacdf2014-10-07 20:23:36 +01002281void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2282 LocationSummary* locations =
2283 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2284 switch (mul->GetResultType()) {
2285 case Primitive::kPrimInt: {
2286 locations->SetInAt(0, Location::RequiresRegister());
2287 locations->SetInAt(1, Location::Any());
2288 locations->SetOut(Location::SameAsFirstInput());
2289 break;
2290 }
2291 case Primitive::kPrimLong: {
2292 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002293 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2294 if (locations->InAt(1).IsConstant()) {
2295 // Can use 3 operand multiply.
2296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2297 } else {
2298 locations->SetOut(Location::SameAsFirstInput());
2299 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002300 break;
2301 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002302 case Primitive::kPrimFloat:
2303 case Primitive::kPrimDouble: {
2304 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002305 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002306 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002307 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002308 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002309
2310 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002311 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002312 }
2313}
2314
2315void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2316 LocationSummary* locations = mul->GetLocations();
2317 Location first = locations->InAt(0);
2318 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002319 switch (mul->GetResultType()) {
2320 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002321 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002322 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002323 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002324 } else if (second.IsConstant()) {
2325 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002327 } else {
2328 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002329 __ imull(first.AsRegister<CpuRegister>(),
2330 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002331 }
2332 break;
2333 }
2334 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002335 if (second.IsConstant()) {
2336 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2337 DCHECK(IsInt<32>(value));
2338 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2339 first.AsRegister<CpuRegister>(),
2340 Immediate(static_cast<int32_t>(value)));
2341 } else {
2342 DCHECK(first.Equals(locations->Out()));
2343 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2344 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002345 break;
2346 }
2347
Calin Juravleb5bfa962014-10-21 18:02:24 +01002348 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002349 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002350 if (second.IsFpuRegister()) {
2351 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2352 } else if (second.IsConstant()) {
2353 __ mulss(first.AsFpuRegister<XmmRegister>(),
2354 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2355 } else {
2356 DCHECK(second.IsStackSlot());
2357 __ mulss(first.AsFpuRegister<XmmRegister>(),
2358 Address(CpuRegister(RSP), second.GetStackIndex()));
2359 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002360 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002361 }
2362
2363 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002364 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002365 if (second.IsFpuRegister()) {
2366 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2367 } else if (second.IsConstant()) {
2368 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2369 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2370 } else {
2371 DCHECK(second.IsDoubleStackSlot());
2372 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2373 Address(CpuRegister(RSP), second.GetStackIndex()));
2374 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002375 break;
2376 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002377
2378 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002379 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002380 }
2381}
2382
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002383void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2384 uint32_t stack_adjustment, bool is_float) {
2385 if (source.IsStackSlot()) {
2386 DCHECK(is_float);
2387 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2388 } else if (source.IsDoubleStackSlot()) {
2389 DCHECK(!is_float);
2390 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2391 } else {
2392 // Write the value to the temporary location on the stack and load to FP stack.
2393 if (is_float) {
2394 Location stack_temp = Location::StackSlot(temp_offset);
2395 codegen_->Move(stack_temp, source);
2396 __ flds(Address(CpuRegister(RSP), temp_offset));
2397 } else {
2398 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2399 codegen_->Move(stack_temp, source);
2400 __ fldl(Address(CpuRegister(RSP), temp_offset));
2401 }
2402 }
2403}
2404
2405void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2406 Primitive::Type type = rem->GetResultType();
2407 bool is_float = type == Primitive::kPrimFloat;
2408 size_t elem_size = Primitive::ComponentSize(type);
2409 LocationSummary* locations = rem->GetLocations();
2410 Location first = locations->InAt(0);
2411 Location second = locations->InAt(1);
2412 Location out = locations->Out();
2413
2414 // Create stack space for 2 elements.
2415 // TODO: enhance register allocator to ask for stack temporaries.
2416 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2417
2418 // Load the values to the FP stack in reverse order, using temporaries if needed.
2419 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2420 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2421
2422 // Loop doing FPREM until we stabilize.
2423 Label retry;
2424 __ Bind(&retry);
2425 __ fprem();
2426
2427 // Move FP status to AX.
2428 __ fstsw();
2429
2430 // And see if the argument reduction is complete. This is signaled by the
2431 // C2 FPU flag bit set to 0.
2432 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2433 __ j(kNotEqual, &retry);
2434
2435 // We have settled on the final value. Retrieve it into an XMM register.
2436 // Store FP top of stack to real stack.
2437 if (is_float) {
2438 __ fsts(Address(CpuRegister(RSP), 0));
2439 } else {
2440 __ fstl(Address(CpuRegister(RSP), 0));
2441 }
2442
2443 // Pop the 2 items from the FP stack.
2444 __ fucompp();
2445
2446 // Load the value from the stack into an XMM register.
2447 DCHECK(out.IsFpuRegister()) << out;
2448 if (is_float) {
2449 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2450 } else {
2451 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2452 }
2453
2454 // And remove the temporary stack space we allocated.
2455 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2456}
2457
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002458void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2459 DCHECK(instruction->IsDiv() || instruction->IsRem());
2460
2461 LocationSummary* locations = instruction->GetLocations();
2462 Location second = locations->InAt(1);
2463 DCHECK(second.IsConstant());
2464
2465 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2466 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002467 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002468
2469 DCHECK(imm == 1 || imm == -1);
2470
2471 switch (instruction->GetResultType()) {
2472 case Primitive::kPrimInt: {
2473 if (instruction->IsRem()) {
2474 __ xorl(output_register, output_register);
2475 } else {
2476 __ movl(output_register, input_register);
2477 if (imm == -1) {
2478 __ negl(output_register);
2479 }
2480 }
2481 break;
2482 }
2483
2484 case Primitive::kPrimLong: {
2485 if (instruction->IsRem()) {
2486 __ xorq(output_register, output_register);
2487 } else {
2488 __ movq(output_register, input_register);
2489 if (imm == -1) {
2490 __ negq(output_register);
2491 }
2492 }
2493 break;
2494 }
2495
2496 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002497 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002498 }
2499}
2500
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002501void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002502 LocationSummary* locations = instruction->GetLocations();
2503 Location second = locations->InAt(1);
2504
2505 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2506 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2507
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002508 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002509
2510 DCHECK(IsPowerOfTwo(std::abs(imm)));
2511
2512 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2513
2514 if (instruction->GetResultType() == Primitive::kPrimInt) {
2515 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2516 __ testl(numerator, numerator);
2517 __ cmov(kGreaterEqual, tmp, numerator);
2518 int shift = CTZ(imm);
2519 __ sarl(tmp, Immediate(shift));
2520
2521 if (imm < 0) {
2522 __ negl(tmp);
2523 }
2524
2525 __ movl(output_register, tmp);
2526 } else {
2527 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2528 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2529
2530 __ movq(rdx, Immediate(std::abs(imm) - 1));
2531 __ addq(rdx, numerator);
2532 __ testq(numerator, numerator);
2533 __ cmov(kGreaterEqual, rdx, numerator);
2534 int shift = CTZ(imm);
2535 __ sarq(rdx, Immediate(shift));
2536
2537 if (imm < 0) {
2538 __ negq(rdx);
2539 }
2540
2541 __ movq(output_register, rdx);
2542 }
2543}
2544
2545void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2546 DCHECK(instruction->IsDiv() || instruction->IsRem());
2547
2548 LocationSummary* locations = instruction->GetLocations();
2549 Location second = locations->InAt(1);
2550
2551 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2552 : locations->GetTemp(0).AsRegister<CpuRegister>();
2553 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2554 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2555 : locations->Out().AsRegister<CpuRegister>();
2556 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2557
2558 DCHECK_EQ(RAX, eax.AsRegister());
2559 DCHECK_EQ(RDX, edx.AsRegister());
2560 if (instruction->IsDiv()) {
2561 DCHECK_EQ(RAX, out.AsRegister());
2562 } else {
2563 DCHECK_EQ(RDX, out.AsRegister());
2564 }
2565
2566 int64_t magic;
2567 int shift;
2568
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002569 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002570 if (instruction->GetResultType() == Primitive::kPrimInt) {
2571 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2572
2573 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2574
2575 __ movl(numerator, eax);
2576
2577 Label no_div;
2578 Label end;
2579 __ testl(eax, eax);
2580 __ j(kNotEqual, &no_div);
2581
2582 __ xorl(out, out);
2583 __ jmp(&end);
2584
2585 __ Bind(&no_div);
2586
2587 __ movl(eax, Immediate(magic));
2588 __ imull(numerator);
2589
2590 if (imm > 0 && magic < 0) {
2591 __ addl(edx, numerator);
2592 } else if (imm < 0 && magic > 0) {
2593 __ subl(edx, numerator);
2594 }
2595
2596 if (shift != 0) {
2597 __ sarl(edx, Immediate(shift));
2598 }
2599
2600 __ movl(eax, edx);
2601 __ shrl(edx, Immediate(31));
2602 __ addl(edx, eax);
2603
2604 if (instruction->IsRem()) {
2605 __ movl(eax, numerator);
2606 __ imull(edx, Immediate(imm));
2607 __ subl(eax, edx);
2608 __ movl(edx, eax);
2609 } else {
2610 __ movl(eax, edx);
2611 }
2612 __ Bind(&end);
2613 } else {
2614 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2615
2616 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2617
2618 CpuRegister rax = eax;
2619 CpuRegister rdx = edx;
2620
2621 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2622
2623 // Save the numerator.
2624 __ movq(numerator, rax);
2625
2626 // RAX = magic
2627 __ movq(rax, Immediate(magic));
2628
2629 // RDX:RAX = magic * numerator
2630 __ imulq(numerator);
2631
2632 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002633 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002634 __ addq(rdx, numerator);
2635 } else if (imm < 0 && magic > 0) {
2636 // RDX -= numerator
2637 __ subq(rdx, numerator);
2638 }
2639
2640 // Shift if needed.
2641 if (shift != 0) {
2642 __ sarq(rdx, Immediate(shift));
2643 }
2644
2645 // RDX += 1 if RDX < 0
2646 __ movq(rax, rdx);
2647 __ shrq(rdx, Immediate(63));
2648 __ addq(rdx, rax);
2649
2650 if (instruction->IsRem()) {
2651 __ movq(rax, numerator);
2652
2653 if (IsInt<32>(imm)) {
2654 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2655 } else {
2656 __ movq(numerator, Immediate(imm));
2657 __ imulq(rdx, numerator);
2658 }
2659
2660 __ subq(rax, rdx);
2661 __ movq(rdx, rax);
2662 } else {
2663 __ movq(rax, rdx);
2664 }
2665 }
2666}
2667
Calin Juravlebacfec32014-11-14 15:54:36 +00002668void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2669 DCHECK(instruction->IsDiv() || instruction->IsRem());
2670 Primitive::Type type = instruction->GetResultType();
2671 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2672
2673 bool is_div = instruction->IsDiv();
2674 LocationSummary* locations = instruction->GetLocations();
2675
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002676 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2677 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002678
Roland Levillain271ab9c2014-11-27 15:23:57 +00002679 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002680 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002681
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002682 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002683 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002684
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002685 if (imm == 0) {
2686 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2687 } else if (imm == 1 || imm == -1) {
2688 DivRemOneOrMinusOne(instruction);
2689 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002690 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002691 } else {
2692 DCHECK(imm <= -2 || imm >= 2);
2693 GenerateDivRemWithAnyConstant(instruction);
2694 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002695 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002696 SlowPathCodeX86_64* slow_path =
2697 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2698 out.AsRegister(), type, is_div);
2699 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002700
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002701 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2702 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2703 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2704 // so it's safe to just use negl instead of more complex comparisons.
2705 if (type == Primitive::kPrimInt) {
2706 __ cmpl(second_reg, Immediate(-1));
2707 __ j(kEqual, slow_path->GetEntryLabel());
2708 // edx:eax <- sign-extended of eax
2709 __ cdq();
2710 // eax = quotient, edx = remainder
2711 __ idivl(second_reg);
2712 } else {
2713 __ cmpq(second_reg, Immediate(-1));
2714 __ j(kEqual, slow_path->GetEntryLabel());
2715 // rdx:rax <- sign-extended of rax
2716 __ cqo();
2717 // rax = quotient, rdx = remainder
2718 __ idivq(second_reg);
2719 }
2720 __ Bind(slow_path->GetExitLabel());
2721 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002722}
2723
Calin Juravle7c4954d2014-10-28 16:57:40 +00002724void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2725 LocationSummary* locations =
2726 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2727 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002728 case Primitive::kPrimInt:
2729 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002730 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002731 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002732 locations->SetOut(Location::SameAsFirstInput());
2733 // Intel uses edx:eax as the dividend.
2734 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002735 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2736 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2737 // output and request another temp.
2738 if (div->InputAt(1)->IsConstant()) {
2739 locations->AddTemp(Location::RequiresRegister());
2740 }
Calin Juravled0d48522014-11-04 16:40:20 +00002741 break;
2742 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002743
Calin Juravle7c4954d2014-10-28 16:57:40 +00002744 case Primitive::kPrimFloat:
2745 case Primitive::kPrimDouble: {
2746 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002747 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002748 locations->SetOut(Location::SameAsFirstInput());
2749 break;
2750 }
2751
2752 default:
2753 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2754 }
2755}
2756
2757void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2758 LocationSummary* locations = div->GetLocations();
2759 Location first = locations->InAt(0);
2760 Location second = locations->InAt(1);
2761 DCHECK(first.Equals(locations->Out()));
2762
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002763 Primitive::Type type = div->GetResultType();
2764 switch (type) {
2765 case Primitive::kPrimInt:
2766 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002767 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002768 break;
2769 }
2770
Calin Juravle7c4954d2014-10-28 16:57:40 +00002771 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002772 if (second.IsFpuRegister()) {
2773 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2774 } else if (second.IsConstant()) {
2775 __ divss(first.AsFpuRegister<XmmRegister>(),
2776 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2777 } else {
2778 DCHECK(second.IsStackSlot());
2779 __ divss(first.AsFpuRegister<XmmRegister>(),
2780 Address(CpuRegister(RSP), second.GetStackIndex()));
2781 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002782 break;
2783 }
2784
2785 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002786 if (second.IsFpuRegister()) {
2787 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2788 } else if (second.IsConstant()) {
2789 __ divsd(first.AsFpuRegister<XmmRegister>(),
2790 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2791 } else {
2792 DCHECK(second.IsDoubleStackSlot());
2793 __ divsd(first.AsFpuRegister<XmmRegister>(),
2794 Address(CpuRegister(RSP), second.GetStackIndex()));
2795 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002796 break;
2797 }
2798
2799 default:
2800 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2801 }
2802}
2803
Calin Juravlebacfec32014-11-14 15:54:36 +00002804void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002805 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002806 LocationSummary* locations =
2807 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002808
2809 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002810 case Primitive::kPrimInt:
2811 case Primitive::kPrimLong: {
2812 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002813 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002814 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2815 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002816 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2817 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2818 // output and request another temp.
2819 if (rem->InputAt(1)->IsConstant()) {
2820 locations->AddTemp(Location::RequiresRegister());
2821 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002822 break;
2823 }
2824
2825 case Primitive::kPrimFloat:
2826 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002827 locations->SetInAt(0, Location::Any());
2828 locations->SetInAt(1, Location::Any());
2829 locations->SetOut(Location::RequiresFpuRegister());
2830 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002831 break;
2832 }
2833
2834 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002835 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002836 }
2837}
2838
2839void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2840 Primitive::Type type = rem->GetResultType();
2841 switch (type) {
2842 case Primitive::kPrimInt:
2843 case Primitive::kPrimLong: {
2844 GenerateDivRemIntegral(rem);
2845 break;
2846 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002847 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002848 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002849 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002850 break;
2851 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002852 default:
2853 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2854 }
2855}
2856
Calin Juravled0d48522014-11-04 16:40:20 +00002857void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2858 LocationSummary* locations =
2859 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2860 locations->SetInAt(0, Location::Any());
2861 if (instruction->HasUses()) {
2862 locations->SetOut(Location::SameAsFirstInput());
2863 }
2864}
2865
2866void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2867 SlowPathCodeX86_64* slow_path =
2868 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2869 codegen_->AddSlowPath(slow_path);
2870
2871 LocationSummary* locations = instruction->GetLocations();
2872 Location value = locations->InAt(0);
2873
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002874 switch (instruction->GetType()) {
2875 case Primitive::kPrimInt: {
2876 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002878 __ j(kEqual, slow_path->GetEntryLabel());
2879 } else if (value.IsStackSlot()) {
2880 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2881 __ j(kEqual, slow_path->GetEntryLabel());
2882 } else {
2883 DCHECK(value.IsConstant()) << value;
2884 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2885 __ jmp(slow_path->GetEntryLabel());
2886 }
2887 }
2888 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002889 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002890 case Primitive::kPrimLong: {
2891 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002892 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002893 __ j(kEqual, slow_path->GetEntryLabel());
2894 } else if (value.IsDoubleStackSlot()) {
2895 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2896 __ j(kEqual, slow_path->GetEntryLabel());
2897 } else {
2898 DCHECK(value.IsConstant()) << value;
2899 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2900 __ jmp(slow_path->GetEntryLabel());
2901 }
2902 }
2903 break;
2904 }
2905 default:
2906 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002907 }
Calin Juravled0d48522014-11-04 16:40:20 +00002908}
2909
Calin Juravle9aec02f2014-11-18 23:06:35 +00002910void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2911 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2912
2913 LocationSummary* locations =
2914 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2915
2916 switch (op->GetResultType()) {
2917 case Primitive::kPrimInt:
2918 case Primitive::kPrimLong: {
2919 locations->SetInAt(0, Location::RequiresRegister());
2920 // The shift count needs to be in CL.
2921 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2922 locations->SetOut(Location::SameAsFirstInput());
2923 break;
2924 }
2925 default:
2926 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2927 }
2928}
2929
2930void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2931 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2932
2933 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002934 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002935 Location second = locations->InAt(1);
2936
2937 switch (op->GetResultType()) {
2938 case Primitive::kPrimInt: {
2939 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002940 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002941 if (op->IsShl()) {
2942 __ shll(first_reg, second_reg);
2943 } else if (op->IsShr()) {
2944 __ sarl(first_reg, second_reg);
2945 } else {
2946 __ shrl(first_reg, second_reg);
2947 }
2948 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002949 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002950 if (op->IsShl()) {
2951 __ shll(first_reg, imm);
2952 } else if (op->IsShr()) {
2953 __ sarl(first_reg, imm);
2954 } else {
2955 __ shrl(first_reg, imm);
2956 }
2957 }
2958 break;
2959 }
2960 case Primitive::kPrimLong: {
2961 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002962 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002963 if (op->IsShl()) {
2964 __ shlq(first_reg, second_reg);
2965 } else if (op->IsShr()) {
2966 __ sarq(first_reg, second_reg);
2967 } else {
2968 __ shrq(first_reg, second_reg);
2969 }
2970 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002971 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002972 if (op->IsShl()) {
2973 __ shlq(first_reg, imm);
2974 } else if (op->IsShr()) {
2975 __ sarq(first_reg, imm);
2976 } else {
2977 __ shrq(first_reg, imm);
2978 }
2979 }
2980 break;
2981 }
2982 default:
2983 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2984 }
2985}
2986
2987void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2988 HandleShift(shl);
2989}
2990
2991void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2992 HandleShift(shl);
2993}
2994
2995void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2996 HandleShift(shr);
2997}
2998
2999void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3000 HandleShift(shr);
3001}
3002
3003void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3004 HandleShift(ushr);
3005}
3006
3007void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3008 HandleShift(ushr);
3009}
3010
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003011void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003012 LocationSummary* locations =
3013 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003014 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003015 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3016 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3017 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003018}
3019
3020void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3021 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003022 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003023 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
3024
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003025 __ gs()->call(
3026 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003027
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003028 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003029 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003030}
3031
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003032void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3033 LocationSummary* locations =
3034 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3035 InvokeRuntimeCallingConvention calling_convention;
3036 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003037 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003038 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003039 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003040}
3041
3042void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3043 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003044 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003045 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
3046
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003047 __ gs()->call(
3048 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003049
3050 DCHECK(!codegen_->IsLeafMethod());
3051 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3052}
3053
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003054void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003057 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3058 if (location.IsStackSlot()) {
3059 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3060 } else if (location.IsDoubleStackSlot()) {
3061 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3062 }
3063 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003064}
3065
3066void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
3067 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003068 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003069}
3070
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003071void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003072 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003073 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003074 locations->SetInAt(0, Location::RequiresRegister());
3075 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003076}
3077
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003078void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3079 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003080 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3081 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003082 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003083 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003084 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003086 break;
3087
3088 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003090 break;
3091
3092 default:
3093 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3094 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003095}
3096
David Brazdil66d126e2015-04-03 16:02:44 +01003097void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3098 LocationSummary* locations =
3099 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3100 locations->SetInAt(0, Location::RequiresRegister());
3101 locations->SetOut(Location::SameAsFirstInput());
3102}
3103
3104void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003105 LocationSummary* locations = bool_not->GetLocations();
3106 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3107 locations->Out().AsRegister<CpuRegister>().AsRegister());
3108 Location out = locations->Out();
3109 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3110}
3111
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003112void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003113 LocationSummary* locations =
3114 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003115 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3116 locations->SetInAt(i, Location::Any());
3117 }
3118 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003119}
3120
3121void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003122 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003123 LOG(FATAL) << "Unimplemented";
3124}
3125
Calin Juravle52c48962014-12-16 17:02:57 +00003126void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3127 /*
3128 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3129 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3130 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3131 */
3132 switch (kind) {
3133 case MemBarrierKind::kAnyAny: {
3134 __ mfence();
3135 break;
3136 }
3137 case MemBarrierKind::kAnyStore:
3138 case MemBarrierKind::kLoadAny:
3139 case MemBarrierKind::kStoreStore: {
3140 // nop
3141 break;
3142 }
3143 default:
3144 LOG(FATAL) << "Unexpected memory barier " << kind;
3145 }
3146}
3147
3148void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3149 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3150
Nicolas Geoffray39468442014-09-02 15:17:15 +01003151 LocationSummary* locations =
3152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003153 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003154 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3155 locations->SetOut(Location::RequiresFpuRegister());
3156 } else {
3157 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3158 }
Calin Juravle52c48962014-12-16 17:02:57 +00003159}
3160
3161void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3162 const FieldInfo& field_info) {
3163 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3164
3165 LocationSummary* locations = instruction->GetLocations();
3166 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3167 Location out = locations->Out();
3168 bool is_volatile = field_info.IsVolatile();
3169 Primitive::Type field_type = field_info.GetFieldType();
3170 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3171
3172 switch (field_type) {
3173 case Primitive::kPrimBoolean: {
3174 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3175 break;
3176 }
3177
3178 case Primitive::kPrimByte: {
3179 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3180 break;
3181 }
3182
3183 case Primitive::kPrimShort: {
3184 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3185 break;
3186 }
3187
3188 case Primitive::kPrimChar: {
3189 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3190 break;
3191 }
3192
3193 case Primitive::kPrimInt:
3194 case Primitive::kPrimNot: {
3195 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3196 break;
3197 }
3198
3199 case Primitive::kPrimLong: {
3200 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3201 break;
3202 }
3203
3204 case Primitive::kPrimFloat: {
3205 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3206 break;
3207 }
3208
3209 case Primitive::kPrimDouble: {
3210 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3211 break;
3212 }
3213
3214 case Primitive::kPrimVoid:
3215 LOG(FATAL) << "Unreachable type " << field_type;
3216 UNREACHABLE();
3217 }
3218
Calin Juravle77520bc2015-01-12 18:45:46 +00003219 codegen_->MaybeRecordImplicitNullCheck(instruction);
3220
Calin Juravle52c48962014-12-16 17:02:57 +00003221 if (is_volatile) {
3222 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3223 }
3224}
3225
3226void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3227 const FieldInfo& field_info) {
3228 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3229
3230 LocationSummary* locations =
3231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003232 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003233 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3234
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003235 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003236 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3237 locations->SetInAt(1, Location::RequiresFpuRegister());
3238 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003239 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003240 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003241 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003242 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003243 locations->AddTemp(Location::RequiresRegister());
3244 locations->AddTemp(Location::RequiresRegister());
3245 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003246}
3247
Calin Juravle52c48962014-12-16 17:02:57 +00003248void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3249 const FieldInfo& field_info) {
3250 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3251
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003252 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003253 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3254 Location value = locations->InAt(1);
3255 bool is_volatile = field_info.IsVolatile();
3256 Primitive::Type field_type = field_info.GetFieldType();
3257 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3258
3259 if (is_volatile) {
3260 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3261 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003262
3263 switch (field_type) {
3264 case Primitive::kPrimBoolean:
3265 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003266 if (value.IsConstant()) {
3267 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3268 __ movb(Address(base, offset), Immediate(v));
3269 } else {
3270 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3271 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003272 break;
3273 }
3274
3275 case Primitive::kPrimShort:
3276 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003277 if (value.IsConstant()) {
3278 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3279 __ movw(Address(base, offset), Immediate(v));
3280 } else {
3281 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3282 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003283 break;
3284 }
3285
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003286 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003287 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003288 if (value.IsConstant()) {
3289 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3290 __ movw(Address(base, offset), Immediate(v));
3291 } else {
3292 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3293 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003294 break;
3295 }
3296
3297 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003298 if (value.IsConstant()) {
3299 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3300 DCHECK(IsInt<32>(v));
3301 int32_t v_32 = v;
3302 __ movq(Address(base, offset), Immediate(v_32));
3303 } else {
3304 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3305 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003306 break;
3307 }
3308
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003309 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003310 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003311 break;
3312 }
3313
3314 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003315 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003316 break;
3317 }
3318
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003319 case Primitive::kPrimVoid:
3320 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003321 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003322 }
Calin Juravle52c48962014-12-16 17:02:57 +00003323
Calin Juravle77520bc2015-01-12 18:45:46 +00003324 codegen_->MaybeRecordImplicitNullCheck(instruction);
3325
3326 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3327 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3328 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3329 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3330 }
3331
Calin Juravle52c48962014-12-16 17:02:57 +00003332 if (is_volatile) {
3333 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3334 }
3335}
3336
3337void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3338 HandleFieldSet(instruction, instruction->GetFieldInfo());
3339}
3340
3341void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3342 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003343}
3344
3345void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003346 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003347}
3348
3349void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003350 HandleFieldGet(instruction, instruction->GetFieldInfo());
3351}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003352
Calin Juravle52c48962014-12-16 17:02:57 +00003353void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3354 HandleFieldGet(instruction);
3355}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003356
Calin Juravle52c48962014-12-16 17:02:57 +00003357void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3358 HandleFieldGet(instruction, instruction->GetFieldInfo());
3359}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003360
Calin Juravle52c48962014-12-16 17:02:57 +00003361void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3362 HandleFieldSet(instruction, instruction->GetFieldInfo());
3363}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003364
Calin Juravle52c48962014-12-16 17:02:57 +00003365void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3366 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003367}
3368
3369void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003370 LocationSummary* locations =
3371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003372 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3373 ? Location::RequiresRegister()
3374 : Location::Any();
3375 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003376 if (instruction->HasUses()) {
3377 locations->SetOut(Location::SameAsFirstInput());
3378 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003379}
3380
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003381void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003382 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3383 return;
3384 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003385 LocationSummary* locations = instruction->GetLocations();
3386 Location obj = locations->InAt(0);
3387
3388 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3389 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3390}
3391
3392void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003393 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003394 codegen_->AddSlowPath(slow_path);
3395
3396 LocationSummary* locations = instruction->GetLocations();
3397 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003398
3399 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003400 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003401 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003402 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003403 } else {
3404 DCHECK(obj.IsConstant()) << obj;
3405 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3406 __ jmp(slow_path->GetEntryLabel());
3407 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003408 }
3409 __ j(kEqual, slow_path->GetEntryLabel());
3410}
3411
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003412void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3413 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3414 GenerateImplicitNullCheck(instruction);
3415 } else {
3416 GenerateExplicitNullCheck(instruction);
3417 }
3418}
3419
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003420void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003421 LocationSummary* locations =
3422 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003423 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003424 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003425 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3426 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3427 } else {
3428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3429 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003430}
3431
3432void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3433 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003434 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003435 Location index = locations->InAt(1);
3436
3437 switch (instruction->GetType()) {
3438 case Primitive::kPrimBoolean: {
3439 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003440 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003441 if (index.IsConstant()) {
3442 __ movzxb(out, Address(obj,
3443 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3444 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003446 }
3447 break;
3448 }
3449
3450 case Primitive::kPrimByte: {
3451 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003452 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 if (index.IsConstant()) {
3454 __ movsxb(out, Address(obj,
3455 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3456 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003458 }
3459 break;
3460 }
3461
3462 case Primitive::kPrimShort: {
3463 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003464 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003465 if (index.IsConstant()) {
3466 __ movsxw(out, Address(obj,
3467 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3468 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003470 }
3471 break;
3472 }
3473
3474 case Primitive::kPrimChar: {
3475 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003476 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003477 if (index.IsConstant()) {
3478 __ movzxw(out, Address(obj,
3479 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3480 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003482 }
3483 break;
3484 }
3485
3486 case Primitive::kPrimInt:
3487 case Primitive::kPrimNot: {
3488 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3489 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003490 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003491 if (index.IsConstant()) {
3492 __ movl(out, Address(obj,
3493 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3494 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003495 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003496 }
3497 break;
3498 }
3499
3500 case Primitive::kPrimLong: {
3501 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003502 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003503 if (index.IsConstant()) {
3504 __ movq(out, Address(obj,
3505 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3506 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003507 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003508 }
3509 break;
3510 }
3511
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003512 case Primitive::kPrimFloat: {
3513 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003514 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003515 if (index.IsConstant()) {
3516 __ movss(out, Address(obj,
3517 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3518 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003519 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003520 }
3521 break;
3522 }
3523
3524 case Primitive::kPrimDouble: {
3525 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003526 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003527 if (index.IsConstant()) {
3528 __ movsd(out, Address(obj,
3529 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3530 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003531 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003532 }
3533 break;
3534 }
3535
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003536 case Primitive::kPrimVoid:
3537 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003538 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003539 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003540 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003541}
3542
3543void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003544 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003545
3546 bool needs_write_barrier =
3547 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3548 bool needs_runtime_call = instruction->NeedsTypeCheck();
3549
Nicolas Geoffray39468442014-09-02 15:17:15 +01003550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003551 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3552 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003553 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003554 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3555 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3556 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003557 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003558 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003559 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003560 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3561 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003562 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003563 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003564 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3565 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003566 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003567 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003568 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003569
3570 if (needs_write_barrier) {
3571 // Temporary registers for the write barrier.
3572 locations->AddTemp(Location::RequiresRegister());
3573 locations->AddTemp(Location::RequiresRegister());
3574 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003576}
3577
3578void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3579 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003581 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003582 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003583 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003584 bool needs_runtime_call = locations->WillCall();
3585 bool needs_write_barrier =
3586 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003587
3588 switch (value_type) {
3589 case Primitive::kPrimBoolean:
3590 case Primitive::kPrimByte: {
3591 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 if (index.IsConstant()) {
3593 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003594 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003596 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003597 __ movb(Address(obj, offset),
3598 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003600 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003601 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3603 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003604 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003606 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3607 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003609 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 break;
3611 }
3612
3613 case Primitive::kPrimShort:
3614 case Primitive::kPrimChar: {
3615 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 if (index.IsConstant()) {
3617 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003618 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003620 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003621 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003622 __ movw(Address(obj, offset),
3623 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003624 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003625 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003626 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003627 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3629 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003630 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003631 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003633 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3634 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003637 break;
3638 }
3639
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003640 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003641 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003642 if (!needs_runtime_call) {
3643 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3644 if (index.IsConstant()) {
3645 size_t offset =
3646 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3647 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003649 } else {
3650 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003651 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3652 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003653 }
3654 } else {
3655 DCHECK(index.IsRegister()) << index;
3656 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003657 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3658 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003659 } else {
3660 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003661 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003662 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003663 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003664 }
3665 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003666 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003667 if (needs_write_barrier) {
3668 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003669 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3670 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3671 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003672 }
3673 } else {
3674 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003675 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3676 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003677 DCHECK(!codegen_->IsLeafMethod());
3678 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3679 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003680 break;
3681 }
3682
3683 case Primitive::kPrimLong: {
3684 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003685 if (index.IsConstant()) {
3686 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04003687 if (value.IsRegister()) {
3688 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
3689 } else {
3690 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3691 DCHECK(IsInt<32>(v));
3692 int32_t v_32 = v;
3693 __ movq(Address(obj, offset), Immediate(v_32));
3694 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003695 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003696 if (value.IsRegister()) {
3697 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3698 value.AsRegister<CpuRegister>());
3699 } else {
3700 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3701 DCHECK(IsInt<32>(v));
3702 int32_t v_32 = v;
3703 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3704 Immediate(v_32));
3705 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003706 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003707 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003708 break;
3709 }
3710
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003711 case Primitive::kPrimFloat: {
3712 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3713 if (index.IsConstant()) {
3714 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3715 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003716 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003717 } else {
3718 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003719 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3720 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003721 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003722 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003723 break;
3724 }
3725
3726 case Primitive::kPrimDouble: {
3727 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3728 if (index.IsConstant()) {
3729 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3730 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003731 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003732 } else {
3733 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003734 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3735 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003736 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003737 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003738 break;
3739 }
3740
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003741 case Primitive::kPrimVoid:
3742 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003743 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744 }
3745}
3746
3747void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003748 LocationSummary* locations =
3749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003750 locations->SetInAt(0, Location::RequiresRegister());
3751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003752}
3753
3754void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3755 LocationSummary* locations = instruction->GetLocations();
3756 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003757 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3758 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003760 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003761}
3762
3763void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003764 LocationSummary* locations =
3765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003766 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003767 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003768 if (instruction->HasUses()) {
3769 locations->SetOut(Location::SameAsFirstInput());
3770 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771}
3772
3773void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3774 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003775 Location index_loc = locations->InAt(0);
3776 Location length_loc = locations->InAt(1);
3777 SlowPathCodeX86_64* slow_path =
3778 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003779
Mark Mendell99dbd682015-04-22 16:18:52 -04003780 if (length_loc.IsConstant()) {
3781 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3782 if (index_loc.IsConstant()) {
3783 // BCE will remove the bounds check if we are guarenteed to pass.
3784 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3785 if (index < 0 || index >= length) {
3786 codegen_->AddSlowPath(slow_path);
3787 __ jmp(slow_path->GetEntryLabel());
3788 } else {
3789 // Some optimization after BCE may have generated this, and we should not
3790 // generate a bounds check if it is a valid range.
3791 }
3792 return;
3793 }
3794
3795 // We have to reverse the jump condition because the length is the constant.
3796 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
3797 __ cmpl(index_reg, Immediate(length));
3798 codegen_->AddSlowPath(slow_path);
3799 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003800 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003801 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3802 if (index_loc.IsConstant()) {
3803 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3804 __ cmpl(length, Immediate(value));
3805 } else {
3806 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3807 }
3808 codegen_->AddSlowPath(slow_path);
3809 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003811}
3812
3813void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3814 CpuRegister card,
3815 CpuRegister object,
3816 CpuRegister value) {
3817 Label is_null;
3818 __ testl(value, value);
3819 __ j(kEqual, &is_null);
3820 __ gs()->movq(card, Address::Absolute(
3821 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3822 __ movq(temp, object);
3823 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3824 __ movb(Address(temp, card, TIMES_1, 0), card);
3825 __ Bind(&is_null);
3826}
3827
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003828void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3829 temp->SetLocations(nullptr);
3830}
3831
3832void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3833 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003834 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003835}
3836
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003837void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003838 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003839 LOG(FATAL) << "Unimplemented";
3840}
3841
3842void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003843 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3844}
3845
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003846void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3848}
3849
3850void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003851 HBasicBlock* block = instruction->GetBlock();
3852 if (block->GetLoopInformation() != nullptr) {
3853 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3854 // The back edge will generate the suspend check.
3855 return;
3856 }
3857 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3858 // The goto will generate the suspend check.
3859 return;
3860 }
3861 GenerateSuspendCheck(instruction, nullptr);
3862}
3863
3864void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3865 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003866 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003867 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003868 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003869 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003870 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003871 if (successor == nullptr) {
3872 __ j(kNotEqual, slow_path->GetEntryLabel());
3873 __ Bind(slow_path->GetReturnLabel());
3874 } else {
3875 __ j(kEqual, codegen_->GetLabelOf(successor));
3876 __ jmp(slow_path->GetEntryLabel());
3877 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003878}
3879
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003880X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3881 return codegen_->GetAssembler();
3882}
3883
3884void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3885 MoveOperands* move = moves_.Get(index);
3886 Location source = move->GetSource();
3887 Location destination = move->GetDestination();
3888
3889 if (source.IsRegister()) {
3890 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003891 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003892 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003893 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003894 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003895 } else {
3896 DCHECK(destination.IsDoubleStackSlot());
3897 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003898 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003899 }
3900 } else if (source.IsStackSlot()) {
3901 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003903 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003904 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003905 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003906 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003907 } else {
3908 DCHECK(destination.IsStackSlot());
3909 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3910 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3911 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003912 } else if (source.IsDoubleStackSlot()) {
3913 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003914 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003915 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003916 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003917 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3918 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003919 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003920 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003921 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3922 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3923 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003924 } else if (source.IsConstant()) {
3925 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003926 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3927 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003928 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003929 if (value == 0) {
3930 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3931 } else {
3932 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3933 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003934 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003935 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003936 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003937 }
3938 } else if (constant->IsLongConstant()) {
3939 int64_t value = constant->AsLongConstant()->GetValue();
3940 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003941 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003942 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003943 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003944 __ movq(CpuRegister(TMP), Immediate(value));
3945 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3946 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003947 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003948 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003949 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003950 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003951 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003952 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3953 if (value == 0) {
3954 // easy FP 0.0.
3955 __ xorps(dest, dest);
3956 } else {
3957 __ movl(CpuRegister(TMP), imm);
3958 __ movd(dest, CpuRegister(TMP));
3959 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003960 } else {
3961 DCHECK(destination.IsStackSlot()) << destination;
3962 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3963 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003964 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003965 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003966 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003967 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003968 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003969 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003970 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3971 if (value == 0) {
3972 __ xorpd(dest, dest);
3973 } else {
3974 __ movq(CpuRegister(TMP), imm);
3975 __ movd(dest, CpuRegister(TMP));
3976 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003977 } else {
3978 DCHECK(destination.IsDoubleStackSlot()) << destination;
3979 __ movq(CpuRegister(TMP), imm);
3980 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3981 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003982 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003983 } else if (source.IsFpuRegister()) {
3984 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003985 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003986 } else if (destination.IsStackSlot()) {
3987 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003988 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003989 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003990 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003991 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003992 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003993 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003994 }
3995}
3996
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003997void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003998 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003999 __ movl(Address(CpuRegister(RSP), mem), reg);
4000 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004001}
4002
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004003void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004004 ScratchRegisterScope ensure_scratch(
4005 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4006
4007 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4008 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4009 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4010 Address(CpuRegister(RSP), mem2 + stack_offset));
4011 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4012 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4013 CpuRegister(ensure_scratch.GetRegister()));
4014}
4015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004016void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4017 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4018 __ movq(Address(CpuRegister(RSP), mem), reg);
4019 __ movq(reg, CpuRegister(TMP));
4020}
4021
4022void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4023 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004024 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004025
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004026 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4027 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4028 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4029 Address(CpuRegister(RSP), mem2 + stack_offset));
4030 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4031 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4032 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004033}
4034
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004035void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4036 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4037 __ movss(Address(CpuRegister(RSP), mem), reg);
4038 __ movd(reg, CpuRegister(TMP));
4039}
4040
4041void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4042 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4043 __ movsd(Address(CpuRegister(RSP), mem), reg);
4044 __ movd(reg, CpuRegister(TMP));
4045}
4046
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004047void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4048 MoveOperands* move = moves_.Get(index);
4049 Location source = move->GetSource();
4050 Location destination = move->GetDestination();
4051
4052 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004053 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004054 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004055 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004056 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004057 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004058 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4060 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004061 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004062 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004063 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004064 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4065 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004066 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004067 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4068 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4069 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004070 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004072 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004073 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004074 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004076 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004077 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004078 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004079 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004080 }
4081}
4082
4083
4084void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4085 __ pushq(CpuRegister(reg));
4086}
4087
4088
4089void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4090 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004091}
4092
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004093void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4094 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4095 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4096 Immediate(mirror::Class::kStatusInitialized));
4097 __ j(kLess, slow_path->GetEntryLabel());
4098 __ Bind(slow_path->GetExitLabel());
4099 // No need for memory fence, thanks to the X86_64 memory model.
4100}
4101
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004102void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004103 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4104 ? LocationSummary::kCallOnSlowPath
4105 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004106 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004107 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004108 locations->SetOut(Location::RequiresRegister());
4109}
4110
4111void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004112 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004113 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004114 DCHECK(!cls->CanCallRuntime());
4115 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004116 codegen_->LoadCurrentMethod(out);
4117 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4118 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004119 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004120 codegen_->LoadCurrentMethod(out);
4121 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4122 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004123 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4124 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4125 codegen_->AddSlowPath(slow_path);
4126 __ testl(out, out);
4127 __ j(kEqual, slow_path->GetEntryLabel());
4128 if (cls->MustGenerateClinitCheck()) {
4129 GenerateClassInitializationCheck(slow_path, out);
4130 } else {
4131 __ Bind(slow_path->GetExitLabel());
4132 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004133 }
4134}
4135
4136void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4137 LocationSummary* locations =
4138 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4139 locations->SetInAt(0, Location::RequiresRegister());
4140 if (check->HasUses()) {
4141 locations->SetOut(Location::SameAsFirstInput());
4142 }
4143}
4144
4145void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004146 // We assume the class to not be null.
4147 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4148 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004149 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004150 GenerateClassInitializationCheck(slow_path,
4151 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004152}
4153
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004154void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4155 LocationSummary* locations =
4156 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4157 locations->SetOut(Location::RequiresRegister());
4158}
4159
4160void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4161 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4162 codegen_->AddSlowPath(slow_path);
4163
Roland Levillain271ab9c2014-11-27 15:23:57 +00004164 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004165 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004166 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4167 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004168 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4169 __ testl(out, out);
4170 __ j(kEqual, slow_path->GetEntryLabel());
4171 __ Bind(slow_path->GetExitLabel());
4172}
4173
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004174void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4175 LocationSummary* locations =
4176 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4177 locations->SetOut(Location::RequiresRegister());
4178}
4179
4180void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4181 Address address = Address::Absolute(
4182 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004183 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004184 __ gs()->movl(address, Immediate(0));
4185}
4186
4187void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4188 LocationSummary* locations =
4189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4190 InvokeRuntimeCallingConvention calling_convention;
4191 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4192}
4193
4194void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4195 __ gs()->call(
4196 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4197 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4198}
4199
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004200void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004201 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4202 ? LocationSummary::kNoCall
4203 : LocationSummary::kCallOnSlowPath;
4204 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4205 locations->SetInAt(0, Location::RequiresRegister());
4206 locations->SetInAt(1, Location::Any());
4207 locations->SetOut(Location::RequiresRegister());
4208}
4209
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004210void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004211 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004212 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004213 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004214 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004215 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4216 Label done, zero;
4217 SlowPathCodeX86_64* slow_path = nullptr;
4218
4219 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004220 // Avoid null check if we know obj is not null.
4221 if (instruction->MustDoNullCheck()) {
4222 __ testl(obj, obj);
4223 __ j(kEqual, &zero);
4224 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004225 // Compare the class of `obj` with `cls`.
4226 __ movl(out, Address(obj, class_offset));
4227 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004228 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004229 } else {
4230 DCHECK(cls.IsStackSlot()) << cls;
4231 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4232 }
4233 if (instruction->IsClassFinal()) {
4234 // Classes must be equal for the instanceof to succeed.
4235 __ j(kNotEqual, &zero);
4236 __ movl(out, Immediate(1));
4237 __ jmp(&done);
4238 } else {
4239 // If the classes are not equal, we go into a slow path.
4240 DCHECK(locations->OnlyCallsOnSlowPath());
4241 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004242 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004243 codegen_->AddSlowPath(slow_path);
4244 __ j(kNotEqual, slow_path->GetEntryLabel());
4245 __ movl(out, Immediate(1));
4246 __ jmp(&done);
4247 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004248
4249 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4250 __ Bind(&zero);
4251 __ movl(out, Immediate(0));
4252 }
4253
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004254 if (slow_path != nullptr) {
4255 __ Bind(slow_path->GetExitLabel());
4256 }
4257 __ Bind(&done);
4258}
4259
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004260void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4262 instruction, LocationSummary::kCallOnSlowPath);
4263 locations->SetInAt(0, Location::RequiresRegister());
4264 locations->SetInAt(1, Location::Any());
4265 locations->AddTemp(Location::RequiresRegister());
4266}
4267
4268void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4269 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004270 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004271 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004272 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004273 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4274 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4275 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4276 codegen_->AddSlowPath(slow_path);
4277
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004278 // Avoid null check if we know obj is not null.
4279 if (instruction->MustDoNullCheck()) {
4280 __ testl(obj, obj);
4281 __ j(kEqual, slow_path->GetExitLabel());
4282 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004283 // Compare the class of `obj` with `cls`.
4284 __ movl(temp, Address(obj, class_offset));
4285 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004286 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004287 } else {
4288 DCHECK(cls.IsStackSlot()) << cls;
4289 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4290 }
4291 // Classes must be equal for the checkcast to succeed.
4292 __ j(kNotEqual, slow_path->GetEntryLabel());
4293 __ Bind(slow_path->GetExitLabel());
4294}
4295
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004296void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4297 LocationSummary* locations =
4298 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4299 InvokeRuntimeCallingConvention calling_convention;
4300 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4301}
4302
4303void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4304 __ gs()->call(Address::Absolute(instruction->IsEnter()
4305 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4306 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4307 true));
4308 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4309}
4310
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004311void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4312void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4313void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4314
4315void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4316 LocationSummary* locations =
4317 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4318 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4319 || instruction->GetResultType() == Primitive::kPrimLong);
4320 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004321 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004322 locations->SetOut(Location::SameAsFirstInput());
4323}
4324
4325void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4326 HandleBitwiseOperation(instruction);
4327}
4328
4329void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4330 HandleBitwiseOperation(instruction);
4331}
4332
4333void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4334 HandleBitwiseOperation(instruction);
4335}
4336
4337void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4338 LocationSummary* locations = instruction->GetLocations();
4339 Location first = locations->InAt(0);
4340 Location second = locations->InAt(1);
4341 DCHECK(first.Equals(locations->Out()));
4342
4343 if (instruction->GetResultType() == Primitive::kPrimInt) {
4344 if (second.IsRegister()) {
4345 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004346 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004347 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004348 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004349 } else {
4350 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004351 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004352 }
4353 } else if (second.IsConstant()) {
4354 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4355 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004356 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004357 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004358 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004359 } else {
4360 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004361 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004362 }
4363 } else {
4364 Address address(CpuRegister(RSP), second.GetStackIndex());
4365 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004366 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004367 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004369 } else {
4370 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004371 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004372 }
4373 }
4374 } else {
4375 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004376 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4377 bool second_is_constant = false;
4378 int64_t value = 0;
4379 if (second.IsConstant()) {
4380 second_is_constant = true;
4381 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004382 }
Mark Mendell40741f32015-04-20 22:10:34 -04004383 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004384
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004385 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004386 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004387 if (is_int32_value) {
4388 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4389 } else {
4390 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4391 }
4392 } else if (second.IsDoubleStackSlot()) {
4393 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004394 } else {
4395 __ andq(first_reg, second.AsRegister<CpuRegister>());
4396 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004397 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004399 if (is_int32_value) {
4400 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4401 } else {
4402 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4403 }
4404 } else if (second.IsDoubleStackSlot()) {
4405 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004406 } else {
4407 __ orq(first_reg, second.AsRegister<CpuRegister>());
4408 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004409 } else {
4410 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004411 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004412 if (is_int32_value) {
4413 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4414 } else {
4415 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4416 }
4417 } else if (second.IsDoubleStackSlot()) {
4418 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004419 } else {
4420 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4421 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004422 }
4423 }
4424}
4425
Calin Juravleb1498f62015-02-16 13:13:29 +00004426void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4427 // Nothing to do, this should be removed during prepare for register allocator.
4428 UNUSED(instruction);
4429 LOG(FATAL) << "Unreachable";
4430}
4431
4432void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4433 // Nothing to do, this should be removed during prepare for register allocator.
4434 UNUSED(instruction);
4435 LOG(FATAL) << "Unreachable";
4436}
4437
Mark Mendellf55c3e02015-03-26 21:07:46 -04004438void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4439 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004440 X86_64Assembler* assembler = GetAssembler();
4441 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004442 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4443 // byte values. If used for vectors at a later time, this will need to be
4444 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004445 assembler->Align(4, 0);
4446 constant_area_start_ = assembler->CodeSize();
4447 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004448 }
4449
4450 // And finish up.
4451 CodeGenerator::Finalize(allocator);
4452}
4453
4454/**
4455 * Class to handle late fixup of offsets into constant area.
4456 */
4457class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4458 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004459 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004460 : codegen_(codegen), offset_into_constant_area_(offset) {}
4461
4462 private:
4463 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4464 // Patch the correct offset for the instruction. We use the address of the
4465 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4466 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4467 int relative_position = constant_offset - pos;
4468
4469 // Patch in the right value.
4470 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4471 }
4472
Mark Mendell39dcf552015-04-09 20:42:42 -04004473 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004474
4475 // Location in constant area that the fixup refers to.
4476 int offset_into_constant_area_;
4477};
4478
4479Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4480 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4481 return Address::RIP(fixup);
4482}
4483
4484Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4485 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4486 return Address::RIP(fixup);
4487}
4488
4489Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4490 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4491 return Address::RIP(fixup);
4492}
4493
4494Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4495 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4496 return Address::RIP(fixup);
4497}
4498
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004499} // namespace x86_64
4500} // namespace art