blob: b7b56515db0ab445a649f91d38e9f32fbeae4f0d [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
369 // temp = method;
370 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000371 if (!invoke->IsRecursive()) {
372 // temp = temp->dex_cache_resolved_methods_;
373 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
374 // temp = temp[index_in_cache]
375 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
376 // (temp + offset_of_quick_compiled_code)()
377 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
378 kX86_64WordSize).SizeValue()));
379 } else {
380 __ call(&frame_entry_label_);
381 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800382
383 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800384}
385
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100386void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
387 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
388}
389
390void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
391 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
392}
393
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100394size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
395 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
396 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100397}
398
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100399size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
400 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
401 return kX86_64WordSize;
402}
403
404size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
405 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
406 return kX86_64WordSize;
407}
408
409size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
410 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
411 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100412}
413
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000414static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000415// Use a fake return address register to mimic Quick.
416static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400417CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
418 const X86_64InstructionSetFeatures& isa_features,
419 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000420 : CodeGenerator(graph,
421 kNumberOfCpuRegisters,
422 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000423 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000424 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
425 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000426 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000427 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
428 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000429 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100430 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100431 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000432 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400433 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400434 isa_features_(isa_features),
435 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000436 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
437}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100438
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100439InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
440 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441 : HGraphVisitor(graph),
442 assembler_(codegen->GetAssembler()),
443 codegen_(codegen) {}
444
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100445Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100446 switch (type) {
447 case Primitive::kPrimLong:
448 case Primitive::kPrimByte:
449 case Primitive::kPrimBoolean:
450 case Primitive::kPrimChar:
451 case Primitive::kPrimShort:
452 case Primitive::kPrimInt:
453 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100454 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100455 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100456 }
457
458 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100459 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100460 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100461 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100462 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100463
464 case Primitive::kPrimVoid:
465 LOG(FATAL) << "Unreachable type " << type;
466 }
467
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100468 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100469}
470
Nicolas Geoffray98893962015-01-21 12:32:32 +0000471void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100472 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100473 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100474
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000475 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100476 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000477
Nicolas Geoffray98893962015-01-21 12:32:32 +0000478 if (is_baseline) {
479 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
480 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
481 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000482 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
483 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
484 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000485 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100486}
487
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100488static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100489 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100490}
David Srbecky9d8606d2015-04-12 09:35:32 +0100491
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100492static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100493 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494}
495
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100496void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100497 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000498 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100499 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700500 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000501 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100502
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000503 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100504 __ testq(CpuRegister(RAX), Address(
505 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100506 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100507 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000508
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000509 if (HasEmptyFrame()) {
510 return;
511 }
512
Nicolas Geoffray98893962015-01-21 12:32:32 +0000513 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000514 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000515 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000516 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100517 __ cfi().AdjustCFAOffset(kX86_64WordSize);
518 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000519 }
520 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100521
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100522 int adjust = GetFrameSize() - GetCoreSpillSize();
523 __ subq(CpuRegister(RSP), Immediate(adjust));
524 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000525 uint32_t xmm_spill_location = GetFpuSpillStart();
526 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100527
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000528 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
529 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100530 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
531 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
532 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100534 }
535
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
537}
538
539void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100540 __ cfi().RememberState();
541 if (!HasEmptyFrame()) {
542 uint32_t xmm_spill_location = GetFpuSpillStart();
543 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
544 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
545 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
546 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
547 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
548 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
549 }
550 }
551
552 int adjust = GetFrameSize() - GetCoreSpillSize();
553 __ addq(CpuRegister(RSP), Immediate(adjust));
554 __ cfi().AdjustCFAOffset(-adjust);
555
556 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
557 Register reg = kCoreCalleeSaves[i];
558 if (allocated_registers_.ContainsCoreRegister(reg)) {
559 __ popq(CpuRegister(reg));
560 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
561 __ cfi().Restore(DWARFReg(reg));
562 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000563 }
564 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100565 __ ret();
566 __ cfi().RestoreState();
567 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100568}
569
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100570void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
571 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100572}
573
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100574void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000575 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
577}
578
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
580 switch (load->GetType()) {
581 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584
585 case Primitive::kPrimInt:
586 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100587 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100589
590 case Primitive::kPrimBoolean:
591 case Primitive::kPrimByte:
592 case Primitive::kPrimChar:
593 case Primitive::kPrimShort:
594 case Primitive::kPrimVoid:
595 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700596 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100597 }
598
599 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700600 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601}
602
603void CodeGeneratorX86_64::Move(Location destination, Location source) {
604 if (source.Equals(destination)) {
605 return;
606 }
607 if (destination.IsRegister()) {
608 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000609 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100610 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000611 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100612 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000613 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100614 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615 } else {
616 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 Address(CpuRegister(RSP), source.GetStackIndex()));
619 }
620 } else if (destination.IsFpuRegister()) {
621 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 Address(CpuRegister(RSP), source.GetStackIndex()));
628 } else {
629 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100632 }
633 } else if (destination.IsStackSlot()) {
634 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 } else if (source.IsFpuRegister()) {
638 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000639 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500640 } else if (source.IsConstant()) {
641 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000642 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500643 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100644 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500645 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000646 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
647 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648 }
649 } else {
650 DCHECK(destination.IsDoubleStackSlot());
651 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100652 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100654 } else if (source.IsFpuRegister()) {
655 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000656 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500657 } else if (source.IsConstant()) {
658 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800659 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500660 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000661 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500662 } else {
663 DCHECK(constant->IsLongConstant());
664 value = constant->AsLongConstant()->GetValue();
665 }
666 __ movq(CpuRegister(TMP), Immediate(value));
667 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100668 } else {
669 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000670 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
671 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100672 }
673 }
674}
675
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100676void CodeGeneratorX86_64::Move(HInstruction* instruction,
677 Location location,
678 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000679 LocationSummary* locations = instruction->GetLocations();
680 if (locations != nullptr && locations->Out().Equals(location)) {
681 return;
682 }
683
684 if (locations != nullptr && locations->Out().IsConstant()) {
685 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000686 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
687 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000688 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000689 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000690 } else if (location.IsStackSlot()) {
691 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
692 } else {
693 DCHECK(location.IsConstant());
694 DCHECK_EQ(location.GetConstant(), const_to_move);
695 }
696 } else if (const_to_move->IsLongConstant()) {
697 int64_t value = const_to_move->AsLongConstant()->GetValue();
698 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000699 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000700 } else if (location.IsDoubleStackSlot()) {
701 __ movq(CpuRegister(TMP), Immediate(value));
702 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
703 } else {
704 DCHECK(location.IsConstant());
705 DCHECK_EQ(location.GetConstant(), const_to_move);
706 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100707 }
Roland Levillain476df552014-10-09 17:51:36 +0100708 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100709 switch (instruction->GetType()) {
710 case Primitive::kPrimBoolean:
711 case Primitive::kPrimByte:
712 case Primitive::kPrimChar:
713 case Primitive::kPrimShort:
714 case Primitive::kPrimInt:
715 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
718 break;
719
720 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000722 Move(location,
723 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100724 break;
725
726 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100727 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000729 } else if (instruction->IsTemporary()) {
730 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
731 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100733 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100734 switch (instruction->GetType()) {
735 case Primitive::kPrimBoolean:
736 case Primitive::kPrimByte:
737 case Primitive::kPrimChar:
738 case Primitive::kPrimShort:
739 case Primitive::kPrimInt:
740 case Primitive::kPrimNot:
741 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 case Primitive::kPrimFloat:
743 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000744 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100745 break;
746
747 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100748 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100749 }
750 }
751}
752
753void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
754 got->SetLocations(nullptr);
755}
756
757void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
758 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100759 DCHECK(!successor->IsExitBlock());
760
761 HBasicBlock* block = got->GetBlock();
762 HInstruction* previous = got->GetPrevious();
763
764 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000765 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100766 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
767 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
768 return;
769 }
770
771 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
772 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
773 }
774 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100775 __ jmp(codegen_->GetLabelOf(successor));
776 }
777}
778
779void LocationsBuilderX86_64::VisitExit(HExit* exit) {
780 exit->SetLocations(nullptr);
781}
782
783void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700784 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100785}
786
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700787void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
788 Label* true_target,
789 Label* false_target,
790 Label* always_true_target) {
791 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100792 if (cond->IsIntConstant()) {
793 // Constant condition, statically compared against 1.
794 int32_t cond_value = cond->AsIntConstant()->GetValue();
795 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700796 if (always_true_target != nullptr) {
797 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100798 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100799 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100800 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100801 DCHECK_EQ(cond_value, 0);
802 }
803 } else {
804 bool materialized =
805 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
806 // Moves do not affect the eflags register, so if the condition is
807 // evaluated just before the if, we don't need to evaluate it
808 // again.
809 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700810 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100811 if (materialized) {
812 if (!eflags_set) {
813 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700814 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100815 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000816 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 } else {
818 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
819 Immediate(0));
820 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700821 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700823 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 }
825 } else {
826 Location lhs = cond->GetLocations()->InAt(0);
827 Location rhs = cond->GetLocations()->InAt(1);
828 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000829 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000831 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000832 if (constant == 0) {
833 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
834 } else {
835 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
836 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100837 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000838 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 Address(CpuRegister(RSP), rhs.GetStackIndex()));
840 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700841 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700842 }
Dave Allison20dfc792014-06-16 20:44:29 -0700843 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700844 if (false_target != nullptr) {
845 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 }
847}
848
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700849void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
850 LocationSummary* locations =
851 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
852 HInstruction* cond = if_instr->InputAt(0);
853 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
854 locations->SetInAt(0, Location::Any());
855 }
856}
857
858void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
859 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
860 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
861 Label* always_true_target = true_target;
862 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
863 if_instr->IfTrueSuccessor())) {
864 always_true_target = nullptr;
865 }
866 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
867 if_instr->IfFalseSuccessor())) {
868 false_target = nullptr;
869 }
870 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
871}
872
873void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
874 LocationSummary* locations = new (GetGraph()->GetArena())
875 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
876 HInstruction* cond = deoptimize->InputAt(0);
877 DCHECK(cond->IsCondition());
878 if (cond->AsCondition()->NeedsMaterialization()) {
879 locations->SetInAt(0, Location::Any());
880 }
881}
882
883void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
884 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
885 DeoptimizationSlowPathX86_64(deoptimize);
886 codegen_->AddSlowPath(slow_path);
887 Label* slow_path_entry = slow_path->GetEntryLabel();
888 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
889}
890
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100891void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
892 local->SetLocations(nullptr);
893}
894
895void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
896 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
897}
898
899void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
900 local->SetLocations(nullptr);
901}
902
903void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
904 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700905 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100906}
907
908void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100909 LocationSummary* locations =
910 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100911 switch (store->InputAt(1)->GetType()) {
912 case Primitive::kPrimBoolean:
913 case Primitive::kPrimByte:
914 case Primitive::kPrimChar:
915 case Primitive::kPrimShort:
916 case Primitive::kPrimInt:
917 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100918 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100919 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
920 break;
921
922 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100923 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
925 break;
926
927 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100928 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100929 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100930}
931
932void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700933 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100934}
935
Dave Allison20dfc792014-06-16 20:44:29 -0700936void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100937 LocationSummary* locations =
938 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100939 locations->SetInAt(0, Location::RequiresRegister());
940 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100941 if (comp->NeedsMaterialization()) {
942 locations->SetOut(Location::RequiresRegister());
943 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944}
945
Dave Allison20dfc792014-06-16 20:44:29 -0700946void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
947 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100948 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000949 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100950 // Clear register: setcc only sets the low byte.
951 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000952 Location lhs = locations->InAt(0);
953 Location rhs = locations->InAt(1);
954 if (rhs.IsRegister()) {
955 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
956 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800957 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000958 if (constant == 0) {
959 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
960 } else {
961 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
962 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100963 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000964 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100965 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100966 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700967 }
968}
969
970void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
971 VisitCondition(comp);
972}
973
974void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
975 VisitCondition(comp);
976}
977
978void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
979 VisitCondition(comp);
980}
981
982void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
983 VisitCondition(comp);
984}
985
986void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
987 VisitCondition(comp);
988}
989
990void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
991 VisitCondition(comp);
992}
993
994void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
995 VisitCondition(comp);
996}
997
998void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
999 VisitCondition(comp);
1000}
1001
1002void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1003 VisitCondition(comp);
1004}
1005
1006void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1007 VisitCondition(comp);
1008}
1009
1010void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1011 VisitCondition(comp);
1012}
1013
1014void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1015 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001016}
1017
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001018void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001019 LocationSummary* locations =
1020 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001021 switch (compare->InputAt(0)->GetType()) {
1022 case Primitive::kPrimLong: {
1023 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001024 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001025 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1026 break;
1027 }
1028 case Primitive::kPrimFloat:
1029 case Primitive::kPrimDouble: {
1030 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001031 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001032 locations->SetOut(Location::RequiresRegister());
1033 break;
1034 }
1035 default:
1036 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1037 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001038}
1039
1040void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001041 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001043 Location left = locations->InAt(0);
1044 Location right = locations->InAt(1);
1045
1046 Label less, greater, done;
1047 Primitive::Type type = compare->InputAt(0)->GetType();
1048 switch (type) {
1049 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001050 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1051 if (right.IsConstant()) {
1052 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001053 if (IsInt<32>(value)) {
1054 if (value == 0) {
1055 __ testq(left_reg, left_reg);
1056 } else {
1057 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1058 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001059 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001060 // Value won't fit in an int.
1061 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001062 }
Mark Mendell40741f32015-04-20 22:10:34 -04001063 } else if (right.IsDoubleStackSlot()) {
1064 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001065 } else {
1066 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1067 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001068 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001069 }
1070 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001071 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1072 if (right.IsConstant()) {
1073 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1074 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1075 } else if (right.IsStackSlot()) {
1076 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1077 } else {
1078 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1079 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001080 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1081 break;
1082 }
1083 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001084 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1085 if (right.IsConstant()) {
1086 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1087 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1088 } else if (right.IsDoubleStackSlot()) {
1089 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1090 } else {
1091 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1092 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001093 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1094 break;
1095 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001096 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001097 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001098 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001099 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001100 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001101 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001102
Calin Juravle91debbc2014-11-26 19:01:09 +00001103 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001104 __ movl(out, Immediate(1));
1105 __ jmp(&done);
1106
1107 __ Bind(&less);
1108 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001109
1110 __ Bind(&done);
1111}
1112
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001113void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001114 LocationSummary* locations =
1115 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001116 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117}
1118
1119void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001120 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001121 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001122}
1123
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001124void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1127 locations->SetOut(Location::ConstantLocation(constant));
1128}
1129
1130void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1131 // Will be generated at use site.
1132 UNUSED(constant);
1133}
1134
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001136 LocationSummary* locations =
1137 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001138 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139}
1140
1141void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001142 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001143 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001144}
1145
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001146void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1147 LocationSummary* locations =
1148 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1149 locations->SetOut(Location::ConstantLocation(constant));
1150}
1151
1152void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1153 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001154 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001155}
1156
1157void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1158 LocationSummary* locations =
1159 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1160 locations->SetOut(Location::ConstantLocation(constant));
1161}
1162
1163void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1164 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001166}
1167
Calin Juravle27df7582015-04-17 19:12:31 +01001168void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1169 memory_barrier->SetLocations(nullptr);
1170}
1171
1172void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1173 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1174}
1175
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001176void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1177 ret->SetLocations(nullptr);
1178}
1179
1180void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001181 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001183}
1184
1185void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001186 LocationSummary* locations =
1187 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188 switch (ret->InputAt(0)->GetType()) {
1189 case Primitive::kPrimBoolean:
1190 case Primitive::kPrimByte:
1191 case Primitive::kPrimChar:
1192 case Primitive::kPrimShort:
1193 case Primitive::kPrimInt:
1194 case Primitive::kPrimNot:
1195 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001196 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001197 break;
1198
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001199 case Primitive::kPrimFloat:
1200 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001201 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001202 break;
1203
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001204 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207}
1208
1209void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1210 if (kIsDebugBuild) {
1211 switch (ret->InputAt(0)->GetType()) {
1212 case Primitive::kPrimBoolean:
1213 case Primitive::kPrimByte:
1214 case Primitive::kPrimChar:
1215 case Primitive::kPrimShort:
1216 case Primitive::kPrimInt:
1217 case Primitive::kPrimNot:
1218 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001220 break;
1221
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 case Primitive::kPrimFloat:
1223 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 XMM0);
1226 break;
1227
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230 }
1231 }
1232 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001233}
1234
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001235Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1236 switch (type) {
1237 case Primitive::kPrimBoolean:
1238 case Primitive::kPrimByte:
1239 case Primitive::kPrimChar:
1240 case Primitive::kPrimShort:
1241 case Primitive::kPrimInt:
1242 case Primitive::kPrimNot: {
1243 uint32_t index = gp_index_++;
1244 stack_index_++;
1245 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001246 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247 } else {
1248 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1249 }
1250 }
1251
1252 case Primitive::kPrimLong: {
1253 uint32_t index = gp_index_;
1254 stack_index_ += 2;
1255 if (index < calling_convention.GetNumberOfRegisters()) {
1256 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001257 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001258 } else {
1259 gp_index_ += 2;
1260 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1261 }
1262 }
1263
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001264 case Primitive::kPrimFloat: {
1265 uint32_t index = fp_index_++;
1266 stack_index_++;
1267 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001268 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269 } else {
1270 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1271 }
1272 }
1273
1274 case Primitive::kPrimDouble: {
1275 uint32_t index = fp_index_++;
1276 stack_index_ += 2;
1277 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001278 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 } else {
1280 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1281 }
1282 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001283
1284 case Primitive::kPrimVoid:
1285 LOG(FATAL) << "Unexpected parameter type " << type;
1286 break;
1287 }
1288 return Location();
1289}
1290
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001291void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001292 // Explicit clinit checks triggered by static invokes must have been
1293 // pruned by art::PrepareForRegisterAllocation.
1294 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1295
Mark Mendellfb8d2792015-03-31 22:16:59 -04001296 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001297 if (intrinsic.TryDispatch(invoke)) {
1298 return;
1299 }
1300
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001301 HandleInvoke(invoke);
1302}
1303
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001304static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1305 if (invoke->GetLocations()->Intrinsified()) {
1306 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1307 intrinsic.Dispatch(invoke);
1308 return true;
1309 }
1310 return false;
1311}
1312
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001313void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001314 // Explicit clinit checks triggered by static invokes must have been
1315 // pruned by art::PrepareForRegisterAllocation.
1316 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1317
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001318 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1319 return;
1320 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001321
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001322 codegen_->GenerateStaticOrDirectCall(
1323 invoke,
1324 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001325 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001326}
1327
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001328void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001329 LocationSummary* locations =
1330 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001331 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001332
1333 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001334 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001335 HInstruction* input = invoke->InputAt(i);
1336 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1337 }
1338
1339 switch (invoke->GetType()) {
1340 case Primitive::kPrimBoolean:
1341 case Primitive::kPrimByte:
1342 case Primitive::kPrimChar:
1343 case Primitive::kPrimShort:
1344 case Primitive::kPrimInt:
1345 case Primitive::kPrimNot:
1346 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001347 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001348 break;
1349
1350 case Primitive::kPrimVoid:
1351 break;
1352
1353 case Primitive::kPrimDouble:
1354 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001355 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001356 break;
1357 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001358}
1359
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001360void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001361 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001362 if (intrinsic.TryDispatch(invoke)) {
1363 return;
1364 }
1365
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001366 HandleInvoke(invoke);
1367}
1368
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001369void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001370 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1371 return;
1372 }
1373
Roland Levillain271ab9c2014-11-27 15:23:57 +00001374 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001375 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1376 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1377 LocationSummary* locations = invoke->GetLocations();
1378 Location receiver = locations->InAt(0);
1379 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1380 // temp = object->GetClass();
1381 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001382 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1383 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001384 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001385 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001386 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001387 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001388 // temp = temp->GetMethodAt(method_offset);
1389 __ movl(temp, Address(temp, method_offset));
1390 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001391 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001392 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001393
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001394 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001395 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001396}
1397
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001398void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1399 HandleInvoke(invoke);
1400 // Add the hidden argument.
1401 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1402}
1403
1404void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1405 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001407 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1408 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1409 LocationSummary* locations = invoke->GetLocations();
1410 Location receiver = locations->InAt(0);
1411 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1412
1413 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001414 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001415 Immediate(invoke->GetDexMethodIndex()));
1416
1417 // temp = object->GetClass();
1418 if (receiver.IsStackSlot()) {
1419 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1420 __ movl(temp, Address(temp, class_offset));
1421 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001422 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001423 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001424 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001425 // temp = temp->GetImtEntryAt(method_offset);
1426 __ movl(temp, Address(temp, method_offset));
1427 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001428 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001429 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001430
1431 DCHECK(!codegen_->IsLeafMethod());
1432 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1433}
1434
Roland Levillain88cb1752014-10-20 16:36:47 +01001435void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1436 LocationSummary* locations =
1437 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1438 switch (neg->GetResultType()) {
1439 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001440 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001441 locations->SetInAt(0, Location::RequiresRegister());
1442 locations->SetOut(Location::SameAsFirstInput());
1443 break;
1444
Roland Levillain88cb1752014-10-20 16:36:47 +01001445 case Primitive::kPrimFloat:
1446 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001447 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001448 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001449 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001450 break;
1451
1452 default:
1453 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1454 }
1455}
1456
1457void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1458 LocationSummary* locations = neg->GetLocations();
1459 Location out = locations->Out();
1460 Location in = locations->InAt(0);
1461 switch (neg->GetResultType()) {
1462 case Primitive::kPrimInt:
1463 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001464 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001465 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001466 break;
1467
1468 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001469 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001470 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001471 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001472 break;
1473
Roland Levillain5368c212014-11-27 15:03:41 +00001474 case Primitive::kPrimFloat: {
1475 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001476 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001477 // Implement float negation with an exclusive or with value
1478 // 0x80000000 (mask for bit 31, representing the sign of a
1479 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001480 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001481 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001482 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001483 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001484
Roland Levillain5368c212014-11-27 15:03:41 +00001485 case Primitive::kPrimDouble: {
1486 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001487 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001488 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001489 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001490 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001491 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001492 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001493 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001494 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001495
1496 default:
1497 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1498 }
1499}
1500
Roland Levillaindff1f282014-11-05 14:15:05 +00001501void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1502 LocationSummary* locations =
1503 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1504 Primitive::Type result_type = conversion->GetResultType();
1505 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001506 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001507
David Brazdilb2bd1c52015-03-25 11:17:37 +00001508 // The Java language does not allow treating boolean as an integral type but
1509 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001510
Roland Levillaindff1f282014-11-05 14:15:05 +00001511 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001512 case Primitive::kPrimByte:
1513 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001514 case Primitive::kPrimBoolean:
1515 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001516 case Primitive::kPrimShort:
1517 case Primitive::kPrimInt:
1518 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001519 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001520 locations->SetInAt(0, Location::Any());
1521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1522 break;
1523
1524 default:
1525 LOG(FATAL) << "Unexpected type conversion from " << input_type
1526 << " to " << result_type;
1527 }
1528 break;
1529
Roland Levillain01a8d712014-11-14 16:27:39 +00001530 case Primitive::kPrimShort:
1531 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001532 case Primitive::kPrimBoolean:
1533 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001534 case Primitive::kPrimByte:
1535 case Primitive::kPrimInt:
1536 case Primitive::kPrimChar:
1537 // Processing a Dex `int-to-short' instruction.
1538 locations->SetInAt(0, Location::Any());
1539 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1540 break;
1541
1542 default:
1543 LOG(FATAL) << "Unexpected type conversion from " << input_type
1544 << " to " << result_type;
1545 }
1546 break;
1547
Roland Levillain946e1432014-11-11 17:35:19 +00001548 case Primitive::kPrimInt:
1549 switch (input_type) {
1550 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001551 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001552 locations->SetInAt(0, Location::Any());
1553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1554 break;
1555
1556 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001557 // Processing a Dex `float-to-int' instruction.
1558 locations->SetInAt(0, Location::RequiresFpuRegister());
1559 locations->SetOut(Location::RequiresRegister());
1560 locations->AddTemp(Location::RequiresFpuRegister());
1561 break;
1562
Roland Levillain946e1432014-11-11 17:35:19 +00001563 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001564 // Processing a Dex `double-to-int' instruction.
1565 locations->SetInAt(0, Location::RequiresFpuRegister());
1566 locations->SetOut(Location::RequiresRegister());
1567 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001568 break;
1569
1570 default:
1571 LOG(FATAL) << "Unexpected type conversion from " << input_type
1572 << " to " << result_type;
1573 }
1574 break;
1575
Roland Levillaindff1f282014-11-05 14:15:05 +00001576 case Primitive::kPrimLong:
1577 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001578 case Primitive::kPrimBoolean:
1579 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001580 case Primitive::kPrimByte:
1581 case Primitive::kPrimShort:
1582 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001583 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001584 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001585 // TODO: We would benefit from a (to-be-implemented)
1586 // Location::RegisterOrStackSlot requirement for this input.
1587 locations->SetInAt(0, Location::RequiresRegister());
1588 locations->SetOut(Location::RequiresRegister());
1589 break;
1590
1591 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001592 // Processing a Dex `float-to-long' instruction.
1593 locations->SetInAt(0, Location::RequiresFpuRegister());
1594 locations->SetOut(Location::RequiresRegister());
1595 locations->AddTemp(Location::RequiresFpuRegister());
1596 break;
1597
Roland Levillaindff1f282014-11-05 14:15:05 +00001598 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001599 // Processing a Dex `double-to-long' instruction.
1600 locations->SetInAt(0, Location::RequiresFpuRegister());
1601 locations->SetOut(Location::RequiresRegister());
1602 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001603 break;
1604
1605 default:
1606 LOG(FATAL) << "Unexpected type conversion from " << input_type
1607 << " to " << result_type;
1608 }
1609 break;
1610
Roland Levillain981e4542014-11-14 11:47:14 +00001611 case Primitive::kPrimChar:
1612 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001613 case Primitive::kPrimBoolean:
1614 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001615 case Primitive::kPrimByte:
1616 case Primitive::kPrimShort:
1617 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001618 // Processing a Dex `int-to-char' instruction.
1619 locations->SetInAt(0, Location::Any());
1620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1621 break;
1622
1623 default:
1624 LOG(FATAL) << "Unexpected type conversion from " << input_type
1625 << " to " << result_type;
1626 }
1627 break;
1628
Roland Levillaindff1f282014-11-05 14:15:05 +00001629 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001630 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001631 case Primitive::kPrimBoolean:
1632 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001633 case Primitive::kPrimByte:
1634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimChar:
1637 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001638 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001639 locations->SetOut(Location::RequiresFpuRegister());
1640 break;
1641
1642 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001643 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001644 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001645 locations->SetOut(Location::RequiresFpuRegister());
1646 break;
1647
Roland Levillaincff13742014-11-17 14:32:17 +00001648 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001649 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001650 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001651 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001652 break;
1653
1654 default:
1655 LOG(FATAL) << "Unexpected type conversion from " << input_type
1656 << " to " << result_type;
1657 };
1658 break;
1659
Roland Levillaindff1f282014-11-05 14:15:05 +00001660 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001661 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001662 case Primitive::kPrimBoolean:
1663 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001664 case Primitive::kPrimByte:
1665 case Primitive::kPrimShort:
1666 case Primitive::kPrimInt:
1667 case Primitive::kPrimChar:
1668 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001669 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001670 locations->SetOut(Location::RequiresFpuRegister());
1671 break;
1672
1673 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001674 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001675 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001676 locations->SetOut(Location::RequiresFpuRegister());
1677 break;
1678
Roland Levillaincff13742014-11-17 14:32:17 +00001679 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001680 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001681 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001682 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001683 break;
1684
1685 default:
1686 LOG(FATAL) << "Unexpected type conversion from " << input_type
1687 << " to " << result_type;
1688 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001689 break;
1690
1691 default:
1692 LOG(FATAL) << "Unexpected type conversion from " << input_type
1693 << " to " << result_type;
1694 }
1695}
1696
1697void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1698 LocationSummary* locations = conversion->GetLocations();
1699 Location out = locations->Out();
1700 Location in = locations->InAt(0);
1701 Primitive::Type result_type = conversion->GetResultType();
1702 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001703 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001704 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001705 case Primitive::kPrimByte:
1706 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001707 case Primitive::kPrimBoolean:
1708 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001709 case Primitive::kPrimShort:
1710 case Primitive::kPrimInt:
1711 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001712 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001713 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001714 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001715 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001716 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001717 Address(CpuRegister(RSP), in.GetStackIndex()));
1718 } else {
1719 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001720 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001721 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1722 }
1723 break;
1724
1725 default:
1726 LOG(FATAL) << "Unexpected type conversion from " << input_type
1727 << " to " << result_type;
1728 }
1729 break;
1730
Roland Levillain01a8d712014-11-14 16:27:39 +00001731 case Primitive::kPrimShort:
1732 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001733 case Primitive::kPrimBoolean:
1734 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001735 case Primitive::kPrimByte:
1736 case Primitive::kPrimInt:
1737 case Primitive::kPrimChar:
1738 // Processing a Dex `int-to-short' instruction.
1739 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001740 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001741 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001742 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001743 Address(CpuRegister(RSP), in.GetStackIndex()));
1744 } else {
1745 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001746 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001747 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1748 }
1749 break;
1750
1751 default:
1752 LOG(FATAL) << "Unexpected type conversion from " << input_type
1753 << " to " << result_type;
1754 }
1755 break;
1756
Roland Levillain946e1432014-11-11 17:35:19 +00001757 case Primitive::kPrimInt:
1758 switch (input_type) {
1759 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001760 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001761 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001762 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001763 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001765 Address(CpuRegister(RSP), in.GetStackIndex()));
1766 } else {
1767 DCHECK(in.IsConstant());
1768 DCHECK(in.GetConstant()->IsLongConstant());
1769 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001770 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001771 }
1772 break;
1773
Roland Levillain3f8f9362014-12-02 17:45:01 +00001774 case Primitive::kPrimFloat: {
1775 // Processing a Dex `float-to-int' instruction.
1776 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1777 CpuRegister output = out.AsRegister<CpuRegister>();
1778 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1779 Label done, nan;
1780
1781 __ movl(output, Immediate(kPrimIntMax));
1782 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001783 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001784 // if input >= temp goto done
1785 __ comiss(input, temp);
1786 __ j(kAboveEqual, &done);
1787 // if input == NaN goto nan
1788 __ j(kUnordered, &nan);
1789 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001790 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001791 __ jmp(&done);
1792 __ Bind(&nan);
1793 // output = 0
1794 __ xorl(output, output);
1795 __ Bind(&done);
1796 break;
1797 }
1798
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001799 case Primitive::kPrimDouble: {
1800 // Processing a Dex `double-to-int' instruction.
1801 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1802 CpuRegister output = out.AsRegister<CpuRegister>();
1803 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1804 Label done, nan;
1805
1806 __ movl(output, Immediate(kPrimIntMax));
1807 // temp = int-to-double(output)
1808 __ cvtsi2sd(temp, output);
1809 // if input >= temp goto done
1810 __ comisd(input, temp);
1811 __ j(kAboveEqual, &done);
1812 // if input == NaN goto nan
1813 __ j(kUnordered, &nan);
1814 // output = double-to-int-truncate(input)
1815 __ cvttsd2si(output, input);
1816 __ jmp(&done);
1817 __ Bind(&nan);
1818 // output = 0
1819 __ xorl(output, output);
1820 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001821 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001822 }
Roland Levillain946e1432014-11-11 17:35:19 +00001823
1824 default:
1825 LOG(FATAL) << "Unexpected type conversion from " << input_type
1826 << " to " << result_type;
1827 }
1828 break;
1829
Roland Levillaindff1f282014-11-05 14:15:05 +00001830 case Primitive::kPrimLong:
1831 switch (input_type) {
1832 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001833 case Primitive::kPrimBoolean:
1834 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001835 case Primitive::kPrimByte:
1836 case Primitive::kPrimShort:
1837 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001838 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001839 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001840 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001841 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001842 break;
1843
Roland Levillain624279f2014-12-04 11:54:28 +00001844 case Primitive::kPrimFloat: {
1845 // Processing a Dex `float-to-long' instruction.
1846 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1847 CpuRegister output = out.AsRegister<CpuRegister>();
1848 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1849 Label done, nan;
1850
1851 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001852 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001853 __ cvtsi2ss(temp, output, true);
1854 // if input >= temp goto done
1855 __ comiss(input, temp);
1856 __ j(kAboveEqual, &done);
1857 // if input == NaN goto nan
1858 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001859 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001860 __ cvttss2si(output, input, true);
1861 __ jmp(&done);
1862 __ Bind(&nan);
1863 // output = 0
1864 __ xorq(output, output);
1865 __ Bind(&done);
1866 break;
1867 }
1868
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001869 case Primitive::kPrimDouble: {
1870 // Processing a Dex `double-to-long' instruction.
1871 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1872 CpuRegister output = out.AsRegister<CpuRegister>();
1873 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1874 Label done, nan;
1875
1876 __ movq(output, Immediate(kPrimLongMax));
1877 // temp = long-to-double(output)
1878 __ cvtsi2sd(temp, output, true);
1879 // if input >= temp goto done
1880 __ comisd(input, temp);
1881 __ j(kAboveEqual, &done);
1882 // if input == NaN goto nan
1883 __ j(kUnordered, &nan);
1884 // output = double-to-long-truncate(input)
1885 __ cvttsd2si(output, input, true);
1886 __ jmp(&done);
1887 __ Bind(&nan);
1888 // output = 0
1889 __ xorq(output, output);
1890 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001891 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001892 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001893
1894 default:
1895 LOG(FATAL) << "Unexpected type conversion from " << input_type
1896 << " to " << result_type;
1897 }
1898 break;
1899
Roland Levillain981e4542014-11-14 11:47:14 +00001900 case Primitive::kPrimChar:
1901 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001902 case Primitive::kPrimBoolean:
1903 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001904 case Primitive::kPrimByte:
1905 case Primitive::kPrimShort:
1906 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001907 // Processing a Dex `int-to-char' instruction.
1908 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001909 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001910 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001911 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001912 Address(CpuRegister(RSP), in.GetStackIndex()));
1913 } else {
1914 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001915 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001916 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1917 }
1918 break;
1919
1920 default:
1921 LOG(FATAL) << "Unexpected type conversion from " << input_type
1922 << " to " << result_type;
1923 }
1924 break;
1925
Roland Levillaindff1f282014-11-05 14:15:05 +00001926 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001927 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001928 case Primitive::kPrimBoolean:
1929 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001930 case Primitive::kPrimByte:
1931 case Primitive::kPrimShort:
1932 case Primitive::kPrimInt:
1933 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001934 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001935 if (in.IsRegister()) {
1936 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1937 } else if (in.IsConstant()) {
1938 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
1939 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1940 if (v == 0) {
1941 __ xorps(dest, dest);
1942 } else {
1943 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1944 }
1945 } else {
1946 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1947 Address(CpuRegister(RSP), in.GetStackIndex()), false);
1948 }
Roland Levillaincff13742014-11-17 14:32:17 +00001949 break;
1950
1951 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001952 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001953 if (in.IsRegister()) {
1954 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1955 } else if (in.IsConstant()) {
1956 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
1957 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1958 if (v == 0) {
1959 __ xorps(dest, dest);
1960 } else {
1961 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1962 }
1963 } else {
1964 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1965 Address(CpuRegister(RSP), in.GetStackIndex()), true);
1966 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001967 break;
1968
Roland Levillaincff13742014-11-17 14:32:17 +00001969 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001970 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001971 if (in.IsFpuRegister()) {
1972 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
1973 } else if (in.IsConstant()) {
1974 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
1975 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1976 if (bit_cast<int64_t, double>(v) == 0) {
1977 __ xorps(dest, dest);
1978 } else {
1979 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1980 }
1981 } else {
1982 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
1983 Address(CpuRegister(RSP), in.GetStackIndex()));
1984 }
Roland Levillaincff13742014-11-17 14:32:17 +00001985 break;
1986
1987 default:
1988 LOG(FATAL) << "Unexpected type conversion from " << input_type
1989 << " to " << result_type;
1990 };
1991 break;
1992
Roland Levillaindff1f282014-11-05 14:15:05 +00001993 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001994 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001995 case Primitive::kPrimBoolean:
1996 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001997 case Primitive::kPrimByte:
1998 case Primitive::kPrimShort:
1999 case Primitive::kPrimInt:
2000 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002001 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002002 if (in.IsRegister()) {
2003 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2004 } else if (in.IsConstant()) {
2005 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2006 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2007 if (v == 0) {
2008 __ xorpd(dest, dest);
2009 } else {
2010 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2011 }
2012 } else {
2013 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2014 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2015 }
Roland Levillaincff13742014-11-17 14:32:17 +00002016 break;
2017
2018 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002019 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002020 if (in.IsRegister()) {
2021 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2022 } else if (in.IsConstant()) {
2023 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2024 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2025 if (v == 0) {
2026 __ xorpd(dest, dest);
2027 } else {
2028 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2029 }
2030 } else {
2031 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2032 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2033 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002034 break;
2035
Roland Levillaincff13742014-11-17 14:32:17 +00002036 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002037 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002038 if (in.IsFpuRegister()) {
2039 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2040 } else if (in.IsConstant()) {
2041 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2042 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2043 if (bit_cast<int32_t, float>(v) == 0) {
2044 __ xorpd(dest, dest);
2045 } else {
2046 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2047 }
2048 } else {
2049 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2050 Address(CpuRegister(RSP), in.GetStackIndex()));
2051 }
Roland Levillaincff13742014-11-17 14:32:17 +00002052 break;
2053
2054 default:
2055 LOG(FATAL) << "Unexpected type conversion from " << input_type
2056 << " to " << result_type;
2057 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002058 break;
2059
2060 default:
2061 LOG(FATAL) << "Unexpected type conversion from " << input_type
2062 << " to " << result_type;
2063 }
2064}
2065
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002066void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002067 LocationSummary* locations =
2068 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002069 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002070 case Primitive::kPrimInt: {
2071 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002072 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002074 break;
2075 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002076
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002077 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002078 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002079 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002080 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002082 break;
2083 }
2084
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002085 case Primitive::kPrimDouble:
2086 case Primitive::kPrimFloat: {
2087 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002088 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002089 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002090 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002091 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002092
2093 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002094 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002095 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002096}
2097
2098void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2099 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002100 Location first = locations->InAt(0);
2101 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002102 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002103
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002105 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002106 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002107 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2108 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2109 } else {
2110 __ leal(out.AsRegister<CpuRegister>(), Address(
2111 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2112 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002113 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002114 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2115 __ addl(out.AsRegister<CpuRegister>(),
2116 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2117 } else {
2118 __ leal(out.AsRegister<CpuRegister>(), Address(
2119 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2120 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002121 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002122 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002124 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002125 break;
2126 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002128 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002129 if (second.IsRegister()) {
2130 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2131 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2132 } else {
2133 __ leaq(out.AsRegister<CpuRegister>(), Address(
2134 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2135 }
2136 } else {
2137 DCHECK(second.IsConstant());
2138 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2139 int32_t int32_value = Low32Bits(value);
2140 DCHECK_EQ(int32_value, value);
2141 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2142 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2143 } else {
2144 __ leaq(out.AsRegister<CpuRegister>(), Address(
2145 first.AsRegister<CpuRegister>(), int32_value));
2146 }
2147 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002148 break;
2149 }
2150
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002151 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002152 if (second.IsFpuRegister()) {
2153 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2154 } else if (second.IsConstant()) {
2155 __ addss(first.AsFpuRegister<XmmRegister>(),
2156 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2157 } else {
2158 DCHECK(second.IsStackSlot());
2159 __ addss(first.AsFpuRegister<XmmRegister>(),
2160 Address(CpuRegister(RSP), second.GetStackIndex()));
2161 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002162 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002163 }
2164
2165 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002166 if (second.IsFpuRegister()) {
2167 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2168 } else if (second.IsConstant()) {
2169 __ addsd(first.AsFpuRegister<XmmRegister>(),
2170 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2171 } else {
2172 DCHECK(second.IsDoubleStackSlot());
2173 __ addsd(first.AsFpuRegister<XmmRegister>(),
2174 Address(CpuRegister(RSP), second.GetStackIndex()));
2175 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002176 break;
2177 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002178
2179 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002180 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002181 }
2182}
2183
2184void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002187 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002188 case Primitive::kPrimInt: {
2189 locations->SetInAt(0, Location::RequiresRegister());
2190 locations->SetInAt(1, Location::Any());
2191 locations->SetOut(Location::SameAsFirstInput());
2192 break;
2193 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002194 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002195 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002196 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002197 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002198 break;
2199 }
Calin Juravle11351682014-10-23 15:38:15 +01002200 case Primitive::kPrimFloat:
2201 case Primitive::kPrimDouble: {
2202 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002203 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002204 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205 break;
Calin Juravle11351682014-10-23 15:38:15 +01002206 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002207 default:
Calin Juravle11351682014-10-23 15:38:15 +01002208 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002209 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002210}
2211
2212void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2213 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002214 Location first = locations->InAt(0);
2215 Location second = locations->InAt(1);
2216 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002217 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002218 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002219 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002220 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002221 } else if (second.IsConstant()) {
2222 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002223 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002224 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002225 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002226 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002227 break;
2228 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002229 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002230 if (second.IsConstant()) {
2231 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2232 DCHECK(IsInt<32>(value));
2233 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2234 } else {
2235 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2236 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237 break;
2238 }
2239
Calin Juravle11351682014-10-23 15:38:15 +01002240 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002241 if (second.IsFpuRegister()) {
2242 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2243 } else if (second.IsConstant()) {
2244 __ subss(first.AsFpuRegister<XmmRegister>(),
2245 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2246 } else {
2247 DCHECK(second.IsStackSlot());
2248 __ subss(first.AsFpuRegister<XmmRegister>(),
2249 Address(CpuRegister(RSP), second.GetStackIndex()));
2250 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002251 break;
Calin Juravle11351682014-10-23 15:38:15 +01002252 }
2253
2254 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002255 if (second.IsFpuRegister()) {
2256 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2257 } else if (second.IsConstant()) {
2258 __ subsd(first.AsFpuRegister<XmmRegister>(),
2259 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2260 } else {
2261 DCHECK(second.IsDoubleStackSlot());
2262 __ subsd(first.AsFpuRegister<XmmRegister>(),
2263 Address(CpuRegister(RSP), second.GetStackIndex()));
2264 }
Calin Juravle11351682014-10-23 15:38:15 +01002265 break;
2266 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002267
2268 default:
Calin Juravle11351682014-10-23 15:38:15 +01002269 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002270 }
2271}
2272
Calin Juravle34bacdf2014-10-07 20:23:36 +01002273void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2274 LocationSummary* locations =
2275 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2276 switch (mul->GetResultType()) {
2277 case Primitive::kPrimInt: {
2278 locations->SetInAt(0, Location::RequiresRegister());
2279 locations->SetInAt(1, Location::Any());
2280 locations->SetOut(Location::SameAsFirstInput());
2281 break;
2282 }
2283 case Primitive::kPrimLong: {
2284 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002285 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2286 if (locations->InAt(1).IsConstant()) {
2287 // Can use 3 operand multiply.
2288 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2289 } else {
2290 locations->SetOut(Location::SameAsFirstInput());
2291 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002292 break;
2293 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002294 case Primitive::kPrimFloat:
2295 case Primitive::kPrimDouble: {
2296 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002297 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002298 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002299 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002300 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002301
2302 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002303 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002304 }
2305}
2306
2307void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2308 LocationSummary* locations = mul->GetLocations();
2309 Location first = locations->InAt(0);
2310 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002311 switch (mul->GetResultType()) {
2312 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002313 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002314 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002316 } else if (second.IsConstant()) {
2317 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002318 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002319 } else {
2320 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002321 __ imull(first.AsRegister<CpuRegister>(),
2322 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002323 }
2324 break;
2325 }
2326 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002327 if (second.IsConstant()) {
2328 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2329 DCHECK(IsInt<32>(value));
2330 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2331 first.AsRegister<CpuRegister>(),
2332 Immediate(static_cast<int32_t>(value)));
2333 } else {
2334 DCHECK(first.Equals(locations->Out()));
2335 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2336 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002337 break;
2338 }
2339
Calin Juravleb5bfa962014-10-21 18:02:24 +01002340 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002341 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002342 if (second.IsFpuRegister()) {
2343 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2344 } else if (second.IsConstant()) {
2345 __ mulss(first.AsFpuRegister<XmmRegister>(),
2346 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2347 } else {
2348 DCHECK(second.IsStackSlot());
2349 __ mulss(first.AsFpuRegister<XmmRegister>(),
2350 Address(CpuRegister(RSP), second.GetStackIndex()));
2351 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002352 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002353 }
2354
2355 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002356 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002357 if (second.IsFpuRegister()) {
2358 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2359 } else if (second.IsConstant()) {
2360 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2361 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2362 } else {
2363 DCHECK(second.IsDoubleStackSlot());
2364 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2365 Address(CpuRegister(RSP), second.GetStackIndex()));
2366 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002367 break;
2368 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002369
2370 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002371 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002372 }
2373}
2374
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002375void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2376 uint32_t stack_adjustment, bool is_float) {
2377 if (source.IsStackSlot()) {
2378 DCHECK(is_float);
2379 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2380 } else if (source.IsDoubleStackSlot()) {
2381 DCHECK(!is_float);
2382 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2383 } else {
2384 // Write the value to the temporary location on the stack and load to FP stack.
2385 if (is_float) {
2386 Location stack_temp = Location::StackSlot(temp_offset);
2387 codegen_->Move(stack_temp, source);
2388 __ flds(Address(CpuRegister(RSP), temp_offset));
2389 } else {
2390 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2391 codegen_->Move(stack_temp, source);
2392 __ fldl(Address(CpuRegister(RSP), temp_offset));
2393 }
2394 }
2395}
2396
2397void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2398 Primitive::Type type = rem->GetResultType();
2399 bool is_float = type == Primitive::kPrimFloat;
2400 size_t elem_size = Primitive::ComponentSize(type);
2401 LocationSummary* locations = rem->GetLocations();
2402 Location first = locations->InAt(0);
2403 Location second = locations->InAt(1);
2404 Location out = locations->Out();
2405
2406 // Create stack space for 2 elements.
2407 // TODO: enhance register allocator to ask for stack temporaries.
2408 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2409
2410 // Load the values to the FP stack in reverse order, using temporaries if needed.
2411 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2412 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2413
2414 // Loop doing FPREM until we stabilize.
2415 Label retry;
2416 __ Bind(&retry);
2417 __ fprem();
2418
2419 // Move FP status to AX.
2420 __ fstsw();
2421
2422 // And see if the argument reduction is complete. This is signaled by the
2423 // C2 FPU flag bit set to 0.
2424 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2425 __ j(kNotEqual, &retry);
2426
2427 // We have settled on the final value. Retrieve it into an XMM register.
2428 // Store FP top of stack to real stack.
2429 if (is_float) {
2430 __ fsts(Address(CpuRegister(RSP), 0));
2431 } else {
2432 __ fstl(Address(CpuRegister(RSP), 0));
2433 }
2434
2435 // Pop the 2 items from the FP stack.
2436 __ fucompp();
2437
2438 // Load the value from the stack into an XMM register.
2439 DCHECK(out.IsFpuRegister()) << out;
2440 if (is_float) {
2441 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2442 } else {
2443 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2444 }
2445
2446 // And remove the temporary stack space we allocated.
2447 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2448}
2449
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002450void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2451 DCHECK(instruction->IsDiv() || instruction->IsRem());
2452
2453 LocationSummary* locations = instruction->GetLocations();
2454 Location second = locations->InAt(1);
2455 DCHECK(second.IsConstant());
2456
2457 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2458 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002459 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002460
2461 DCHECK(imm == 1 || imm == -1);
2462
2463 switch (instruction->GetResultType()) {
2464 case Primitive::kPrimInt: {
2465 if (instruction->IsRem()) {
2466 __ xorl(output_register, output_register);
2467 } else {
2468 __ movl(output_register, input_register);
2469 if (imm == -1) {
2470 __ negl(output_register);
2471 }
2472 }
2473 break;
2474 }
2475
2476 case Primitive::kPrimLong: {
2477 if (instruction->IsRem()) {
2478 __ xorq(output_register, output_register);
2479 } else {
2480 __ movq(output_register, input_register);
2481 if (imm == -1) {
2482 __ negq(output_register);
2483 }
2484 }
2485 break;
2486 }
2487
2488 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002489 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002490 }
2491}
2492
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002493void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002494 LocationSummary* locations = instruction->GetLocations();
2495 Location second = locations->InAt(1);
2496
2497 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2498 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2499
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002500 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002501
2502 DCHECK(IsPowerOfTwo(std::abs(imm)));
2503
2504 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2505
2506 if (instruction->GetResultType() == Primitive::kPrimInt) {
2507 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2508 __ testl(numerator, numerator);
2509 __ cmov(kGreaterEqual, tmp, numerator);
2510 int shift = CTZ(imm);
2511 __ sarl(tmp, Immediate(shift));
2512
2513 if (imm < 0) {
2514 __ negl(tmp);
2515 }
2516
2517 __ movl(output_register, tmp);
2518 } else {
2519 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2520 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2521
2522 __ movq(rdx, Immediate(std::abs(imm) - 1));
2523 __ addq(rdx, numerator);
2524 __ testq(numerator, numerator);
2525 __ cmov(kGreaterEqual, rdx, numerator);
2526 int shift = CTZ(imm);
2527 __ sarq(rdx, Immediate(shift));
2528
2529 if (imm < 0) {
2530 __ negq(rdx);
2531 }
2532
2533 __ movq(output_register, rdx);
2534 }
2535}
2536
2537void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2538 DCHECK(instruction->IsDiv() || instruction->IsRem());
2539
2540 LocationSummary* locations = instruction->GetLocations();
2541 Location second = locations->InAt(1);
2542
2543 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2544 : locations->GetTemp(0).AsRegister<CpuRegister>();
2545 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2546 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2547 : locations->Out().AsRegister<CpuRegister>();
2548 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2549
2550 DCHECK_EQ(RAX, eax.AsRegister());
2551 DCHECK_EQ(RDX, edx.AsRegister());
2552 if (instruction->IsDiv()) {
2553 DCHECK_EQ(RAX, out.AsRegister());
2554 } else {
2555 DCHECK_EQ(RDX, out.AsRegister());
2556 }
2557
2558 int64_t magic;
2559 int shift;
2560
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002561 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002562 if (instruction->GetResultType() == Primitive::kPrimInt) {
2563 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2564
2565 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2566
2567 __ movl(numerator, eax);
2568
2569 Label no_div;
2570 Label end;
2571 __ testl(eax, eax);
2572 __ j(kNotEqual, &no_div);
2573
2574 __ xorl(out, out);
2575 __ jmp(&end);
2576
2577 __ Bind(&no_div);
2578
2579 __ movl(eax, Immediate(magic));
2580 __ imull(numerator);
2581
2582 if (imm > 0 && magic < 0) {
2583 __ addl(edx, numerator);
2584 } else if (imm < 0 && magic > 0) {
2585 __ subl(edx, numerator);
2586 }
2587
2588 if (shift != 0) {
2589 __ sarl(edx, Immediate(shift));
2590 }
2591
2592 __ movl(eax, edx);
2593 __ shrl(edx, Immediate(31));
2594 __ addl(edx, eax);
2595
2596 if (instruction->IsRem()) {
2597 __ movl(eax, numerator);
2598 __ imull(edx, Immediate(imm));
2599 __ subl(eax, edx);
2600 __ movl(edx, eax);
2601 } else {
2602 __ movl(eax, edx);
2603 }
2604 __ Bind(&end);
2605 } else {
2606 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2607
2608 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2609
2610 CpuRegister rax = eax;
2611 CpuRegister rdx = edx;
2612
2613 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2614
2615 // Save the numerator.
2616 __ movq(numerator, rax);
2617
2618 // RAX = magic
2619 __ movq(rax, Immediate(magic));
2620
2621 // RDX:RAX = magic * numerator
2622 __ imulq(numerator);
2623
2624 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002625 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002626 __ addq(rdx, numerator);
2627 } else if (imm < 0 && magic > 0) {
2628 // RDX -= numerator
2629 __ subq(rdx, numerator);
2630 }
2631
2632 // Shift if needed.
2633 if (shift != 0) {
2634 __ sarq(rdx, Immediate(shift));
2635 }
2636
2637 // RDX += 1 if RDX < 0
2638 __ movq(rax, rdx);
2639 __ shrq(rdx, Immediate(63));
2640 __ addq(rdx, rax);
2641
2642 if (instruction->IsRem()) {
2643 __ movq(rax, numerator);
2644
2645 if (IsInt<32>(imm)) {
2646 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2647 } else {
2648 __ movq(numerator, Immediate(imm));
2649 __ imulq(rdx, numerator);
2650 }
2651
2652 __ subq(rax, rdx);
2653 __ movq(rdx, rax);
2654 } else {
2655 __ movq(rax, rdx);
2656 }
2657 }
2658}
2659
Calin Juravlebacfec32014-11-14 15:54:36 +00002660void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2661 DCHECK(instruction->IsDiv() || instruction->IsRem());
2662 Primitive::Type type = instruction->GetResultType();
2663 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2664
2665 bool is_div = instruction->IsDiv();
2666 LocationSummary* locations = instruction->GetLocations();
2667
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002668 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2669 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002670
Roland Levillain271ab9c2014-11-27 15:23:57 +00002671 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002672 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002673
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002674 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002675 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002676
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002677 if (imm == 0) {
2678 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2679 } else if (imm == 1 || imm == -1) {
2680 DivRemOneOrMinusOne(instruction);
2681 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002682 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002683 } else {
2684 DCHECK(imm <= -2 || imm >= 2);
2685 GenerateDivRemWithAnyConstant(instruction);
2686 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002687 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002688 SlowPathCodeX86_64* slow_path =
2689 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2690 out.AsRegister(), type, is_div);
2691 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002692
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002693 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2694 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2695 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2696 // so it's safe to just use negl instead of more complex comparisons.
2697 if (type == Primitive::kPrimInt) {
2698 __ cmpl(second_reg, Immediate(-1));
2699 __ j(kEqual, slow_path->GetEntryLabel());
2700 // edx:eax <- sign-extended of eax
2701 __ cdq();
2702 // eax = quotient, edx = remainder
2703 __ idivl(second_reg);
2704 } else {
2705 __ cmpq(second_reg, Immediate(-1));
2706 __ j(kEqual, slow_path->GetEntryLabel());
2707 // rdx:rax <- sign-extended of rax
2708 __ cqo();
2709 // rax = quotient, rdx = remainder
2710 __ idivq(second_reg);
2711 }
2712 __ Bind(slow_path->GetExitLabel());
2713 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002714}
2715
Calin Juravle7c4954d2014-10-28 16:57:40 +00002716void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2717 LocationSummary* locations =
2718 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2719 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002720 case Primitive::kPrimInt:
2721 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002722 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002723 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002724 locations->SetOut(Location::SameAsFirstInput());
2725 // Intel uses edx:eax as the dividend.
2726 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002727 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2728 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2729 // output and request another temp.
2730 if (div->InputAt(1)->IsConstant()) {
2731 locations->AddTemp(Location::RequiresRegister());
2732 }
Calin Juravled0d48522014-11-04 16:40:20 +00002733 break;
2734 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002735
Calin Juravle7c4954d2014-10-28 16:57:40 +00002736 case Primitive::kPrimFloat:
2737 case Primitive::kPrimDouble: {
2738 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002739 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002740 locations->SetOut(Location::SameAsFirstInput());
2741 break;
2742 }
2743
2744 default:
2745 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2746 }
2747}
2748
2749void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2750 LocationSummary* locations = div->GetLocations();
2751 Location first = locations->InAt(0);
2752 Location second = locations->InAt(1);
2753 DCHECK(first.Equals(locations->Out()));
2754
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002755 Primitive::Type type = div->GetResultType();
2756 switch (type) {
2757 case Primitive::kPrimInt:
2758 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002759 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002760 break;
2761 }
2762
Calin Juravle7c4954d2014-10-28 16:57:40 +00002763 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002764 if (second.IsFpuRegister()) {
2765 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2766 } else if (second.IsConstant()) {
2767 __ divss(first.AsFpuRegister<XmmRegister>(),
2768 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2769 } else {
2770 DCHECK(second.IsStackSlot());
2771 __ divss(first.AsFpuRegister<XmmRegister>(),
2772 Address(CpuRegister(RSP), second.GetStackIndex()));
2773 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002774 break;
2775 }
2776
2777 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002778 if (second.IsFpuRegister()) {
2779 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2780 } else if (second.IsConstant()) {
2781 __ divsd(first.AsFpuRegister<XmmRegister>(),
2782 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2783 } else {
2784 DCHECK(second.IsDoubleStackSlot());
2785 __ divsd(first.AsFpuRegister<XmmRegister>(),
2786 Address(CpuRegister(RSP), second.GetStackIndex()));
2787 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002788 break;
2789 }
2790
2791 default:
2792 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2793 }
2794}
2795
Calin Juravlebacfec32014-11-14 15:54:36 +00002796void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002797 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002800
2801 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002802 case Primitive::kPrimInt:
2803 case Primitive::kPrimLong: {
2804 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002805 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002806 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2807 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002808 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2809 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2810 // output and request another temp.
2811 if (rem->InputAt(1)->IsConstant()) {
2812 locations->AddTemp(Location::RequiresRegister());
2813 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002814 break;
2815 }
2816
2817 case Primitive::kPrimFloat:
2818 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002819 locations->SetInAt(0, Location::Any());
2820 locations->SetInAt(1, Location::Any());
2821 locations->SetOut(Location::RequiresFpuRegister());
2822 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002823 break;
2824 }
2825
2826 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002827 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002828 }
2829}
2830
2831void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2832 Primitive::Type type = rem->GetResultType();
2833 switch (type) {
2834 case Primitive::kPrimInt:
2835 case Primitive::kPrimLong: {
2836 GenerateDivRemIntegral(rem);
2837 break;
2838 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002839 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002840 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002841 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002842 break;
2843 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002844 default:
2845 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2846 }
2847}
2848
Calin Juravled0d48522014-11-04 16:40:20 +00002849void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2850 LocationSummary* locations =
2851 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2852 locations->SetInAt(0, Location::Any());
2853 if (instruction->HasUses()) {
2854 locations->SetOut(Location::SameAsFirstInput());
2855 }
2856}
2857
2858void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2859 SlowPathCodeX86_64* slow_path =
2860 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2861 codegen_->AddSlowPath(slow_path);
2862
2863 LocationSummary* locations = instruction->GetLocations();
2864 Location value = locations->InAt(0);
2865
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002866 switch (instruction->GetType()) {
2867 case Primitive::kPrimInt: {
2868 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002869 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002870 __ j(kEqual, slow_path->GetEntryLabel());
2871 } else if (value.IsStackSlot()) {
2872 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2873 __ j(kEqual, slow_path->GetEntryLabel());
2874 } else {
2875 DCHECK(value.IsConstant()) << value;
2876 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2877 __ jmp(slow_path->GetEntryLabel());
2878 }
2879 }
2880 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002881 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002882 case Primitive::kPrimLong: {
2883 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002885 __ j(kEqual, slow_path->GetEntryLabel());
2886 } else if (value.IsDoubleStackSlot()) {
2887 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2888 __ j(kEqual, slow_path->GetEntryLabel());
2889 } else {
2890 DCHECK(value.IsConstant()) << value;
2891 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2892 __ jmp(slow_path->GetEntryLabel());
2893 }
2894 }
2895 break;
2896 }
2897 default:
2898 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002899 }
Calin Juravled0d48522014-11-04 16:40:20 +00002900}
2901
Calin Juravle9aec02f2014-11-18 23:06:35 +00002902void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2903 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2904
2905 LocationSummary* locations =
2906 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2907
2908 switch (op->GetResultType()) {
2909 case Primitive::kPrimInt:
2910 case Primitive::kPrimLong: {
2911 locations->SetInAt(0, Location::RequiresRegister());
2912 // The shift count needs to be in CL.
2913 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2914 locations->SetOut(Location::SameAsFirstInput());
2915 break;
2916 }
2917 default:
2918 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2919 }
2920}
2921
2922void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2923 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2924
2925 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002927 Location second = locations->InAt(1);
2928
2929 switch (op->GetResultType()) {
2930 case Primitive::kPrimInt: {
2931 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002932 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002933 if (op->IsShl()) {
2934 __ shll(first_reg, second_reg);
2935 } else if (op->IsShr()) {
2936 __ sarl(first_reg, second_reg);
2937 } else {
2938 __ shrl(first_reg, second_reg);
2939 }
2940 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002941 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002942 if (op->IsShl()) {
2943 __ shll(first_reg, imm);
2944 } else if (op->IsShr()) {
2945 __ sarl(first_reg, imm);
2946 } else {
2947 __ shrl(first_reg, imm);
2948 }
2949 }
2950 break;
2951 }
2952 case Primitive::kPrimLong: {
2953 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002954 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002955 if (op->IsShl()) {
2956 __ shlq(first_reg, second_reg);
2957 } else if (op->IsShr()) {
2958 __ sarq(first_reg, second_reg);
2959 } else {
2960 __ shrq(first_reg, second_reg);
2961 }
2962 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002963 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002964 if (op->IsShl()) {
2965 __ shlq(first_reg, imm);
2966 } else if (op->IsShr()) {
2967 __ sarq(first_reg, imm);
2968 } else {
2969 __ shrq(first_reg, imm);
2970 }
2971 }
2972 break;
2973 }
2974 default:
2975 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2976 }
2977}
2978
2979void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2980 HandleShift(shl);
2981}
2982
2983void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2984 HandleShift(shl);
2985}
2986
2987void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2988 HandleShift(shr);
2989}
2990
2991void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2992 HandleShift(shr);
2993}
2994
2995void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2996 HandleShift(ushr);
2997}
2998
2999void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3000 HandleShift(ushr);
3001}
3002
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003003void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003004 LocationSummary* locations =
3005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003006 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003007 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3008 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3009 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003010}
3011
3012void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3013 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003014 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003015 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
3016
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003017 __ gs()->call(
3018 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003020 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003021 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003022}
3023
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003024void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3025 LocationSummary* locations =
3026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3027 InvokeRuntimeCallingConvention calling_convention;
3028 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003029 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003030 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003031 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003032}
3033
3034void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3035 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003036 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003037 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
3038
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003039 __ gs()->call(
3040 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003041
3042 DCHECK(!codegen_->IsLeafMethod());
3043 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3044}
3045
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003046void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003047 LocationSummary* locations =
3048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003049 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3050 if (location.IsStackSlot()) {
3051 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3052 } else if (location.IsDoubleStackSlot()) {
3053 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3054 }
3055 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003056}
3057
3058void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
3059 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003060 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003061}
3062
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003063void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003064 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003065 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003066 locations->SetInAt(0, Location::RequiresRegister());
3067 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003068}
3069
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003070void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3071 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3073 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003074 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003075 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003076 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003077 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003078 break;
3079
3080 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003081 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003082 break;
3083
3084 default:
3085 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3086 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003087}
3088
David Brazdil66d126e2015-04-03 16:02:44 +01003089void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3090 LocationSummary* locations =
3091 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3092 locations->SetInAt(0, Location::RequiresRegister());
3093 locations->SetOut(Location::SameAsFirstInput());
3094}
3095
3096void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003097 LocationSummary* locations = bool_not->GetLocations();
3098 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3099 locations->Out().AsRegister<CpuRegister>().AsRegister());
3100 Location out = locations->Out();
3101 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3102}
3103
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003104void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003105 LocationSummary* locations =
3106 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003107 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3108 locations->SetInAt(i, Location::Any());
3109 }
3110 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003111}
3112
3113void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003114 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003115 LOG(FATAL) << "Unimplemented";
3116}
3117
Calin Juravle52c48962014-12-16 17:02:57 +00003118void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3119 /*
3120 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3121 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3122 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3123 */
3124 switch (kind) {
3125 case MemBarrierKind::kAnyAny: {
3126 __ mfence();
3127 break;
3128 }
3129 case MemBarrierKind::kAnyStore:
3130 case MemBarrierKind::kLoadAny:
3131 case MemBarrierKind::kStoreStore: {
3132 // nop
3133 break;
3134 }
3135 default:
3136 LOG(FATAL) << "Unexpected memory barier " << kind;
3137 }
3138}
3139
3140void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3141 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3142
Nicolas Geoffray39468442014-09-02 15:17:15 +01003143 LocationSummary* locations =
3144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003145 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003146 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3147 locations->SetOut(Location::RequiresFpuRegister());
3148 } else {
3149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3150 }
Calin Juravle52c48962014-12-16 17:02:57 +00003151}
3152
3153void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3154 const FieldInfo& field_info) {
3155 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3156
3157 LocationSummary* locations = instruction->GetLocations();
3158 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3159 Location out = locations->Out();
3160 bool is_volatile = field_info.IsVolatile();
3161 Primitive::Type field_type = field_info.GetFieldType();
3162 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3163
3164 switch (field_type) {
3165 case Primitive::kPrimBoolean: {
3166 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3167 break;
3168 }
3169
3170 case Primitive::kPrimByte: {
3171 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3172 break;
3173 }
3174
3175 case Primitive::kPrimShort: {
3176 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3177 break;
3178 }
3179
3180 case Primitive::kPrimChar: {
3181 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3182 break;
3183 }
3184
3185 case Primitive::kPrimInt:
3186 case Primitive::kPrimNot: {
3187 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3188 break;
3189 }
3190
3191 case Primitive::kPrimLong: {
3192 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3193 break;
3194 }
3195
3196 case Primitive::kPrimFloat: {
3197 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3198 break;
3199 }
3200
3201 case Primitive::kPrimDouble: {
3202 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3203 break;
3204 }
3205
3206 case Primitive::kPrimVoid:
3207 LOG(FATAL) << "Unreachable type " << field_type;
3208 UNREACHABLE();
3209 }
3210
Calin Juravle77520bc2015-01-12 18:45:46 +00003211 codegen_->MaybeRecordImplicitNullCheck(instruction);
3212
Calin Juravle52c48962014-12-16 17:02:57 +00003213 if (is_volatile) {
3214 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3215 }
3216}
3217
3218void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3219 const FieldInfo& field_info) {
3220 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3221
3222 LocationSummary* locations =
3223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003224 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003225 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3226
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003227 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003228 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3229 locations->SetInAt(1, Location::RequiresFpuRegister());
3230 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003231 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003232 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003233 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003234 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003235 locations->AddTemp(Location::RequiresRegister());
3236 locations->AddTemp(Location::RequiresRegister());
3237 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238}
3239
Calin Juravle52c48962014-12-16 17:02:57 +00003240void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
3241 const FieldInfo& field_info) {
3242 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3243
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003244 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003245 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3246 Location value = locations->InAt(1);
3247 bool is_volatile = field_info.IsVolatile();
3248 Primitive::Type field_type = field_info.GetFieldType();
3249 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3250
3251 if (is_volatile) {
3252 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3253 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003254
3255 switch (field_type) {
3256 case Primitive::kPrimBoolean:
3257 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003258 if (value.IsConstant()) {
3259 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3260 __ movb(Address(base, offset), Immediate(v));
3261 } else {
3262 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3263 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003264 break;
3265 }
3266
3267 case Primitive::kPrimShort:
3268 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003269 if (value.IsConstant()) {
3270 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3271 __ movw(Address(base, offset), Immediate(v));
3272 } else {
3273 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3274 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003275 break;
3276 }
3277
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003279 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003280 if (value.IsConstant()) {
3281 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3282 __ movw(Address(base, offset), Immediate(v));
3283 } else {
3284 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3285 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003286 break;
3287 }
3288
3289 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003290 if (value.IsConstant()) {
3291 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3292 DCHECK(IsInt<32>(v));
3293 int32_t v_32 = v;
3294 __ movq(Address(base, offset), Immediate(v_32));
3295 } else {
3296 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3297 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003298 break;
3299 }
3300
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003301 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003302 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003303 break;
3304 }
3305
3306 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003307 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003308 break;
3309 }
3310
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003311 case Primitive::kPrimVoid:
3312 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003313 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003314 }
Calin Juravle52c48962014-12-16 17:02:57 +00003315
Calin Juravle77520bc2015-01-12 18:45:46 +00003316 codegen_->MaybeRecordImplicitNullCheck(instruction);
3317
3318 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3319 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3320 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3321 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
3322 }
3323
Calin Juravle52c48962014-12-16 17:02:57 +00003324 if (is_volatile) {
3325 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3326 }
3327}
3328
3329void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3330 HandleFieldSet(instruction, instruction->GetFieldInfo());
3331}
3332
3333void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3334 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003335}
3336
3337void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003338 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003339}
3340
3341void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003342 HandleFieldGet(instruction, instruction->GetFieldInfo());
3343}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003344
Calin Juravle52c48962014-12-16 17:02:57 +00003345void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3346 HandleFieldGet(instruction);
3347}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003348
Calin Juravle52c48962014-12-16 17:02:57 +00003349void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3350 HandleFieldGet(instruction, instruction->GetFieldInfo());
3351}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003352
Calin Juravle52c48962014-12-16 17:02:57 +00003353void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3354 HandleFieldSet(instruction, instruction->GetFieldInfo());
3355}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003356
Calin Juravle52c48962014-12-16 17:02:57 +00003357void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3358 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003359}
3360
3361void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003362 LocationSummary* locations =
3363 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003364 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3365 ? Location::RequiresRegister()
3366 : Location::Any();
3367 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003368 if (instruction->HasUses()) {
3369 locations->SetOut(Location::SameAsFirstInput());
3370 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003371}
3372
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003373void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003374 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3375 return;
3376 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003377 LocationSummary* locations = instruction->GetLocations();
3378 Location obj = locations->InAt(0);
3379
3380 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3381 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3382}
3383
3384void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003385 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003386 codegen_->AddSlowPath(slow_path);
3387
3388 LocationSummary* locations = instruction->GetLocations();
3389 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003390
3391 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003392 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003393 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003394 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003395 } else {
3396 DCHECK(obj.IsConstant()) << obj;
3397 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3398 __ jmp(slow_path->GetEntryLabel());
3399 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003400 }
3401 __ j(kEqual, slow_path->GetEntryLabel());
3402}
3403
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003404void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3405 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3406 GenerateImplicitNullCheck(instruction);
3407 } else {
3408 GenerateExplicitNullCheck(instruction);
3409 }
3410}
3411
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003412void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003413 LocationSummary* locations =
3414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003415 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003416 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003417 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3418 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3419 } else {
3420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3421 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003422}
3423
3424void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3425 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003427 Location index = locations->InAt(1);
3428
3429 switch (instruction->GetType()) {
3430 case Primitive::kPrimBoolean: {
3431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003432 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003433 if (index.IsConstant()) {
3434 __ movzxb(out, Address(obj,
3435 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3436 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003437 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003438 }
3439 break;
3440 }
3441
3442 case Primitive::kPrimByte: {
3443 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003444 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445 if (index.IsConstant()) {
3446 __ movsxb(out, Address(obj,
3447 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3448 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003449 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003450 }
3451 break;
3452 }
3453
3454 case Primitive::kPrimShort: {
3455 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003457 if (index.IsConstant()) {
3458 __ movsxw(out, Address(obj,
3459 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3460 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003461 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003462 }
3463 break;
3464 }
3465
3466 case Primitive::kPrimChar: {
3467 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003468 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003469 if (index.IsConstant()) {
3470 __ movzxw(out, Address(obj,
3471 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3472 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003473 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003474 }
3475 break;
3476 }
3477
3478 case Primitive::kPrimInt:
3479 case Primitive::kPrimNot: {
3480 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003483 if (index.IsConstant()) {
3484 __ movl(out, Address(obj,
3485 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3486 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488 }
3489 break;
3490 }
3491
3492 case Primitive::kPrimLong: {
3493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003495 if (index.IsConstant()) {
3496 __ movq(out, Address(obj,
3497 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3498 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500 }
3501 break;
3502 }
3503
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003504 case Primitive::kPrimFloat: {
3505 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003506 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003507 if (index.IsConstant()) {
3508 __ movss(out, Address(obj,
3509 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3510 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003511 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003512 }
3513 break;
3514 }
3515
3516 case Primitive::kPrimDouble: {
3517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003518 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003519 if (index.IsConstant()) {
3520 __ movsd(out, Address(obj,
3521 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3522 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003523 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003524 }
3525 break;
3526 }
3527
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003528 case Primitive::kPrimVoid:
3529 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003530 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003531 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003532 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003533}
3534
3535void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003536 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003537
3538 bool needs_write_barrier =
3539 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3540 bool needs_runtime_call = instruction->NeedsTypeCheck();
3541
Nicolas Geoffray39468442014-09-02 15:17:15 +01003542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003543 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3544 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003545 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003546 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3547 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3548 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003549 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003550 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003551 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003552 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3553 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003554 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003555 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003556 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3557 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003558 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003559 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003560 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003561
3562 if (needs_write_barrier) {
3563 // Temporary registers for the write barrier.
3564 locations->AddTemp(Location::RequiresRegister());
3565 locations->AddTemp(Location::RequiresRegister());
3566 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003568}
3569
3570void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3571 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003574 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003575 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003576 bool needs_runtime_call = locations->WillCall();
3577 bool needs_write_barrier =
3578 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579
3580 switch (value_type) {
3581 case Primitive::kPrimBoolean:
3582 case Primitive::kPrimByte: {
3583 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 if (index.IsConstant()) {
3585 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003586 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003587 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003588 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003589 __ movb(Address(obj, offset),
3590 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003591 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003593 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003594 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3595 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003598 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003600 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003601 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 break;
3603 }
3604
3605 case Primitive::kPrimShort:
3606 case Primitive::kPrimChar: {
3607 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 if (index.IsConstant()) {
3609 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003610 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003611 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003612 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003613 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003614 __ movw(Address(obj, offset),
3615 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003616 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003617 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003618 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003619 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3621 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003622 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003623 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003625 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3626 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003627 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003628 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 break;
3630 }
3631
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003632 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003633 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003634 if (!needs_runtime_call) {
3635 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3636 if (index.IsConstant()) {
3637 size_t offset =
3638 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3639 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003641 } else {
3642 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003643 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3644 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003645 }
3646 } else {
3647 DCHECK(index.IsRegister()) << index;
3648 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3650 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003651 } else {
3652 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003653 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003655 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003656 }
3657 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003658 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003659 if (needs_write_barrier) {
3660 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003661 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3662 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3663 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003664 }
3665 } else {
3666 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003667 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3668 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003669 DCHECK(!codegen_->IsLeafMethod());
3670 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3671 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003672 break;
3673 }
3674
3675 case Primitive::kPrimLong: {
3676 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003677 if (index.IsConstant()) {
3678 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04003679 if (value.IsRegister()) {
3680 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
3681 } else {
3682 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3683 DCHECK(IsInt<32>(v));
3684 int32_t v_32 = v;
3685 __ movq(Address(obj, offset), Immediate(v_32));
3686 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003687 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003688 if (value.IsRegister()) {
3689 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3690 value.AsRegister<CpuRegister>());
3691 } else {
3692 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3693 DCHECK(IsInt<32>(v));
3694 int32_t v_32 = v;
3695 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3696 Immediate(v_32));
3697 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003698 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003699 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003700 break;
3701 }
3702
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003703 case Primitive::kPrimFloat: {
3704 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3705 if (index.IsConstant()) {
3706 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3707 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003709 } else {
3710 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003711 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3712 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003713 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003714 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003715 break;
3716 }
3717
3718 case Primitive::kPrimDouble: {
3719 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3720 if (index.IsConstant()) {
3721 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3722 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003723 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003724 } else {
3725 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3727 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003728 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003729 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003730 break;
3731 }
3732
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003733 case Primitive::kPrimVoid:
3734 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003735 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003736 }
3737}
3738
3739void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003740 LocationSummary* locations =
3741 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003742 locations->SetInAt(0, Location::RequiresRegister());
3743 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744}
3745
3746void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3747 LocationSummary* locations = instruction->GetLocations();
3748 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003749 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3750 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003751 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003752 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003753}
3754
3755void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003756 LocationSummary* locations =
3757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003758 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003759 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003760 if (instruction->HasUses()) {
3761 locations->SetOut(Location::SameAsFirstInput());
3762 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763}
3764
3765void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3766 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003767 Location index_loc = locations->InAt(0);
3768 Location length_loc = locations->InAt(1);
3769 SlowPathCodeX86_64* slow_path =
3770 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771
Mark Mendell99dbd682015-04-22 16:18:52 -04003772 if (length_loc.IsConstant()) {
3773 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3774 if (index_loc.IsConstant()) {
3775 // BCE will remove the bounds check if we are guarenteed to pass.
3776 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3777 if (index < 0 || index >= length) {
3778 codegen_->AddSlowPath(slow_path);
3779 __ jmp(slow_path->GetEntryLabel());
3780 } else {
3781 // Some optimization after BCE may have generated this, and we should not
3782 // generate a bounds check if it is a valid range.
3783 }
3784 return;
3785 }
3786
3787 // We have to reverse the jump condition because the length is the constant.
3788 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
3789 __ cmpl(index_reg, Immediate(length));
3790 codegen_->AddSlowPath(slow_path);
3791 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003792 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003793 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3794 if (index_loc.IsConstant()) {
3795 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3796 __ cmpl(length, Immediate(value));
3797 } else {
3798 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3799 }
3800 codegen_->AddSlowPath(slow_path);
3801 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003802 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003803}
3804
3805void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3806 CpuRegister card,
3807 CpuRegister object,
3808 CpuRegister value) {
3809 Label is_null;
3810 __ testl(value, value);
3811 __ j(kEqual, &is_null);
3812 __ gs()->movq(card, Address::Absolute(
3813 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3814 __ movq(temp, object);
3815 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3816 __ movb(Address(temp, card, TIMES_1, 0), card);
3817 __ Bind(&is_null);
3818}
3819
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003820void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3821 temp->SetLocations(nullptr);
3822}
3823
3824void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3825 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003826 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003827}
3828
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003829void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003830 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003831 LOG(FATAL) << "Unimplemented";
3832}
3833
3834void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003835 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3836}
3837
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003838void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3839 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3840}
3841
3842void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003843 HBasicBlock* block = instruction->GetBlock();
3844 if (block->GetLoopInformation() != nullptr) {
3845 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3846 // The back edge will generate the suspend check.
3847 return;
3848 }
3849 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3850 // The goto will generate the suspend check.
3851 return;
3852 }
3853 GenerateSuspendCheck(instruction, nullptr);
3854}
3855
3856void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3857 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003858 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003859 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003860 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003861 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003862 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003863 if (successor == nullptr) {
3864 __ j(kNotEqual, slow_path->GetEntryLabel());
3865 __ Bind(slow_path->GetReturnLabel());
3866 } else {
3867 __ j(kEqual, codegen_->GetLabelOf(successor));
3868 __ jmp(slow_path->GetEntryLabel());
3869 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003870}
3871
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003872X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3873 return codegen_->GetAssembler();
3874}
3875
3876void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3877 MoveOperands* move = moves_.Get(index);
3878 Location source = move->GetSource();
3879 Location destination = move->GetDestination();
3880
3881 if (source.IsRegister()) {
3882 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003883 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003884 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003885 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003887 } else {
3888 DCHECK(destination.IsDoubleStackSlot());
3889 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003890 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003891 }
3892 } else if (source.IsStackSlot()) {
3893 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003894 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003895 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003896 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003897 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003898 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003899 } else {
3900 DCHECK(destination.IsStackSlot());
3901 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3902 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3903 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003904 } else if (source.IsDoubleStackSlot()) {
3905 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003907 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003908 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003909 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3910 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003911 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003912 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003913 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3914 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3915 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003916 } else if (source.IsConstant()) {
3917 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003918 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3919 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003920 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003921 if (value == 0) {
3922 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3923 } else {
3924 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3925 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003926 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003927 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003928 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003929 }
3930 } else if (constant->IsLongConstant()) {
3931 int64_t value = constant->AsLongConstant()->GetValue();
3932 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003934 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003935 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003936 __ movq(CpuRegister(TMP), Immediate(value));
3937 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3938 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003939 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003940 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003941 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003942 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003943 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003944 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3945 if (value == 0) {
3946 // easy FP 0.0.
3947 __ xorps(dest, dest);
3948 } else {
3949 __ movl(CpuRegister(TMP), imm);
3950 __ movd(dest, CpuRegister(TMP));
3951 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003952 } else {
3953 DCHECK(destination.IsStackSlot()) << destination;
3954 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3955 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003956 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003957 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003958 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003959 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003960 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003961 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003962 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3963 if (value == 0) {
3964 __ xorpd(dest, dest);
3965 } else {
3966 __ movq(CpuRegister(TMP), imm);
3967 __ movd(dest, CpuRegister(TMP));
3968 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003969 } else {
3970 DCHECK(destination.IsDoubleStackSlot()) << destination;
3971 __ movq(CpuRegister(TMP), imm);
3972 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3973 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003974 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003975 } else if (source.IsFpuRegister()) {
3976 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003977 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003978 } else if (destination.IsStackSlot()) {
3979 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003980 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003981 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003982 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003983 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003984 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003985 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003986 }
3987}
3988
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003989void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003990 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003991 __ movl(Address(CpuRegister(RSP), mem), reg);
3992 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003993}
3994
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003995void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003996 ScratchRegisterScope ensure_scratch(
3997 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3998
3999 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4000 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4001 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4002 Address(CpuRegister(RSP), mem2 + stack_offset));
4003 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4004 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4005 CpuRegister(ensure_scratch.GetRegister()));
4006}
4007
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004008void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4009 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4010 __ movq(Address(CpuRegister(RSP), mem), reg);
4011 __ movq(reg, CpuRegister(TMP));
4012}
4013
4014void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4015 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004016 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004017
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004018 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4019 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4020 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4021 Address(CpuRegister(RSP), mem2 + stack_offset));
4022 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4023 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4024 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004025}
4026
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004027void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4028 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4029 __ movss(Address(CpuRegister(RSP), mem), reg);
4030 __ movd(reg, CpuRegister(TMP));
4031}
4032
4033void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4034 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4035 __ movsd(Address(CpuRegister(RSP), mem), reg);
4036 __ movd(reg, CpuRegister(TMP));
4037}
4038
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004039void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4040 MoveOperands* move = moves_.Get(index);
4041 Location source = move->GetSource();
4042 Location destination = move->GetDestination();
4043
4044 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004045 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004046 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004047 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004048 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004049 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004050 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004051 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4052 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004053 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004054 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004055 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004056 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4057 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004058 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004059 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4060 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4061 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004062 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004063 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004064 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004065 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004066 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004067 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004068 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004069 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004070 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004071 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004072 }
4073}
4074
4075
4076void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4077 __ pushq(CpuRegister(reg));
4078}
4079
4080
4081void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4082 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004083}
4084
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004085void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4086 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4087 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4088 Immediate(mirror::Class::kStatusInitialized));
4089 __ j(kLess, slow_path->GetEntryLabel());
4090 __ Bind(slow_path->GetExitLabel());
4091 // No need for memory fence, thanks to the X86_64 memory model.
4092}
4093
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004094void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004095 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4096 ? LocationSummary::kCallOnSlowPath
4097 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004098 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004099 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004100 locations->SetOut(Location::RequiresRegister());
4101}
4102
4103void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004104 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004105 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004106 DCHECK(!cls->CanCallRuntime());
4107 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004108 codegen_->LoadCurrentMethod(out);
4109 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4110 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004111 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004112 codegen_->LoadCurrentMethod(out);
4113 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4114 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004115 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4116 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4117 codegen_->AddSlowPath(slow_path);
4118 __ testl(out, out);
4119 __ j(kEqual, slow_path->GetEntryLabel());
4120 if (cls->MustGenerateClinitCheck()) {
4121 GenerateClassInitializationCheck(slow_path, out);
4122 } else {
4123 __ Bind(slow_path->GetExitLabel());
4124 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004125 }
4126}
4127
4128void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4129 LocationSummary* locations =
4130 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4131 locations->SetInAt(0, Location::RequiresRegister());
4132 if (check->HasUses()) {
4133 locations->SetOut(Location::SameAsFirstInput());
4134 }
4135}
4136
4137void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004138 // We assume the class to not be null.
4139 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4140 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004142 GenerateClassInitializationCheck(slow_path,
4143 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004144}
4145
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004146void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4147 LocationSummary* locations =
4148 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4149 locations->SetOut(Location::RequiresRegister());
4150}
4151
4152void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4153 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4154 codegen_->AddSlowPath(slow_path);
4155
Roland Levillain271ab9c2014-11-27 15:23:57 +00004156 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004157 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004158 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4159 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004160 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4161 __ testl(out, out);
4162 __ j(kEqual, slow_path->GetEntryLabel());
4163 __ Bind(slow_path->GetExitLabel());
4164}
4165
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004166void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4167 LocationSummary* locations =
4168 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4169 locations->SetOut(Location::RequiresRegister());
4170}
4171
4172void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4173 Address address = Address::Absolute(
4174 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004175 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004176 __ gs()->movl(address, Immediate(0));
4177}
4178
4179void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4180 LocationSummary* locations =
4181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4182 InvokeRuntimeCallingConvention calling_convention;
4183 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4184}
4185
4186void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4187 __ gs()->call(
4188 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4189 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4190}
4191
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004192void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004193 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4194 ? LocationSummary::kNoCall
4195 : LocationSummary::kCallOnSlowPath;
4196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4197 locations->SetInAt(0, Location::RequiresRegister());
4198 locations->SetInAt(1, Location::Any());
4199 locations->SetOut(Location::RequiresRegister());
4200}
4201
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004202void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004203 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004204 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004205 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004206 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004207 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4208 Label done, zero;
4209 SlowPathCodeX86_64* slow_path = nullptr;
4210
4211 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004212 // Avoid null check if we know obj is not null.
4213 if (instruction->MustDoNullCheck()) {
4214 __ testl(obj, obj);
4215 __ j(kEqual, &zero);
4216 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004217 // Compare the class of `obj` with `cls`.
4218 __ movl(out, Address(obj, class_offset));
4219 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004220 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004221 } else {
4222 DCHECK(cls.IsStackSlot()) << cls;
4223 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4224 }
4225 if (instruction->IsClassFinal()) {
4226 // Classes must be equal for the instanceof to succeed.
4227 __ j(kNotEqual, &zero);
4228 __ movl(out, Immediate(1));
4229 __ jmp(&done);
4230 } else {
4231 // If the classes are not equal, we go into a slow path.
4232 DCHECK(locations->OnlyCallsOnSlowPath());
4233 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004234 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004235 codegen_->AddSlowPath(slow_path);
4236 __ j(kNotEqual, slow_path->GetEntryLabel());
4237 __ movl(out, Immediate(1));
4238 __ jmp(&done);
4239 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004240
4241 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4242 __ Bind(&zero);
4243 __ movl(out, Immediate(0));
4244 }
4245
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004246 if (slow_path != nullptr) {
4247 __ Bind(slow_path->GetExitLabel());
4248 }
4249 __ Bind(&done);
4250}
4251
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004252void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4254 instruction, LocationSummary::kCallOnSlowPath);
4255 locations->SetInAt(0, Location::RequiresRegister());
4256 locations->SetInAt(1, Location::Any());
4257 locations->AddTemp(Location::RequiresRegister());
4258}
4259
4260void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4261 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004262 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004263 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004264 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004265 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4266 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4267 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4268 codegen_->AddSlowPath(slow_path);
4269
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004270 // Avoid null check if we know obj is not null.
4271 if (instruction->MustDoNullCheck()) {
4272 __ testl(obj, obj);
4273 __ j(kEqual, slow_path->GetExitLabel());
4274 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004275 // Compare the class of `obj` with `cls`.
4276 __ movl(temp, Address(obj, class_offset));
4277 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004278 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004279 } else {
4280 DCHECK(cls.IsStackSlot()) << cls;
4281 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4282 }
4283 // Classes must be equal for the checkcast to succeed.
4284 __ j(kNotEqual, slow_path->GetEntryLabel());
4285 __ Bind(slow_path->GetExitLabel());
4286}
4287
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004288void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4289 LocationSummary* locations =
4290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4291 InvokeRuntimeCallingConvention calling_convention;
4292 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4293}
4294
4295void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4296 __ gs()->call(Address::Absolute(instruction->IsEnter()
4297 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4298 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4299 true));
4300 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4301}
4302
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004303void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4304void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4305void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4306
4307void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4308 LocationSummary* locations =
4309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4310 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4311 || instruction->GetResultType() == Primitive::kPrimLong);
4312 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004313 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004314 locations->SetOut(Location::SameAsFirstInput());
4315}
4316
4317void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4318 HandleBitwiseOperation(instruction);
4319}
4320
4321void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4322 HandleBitwiseOperation(instruction);
4323}
4324
4325void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4326 HandleBitwiseOperation(instruction);
4327}
4328
4329void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4330 LocationSummary* locations = instruction->GetLocations();
4331 Location first = locations->InAt(0);
4332 Location second = locations->InAt(1);
4333 DCHECK(first.Equals(locations->Out()));
4334
4335 if (instruction->GetResultType() == Primitive::kPrimInt) {
4336 if (second.IsRegister()) {
4337 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004338 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004339 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004340 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004341 } else {
4342 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004343 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004344 }
4345 } else if (second.IsConstant()) {
4346 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4347 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004348 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004349 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004350 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004351 } else {
4352 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004353 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004354 }
4355 } else {
4356 Address address(CpuRegister(RSP), second.GetStackIndex());
4357 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004358 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004359 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004361 } else {
4362 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004363 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004364 }
4365 }
4366 } else {
4367 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004368 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4369 bool second_is_constant = false;
4370 int64_t value = 0;
4371 if (second.IsConstant()) {
4372 second_is_constant = true;
4373 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004374 }
Mark Mendell40741f32015-04-20 22:10:34 -04004375 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004376
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004377 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004378 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004379 if (is_int32_value) {
4380 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4381 } else {
4382 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4383 }
4384 } else if (second.IsDoubleStackSlot()) {
4385 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004386 } else {
4387 __ andq(first_reg, second.AsRegister<CpuRegister>());
4388 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004389 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004390 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004391 if (is_int32_value) {
4392 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4393 } else {
4394 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4395 }
4396 } else if (second.IsDoubleStackSlot()) {
4397 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 } else {
4399 __ orq(first_reg, second.AsRegister<CpuRegister>());
4400 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004401 } else {
4402 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004403 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004404 if (is_int32_value) {
4405 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4406 } else {
4407 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4408 }
4409 } else if (second.IsDoubleStackSlot()) {
4410 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004411 } else {
4412 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4413 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004414 }
4415 }
4416}
4417
Calin Juravleb1498f62015-02-16 13:13:29 +00004418void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4419 // Nothing to do, this should be removed during prepare for register allocator.
4420 UNUSED(instruction);
4421 LOG(FATAL) << "Unreachable";
4422}
4423
4424void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4425 // Nothing to do, this should be removed during prepare for register allocator.
4426 UNUSED(instruction);
4427 LOG(FATAL) << "Unreachable";
4428}
4429
Mark Mendellf55c3e02015-03-26 21:07:46 -04004430void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4431 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004432 X86_64Assembler* assembler = GetAssembler();
4433 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004434 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4435 // byte values. If used for vectors at a later time, this will need to be
4436 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004437 assembler->Align(4, 0);
4438 constant_area_start_ = assembler->CodeSize();
4439 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004440 }
4441
4442 // And finish up.
4443 CodeGenerator::Finalize(allocator);
4444}
4445
4446/**
4447 * Class to handle late fixup of offsets into constant area.
4448 */
4449class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4450 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004451 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004452 : codegen_(codegen), offset_into_constant_area_(offset) {}
4453
4454 private:
4455 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4456 // Patch the correct offset for the instruction. We use the address of the
4457 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4458 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4459 int relative_position = constant_offset - pos;
4460
4461 // Patch in the right value.
4462 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4463 }
4464
Mark Mendell39dcf552015-04-09 20:42:42 -04004465 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004466
4467 // Location in constant area that the fixup refers to.
4468 int offset_into_constant_area_;
4469};
4470
4471Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4472 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4473 return Address::RIP(fixup);
4474}
4475
4476Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4477 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4478 return Address::RIP(fixup);
4479}
4480
4481Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4482 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4483 return Address::RIP(fixup);
4484}
4485
4486Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4487 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4488 return Address::RIP(fixup);
4489}
4490
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004491} // namespace x86_64
4492} // namespace art