blob: 59a956515cd02dbc88a5a74d406d71d3da781425 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080023#include "intrinsics.h"
24#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "mirror/class-inl.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;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010042static constexpr Register kMethodRegisterArgument = RDI;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000044static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
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 {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400102 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000103 }
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
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100139 HBasicBlock* GetSuccessor() const {
140 return successor_;
141 }
142
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143 private:
144 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 Label return_label_;
147
148 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
149};
150
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100153 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
154 Location index_location,
155 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100156 : instruction_(instruction),
157 index_location_(index_location),
158 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000160 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000162 // We're moving two locations to locations that could overlap, so we need a parallel
163 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 codegen->EmitParallelMoves(
166 index_location_,
167 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100168 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000169 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100170 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
171 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 __ gs()->call(Address::Absolute(
173 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000174 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175 }
176
177 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100178 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 const Location index_location_;
180 const Location length_location_;
181
182 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
183};
184
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000185class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100186 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000187 LoadClassSlowPathX86_64(HLoadClass* cls,
188 HInstruction* at,
189 uint32_t dex_pc,
190 bool do_clinit)
191 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
192 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
193 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100194
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100197 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
198 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100199
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000200 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000201
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000203 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 __ gs()->call(Address::Absolute((do_clinit_
205 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
206 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000207 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000209 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000211 if (out.IsValid()) {
212 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
213 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 }
215
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000216 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100217 __ jmp(GetExitLabel());
218 }
219
220 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 // The class this slow path will load.
222 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 // The instruction where this slow path is happening.
225 // (Might be the load class or an initialization check).
226 HInstruction* const at_;
227
228 // The dex PC of `at_`.
229 const uint32_t dex_pc_;
230
231 // Whether to initialize the class.
232 const bool do_clinit_;
233
234 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235};
236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
238 public:
239 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
240
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000241 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000242 LocationSummary* locations = instruction_->GetLocations();
243 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
244
245 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
246 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000247 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000248
249 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800250 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000251 Immediate(instruction_->GetStringIndex()));
252 __ gs()->call(Address::Absolute(
253 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000254 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000257 __ jmp(GetExitLabel());
258 }
259
260 private:
261 HLoadString* const instruction_;
262
263 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
264};
265
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000266class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
267 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000268 TypeCheckSlowPathX86_64(HInstruction* instruction,
269 Location class_to_check,
270 Location object_class,
271 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000273 class_to_check_(class_to_check),
274 object_class_(object_class),
275 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 DCHECK(instruction_->IsCheckCast()
280 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
283 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285
286 // We're moving two locations to locations that could overlap, so we need a parallel
287 // move resolver.
288 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000289 codegen->EmitParallelMoves(
290 class_to_check_,
291 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100292 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000293 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100294 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
295 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 if (instruction_->IsInstanceOf()) {
298 __ gs()->call(
299 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
300 } else {
301 DCHECK(instruction_->IsCheckCast());
302 __ gs()->call(
303 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
304 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000305 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000306
307 if (instruction_->IsInstanceOf()) {
308 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
309 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000311 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 __ jmp(GetExitLabel());
313 }
314
315 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 HInstruction* const instruction_;
317 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
322};
323
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700324class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
325 public:
326 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
327 : instruction_(instruction) {}
328
329 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
330 __ Bind(GetEntryLabel());
331 SaveLiveRegisters(codegen, instruction_->GetLocations());
332 __ gs()->call(
333 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeoptimize), true));
334 DCHECK(instruction_->IsDeoptimize());
335 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
336 uint32_t dex_pc = deoptimize->GetDexPc();
337 codegen->RecordPcInfo(instruction_, dex_pc, this);
338 }
339
340 private:
341 HInstruction* const instruction_;
342 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
343};
344
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100345#undef __
346#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
347
Dave Allison20dfc792014-06-16 20:44:29 -0700348inline Condition X86_64Condition(IfCondition cond) {
349 switch (cond) {
350 case kCondEQ: return kEqual;
351 case kCondNE: return kNotEqual;
352 case kCondLT: return kLess;
353 case kCondLE: return kLessEqual;
354 case kCondGT: return kGreater;
355 case kCondGE: return kGreaterEqual;
356 default:
357 LOG(FATAL) << "Unknown if condition";
358 }
359 return kEqual;
360}
361
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800362void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
363 CpuRegister temp) {
364 // All registers are assumed to be correctly set up.
365
366 // TODO: Implement all kinds of calls:
367 // 1) boot -> boot
368 // 2) app -> boot
369 // 3) app -> app
370 //
371 // Currently we implement the app -> app logic, which looks up in the resolve cache.
372
Jeff Hao848f70a2014-01-15 13:49:50 -0800373 if (invoke->IsStringInit()) {
374 // temp = thread->string_init_entrypoint
375 __ gs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000376 // (temp + offset_of_quick_compiled_code)()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000378 kX86_64WordSize).SizeValue()));
379 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -0800380 // temp = method;
381 LoadCurrentMethod(temp);
382 if (!invoke->IsRecursive()) {
383 // temp = temp->dex_cache_resolved_methods_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 __ movl(temp, Address(temp, ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800385 // temp = temp[index_in_cache]
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 __ movq(temp, Address(
387 temp, CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
Jeff Hao848f70a2014-01-15 13:49:50 -0800388 // (temp + offset_of_quick_compiled_code)()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Jeff Hao848f70a2014-01-15 13:49:50 -0800390 kX86_64WordSize).SizeValue()));
391 } else {
392 __ call(&frame_entry_label_);
393 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000394 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800395
396 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800397}
398
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100399void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100400 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401}
402
403void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100404 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100405}
406
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100407size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
408 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
409 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100410}
411
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100412size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
413 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
414 return kX86_64WordSize;
415}
416
417size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
418 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
419 return kX86_64WordSize;
420}
421
422size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
423 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
424 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100425}
426
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000427static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000428// Use a fake return address register to mimic Quick.
429static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400430CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
431 const X86_64InstructionSetFeatures& isa_features,
432 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000433 : CodeGenerator(graph,
434 kNumberOfCpuRegisters,
435 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000436 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000437 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
438 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000439 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000440 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
441 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000442 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100443 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100444 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000445 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400446 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400447 isa_features_(isa_features),
448 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000449 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
450}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100451
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100452InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
453 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454 : HGraphVisitor(graph),
455 assembler_(codegen->GetAssembler()),
456 codegen_(codegen) {}
457
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459 switch (type) {
460 case Primitive::kPrimLong:
461 case Primitive::kPrimByte:
462 case Primitive::kPrimBoolean:
463 case Primitive::kPrimChar:
464 case Primitive::kPrimShort:
465 case Primitive::kPrimInt:
466 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100468 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100469 }
470
471 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100472 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100473 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100474 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100476
477 case Primitive::kPrimVoid:
478 LOG(FATAL) << "Unreachable type " << type;
479 }
480
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100481 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100482}
483
Nicolas Geoffray98893962015-01-21 12:32:32 +0000484void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100485 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100486 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100487
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000488 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100489 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000490
Nicolas Geoffray98893962015-01-21 12:32:32 +0000491 if (is_baseline) {
492 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
493 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
494 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000495 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
496 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
497 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000498 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499}
500
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100501static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100502 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100503}
David Srbecky9d8606d2015-04-12 09:35:32 +0100504
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100505static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100506 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100507}
508
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100510 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000511 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100512 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700513 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000514 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100515
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000516 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100517 __ testq(CpuRegister(RAX), Address(
518 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100519 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100520 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000521
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000522 if (HasEmptyFrame()) {
523 return;
524 }
525
Nicolas Geoffray98893962015-01-21 12:32:32 +0000526 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000527 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000528 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000529 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100530 __ cfi().AdjustCFAOffset(kX86_64WordSize);
531 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000532 }
533 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100534
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100535 int adjust = GetFrameSize() - GetCoreSpillSize();
536 __ subq(CpuRegister(RSP), Immediate(adjust));
537 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000538 uint32_t xmm_spill_location = GetFpuSpillStart();
539 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100540
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000541 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
542 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100543 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
544 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
545 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000546 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100547 }
548
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100550 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551}
552
553void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100554 __ cfi().RememberState();
555 if (!HasEmptyFrame()) {
556 uint32_t xmm_spill_location = GetFpuSpillStart();
557 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
558 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
559 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
560 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
561 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
562 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
563 }
564 }
565
566 int adjust = GetFrameSize() - GetCoreSpillSize();
567 __ addq(CpuRegister(RSP), Immediate(adjust));
568 __ cfi().AdjustCFAOffset(-adjust);
569
570 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
571 Register reg = kCoreCalleeSaves[i];
572 if (allocated_registers_.ContainsCoreRegister(reg)) {
573 __ popq(CpuRegister(reg));
574 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
575 __ cfi().Restore(DWARFReg(reg));
576 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000577 }
578 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100579 __ ret();
580 __ cfi().RestoreState();
581 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100582}
583
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100584void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
585 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586}
587
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100588void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000589 DCHECK(RequiresCurrentMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591}
592
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100593Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
594 switch (load->GetType()) {
595 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100597 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100598
599 case Primitive::kPrimInt:
600 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100602 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100603
604 case Primitive::kPrimBoolean:
605 case Primitive::kPrimByte:
606 case Primitive::kPrimChar:
607 case Primitive::kPrimShort:
608 case Primitive::kPrimVoid:
609 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700610 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100611 }
612
613 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700614 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615}
616
617void CodeGeneratorX86_64::Move(Location destination, Location source) {
618 if (source.Equals(destination)) {
619 return;
620 }
621 if (destination.IsRegister()) {
622 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000623 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000625 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100626 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100629 } else {
630 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 Address(CpuRegister(RSP), source.GetStackIndex()));
633 }
634 } else if (destination.IsFpuRegister()) {
635 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 Address(CpuRegister(RSP), source.GetStackIndex()));
642 } else {
643 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000644 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100645 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100646 }
647 } else if (destination.IsStackSlot()) {
648 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100649 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (source.IsFpuRegister()) {
652 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500654 } else if (source.IsConstant()) {
655 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000656 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500657 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100658 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500659 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000660 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
661 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100662 }
663 } else {
664 DCHECK(destination.IsDoubleStackSlot());
665 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000667 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 } else if (source.IsFpuRegister()) {
669 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000670 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500671 } else if (source.IsConstant()) {
672 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800673 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500674 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000675 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500676 } else {
677 DCHECK(constant->IsLongConstant());
678 value = constant->AsLongConstant()->GetValue();
679 }
Mark Mendell92e83bf2015-05-07 11:25:03 -0400680 Load64BitValue(CpuRegister(TMP), value);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500681 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682 } else {
683 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000684 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
685 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 }
687 }
688}
689
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100690void CodeGeneratorX86_64::Move(HInstruction* instruction,
691 Location location,
692 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000693 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100694 if (instruction->IsCurrentMethod()) {
695 Move(location, Location::StackSlot(kCurrentMethodStackOffset));
696 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000697 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100698 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000699 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000700 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
701 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000702 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000703 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000704 } else if (location.IsStackSlot()) {
705 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
706 } else {
707 DCHECK(location.IsConstant());
708 DCHECK_EQ(location.GetConstant(), const_to_move);
709 }
710 } else if (const_to_move->IsLongConstant()) {
711 int64_t value = const_to_move->AsLongConstant()->GetValue();
712 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400713 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000714 } else if (location.IsDoubleStackSlot()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400715 Load64BitValue(CpuRegister(TMP), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000716 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
717 } else {
718 DCHECK(location.IsConstant());
719 DCHECK_EQ(location.GetConstant(), const_to_move);
720 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100721 }
Roland Levillain476df552014-10-09 17:51:36 +0100722 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100723 switch (instruction->GetType()) {
724 case Primitive::kPrimBoolean:
725 case Primitive::kPrimByte:
726 case Primitive::kPrimChar:
727 case Primitive::kPrimShort:
728 case Primitive::kPrimInt:
729 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100731 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
732 break;
733
734 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000736 Move(location,
737 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100738 break;
739
740 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000743 } else if (instruction->IsTemporary()) {
744 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
745 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100747 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100748 switch (instruction->GetType()) {
749 case Primitive::kPrimBoolean:
750 case Primitive::kPrimByte:
751 case Primitive::kPrimChar:
752 case Primitive::kPrimShort:
753 case Primitive::kPrimInt:
754 case Primitive::kPrimNot:
755 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100756 case Primitive::kPrimFloat:
757 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000758 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100759 break;
760
761 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100763 }
764 }
765}
766
767void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
768 got->SetLocations(nullptr);
769}
770
771void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
772 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100773 DCHECK(!successor->IsExitBlock());
774
775 HBasicBlock* block = got->GetBlock();
776 HInstruction* previous = got->GetPrevious();
777
778 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000779 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100780 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
781 return;
782 }
783
784 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
785 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
786 }
787 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100788 __ jmp(codegen_->GetLabelOf(successor));
789 }
790}
791
792void LocationsBuilderX86_64::VisitExit(HExit* exit) {
793 exit->SetLocations(nullptr);
794}
795
796void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700797 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100798}
799
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700800void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
801 Label* true_target,
802 Label* false_target,
803 Label* always_true_target) {
804 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 if (cond->IsIntConstant()) {
806 // Constant condition, statically compared against 1.
807 int32_t cond_value = cond->AsIntConstant()->GetValue();
808 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700809 if (always_true_target != nullptr) {
810 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100811 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100812 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100813 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 DCHECK_EQ(cond_value, 0);
815 }
816 } else {
817 bool materialized =
818 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
819 // Moves do not affect the eflags register, so if the condition is
820 // evaluated just before the if, we don't need to evaluate it
821 // again.
822 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700823 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 if (materialized) {
825 if (!eflags_set) {
826 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700827 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100828 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000829 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 } else {
831 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
832 Immediate(0));
833 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700834 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100835 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100837 }
838 } else {
839 Location lhs = cond->GetLocations()->InAt(0);
840 Location rhs = cond->GetLocations()->InAt(1);
841 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000842 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100843 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000844 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000845 if (constant == 0) {
846 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
847 } else {
848 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
849 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000851 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100852 Address(CpuRegister(RSP), rhs.GetStackIndex()));
853 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700854 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700855 }
Dave Allison20dfc792014-06-16 20:44:29 -0700856 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700857 if (false_target != nullptr) {
858 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 }
860}
861
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700862void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
863 LocationSummary* locations =
864 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
865 HInstruction* cond = if_instr->InputAt(0);
866 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
867 locations->SetInAt(0, Location::Any());
868 }
869}
870
871void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
872 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
873 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
874 Label* always_true_target = true_target;
875 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
876 if_instr->IfTrueSuccessor())) {
877 always_true_target = nullptr;
878 }
879 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
880 if_instr->IfFalseSuccessor())) {
881 false_target = nullptr;
882 }
883 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
884}
885
886void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
887 LocationSummary* locations = new (GetGraph()->GetArena())
888 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
889 HInstruction* cond = deoptimize->InputAt(0);
890 DCHECK(cond->IsCondition());
891 if (cond->AsCondition()->NeedsMaterialization()) {
892 locations->SetInAt(0, Location::Any());
893 }
894}
895
896void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
897 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
898 DeoptimizationSlowPathX86_64(deoptimize);
899 codegen_->AddSlowPath(slow_path);
900 Label* slow_path_entry = slow_path->GetEntryLabel();
901 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
902}
903
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100904void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
905 local->SetLocations(nullptr);
906}
907
908void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
909 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
910}
911
912void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
913 local->SetLocations(nullptr);
914}
915
916void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
917 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700918 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100919}
920
921void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100922 LocationSummary* locations =
923 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924 switch (store->InputAt(1)->GetType()) {
925 case Primitive::kPrimBoolean:
926 case Primitive::kPrimByte:
927 case Primitive::kPrimChar:
928 case Primitive::kPrimShort:
929 case Primitive::kPrimInt:
930 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100931 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100932 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
933 break;
934
935 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100936 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100937 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
938 break;
939
940 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100943}
944
945void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700946 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947}
948
Roland Levillain0d37cd02015-05-27 16:39:19 +0100949void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100950 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +0100951 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100952 locations->SetInAt(0, Location::RequiresRegister());
953 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +0100954 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100955 locations->SetOut(Location::RequiresRegister());
956 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100957}
958
Roland Levillain0d37cd02015-05-27 16:39:19 +0100959void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
960 if (cond->NeedsMaterialization()) {
961 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000962 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100963 // Clear register: setcc only sets the low byte.
Mark Mendell92e83bf2015-05-07 11:25:03 -0400964 __ xorl(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000965 Location lhs = locations->InAt(0);
966 Location rhs = locations->InAt(1);
967 if (rhs.IsRegister()) {
968 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
969 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800970 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000971 if (constant == 0) {
972 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
973 } else {
974 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
975 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100976 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000977 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100978 }
Roland Levillain0d37cd02015-05-27 16:39:19 +0100979 __ setcc(X86_64Condition(cond->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700980 }
981}
982
983void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
984 VisitCondition(comp);
985}
986
987void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
988 VisitCondition(comp);
989}
990
991void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
992 VisitCondition(comp);
993}
994
995void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
996 VisitCondition(comp);
997}
998
999void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1000 VisitCondition(comp);
1001}
1002
1003void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1004 VisitCondition(comp);
1005}
1006
1007void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1008 VisitCondition(comp);
1009}
1010
1011void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1012 VisitCondition(comp);
1013}
1014
1015void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1016 VisitCondition(comp);
1017}
1018
1019void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1020 VisitCondition(comp);
1021}
1022
1023void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1024 VisitCondition(comp);
1025}
1026
1027void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1028 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029}
1030
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001031void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001034 switch (compare->InputAt(0)->GetType()) {
1035 case Primitive::kPrimLong: {
1036 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001037 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001038 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1039 break;
1040 }
1041 case Primitive::kPrimFloat:
1042 case Primitive::kPrimDouble: {
1043 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001044 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001045 locations->SetOut(Location::RequiresRegister());
1046 break;
1047 }
1048 default:
1049 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1050 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001051}
1052
1053void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001054 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001055 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001056 Location left = locations->InAt(0);
1057 Location right = locations->InAt(1);
1058
1059 Label less, greater, done;
1060 Primitive::Type type = compare->InputAt(0)->GetType();
1061 switch (type) {
1062 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001063 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1064 if (right.IsConstant()) {
1065 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001066 if (IsInt<32>(value)) {
1067 if (value == 0) {
1068 __ testq(left_reg, left_reg);
1069 } else {
1070 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1071 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001072 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001073 // Value won't fit in an int.
1074 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001075 }
Mark Mendell40741f32015-04-20 22:10:34 -04001076 } else if (right.IsDoubleStackSlot()) {
1077 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001078 } else {
1079 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1080 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001081 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001082 }
1083 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001084 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1085 if (right.IsConstant()) {
1086 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1087 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1088 } else if (right.IsStackSlot()) {
1089 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1090 } else {
1091 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1092 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001093 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1094 break;
1095 }
1096 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001097 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1098 if (right.IsConstant()) {
1099 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1100 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1101 } else if (right.IsDoubleStackSlot()) {
1102 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1103 } else {
1104 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1105 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001106 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1107 break;
1108 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001109 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001110 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001111 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001112 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001113 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001114 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001115
Calin Juravle91debbc2014-11-26 19:01:09 +00001116 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001117 __ movl(out, Immediate(1));
1118 __ jmp(&done);
1119
1120 __ Bind(&less);
1121 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001122
1123 __ Bind(&done);
1124}
1125
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001126void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001127 LocationSummary* locations =
1128 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001129 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130}
1131
1132void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001133 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135}
1136
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001137void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1138 LocationSummary* locations =
1139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1140 locations->SetOut(Location::ConstantLocation(constant));
1141}
1142
1143void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1144 // Will be generated at use site.
1145 UNUSED(constant);
1146}
1147
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001149 LocationSummary* locations =
1150 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001151 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152}
1153
1154void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001155 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001156 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001157}
1158
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001159void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1160 LocationSummary* locations =
1161 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1162 locations->SetOut(Location::ConstantLocation(constant));
1163}
1164
1165void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1166 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001167 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001168}
1169
1170void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1171 LocationSummary* locations =
1172 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1173 locations->SetOut(Location::ConstantLocation(constant));
1174}
1175
1176void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1177 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001178 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001179}
1180
Calin Juravle27df7582015-04-17 19:12:31 +01001181void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1182 memory_barrier->SetLocations(nullptr);
1183}
1184
1185void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1186 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1187}
1188
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001189void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1190 ret->SetLocations(nullptr);
1191}
1192
1193void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001194 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196}
1197
1198void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001199 LocationSummary* locations =
1200 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201 switch (ret->InputAt(0)->GetType()) {
1202 case Primitive::kPrimBoolean:
1203 case Primitive::kPrimByte:
1204 case Primitive::kPrimChar:
1205 case Primitive::kPrimShort:
1206 case Primitive::kPrimInt:
1207 case Primitive::kPrimNot:
1208 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001209 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001210 break;
1211
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 case Primitive::kPrimFloat:
1213 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001214 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 break;
1216
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001217 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001220}
1221
1222void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1223 if (kIsDebugBuild) {
1224 switch (ret->InputAt(0)->GetType()) {
1225 case Primitive::kPrimBoolean:
1226 case Primitive::kPrimByte:
1227 case Primitive::kPrimChar:
1228 case Primitive::kPrimShort:
1229 case Primitive::kPrimInt:
1230 case Primitive::kPrimNot:
1231 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001233 break;
1234
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 case Primitive::kPrimFloat:
1236 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 XMM0);
1239 break;
1240
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243 }
1244 }
1245 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001246}
1247
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001248Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 switch (type) {
1250 case Primitive::kPrimBoolean:
1251 case Primitive::kPrimByte:
1252 case Primitive::kPrimChar:
1253 case Primitive::kPrimShort:
1254 case Primitive::kPrimInt:
1255 case Primitive::kPrimNot: {
1256 uint32_t index = gp_index_++;
1257 stack_index_++;
1258 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001259 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 } else {
1261 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1262 }
1263 }
1264
1265 case Primitive::kPrimLong: {
1266 uint32_t index = gp_index_;
1267 stack_index_ += 2;
1268 if (index < calling_convention.GetNumberOfRegisters()) {
1269 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001270 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001271 } else {
1272 gp_index_ += 2;
1273 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1274 }
1275 }
1276
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001278 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 stack_index_++;
1280 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001281 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001282 } else {
1283 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1284 }
1285 }
1286
1287 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001288 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001289 stack_index_ += 2;
1290 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001291 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 } else {
1293 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1294 }
1295 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296
1297 case Primitive::kPrimVoid:
1298 LOG(FATAL) << "Unexpected parameter type " << type;
1299 break;
1300 }
1301 return Location();
1302}
1303
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001304void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001305 // When we do not run baseline, explicit clinit checks triggered by static
1306 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1307 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001308
Mark Mendellfb8d2792015-03-31 22:16:59 -04001309 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001310 if (intrinsic.TryDispatch(invoke)) {
1311 return;
1312 }
1313
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001314 HandleInvoke(invoke);
1315}
1316
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001317static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1318 if (invoke->GetLocations()->Intrinsified()) {
1319 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1320 intrinsic.Dispatch(invoke);
1321 return true;
1322 }
1323 return false;
1324}
1325
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001326void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001327 // When we do not run baseline, explicit clinit checks triggered by static
1328 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1329 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001330
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001331 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1332 return;
1333 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001334
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001335 codegen_->GenerateStaticOrDirectCall(
1336 invoke,
1337 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001338 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001339}
1340
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001341void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001342 LocationSummary* locations =
1343 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001344 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001345
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001346 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001347 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001348 HInstruction* input = invoke->InputAt(i);
1349 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1350 }
1351
1352 switch (invoke->GetType()) {
1353 case Primitive::kPrimBoolean:
1354 case Primitive::kPrimByte:
1355 case Primitive::kPrimChar:
1356 case Primitive::kPrimShort:
1357 case Primitive::kPrimInt:
1358 case Primitive::kPrimNot:
1359 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001360 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001361 break;
1362
1363 case Primitive::kPrimVoid:
1364 break;
1365
1366 case Primitive::kPrimDouble:
1367 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001368 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001369 break;
1370 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001371}
1372
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001373void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001374 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001375 if (intrinsic.TryDispatch(invoke)) {
1376 return;
1377 }
1378
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001379 HandleInvoke(invoke);
1380}
1381
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001382void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001383 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1384 return;
1385 }
1386
Roland Levillain271ab9c2014-11-27 15:23:57 +00001387 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001388 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1389 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001390 LocationSummary* locations = invoke->GetLocations();
1391 Location receiver = locations->InAt(0);
1392 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1393 // temp = object->GetClass();
1394 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001395 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1396 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001397 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001398 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001399 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001400 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001401 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001402 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001403 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001404 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001405 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001406
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001407 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001408 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001409}
1410
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001411void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1412 HandleInvoke(invoke);
1413 // Add the hidden argument.
1414 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1415}
1416
1417void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1418 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001419 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001420 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1421 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001422 LocationSummary* locations = invoke->GetLocations();
1423 Location receiver = locations->InAt(0);
1424 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1425
1426 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001427 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1428 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001429
1430 // temp = object->GetClass();
1431 if (receiver.IsStackSlot()) {
1432 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1433 __ movl(temp, Address(temp, class_offset));
1434 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001435 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001436 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001437 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001438 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001439 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001440 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001441 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001442 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001443
1444 DCHECK(!codegen_->IsLeafMethod());
1445 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1446}
1447
Roland Levillain88cb1752014-10-20 16:36:47 +01001448void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1449 LocationSummary* locations =
1450 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1451 switch (neg->GetResultType()) {
1452 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001453 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetOut(Location::SameAsFirstInput());
1456 break;
1457
Roland Levillain88cb1752014-10-20 16:36:47 +01001458 case Primitive::kPrimFloat:
1459 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001460 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001461 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001462 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001463 break;
1464
1465 default:
1466 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1467 }
1468}
1469
1470void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1471 LocationSummary* locations = neg->GetLocations();
1472 Location out = locations->Out();
1473 Location in = locations->InAt(0);
1474 switch (neg->GetResultType()) {
1475 case Primitive::kPrimInt:
1476 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001477 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001478 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001479 break;
1480
1481 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001482 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001483 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001484 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001485 break;
1486
Roland Levillain5368c212014-11-27 15:03:41 +00001487 case Primitive::kPrimFloat: {
1488 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001489 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001490 // Implement float negation with an exclusive or with value
1491 // 0x80000000 (mask for bit 31, representing the sign of a
1492 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001493 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001495 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001496 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001497
Roland Levillain5368c212014-11-27 15:03:41 +00001498 case Primitive::kPrimDouble: {
1499 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001500 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001501 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001502 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001503 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001504 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001505 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001506 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001507 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001508
1509 default:
1510 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1511 }
1512}
1513
Roland Levillaindff1f282014-11-05 14:15:05 +00001514void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1515 LocationSummary* locations =
1516 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1517 Primitive::Type result_type = conversion->GetResultType();
1518 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001519 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001520
David Brazdilb2bd1c52015-03-25 11:17:37 +00001521 // The Java language does not allow treating boolean as an integral type but
1522 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001523
Roland Levillaindff1f282014-11-05 14:15:05 +00001524 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001525 case Primitive::kPrimByte:
1526 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001527 case Primitive::kPrimBoolean:
1528 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001529 case Primitive::kPrimShort:
1530 case Primitive::kPrimInt:
1531 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001532 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001533 locations->SetInAt(0, Location::Any());
1534 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1535 break;
1536
1537 default:
1538 LOG(FATAL) << "Unexpected type conversion from " << input_type
1539 << " to " << result_type;
1540 }
1541 break;
1542
Roland Levillain01a8d712014-11-14 16:27:39 +00001543 case Primitive::kPrimShort:
1544 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001545 case Primitive::kPrimBoolean:
1546 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001547 case Primitive::kPrimByte:
1548 case Primitive::kPrimInt:
1549 case Primitive::kPrimChar:
1550 // Processing a Dex `int-to-short' instruction.
1551 locations->SetInAt(0, Location::Any());
1552 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1553 break;
1554
1555 default:
1556 LOG(FATAL) << "Unexpected type conversion from " << input_type
1557 << " to " << result_type;
1558 }
1559 break;
1560
Roland Levillain946e1432014-11-11 17:35:19 +00001561 case Primitive::kPrimInt:
1562 switch (input_type) {
1563 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001564 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001565 locations->SetInAt(0, Location::Any());
1566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1567 break;
1568
1569 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001570 // Processing a Dex `float-to-int' instruction.
1571 locations->SetInAt(0, Location::RequiresFpuRegister());
1572 locations->SetOut(Location::RequiresRegister());
1573 locations->AddTemp(Location::RequiresFpuRegister());
1574 break;
1575
Roland Levillain946e1432014-11-11 17:35:19 +00001576 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001577 // Processing a Dex `double-to-int' instruction.
1578 locations->SetInAt(0, Location::RequiresFpuRegister());
1579 locations->SetOut(Location::RequiresRegister());
1580 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001581 break;
1582
1583 default:
1584 LOG(FATAL) << "Unexpected type conversion from " << input_type
1585 << " to " << result_type;
1586 }
1587 break;
1588
Roland Levillaindff1f282014-11-05 14:15:05 +00001589 case Primitive::kPrimLong:
1590 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001591 case Primitive::kPrimBoolean:
1592 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001593 case Primitive::kPrimByte:
1594 case Primitive::kPrimShort:
1595 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001596 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001597 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001598 // TODO: We would benefit from a (to-be-implemented)
1599 // Location::RegisterOrStackSlot requirement for this input.
1600 locations->SetInAt(0, Location::RequiresRegister());
1601 locations->SetOut(Location::RequiresRegister());
1602 break;
1603
1604 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001605 // Processing a Dex `float-to-long' instruction.
1606 locations->SetInAt(0, Location::RequiresFpuRegister());
1607 locations->SetOut(Location::RequiresRegister());
1608 locations->AddTemp(Location::RequiresFpuRegister());
1609 break;
1610
Roland Levillaindff1f282014-11-05 14:15:05 +00001611 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001612 // Processing a Dex `double-to-long' instruction.
1613 locations->SetInAt(0, Location::RequiresFpuRegister());
1614 locations->SetOut(Location::RequiresRegister());
1615 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001616 break;
1617
1618 default:
1619 LOG(FATAL) << "Unexpected type conversion from " << input_type
1620 << " to " << result_type;
1621 }
1622 break;
1623
Roland Levillain981e4542014-11-14 11:47:14 +00001624 case Primitive::kPrimChar:
1625 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001626 case Primitive::kPrimBoolean:
1627 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001628 case Primitive::kPrimByte:
1629 case Primitive::kPrimShort:
1630 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001631 // Processing a Dex `int-to-char' instruction.
1632 locations->SetInAt(0, Location::Any());
1633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1634 break;
1635
1636 default:
1637 LOG(FATAL) << "Unexpected type conversion from " << input_type
1638 << " to " << result_type;
1639 }
1640 break;
1641
Roland Levillaindff1f282014-11-05 14:15:05 +00001642 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001643 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001644 case Primitive::kPrimBoolean:
1645 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001646 case Primitive::kPrimByte:
1647 case Primitive::kPrimShort:
1648 case Primitive::kPrimInt:
1649 case Primitive::kPrimChar:
1650 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001651 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001652 locations->SetOut(Location::RequiresFpuRegister());
1653 break;
1654
1655 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001656 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001657 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001658 locations->SetOut(Location::RequiresFpuRegister());
1659 break;
1660
Roland Levillaincff13742014-11-17 14:32:17 +00001661 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001662 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001663 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001664 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001665 break;
1666
1667 default:
1668 LOG(FATAL) << "Unexpected type conversion from " << input_type
1669 << " to " << result_type;
1670 };
1671 break;
1672
Roland Levillaindff1f282014-11-05 14:15:05 +00001673 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001674 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001675 case Primitive::kPrimBoolean:
1676 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001677 case Primitive::kPrimByte:
1678 case Primitive::kPrimShort:
1679 case Primitive::kPrimInt:
1680 case Primitive::kPrimChar:
1681 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001682 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001683 locations->SetOut(Location::RequiresFpuRegister());
1684 break;
1685
1686 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001687 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001688 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001689 locations->SetOut(Location::RequiresFpuRegister());
1690 break;
1691
Roland Levillaincff13742014-11-17 14:32:17 +00001692 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001693 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001694 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001695 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001696 break;
1697
1698 default:
1699 LOG(FATAL) << "Unexpected type conversion from " << input_type
1700 << " to " << result_type;
1701 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001702 break;
1703
1704 default:
1705 LOG(FATAL) << "Unexpected type conversion from " << input_type
1706 << " to " << result_type;
1707 }
1708}
1709
1710void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1711 LocationSummary* locations = conversion->GetLocations();
1712 Location out = locations->Out();
1713 Location in = locations->InAt(0);
1714 Primitive::Type result_type = conversion->GetResultType();
1715 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001716 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001717 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001718 case Primitive::kPrimByte:
1719 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001720 case Primitive::kPrimBoolean:
1721 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001722 case Primitive::kPrimShort:
1723 case Primitive::kPrimInt:
1724 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001725 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001726 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001727 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001728 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001729 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001730 Address(CpuRegister(RSP), in.GetStackIndex()));
1731 } else {
1732 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001733 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001734 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1735 }
1736 break;
1737
1738 default:
1739 LOG(FATAL) << "Unexpected type conversion from " << input_type
1740 << " to " << result_type;
1741 }
1742 break;
1743
Roland Levillain01a8d712014-11-14 16:27:39 +00001744 case Primitive::kPrimShort:
1745 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001746 case Primitive::kPrimBoolean:
1747 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001748 case Primitive::kPrimByte:
1749 case Primitive::kPrimInt:
1750 case Primitive::kPrimChar:
1751 // Processing a Dex `int-to-short' instruction.
1752 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001753 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001754 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001756 Address(CpuRegister(RSP), in.GetStackIndex()));
1757 } else {
1758 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001759 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001760 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1761 }
1762 break;
1763
1764 default:
1765 LOG(FATAL) << "Unexpected type conversion from " << input_type
1766 << " to " << result_type;
1767 }
1768 break;
1769
Roland Levillain946e1432014-11-11 17:35:19 +00001770 case Primitive::kPrimInt:
1771 switch (input_type) {
1772 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001773 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001774 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001775 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001776 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001777 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001778 Address(CpuRegister(RSP), in.GetStackIndex()));
1779 } else {
1780 DCHECK(in.IsConstant());
1781 DCHECK(in.GetConstant()->IsLongConstant());
1782 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001783 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001784 }
1785 break;
1786
Roland Levillain3f8f9362014-12-02 17:45:01 +00001787 case Primitive::kPrimFloat: {
1788 // Processing a Dex `float-to-int' instruction.
1789 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1790 CpuRegister output = out.AsRegister<CpuRegister>();
1791 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1792 Label done, nan;
1793
1794 __ movl(output, Immediate(kPrimIntMax));
1795 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001796 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001797 // if input >= temp goto done
1798 __ comiss(input, temp);
1799 __ j(kAboveEqual, &done);
1800 // if input == NaN goto nan
1801 __ j(kUnordered, &nan);
1802 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001803 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001804 __ jmp(&done);
1805 __ Bind(&nan);
1806 // output = 0
1807 __ xorl(output, output);
1808 __ Bind(&done);
1809 break;
1810 }
1811
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001812 case Primitive::kPrimDouble: {
1813 // Processing a Dex `double-to-int' instruction.
1814 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1815 CpuRegister output = out.AsRegister<CpuRegister>();
1816 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1817 Label done, nan;
1818
1819 __ movl(output, Immediate(kPrimIntMax));
1820 // temp = int-to-double(output)
1821 __ cvtsi2sd(temp, output);
1822 // if input >= temp goto done
1823 __ comisd(input, temp);
1824 __ j(kAboveEqual, &done);
1825 // if input == NaN goto nan
1826 __ j(kUnordered, &nan);
1827 // output = double-to-int-truncate(input)
1828 __ cvttsd2si(output, input);
1829 __ jmp(&done);
1830 __ Bind(&nan);
1831 // output = 0
1832 __ xorl(output, output);
1833 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001834 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001835 }
Roland Levillain946e1432014-11-11 17:35:19 +00001836
1837 default:
1838 LOG(FATAL) << "Unexpected type conversion from " << input_type
1839 << " to " << result_type;
1840 }
1841 break;
1842
Roland Levillaindff1f282014-11-05 14:15:05 +00001843 case Primitive::kPrimLong:
1844 switch (input_type) {
1845 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001846 case Primitive::kPrimBoolean:
1847 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001848 case Primitive::kPrimByte:
1849 case Primitive::kPrimShort:
1850 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001851 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001852 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001853 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001854 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001855 break;
1856
Roland Levillain624279f2014-12-04 11:54:28 +00001857 case Primitive::kPrimFloat: {
1858 // Processing a Dex `float-to-long' instruction.
1859 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1860 CpuRegister output = out.AsRegister<CpuRegister>();
1861 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1862 Label done, nan;
1863
Mark Mendell92e83bf2015-05-07 11:25:03 -04001864 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001865 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001866 __ cvtsi2ss(temp, output, true);
1867 // if input >= temp goto done
1868 __ comiss(input, temp);
1869 __ j(kAboveEqual, &done);
1870 // if input == NaN goto nan
1871 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001872 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001873 __ cvttss2si(output, input, true);
1874 __ jmp(&done);
1875 __ Bind(&nan);
1876 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001877 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00001878 __ Bind(&done);
1879 break;
1880 }
1881
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001882 case Primitive::kPrimDouble: {
1883 // Processing a Dex `double-to-long' instruction.
1884 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1885 CpuRegister output = out.AsRegister<CpuRegister>();
1886 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1887 Label done, nan;
1888
Mark Mendell92e83bf2015-05-07 11:25:03 -04001889 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001890 // temp = long-to-double(output)
1891 __ cvtsi2sd(temp, output, true);
1892 // if input >= temp goto done
1893 __ comisd(input, temp);
1894 __ j(kAboveEqual, &done);
1895 // if input == NaN goto nan
1896 __ j(kUnordered, &nan);
1897 // output = double-to-long-truncate(input)
1898 __ cvttsd2si(output, input, true);
1899 __ jmp(&done);
1900 __ Bind(&nan);
1901 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001902 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001903 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001904 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001905 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001906
1907 default:
1908 LOG(FATAL) << "Unexpected type conversion from " << input_type
1909 << " to " << result_type;
1910 }
1911 break;
1912
Roland Levillain981e4542014-11-14 11:47:14 +00001913 case Primitive::kPrimChar:
1914 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001915 case Primitive::kPrimBoolean:
1916 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001917 case Primitive::kPrimByte:
1918 case Primitive::kPrimShort:
1919 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001920 // Processing a Dex `int-to-char' instruction.
1921 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001923 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001924 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001925 Address(CpuRegister(RSP), in.GetStackIndex()));
1926 } else {
1927 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001928 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001929 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1930 }
1931 break;
1932
1933 default:
1934 LOG(FATAL) << "Unexpected type conversion from " << input_type
1935 << " to " << result_type;
1936 }
1937 break;
1938
Roland Levillaindff1f282014-11-05 14:15:05 +00001939 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001940 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001941 case Primitive::kPrimBoolean:
1942 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001943 case Primitive::kPrimByte:
1944 case Primitive::kPrimShort:
1945 case Primitive::kPrimInt:
1946 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001947 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001948 if (in.IsRegister()) {
1949 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1950 } else if (in.IsConstant()) {
1951 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
1952 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1953 if (v == 0) {
1954 __ xorps(dest, dest);
1955 } else {
1956 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1957 }
1958 } else {
1959 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1960 Address(CpuRegister(RSP), in.GetStackIndex()), false);
1961 }
Roland Levillaincff13742014-11-17 14:32:17 +00001962 break;
1963
1964 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001965 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001966 if (in.IsRegister()) {
1967 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1968 } else if (in.IsConstant()) {
1969 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
1970 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1971 if (v == 0) {
1972 __ xorps(dest, dest);
1973 } else {
1974 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1975 }
1976 } else {
1977 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1978 Address(CpuRegister(RSP), in.GetStackIndex()), true);
1979 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001980 break;
1981
Roland Levillaincff13742014-11-17 14:32:17 +00001982 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001983 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001984 if (in.IsFpuRegister()) {
1985 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
1986 } else if (in.IsConstant()) {
1987 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
1988 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1989 if (bit_cast<int64_t, double>(v) == 0) {
1990 __ xorps(dest, dest);
1991 } else {
1992 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1993 }
1994 } else {
1995 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
1996 Address(CpuRegister(RSP), in.GetStackIndex()));
1997 }
Roland Levillaincff13742014-11-17 14:32:17 +00001998 break;
1999
2000 default:
2001 LOG(FATAL) << "Unexpected type conversion from " << input_type
2002 << " to " << result_type;
2003 };
2004 break;
2005
Roland Levillaindff1f282014-11-05 14:15:05 +00002006 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002007 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002008 case Primitive::kPrimBoolean:
2009 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002010 case Primitive::kPrimByte:
2011 case Primitive::kPrimShort:
2012 case Primitive::kPrimInt:
2013 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002014 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002015 if (in.IsRegister()) {
2016 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2017 } else if (in.IsConstant()) {
2018 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2019 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2020 if (v == 0) {
2021 __ xorpd(dest, dest);
2022 } else {
2023 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2024 }
2025 } else {
2026 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2027 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2028 }
Roland Levillaincff13742014-11-17 14:32:17 +00002029 break;
2030
2031 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002032 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002033 if (in.IsRegister()) {
2034 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2035 } else if (in.IsConstant()) {
2036 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2037 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2038 if (v == 0) {
2039 __ xorpd(dest, dest);
2040 } else {
2041 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2042 }
2043 } else {
2044 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2045 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2046 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002047 break;
2048
Roland Levillaincff13742014-11-17 14:32:17 +00002049 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002050 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002051 if (in.IsFpuRegister()) {
2052 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2053 } else if (in.IsConstant()) {
2054 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2055 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2056 if (bit_cast<int32_t, float>(v) == 0) {
2057 __ xorpd(dest, dest);
2058 } else {
2059 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2060 }
2061 } else {
2062 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2063 Address(CpuRegister(RSP), in.GetStackIndex()));
2064 }
Roland Levillaincff13742014-11-17 14:32:17 +00002065 break;
2066
2067 default:
2068 LOG(FATAL) << "Unexpected type conversion from " << input_type
2069 << " to " << result_type;
2070 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002071 break;
2072
2073 default:
2074 LOG(FATAL) << "Unexpected type conversion from " << input_type
2075 << " to " << result_type;
2076 }
2077}
2078
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002079void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002080 LocationSummary* locations =
2081 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002082 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002083 case Primitive::kPrimInt: {
2084 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002085 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002087 break;
2088 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002089
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002090 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002091 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002092 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002093 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002094 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002095 break;
2096 }
2097
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002098 case Primitive::kPrimDouble:
2099 case Primitive::kPrimFloat: {
2100 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002101 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002102 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002104 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002105
2106 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002107 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002108 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002109}
2110
2111void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2112 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002113 Location first = locations->InAt(0);
2114 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002115 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002116
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002117 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002118 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002119 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002120 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2121 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002122 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2123 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002124 } else {
2125 __ leal(out.AsRegister<CpuRegister>(), Address(
2126 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2127 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002128 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002129 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2130 __ addl(out.AsRegister<CpuRegister>(),
2131 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2132 } else {
2133 __ leal(out.AsRegister<CpuRegister>(), Address(
2134 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2135 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002136 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002137 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002139 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002140 break;
2141 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002142
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002143 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002144 if (second.IsRegister()) {
2145 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2146 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002147 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2148 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002149 } else {
2150 __ leaq(out.AsRegister<CpuRegister>(), Address(
2151 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2152 }
2153 } else {
2154 DCHECK(second.IsConstant());
2155 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2156 int32_t int32_value = Low32Bits(value);
2157 DCHECK_EQ(int32_value, value);
2158 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2159 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2160 } else {
2161 __ leaq(out.AsRegister<CpuRegister>(), Address(
2162 first.AsRegister<CpuRegister>(), int32_value));
2163 }
2164 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002165 break;
2166 }
2167
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002168 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002169 if (second.IsFpuRegister()) {
2170 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2171 } else if (second.IsConstant()) {
2172 __ addss(first.AsFpuRegister<XmmRegister>(),
2173 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2174 } else {
2175 DCHECK(second.IsStackSlot());
2176 __ addss(first.AsFpuRegister<XmmRegister>(),
2177 Address(CpuRegister(RSP), second.GetStackIndex()));
2178 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002179 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002180 }
2181
2182 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002183 if (second.IsFpuRegister()) {
2184 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2185 } else if (second.IsConstant()) {
2186 __ addsd(first.AsFpuRegister<XmmRegister>(),
2187 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2188 } else {
2189 DCHECK(second.IsDoubleStackSlot());
2190 __ addsd(first.AsFpuRegister<XmmRegister>(),
2191 Address(CpuRegister(RSP), second.GetStackIndex()));
2192 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002193 break;
2194 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002195
2196 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002197 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002198 }
2199}
2200
2201void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002202 LocationSummary* locations =
2203 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002204 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002205 case Primitive::kPrimInt: {
2206 locations->SetInAt(0, Location::RequiresRegister());
2207 locations->SetInAt(1, Location::Any());
2208 locations->SetOut(Location::SameAsFirstInput());
2209 break;
2210 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002211 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002212 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002213 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002214 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002215 break;
2216 }
Calin Juravle11351682014-10-23 15:38:15 +01002217 case Primitive::kPrimFloat:
2218 case Primitive::kPrimDouble: {
2219 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002220 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002221 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002222 break;
Calin Juravle11351682014-10-23 15:38:15 +01002223 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002224 default:
Calin Juravle11351682014-10-23 15:38:15 +01002225 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002226 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002227}
2228
2229void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2230 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002231 Location first = locations->InAt(0);
2232 Location second = locations->InAt(1);
2233 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002234 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002235 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002236 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002237 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002238 } else if (second.IsConstant()) {
2239 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002241 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002243 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002244 break;
2245 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002246 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002247 if (second.IsConstant()) {
2248 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2249 DCHECK(IsInt<32>(value));
2250 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2251 } else {
2252 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2253 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002254 break;
2255 }
2256
Calin Juravle11351682014-10-23 15:38:15 +01002257 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002258 if (second.IsFpuRegister()) {
2259 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2260 } else if (second.IsConstant()) {
2261 __ subss(first.AsFpuRegister<XmmRegister>(),
2262 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2263 } else {
2264 DCHECK(second.IsStackSlot());
2265 __ subss(first.AsFpuRegister<XmmRegister>(),
2266 Address(CpuRegister(RSP), second.GetStackIndex()));
2267 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002268 break;
Calin Juravle11351682014-10-23 15:38:15 +01002269 }
2270
2271 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002272 if (second.IsFpuRegister()) {
2273 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2274 } else if (second.IsConstant()) {
2275 __ subsd(first.AsFpuRegister<XmmRegister>(),
2276 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2277 } else {
2278 DCHECK(second.IsDoubleStackSlot());
2279 __ subsd(first.AsFpuRegister<XmmRegister>(),
2280 Address(CpuRegister(RSP), second.GetStackIndex()));
2281 }
Calin Juravle11351682014-10-23 15:38:15 +01002282 break;
2283 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002284
2285 default:
Calin Juravle11351682014-10-23 15:38:15 +01002286 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002287 }
2288}
2289
Calin Juravle34bacdf2014-10-07 20:23:36 +01002290void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2291 LocationSummary* locations =
2292 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2293 switch (mul->GetResultType()) {
2294 case Primitive::kPrimInt: {
2295 locations->SetInAt(0, Location::RequiresRegister());
2296 locations->SetInAt(1, Location::Any());
2297 locations->SetOut(Location::SameAsFirstInput());
2298 break;
2299 }
2300 case Primitive::kPrimLong: {
2301 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002302 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2303 if (locations->InAt(1).IsConstant()) {
2304 // Can use 3 operand multiply.
2305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2306 } else {
2307 locations->SetOut(Location::SameAsFirstInput());
2308 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002309 break;
2310 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002311 case Primitive::kPrimFloat:
2312 case Primitive::kPrimDouble: {
2313 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002314 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002315 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002316 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002317 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002318
2319 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002320 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002321 }
2322}
2323
2324void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2325 LocationSummary* locations = mul->GetLocations();
2326 Location first = locations->InAt(0);
2327 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002328 switch (mul->GetResultType()) {
2329 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002330 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002331 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002333 } else if (second.IsConstant()) {
2334 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002335 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002336 } else {
2337 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002338 __ imull(first.AsRegister<CpuRegister>(),
2339 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002340 }
2341 break;
2342 }
2343 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002344 if (second.IsConstant()) {
2345 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2346 DCHECK(IsInt<32>(value));
2347 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2348 first.AsRegister<CpuRegister>(),
2349 Immediate(static_cast<int32_t>(value)));
2350 } else {
2351 DCHECK(first.Equals(locations->Out()));
2352 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2353 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002354 break;
2355 }
2356
Calin Juravleb5bfa962014-10-21 18:02:24 +01002357 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002358 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002359 if (second.IsFpuRegister()) {
2360 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2361 } else if (second.IsConstant()) {
2362 __ mulss(first.AsFpuRegister<XmmRegister>(),
2363 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2364 } else {
2365 DCHECK(second.IsStackSlot());
2366 __ mulss(first.AsFpuRegister<XmmRegister>(),
2367 Address(CpuRegister(RSP), second.GetStackIndex()));
2368 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002369 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002370 }
2371
2372 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002373 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002374 if (second.IsFpuRegister()) {
2375 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2376 } else if (second.IsConstant()) {
2377 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2378 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2379 } else {
2380 DCHECK(second.IsDoubleStackSlot());
2381 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2382 Address(CpuRegister(RSP), second.GetStackIndex()));
2383 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002384 break;
2385 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002386
2387 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002388 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002389 }
2390}
2391
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002392void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2393 uint32_t stack_adjustment, bool is_float) {
2394 if (source.IsStackSlot()) {
2395 DCHECK(is_float);
2396 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2397 } else if (source.IsDoubleStackSlot()) {
2398 DCHECK(!is_float);
2399 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2400 } else {
2401 // Write the value to the temporary location on the stack and load to FP stack.
2402 if (is_float) {
2403 Location stack_temp = Location::StackSlot(temp_offset);
2404 codegen_->Move(stack_temp, source);
2405 __ flds(Address(CpuRegister(RSP), temp_offset));
2406 } else {
2407 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2408 codegen_->Move(stack_temp, source);
2409 __ fldl(Address(CpuRegister(RSP), temp_offset));
2410 }
2411 }
2412}
2413
2414void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2415 Primitive::Type type = rem->GetResultType();
2416 bool is_float = type == Primitive::kPrimFloat;
2417 size_t elem_size = Primitive::ComponentSize(type);
2418 LocationSummary* locations = rem->GetLocations();
2419 Location first = locations->InAt(0);
2420 Location second = locations->InAt(1);
2421 Location out = locations->Out();
2422
2423 // Create stack space for 2 elements.
2424 // TODO: enhance register allocator to ask for stack temporaries.
2425 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2426
2427 // Load the values to the FP stack in reverse order, using temporaries if needed.
2428 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2429 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2430
2431 // Loop doing FPREM until we stabilize.
2432 Label retry;
2433 __ Bind(&retry);
2434 __ fprem();
2435
2436 // Move FP status to AX.
2437 __ fstsw();
2438
2439 // And see if the argument reduction is complete. This is signaled by the
2440 // C2 FPU flag bit set to 0.
2441 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2442 __ j(kNotEqual, &retry);
2443
2444 // We have settled on the final value. Retrieve it into an XMM register.
2445 // Store FP top of stack to real stack.
2446 if (is_float) {
2447 __ fsts(Address(CpuRegister(RSP), 0));
2448 } else {
2449 __ fstl(Address(CpuRegister(RSP), 0));
2450 }
2451
2452 // Pop the 2 items from the FP stack.
2453 __ fucompp();
2454
2455 // Load the value from the stack into an XMM register.
2456 DCHECK(out.IsFpuRegister()) << out;
2457 if (is_float) {
2458 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2459 } else {
2460 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2461 }
2462
2463 // And remove the temporary stack space we allocated.
2464 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2465}
2466
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002467void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2468 DCHECK(instruction->IsDiv() || instruction->IsRem());
2469
2470 LocationSummary* locations = instruction->GetLocations();
2471 Location second = locations->InAt(1);
2472 DCHECK(second.IsConstant());
2473
2474 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2475 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002476 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002477
2478 DCHECK(imm == 1 || imm == -1);
2479
2480 switch (instruction->GetResultType()) {
2481 case Primitive::kPrimInt: {
2482 if (instruction->IsRem()) {
2483 __ xorl(output_register, output_register);
2484 } else {
2485 __ movl(output_register, input_register);
2486 if (imm == -1) {
2487 __ negl(output_register);
2488 }
2489 }
2490 break;
2491 }
2492
2493 case Primitive::kPrimLong: {
2494 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002495 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002496 } else {
2497 __ movq(output_register, input_register);
2498 if (imm == -1) {
2499 __ negq(output_register);
2500 }
2501 }
2502 break;
2503 }
2504
2505 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002506 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002507 }
2508}
2509
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002510void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002511 LocationSummary* locations = instruction->GetLocations();
2512 Location second = locations->InAt(1);
2513
2514 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2515 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2516
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002517 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002518
2519 DCHECK(IsPowerOfTwo(std::abs(imm)));
2520
2521 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2522
2523 if (instruction->GetResultType() == Primitive::kPrimInt) {
2524 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2525 __ testl(numerator, numerator);
2526 __ cmov(kGreaterEqual, tmp, numerator);
2527 int shift = CTZ(imm);
2528 __ sarl(tmp, Immediate(shift));
2529
2530 if (imm < 0) {
2531 __ negl(tmp);
2532 }
2533
2534 __ movl(output_register, tmp);
2535 } else {
2536 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2537 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2538
Mark Mendell92e83bf2015-05-07 11:25:03 -04002539 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002540 __ addq(rdx, numerator);
2541 __ testq(numerator, numerator);
2542 __ cmov(kGreaterEqual, rdx, numerator);
2543 int shift = CTZ(imm);
2544 __ sarq(rdx, Immediate(shift));
2545
2546 if (imm < 0) {
2547 __ negq(rdx);
2548 }
2549
2550 __ movq(output_register, rdx);
2551 }
2552}
2553
2554void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2555 DCHECK(instruction->IsDiv() || instruction->IsRem());
2556
2557 LocationSummary* locations = instruction->GetLocations();
2558 Location second = locations->InAt(1);
2559
2560 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2561 : locations->GetTemp(0).AsRegister<CpuRegister>();
2562 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2563 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2564 : locations->Out().AsRegister<CpuRegister>();
2565 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2566
2567 DCHECK_EQ(RAX, eax.AsRegister());
2568 DCHECK_EQ(RDX, edx.AsRegister());
2569 if (instruction->IsDiv()) {
2570 DCHECK_EQ(RAX, out.AsRegister());
2571 } else {
2572 DCHECK_EQ(RDX, out.AsRegister());
2573 }
2574
2575 int64_t magic;
2576 int shift;
2577
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002578 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002579 if (instruction->GetResultType() == Primitive::kPrimInt) {
2580 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2581
2582 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2583
2584 __ movl(numerator, eax);
2585
2586 Label no_div;
2587 Label end;
2588 __ testl(eax, eax);
2589 __ j(kNotEqual, &no_div);
2590
2591 __ xorl(out, out);
2592 __ jmp(&end);
2593
2594 __ Bind(&no_div);
2595
2596 __ movl(eax, Immediate(magic));
2597 __ imull(numerator);
2598
2599 if (imm > 0 && magic < 0) {
2600 __ addl(edx, numerator);
2601 } else if (imm < 0 && magic > 0) {
2602 __ subl(edx, numerator);
2603 }
2604
2605 if (shift != 0) {
2606 __ sarl(edx, Immediate(shift));
2607 }
2608
2609 __ movl(eax, edx);
2610 __ shrl(edx, Immediate(31));
2611 __ addl(edx, eax);
2612
2613 if (instruction->IsRem()) {
2614 __ movl(eax, numerator);
2615 __ imull(edx, Immediate(imm));
2616 __ subl(eax, edx);
2617 __ movl(edx, eax);
2618 } else {
2619 __ movl(eax, edx);
2620 }
2621 __ Bind(&end);
2622 } else {
2623 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2624
2625 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2626
2627 CpuRegister rax = eax;
2628 CpuRegister rdx = edx;
2629
2630 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2631
2632 // Save the numerator.
2633 __ movq(numerator, rax);
2634
2635 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002636 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002637
2638 // RDX:RAX = magic * numerator
2639 __ imulq(numerator);
2640
2641 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002642 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002643 __ addq(rdx, numerator);
2644 } else if (imm < 0 && magic > 0) {
2645 // RDX -= numerator
2646 __ subq(rdx, numerator);
2647 }
2648
2649 // Shift if needed.
2650 if (shift != 0) {
2651 __ sarq(rdx, Immediate(shift));
2652 }
2653
2654 // RDX += 1 if RDX < 0
2655 __ movq(rax, rdx);
2656 __ shrq(rdx, Immediate(63));
2657 __ addq(rdx, rax);
2658
2659 if (instruction->IsRem()) {
2660 __ movq(rax, numerator);
2661
2662 if (IsInt<32>(imm)) {
2663 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2664 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002665 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002666 }
2667
2668 __ subq(rax, rdx);
2669 __ movq(rdx, rax);
2670 } else {
2671 __ movq(rax, rdx);
2672 }
2673 }
2674}
2675
Calin Juravlebacfec32014-11-14 15:54:36 +00002676void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2677 DCHECK(instruction->IsDiv() || instruction->IsRem());
2678 Primitive::Type type = instruction->GetResultType();
2679 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2680
2681 bool is_div = instruction->IsDiv();
2682 LocationSummary* locations = instruction->GetLocations();
2683
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002684 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2685 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002686
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002688 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002689
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002690 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002691 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002692
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002693 if (imm == 0) {
2694 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2695 } else if (imm == 1 || imm == -1) {
2696 DivRemOneOrMinusOne(instruction);
2697 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002698 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002699 } else {
2700 DCHECK(imm <= -2 || imm >= 2);
2701 GenerateDivRemWithAnyConstant(instruction);
2702 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002703 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002704 SlowPathCodeX86_64* slow_path =
2705 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2706 out.AsRegister(), type, is_div);
2707 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002708
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002709 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2710 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2711 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2712 // so it's safe to just use negl instead of more complex comparisons.
2713 if (type == Primitive::kPrimInt) {
2714 __ cmpl(second_reg, Immediate(-1));
2715 __ j(kEqual, slow_path->GetEntryLabel());
2716 // edx:eax <- sign-extended of eax
2717 __ cdq();
2718 // eax = quotient, edx = remainder
2719 __ idivl(second_reg);
2720 } else {
2721 __ cmpq(second_reg, Immediate(-1));
2722 __ j(kEqual, slow_path->GetEntryLabel());
2723 // rdx:rax <- sign-extended of rax
2724 __ cqo();
2725 // rax = quotient, rdx = remainder
2726 __ idivq(second_reg);
2727 }
2728 __ Bind(slow_path->GetExitLabel());
2729 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002730}
2731
Calin Juravle7c4954d2014-10-28 16:57:40 +00002732void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2733 LocationSummary* locations =
2734 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2735 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002736 case Primitive::kPrimInt:
2737 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002738 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002739 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002740 locations->SetOut(Location::SameAsFirstInput());
2741 // Intel uses edx:eax as the dividend.
2742 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002743 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2744 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2745 // output and request another temp.
2746 if (div->InputAt(1)->IsConstant()) {
2747 locations->AddTemp(Location::RequiresRegister());
2748 }
Calin Juravled0d48522014-11-04 16:40:20 +00002749 break;
2750 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002751
Calin Juravle7c4954d2014-10-28 16:57:40 +00002752 case Primitive::kPrimFloat:
2753 case Primitive::kPrimDouble: {
2754 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002755 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002756 locations->SetOut(Location::SameAsFirstInput());
2757 break;
2758 }
2759
2760 default:
2761 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2762 }
2763}
2764
2765void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2766 LocationSummary* locations = div->GetLocations();
2767 Location first = locations->InAt(0);
2768 Location second = locations->InAt(1);
2769 DCHECK(first.Equals(locations->Out()));
2770
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002771 Primitive::Type type = div->GetResultType();
2772 switch (type) {
2773 case Primitive::kPrimInt:
2774 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002775 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002776 break;
2777 }
2778
Calin Juravle7c4954d2014-10-28 16:57:40 +00002779 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002780 if (second.IsFpuRegister()) {
2781 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2782 } else if (second.IsConstant()) {
2783 __ divss(first.AsFpuRegister<XmmRegister>(),
2784 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2785 } else {
2786 DCHECK(second.IsStackSlot());
2787 __ divss(first.AsFpuRegister<XmmRegister>(),
2788 Address(CpuRegister(RSP), second.GetStackIndex()));
2789 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002790 break;
2791 }
2792
2793 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002794 if (second.IsFpuRegister()) {
2795 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2796 } else if (second.IsConstant()) {
2797 __ divsd(first.AsFpuRegister<XmmRegister>(),
2798 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2799 } else {
2800 DCHECK(second.IsDoubleStackSlot());
2801 __ divsd(first.AsFpuRegister<XmmRegister>(),
2802 Address(CpuRegister(RSP), second.GetStackIndex()));
2803 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002804 break;
2805 }
2806
2807 default:
2808 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2809 }
2810}
2811
Calin Juravlebacfec32014-11-14 15:54:36 +00002812void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002813 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002814 LocationSummary* locations =
2815 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002816
2817 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002818 case Primitive::kPrimInt:
2819 case Primitive::kPrimLong: {
2820 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002821 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002822 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2823 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002824 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2825 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2826 // output and request another temp.
2827 if (rem->InputAt(1)->IsConstant()) {
2828 locations->AddTemp(Location::RequiresRegister());
2829 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002830 break;
2831 }
2832
2833 case Primitive::kPrimFloat:
2834 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002835 locations->SetInAt(0, Location::Any());
2836 locations->SetInAt(1, Location::Any());
2837 locations->SetOut(Location::RequiresFpuRegister());
2838 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002839 break;
2840 }
2841
2842 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002843 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002844 }
2845}
2846
2847void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2848 Primitive::Type type = rem->GetResultType();
2849 switch (type) {
2850 case Primitive::kPrimInt:
2851 case Primitive::kPrimLong: {
2852 GenerateDivRemIntegral(rem);
2853 break;
2854 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002855 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002856 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002857 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002858 break;
2859 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002860 default:
2861 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2862 }
2863}
2864
Calin Juravled0d48522014-11-04 16:40:20 +00002865void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2866 LocationSummary* locations =
2867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2868 locations->SetInAt(0, Location::Any());
2869 if (instruction->HasUses()) {
2870 locations->SetOut(Location::SameAsFirstInput());
2871 }
2872}
2873
2874void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2875 SlowPathCodeX86_64* slow_path =
2876 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2877 codegen_->AddSlowPath(slow_path);
2878
2879 LocationSummary* locations = instruction->GetLocations();
2880 Location value = locations->InAt(0);
2881
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002882 switch (instruction->GetType()) {
2883 case Primitive::kPrimInt: {
2884 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002885 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002886 __ j(kEqual, slow_path->GetEntryLabel());
2887 } else if (value.IsStackSlot()) {
2888 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2889 __ j(kEqual, slow_path->GetEntryLabel());
2890 } else {
2891 DCHECK(value.IsConstant()) << value;
2892 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2893 __ jmp(slow_path->GetEntryLabel());
2894 }
2895 }
2896 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002897 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002898 case Primitive::kPrimLong: {
2899 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002900 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002901 __ j(kEqual, slow_path->GetEntryLabel());
2902 } else if (value.IsDoubleStackSlot()) {
2903 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2904 __ j(kEqual, slow_path->GetEntryLabel());
2905 } else {
2906 DCHECK(value.IsConstant()) << value;
2907 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2908 __ jmp(slow_path->GetEntryLabel());
2909 }
2910 }
2911 break;
2912 }
2913 default:
2914 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002915 }
Calin Juravled0d48522014-11-04 16:40:20 +00002916}
2917
Calin Juravle9aec02f2014-11-18 23:06:35 +00002918void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2919 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2920
2921 LocationSummary* locations =
2922 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2923
2924 switch (op->GetResultType()) {
2925 case Primitive::kPrimInt:
2926 case Primitive::kPrimLong: {
2927 locations->SetInAt(0, Location::RequiresRegister());
2928 // The shift count needs to be in CL.
2929 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2930 locations->SetOut(Location::SameAsFirstInput());
2931 break;
2932 }
2933 default:
2934 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2935 }
2936}
2937
2938void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2939 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2940
2941 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002942 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002943 Location second = locations->InAt(1);
2944
2945 switch (op->GetResultType()) {
2946 case Primitive::kPrimInt: {
2947 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002948 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002949 if (op->IsShl()) {
2950 __ shll(first_reg, second_reg);
2951 } else if (op->IsShr()) {
2952 __ sarl(first_reg, second_reg);
2953 } else {
2954 __ shrl(first_reg, second_reg);
2955 }
2956 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002957 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002958 if (op->IsShl()) {
2959 __ shll(first_reg, imm);
2960 } else if (op->IsShr()) {
2961 __ sarl(first_reg, imm);
2962 } else {
2963 __ shrl(first_reg, imm);
2964 }
2965 }
2966 break;
2967 }
2968 case Primitive::kPrimLong: {
2969 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002971 if (op->IsShl()) {
2972 __ shlq(first_reg, second_reg);
2973 } else if (op->IsShr()) {
2974 __ sarq(first_reg, second_reg);
2975 } else {
2976 __ shrq(first_reg, second_reg);
2977 }
2978 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002979 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002980 if (op->IsShl()) {
2981 __ shlq(first_reg, imm);
2982 } else if (op->IsShr()) {
2983 __ sarq(first_reg, imm);
2984 } else {
2985 __ shrq(first_reg, imm);
2986 }
2987 }
2988 break;
2989 }
2990 default:
2991 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2992 }
2993}
2994
2995void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2996 HandleShift(shl);
2997}
2998
2999void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3000 HandleShift(shl);
3001}
3002
3003void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3004 HandleShift(shr);
3005}
3006
3007void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3008 HandleShift(shr);
3009}
3010
3011void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3012 HandleShift(ushr);
3013}
3014
3015void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3016 HandleShift(ushr);
3017}
3018
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003020 LocationSummary* locations =
3021 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003022 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003023 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3024 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3025 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003026}
3027
3028void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3029 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003030 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Mark Mendell92e83bf2015-05-07 11:25:03 -04003031 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3032 instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003033 __ gs()->call(
3034 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003036 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003037 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003038}
3039
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003040void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3041 LocationSummary* locations =
3042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3043 InvokeRuntimeCallingConvention calling_convention;
3044 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003045 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003046 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003047 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003048}
3049
3050void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3051 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003052 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Mark Mendell92e83bf2015-05-07 11:25:03 -04003053 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3054 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003055
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003056 __ gs()->call(
3057 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003058
3059 DCHECK(!codegen_->IsLeafMethod());
3060 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3061}
3062
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003063void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003064 LocationSummary* locations =
3065 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003066 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3067 if (location.IsStackSlot()) {
3068 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3069 } else if (location.IsDoubleStackSlot()) {
3070 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3071 }
3072 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003073}
3074
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003075void InstructionCodeGeneratorX86_64::VisitParameterValue(
3076 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003077 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003078}
3079
3080void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3081 LocationSummary* locations =
3082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3083 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3084}
3085
3086void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3087 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3088 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003089}
3090
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003091void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003092 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003093 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003094 locations->SetInAt(0, Location::RequiresRegister());
3095 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003096}
3097
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003098void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3099 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003100 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3101 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003102 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003103 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003104 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003106 break;
3107
3108 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003109 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003110 break;
3111
3112 default:
3113 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3114 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003115}
3116
David Brazdil66d126e2015-04-03 16:02:44 +01003117void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3118 LocationSummary* locations =
3119 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3120 locations->SetInAt(0, Location::RequiresRegister());
3121 locations->SetOut(Location::SameAsFirstInput());
3122}
3123
3124void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003125 LocationSummary* locations = bool_not->GetLocations();
3126 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3127 locations->Out().AsRegister<CpuRegister>().AsRegister());
3128 Location out = locations->Out();
3129 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3130}
3131
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003132void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003133 LocationSummary* locations =
3134 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003135 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3136 locations->SetInAt(i, Location::Any());
3137 }
3138 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003139}
3140
3141void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003142 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003143 LOG(FATAL) << "Unimplemented";
3144}
3145
Calin Juravle52c48962014-12-16 17:02:57 +00003146void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3147 /*
3148 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3149 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3150 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3151 */
3152 switch (kind) {
3153 case MemBarrierKind::kAnyAny: {
3154 __ mfence();
3155 break;
3156 }
3157 case MemBarrierKind::kAnyStore:
3158 case MemBarrierKind::kLoadAny:
3159 case MemBarrierKind::kStoreStore: {
3160 // nop
3161 break;
3162 }
3163 default:
3164 LOG(FATAL) << "Unexpected memory barier " << kind;
3165 }
3166}
3167
3168void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3169 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3170
Nicolas Geoffray39468442014-09-02 15:17:15 +01003171 LocationSummary* locations =
3172 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003173 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003174 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3175 locations->SetOut(Location::RequiresFpuRegister());
3176 } else {
3177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3178 }
Calin Juravle52c48962014-12-16 17:02:57 +00003179}
3180
3181void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3182 const FieldInfo& field_info) {
3183 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3184
3185 LocationSummary* locations = instruction->GetLocations();
3186 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3187 Location out = locations->Out();
3188 bool is_volatile = field_info.IsVolatile();
3189 Primitive::Type field_type = field_info.GetFieldType();
3190 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3191
3192 switch (field_type) {
3193 case Primitive::kPrimBoolean: {
3194 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3195 break;
3196 }
3197
3198 case Primitive::kPrimByte: {
3199 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3200 break;
3201 }
3202
3203 case Primitive::kPrimShort: {
3204 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3205 break;
3206 }
3207
3208 case Primitive::kPrimChar: {
3209 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3210 break;
3211 }
3212
3213 case Primitive::kPrimInt:
3214 case Primitive::kPrimNot: {
3215 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3216 break;
3217 }
3218
3219 case Primitive::kPrimLong: {
3220 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3221 break;
3222 }
3223
3224 case Primitive::kPrimFloat: {
3225 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3226 break;
3227 }
3228
3229 case Primitive::kPrimDouble: {
3230 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3231 break;
3232 }
3233
3234 case Primitive::kPrimVoid:
3235 LOG(FATAL) << "Unreachable type " << field_type;
3236 UNREACHABLE();
3237 }
3238
Calin Juravle77520bc2015-01-12 18:45:46 +00003239 codegen_->MaybeRecordImplicitNullCheck(instruction);
3240
Calin Juravle52c48962014-12-16 17:02:57 +00003241 if (is_volatile) {
3242 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3243 }
3244}
3245
3246void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3247 const FieldInfo& field_info) {
3248 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3249
3250 LocationSummary* locations =
3251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003252 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003253 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3254
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003255 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003256 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3257 locations->SetInAt(1, Location::RequiresFpuRegister());
3258 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003259 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003260 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003261 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003262 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003263 locations->AddTemp(Location::RequiresRegister());
3264 locations->AddTemp(Location::RequiresRegister());
3265 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003266}
3267
Calin Juravle52c48962014-12-16 17:02:57 +00003268void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003269 const FieldInfo& field_info,
3270 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003271 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3272
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003273 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003274 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3275 Location value = locations->InAt(1);
3276 bool is_volatile = field_info.IsVolatile();
3277 Primitive::Type field_type = field_info.GetFieldType();
3278 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3279
3280 if (is_volatile) {
3281 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3282 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003283
3284 switch (field_type) {
3285 case Primitive::kPrimBoolean:
3286 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003287 if (value.IsConstant()) {
3288 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3289 __ movb(Address(base, offset), Immediate(v));
3290 } else {
3291 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3292 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003293 break;
3294 }
3295
3296 case Primitive::kPrimShort:
3297 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003298 if (value.IsConstant()) {
3299 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3300 __ movw(Address(base, offset), Immediate(v));
3301 } else {
3302 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3303 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003304 break;
3305 }
3306
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003307 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003308 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003309 if (value.IsConstant()) {
3310 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3311 __ movw(Address(base, offset), Immediate(v));
3312 } else {
3313 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3314 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003315 break;
3316 }
3317
3318 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003319 if (value.IsConstant()) {
3320 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3321 DCHECK(IsInt<32>(v));
3322 int32_t v_32 = v;
3323 __ movq(Address(base, offset), Immediate(v_32));
3324 } else {
3325 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3326 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327 break;
3328 }
3329
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003330 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003331 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003332 break;
3333 }
3334
3335 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003336 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003337 break;
3338 }
3339
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340 case Primitive::kPrimVoid:
3341 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003342 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003343 }
Calin Juravle52c48962014-12-16 17:02:57 +00003344
Calin Juravle77520bc2015-01-12 18:45:46 +00003345 codegen_->MaybeRecordImplicitNullCheck(instruction);
3346
3347 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3348 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3349 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003350 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003351 }
3352
Calin Juravle52c48962014-12-16 17:02:57 +00003353 if (is_volatile) {
3354 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3355 }
3356}
3357
3358void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3359 HandleFieldSet(instruction, instruction->GetFieldInfo());
3360}
3361
3362void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003363 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003364}
3365
3366void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003367 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003368}
3369
3370void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003371 HandleFieldGet(instruction, instruction->GetFieldInfo());
3372}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003373
Calin Juravle52c48962014-12-16 17:02:57 +00003374void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3375 HandleFieldGet(instruction);
3376}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003377
Calin Juravle52c48962014-12-16 17:02:57 +00003378void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3379 HandleFieldGet(instruction, instruction->GetFieldInfo());
3380}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003381
Calin Juravle52c48962014-12-16 17:02:57 +00003382void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3383 HandleFieldSet(instruction, instruction->GetFieldInfo());
3384}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003385
Calin Juravle52c48962014-12-16 17:02:57 +00003386void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003387 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003388}
3389
3390void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003391 LocationSummary* locations =
3392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003393 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3394 ? Location::RequiresRegister()
3395 : Location::Any();
3396 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003397 if (instruction->HasUses()) {
3398 locations->SetOut(Location::SameAsFirstInput());
3399 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003400}
3401
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003402void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003403 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3404 return;
3405 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003406 LocationSummary* locations = instruction->GetLocations();
3407 Location obj = locations->InAt(0);
3408
3409 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3410 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3411}
3412
3413void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003414 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003415 codegen_->AddSlowPath(slow_path);
3416
3417 LocationSummary* locations = instruction->GetLocations();
3418 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003419
3420 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003421 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003422 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003423 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003424 } else {
3425 DCHECK(obj.IsConstant()) << obj;
3426 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3427 __ jmp(slow_path->GetEntryLabel());
3428 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003429 }
3430 __ j(kEqual, slow_path->GetEntryLabel());
3431}
3432
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003433void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3434 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3435 GenerateImplicitNullCheck(instruction);
3436 } else {
3437 GenerateExplicitNullCheck(instruction);
3438 }
3439}
3440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003441void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003442 LocationSummary* locations =
3443 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003444 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003445 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003446 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3447 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3448 } else {
3449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3450 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003451}
3452
3453void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3454 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456 Location index = locations->InAt(1);
3457
3458 switch (instruction->GetType()) {
3459 case Primitive::kPrimBoolean: {
3460 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003461 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003462 if (index.IsConstant()) {
3463 __ movzxb(out, Address(obj,
3464 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3465 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003466 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003467 }
3468 break;
3469 }
3470
3471 case Primitive::kPrimByte: {
3472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003473 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003474 if (index.IsConstant()) {
3475 __ movsxb(out, Address(obj,
3476 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3477 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003478 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003479 }
3480 break;
3481 }
3482
3483 case Primitive::kPrimShort: {
3484 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003486 if (index.IsConstant()) {
3487 __ movsxw(out, Address(obj,
3488 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3489 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003490 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003491 }
3492 break;
3493 }
3494
3495 case Primitive::kPrimChar: {
3496 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003497 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003498 if (index.IsConstant()) {
3499 __ movzxw(out, Address(obj,
3500 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3501 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003502 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003503 }
3504 break;
3505 }
3506
3507 case Primitive::kPrimInt:
3508 case Primitive::kPrimNot: {
3509 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3510 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003511 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003512 if (index.IsConstant()) {
3513 __ movl(out, Address(obj,
3514 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3515 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003516 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003517 }
3518 break;
3519 }
3520
3521 case Primitive::kPrimLong: {
3522 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003523 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003524 if (index.IsConstant()) {
3525 __ movq(out, Address(obj,
3526 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3527 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003528 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003529 }
3530 break;
3531 }
3532
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003533 case Primitive::kPrimFloat: {
3534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003535 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003536 if (index.IsConstant()) {
3537 __ movss(out, Address(obj,
3538 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3539 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003541 }
3542 break;
3543 }
3544
3545 case Primitive::kPrimDouble: {
3546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003547 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003548 if (index.IsConstant()) {
3549 __ movsd(out, Address(obj,
3550 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3551 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003553 }
3554 break;
3555 }
3556
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003557 case Primitive::kPrimVoid:
3558 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003559 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003561 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003562}
3563
3564void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003565 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003566
3567 bool needs_write_barrier =
3568 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3569 bool needs_runtime_call = instruction->NeedsTypeCheck();
3570
Nicolas Geoffray39468442014-09-02 15:17:15 +01003571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003572 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3573 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003574 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003575 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3576 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3577 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003579 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003580 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003581 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3582 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003583 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003584 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003585 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3586 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003587 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003588 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003589 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003590
3591 if (needs_write_barrier) {
3592 // Temporary registers for the write barrier.
3593 locations->AddTemp(Location::RequiresRegister());
3594 locations->AddTemp(Location::RequiresRegister());
3595 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003597}
3598
3599void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3600 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003603 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003604 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003605 bool needs_runtime_call = locations->WillCall();
3606 bool needs_write_barrier =
3607 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608
3609 switch (value_type) {
3610 case Primitive::kPrimBoolean:
3611 case Primitive::kPrimByte: {
3612 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003613 if (index.IsConstant()) {
3614 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003615 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003617 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003618 __ movb(Address(obj, offset),
3619 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003620 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003622 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3624 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003625 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003626 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003627 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3628 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003630 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003631 break;
3632 }
3633
3634 case Primitive::kPrimShort:
3635 case Primitive::kPrimChar: {
3636 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003637 if (index.IsConstant()) {
3638 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003639 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003641 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003642 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003643 __ movw(Address(obj, offset),
3644 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003645 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003646 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003647 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003648 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3650 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003651 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003652 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003654 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3655 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003656 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003657 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003658 break;
3659 }
3660
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003661 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003662 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003663 if (!needs_runtime_call) {
3664 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3665 if (index.IsConstant()) {
3666 size_t offset =
3667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3668 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003669 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003670 } else {
3671 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003672 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3673 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003674 }
3675 } else {
3676 DCHECK(index.IsRegister()) << index;
3677 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3679 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003680 } else {
3681 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003682 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003684 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003685 }
3686 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003687 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003688 if (needs_write_barrier) {
3689 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003690 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3691 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003692 codegen_->MarkGCCard(
3693 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003694 }
3695 } else {
3696 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003697 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3698 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699 DCHECK(!codegen_->IsLeafMethod());
3700 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3701 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003702 break;
3703 }
3704
3705 case Primitive::kPrimLong: {
3706 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003707 if (index.IsConstant()) {
3708 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04003709 if (value.IsRegister()) {
3710 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
3711 } else {
3712 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3713 DCHECK(IsInt<32>(v));
3714 int32_t v_32 = v;
3715 __ movq(Address(obj, offset), Immediate(v_32));
3716 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003717 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003718 if (value.IsRegister()) {
3719 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3720 value.AsRegister<CpuRegister>());
3721 } else {
3722 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3723 DCHECK(IsInt<32>(v));
3724 int32_t v_32 = v;
3725 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3726 Immediate(v_32));
3727 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003728 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003729 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730 break;
3731 }
3732
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003733 case Primitive::kPrimFloat: {
3734 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3735 if (index.IsConstant()) {
3736 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3737 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003739 } else {
3740 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3742 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003743 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003744 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003745 break;
3746 }
3747
3748 case Primitive::kPrimDouble: {
3749 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3750 if (index.IsConstant()) {
3751 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3752 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003754 } else {
3755 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3757 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003758 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003759 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003760 break;
3761 }
3762
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 case Primitive::kPrimVoid:
3764 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003765 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003766 }
3767}
3768
3769void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003770 LocationSummary* locations =
3771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003772 locations->SetInAt(0, Location::RequiresRegister());
3773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003774}
3775
3776void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3777 LocationSummary* locations = instruction->GetLocations();
3778 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003779 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3780 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003781 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003782 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003783}
3784
3785void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003786 LocationSummary* locations =
3787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003788 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003789 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003790 if (instruction->HasUses()) {
3791 locations->SetOut(Location::SameAsFirstInput());
3792 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003793}
3794
3795void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3796 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003797 Location index_loc = locations->InAt(0);
3798 Location length_loc = locations->InAt(1);
3799 SlowPathCodeX86_64* slow_path =
3800 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003801
Mark Mendell99dbd682015-04-22 16:18:52 -04003802 if (length_loc.IsConstant()) {
3803 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3804 if (index_loc.IsConstant()) {
3805 // BCE will remove the bounds check if we are guarenteed to pass.
3806 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3807 if (index < 0 || index >= length) {
3808 codegen_->AddSlowPath(slow_path);
3809 __ jmp(slow_path->GetEntryLabel());
3810 } else {
3811 // Some optimization after BCE may have generated this, and we should not
3812 // generate a bounds check if it is a valid range.
3813 }
3814 return;
3815 }
3816
3817 // We have to reverse the jump condition because the length is the constant.
3818 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
3819 __ cmpl(index_reg, Immediate(length));
3820 codegen_->AddSlowPath(slow_path);
3821 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003822 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003823 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3824 if (index_loc.IsConstant()) {
3825 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3826 __ cmpl(length, Immediate(value));
3827 } else {
3828 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3829 }
3830 codegen_->AddSlowPath(slow_path);
3831 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003832 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003833}
3834
3835void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3836 CpuRegister card,
3837 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003838 CpuRegister value,
3839 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003840 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003841 if (value_can_be_null) {
3842 __ testl(value, value);
3843 __ j(kEqual, &is_null);
3844 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003845 __ gs()->movq(card, Address::Absolute(
3846 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3847 __ movq(temp, object);
3848 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3849 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003850 if (value_can_be_null) {
3851 __ Bind(&is_null);
3852 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853}
3854
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003855void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3856 temp->SetLocations(nullptr);
3857}
3858
3859void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3860 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003861 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862}
3863
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003864void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003865 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003866 LOG(FATAL) << "Unimplemented";
3867}
3868
3869void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003870 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3871}
3872
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003873void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3875}
3876
3877void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003878 HBasicBlock* block = instruction->GetBlock();
3879 if (block->GetLoopInformation() != nullptr) {
3880 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3881 // The back edge will generate the suspend check.
3882 return;
3883 }
3884 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3885 // The goto will generate the suspend check.
3886 return;
3887 }
3888 GenerateSuspendCheck(instruction, nullptr);
3889}
3890
3891void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3892 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003893 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003894 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
3895 if (slow_path == nullptr) {
3896 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
3897 instruction->SetSlowPath(slow_path);
3898 codegen_->AddSlowPath(slow_path);
3899 if (successor != nullptr) {
3900 DCHECK(successor->IsLoopHeader());
3901 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3902 }
3903 } else {
3904 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3905 }
3906
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003907 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003908 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003909 if (successor == nullptr) {
3910 __ j(kNotEqual, slow_path->GetEntryLabel());
3911 __ Bind(slow_path->GetReturnLabel());
3912 } else {
3913 __ j(kEqual, codegen_->GetLabelOf(successor));
3914 __ jmp(slow_path->GetEntryLabel());
3915 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003916}
3917
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003918X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3919 return codegen_->GetAssembler();
3920}
3921
3922void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3923 MoveOperands* move = moves_.Get(index);
3924 Location source = move->GetSource();
3925 Location destination = move->GetDestination();
3926
3927 if (source.IsRegister()) {
3928 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003929 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003930 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003931 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003933 } else {
3934 DCHECK(destination.IsDoubleStackSlot());
3935 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003936 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003937 }
3938 } else if (source.IsStackSlot()) {
3939 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003940 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003941 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003942 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003944 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003945 } else {
3946 DCHECK(destination.IsStackSlot());
3947 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3948 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3949 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003950 } else if (source.IsDoubleStackSlot()) {
3951 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003952 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003953 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003954 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003955 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3956 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003957 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003958 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003959 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3960 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3961 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003962 } else if (source.IsConstant()) {
3963 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003964 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3965 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003966 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003967 if (value == 0) {
3968 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3969 } else {
3970 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3971 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003972 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003973 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003974 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003975 }
3976 } else if (constant->IsLongConstant()) {
3977 int64_t value = constant->AsLongConstant()->GetValue();
3978 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003979 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003980 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003981 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003982 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003983 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3984 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003985 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003986 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003987 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003988 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003989 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3990 if (value == 0) {
3991 // easy FP 0.0.
3992 __ xorps(dest, dest);
3993 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003994 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003995 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003996 } else {
3997 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003998 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003999 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4000 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004001 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004002 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004003 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004004 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004005 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004006 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4007 if (value == 0) {
4008 __ xorpd(dest, dest);
4009 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004010 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004011 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004012 } else {
4013 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004014 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004015 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4016 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004017 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004018 } else if (source.IsFpuRegister()) {
4019 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004020 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004021 } else if (destination.IsStackSlot()) {
4022 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004023 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004024 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004025 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004026 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004027 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004028 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004029 }
4030}
4031
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004032void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004033 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004034 __ movl(Address(CpuRegister(RSP), mem), reg);
4035 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004036}
4037
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004038void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004039 ScratchRegisterScope ensure_scratch(
4040 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4041
4042 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4043 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4044 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4045 Address(CpuRegister(RSP), mem2 + stack_offset));
4046 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4047 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4048 CpuRegister(ensure_scratch.GetRegister()));
4049}
4050
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004051void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4052 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4053 __ movq(Address(CpuRegister(RSP), mem), reg);
4054 __ movq(reg, CpuRegister(TMP));
4055}
4056
4057void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4058 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004059 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004060
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004061 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4062 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4063 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4064 Address(CpuRegister(RSP), mem2 + stack_offset));
4065 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4066 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4067 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004068}
4069
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004070void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4071 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4072 __ movss(Address(CpuRegister(RSP), mem), reg);
4073 __ movd(reg, CpuRegister(TMP));
4074}
4075
4076void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4077 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4078 __ movsd(Address(CpuRegister(RSP), mem), reg);
4079 __ movd(reg, CpuRegister(TMP));
4080}
4081
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004082void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4083 MoveOperands* move = moves_.Get(index);
4084 Location source = move->GetSource();
4085 Location destination = move->GetDestination();
4086
4087 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004088 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004089 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004090 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004091 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004093 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004094 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4095 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004096 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004097 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004099 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4100 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004101 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4103 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4104 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004105 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004106 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004107 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004108 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004109 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004110 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004111 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004112 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004113 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004114 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004115 }
4116}
4117
4118
4119void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4120 __ pushq(CpuRegister(reg));
4121}
4122
4123
4124void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4125 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004126}
4127
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004128void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4129 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4130 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4131 Immediate(mirror::Class::kStatusInitialized));
4132 __ j(kLess, slow_path->GetEntryLabel());
4133 __ Bind(slow_path->GetExitLabel());
4134 // No need for memory fence, thanks to the X86_64 memory model.
4135}
4136
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004137void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004138 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4139 ? LocationSummary::kCallOnSlowPath
4140 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004142 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004143 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004144 locations->SetOut(Location::RequiresRegister());
4145}
4146
4147void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004148 LocationSummary* locations = cls->GetLocations();
4149 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4150 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004151 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004152 DCHECK(!cls->CanCallRuntime());
4153 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004154 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004155 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004156 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004157 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004158 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004159 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004160 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4161 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4162 codegen_->AddSlowPath(slow_path);
4163 __ testl(out, out);
4164 __ j(kEqual, slow_path->GetEntryLabel());
4165 if (cls->MustGenerateClinitCheck()) {
4166 GenerateClassInitializationCheck(slow_path, out);
4167 } else {
4168 __ Bind(slow_path->GetExitLabel());
4169 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004170 }
4171}
4172
4173void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4174 LocationSummary* locations =
4175 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4176 locations->SetInAt(0, Location::RequiresRegister());
4177 if (check->HasUses()) {
4178 locations->SetOut(Location::SameAsFirstInput());
4179 }
4180}
4181
4182void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004183 // We assume the class to not be null.
4184 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4185 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004186 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004187 GenerateClassInitializationCheck(slow_path,
4188 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004189}
4190
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004191void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4192 LocationSummary* locations =
4193 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004194 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004195 locations->SetOut(Location::RequiresRegister());
4196}
4197
4198void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4199 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4200 codegen_->AddSlowPath(slow_path);
4201
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004202 LocationSummary* locations = load->GetLocations();
4203 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4204 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004205 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004206 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004207 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4208 __ testl(out, out);
4209 __ j(kEqual, slow_path->GetEntryLabel());
4210 __ Bind(slow_path->GetExitLabel());
4211}
4212
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004213void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4214 LocationSummary* locations =
4215 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4216 locations->SetOut(Location::RequiresRegister());
4217}
4218
4219void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4220 Address address = Address::Absolute(
4221 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004222 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004223 __ gs()->movl(address, Immediate(0));
4224}
4225
4226void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4227 LocationSummary* locations =
4228 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4229 InvokeRuntimeCallingConvention calling_convention;
4230 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4231}
4232
4233void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4234 __ gs()->call(
4235 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4236 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4237}
4238
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004239void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004240 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4241 ? LocationSummary::kNoCall
4242 : LocationSummary::kCallOnSlowPath;
4243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4244 locations->SetInAt(0, Location::RequiresRegister());
4245 locations->SetInAt(1, Location::Any());
4246 locations->SetOut(Location::RequiresRegister());
4247}
4248
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004249void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004250 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004251 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004252 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004253 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004254 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4255 Label done, zero;
4256 SlowPathCodeX86_64* slow_path = nullptr;
4257
4258 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004259 // Avoid null check if we know obj is not null.
4260 if (instruction->MustDoNullCheck()) {
4261 __ testl(obj, obj);
4262 __ j(kEqual, &zero);
4263 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004264 // Compare the class of `obj` with `cls`.
4265 __ movl(out, Address(obj, class_offset));
4266 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004267 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004268 } else {
4269 DCHECK(cls.IsStackSlot()) << cls;
4270 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4271 }
4272 if (instruction->IsClassFinal()) {
4273 // Classes must be equal for the instanceof to succeed.
4274 __ j(kNotEqual, &zero);
4275 __ movl(out, Immediate(1));
4276 __ jmp(&done);
4277 } else {
4278 // If the classes are not equal, we go into a slow path.
4279 DCHECK(locations->OnlyCallsOnSlowPath());
4280 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004281 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004282 codegen_->AddSlowPath(slow_path);
4283 __ j(kNotEqual, slow_path->GetEntryLabel());
4284 __ movl(out, Immediate(1));
4285 __ jmp(&done);
4286 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004287
4288 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4289 __ Bind(&zero);
4290 __ movl(out, Immediate(0));
4291 }
4292
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004293 if (slow_path != nullptr) {
4294 __ Bind(slow_path->GetExitLabel());
4295 }
4296 __ Bind(&done);
4297}
4298
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004299void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4301 instruction, LocationSummary::kCallOnSlowPath);
4302 locations->SetInAt(0, Location::RequiresRegister());
4303 locations->SetInAt(1, Location::Any());
4304 locations->AddTemp(Location::RequiresRegister());
4305}
4306
4307void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4308 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004309 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004310 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004311 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004312 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4313 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4314 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4315 codegen_->AddSlowPath(slow_path);
4316
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004317 // Avoid null check if we know obj is not null.
4318 if (instruction->MustDoNullCheck()) {
4319 __ testl(obj, obj);
4320 __ j(kEqual, slow_path->GetExitLabel());
4321 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004322 // Compare the class of `obj` with `cls`.
4323 __ movl(temp, Address(obj, class_offset));
4324 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004325 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004326 } else {
4327 DCHECK(cls.IsStackSlot()) << cls;
4328 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4329 }
4330 // Classes must be equal for the checkcast to succeed.
4331 __ j(kNotEqual, slow_path->GetEntryLabel());
4332 __ Bind(slow_path->GetExitLabel());
4333}
4334
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004335void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4336 LocationSummary* locations =
4337 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4338 InvokeRuntimeCallingConvention calling_convention;
4339 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4340}
4341
4342void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4343 __ gs()->call(Address::Absolute(instruction->IsEnter()
4344 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4345 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4346 true));
4347 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4348}
4349
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004350void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4351void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4352void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4353
4354void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4355 LocationSummary* locations =
4356 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4357 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4358 || instruction->GetResultType() == Primitive::kPrimLong);
4359 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004360 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004361 locations->SetOut(Location::SameAsFirstInput());
4362}
4363
4364void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4365 HandleBitwiseOperation(instruction);
4366}
4367
4368void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4369 HandleBitwiseOperation(instruction);
4370}
4371
4372void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4373 HandleBitwiseOperation(instruction);
4374}
4375
4376void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4377 LocationSummary* locations = instruction->GetLocations();
4378 Location first = locations->InAt(0);
4379 Location second = locations->InAt(1);
4380 DCHECK(first.Equals(locations->Out()));
4381
4382 if (instruction->GetResultType() == Primitive::kPrimInt) {
4383 if (second.IsRegister()) {
4384 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004385 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004386 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004387 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004388 } else {
4389 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004390 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004391 }
4392 } else if (second.IsConstant()) {
4393 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4394 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004395 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004396 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004397 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004398 } else {
4399 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004400 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004401 }
4402 } else {
4403 Address address(CpuRegister(RSP), second.GetStackIndex());
4404 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004405 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004406 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004408 } else {
4409 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004411 }
4412 }
4413 } else {
4414 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004415 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4416 bool second_is_constant = false;
4417 int64_t value = 0;
4418 if (second.IsConstant()) {
4419 second_is_constant = true;
4420 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004421 }
Mark Mendell40741f32015-04-20 22:10:34 -04004422 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004423
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004424 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004425 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004426 if (is_int32_value) {
4427 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4428 } else {
4429 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4430 }
4431 } else if (second.IsDoubleStackSlot()) {
4432 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004433 } else {
4434 __ andq(first_reg, second.AsRegister<CpuRegister>());
4435 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004436 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004437 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004438 if (is_int32_value) {
4439 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4440 } else {
4441 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4442 }
4443 } else if (second.IsDoubleStackSlot()) {
4444 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004445 } else {
4446 __ orq(first_reg, second.AsRegister<CpuRegister>());
4447 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004448 } else {
4449 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004450 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004451 if (is_int32_value) {
4452 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4453 } else {
4454 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4455 }
4456 } else if (second.IsDoubleStackSlot()) {
4457 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004458 } else {
4459 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4460 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004461 }
4462 }
4463}
4464
Calin Juravleb1498f62015-02-16 13:13:29 +00004465void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4466 // Nothing to do, this should be removed during prepare for register allocator.
4467 UNUSED(instruction);
4468 LOG(FATAL) << "Unreachable";
4469}
4470
4471void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4472 // Nothing to do, this should be removed during prepare for register allocator.
4473 UNUSED(instruction);
4474 LOG(FATAL) << "Unreachable";
4475}
4476
Mark Mendell92e83bf2015-05-07 11:25:03 -04004477void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4478 if (value == 0) {
4479 __ xorl(dest, dest);
4480 } else if (value > 0 && IsInt<32>(value)) {
4481 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4482 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4483 } else {
4484 __ movq(dest, Immediate(value));
4485 }
4486}
4487
Mark Mendellf55c3e02015-03-26 21:07:46 -04004488void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4489 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004490 X86_64Assembler* assembler = GetAssembler();
4491 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004492 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4493 // byte values. If used for vectors at a later time, this will need to be
4494 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004495 assembler->Align(4, 0);
4496 constant_area_start_ = assembler->CodeSize();
4497 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004498 }
4499
4500 // And finish up.
4501 CodeGenerator::Finalize(allocator);
4502}
4503
4504/**
4505 * Class to handle late fixup of offsets into constant area.
4506 */
4507class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4508 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004509 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004510 : codegen_(codegen), offset_into_constant_area_(offset) {}
4511
4512 private:
4513 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4514 // Patch the correct offset for the instruction. We use the address of the
4515 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4516 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4517 int relative_position = constant_offset - pos;
4518
4519 // Patch in the right value.
4520 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4521 }
4522
Mark Mendell39dcf552015-04-09 20:42:42 -04004523 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004524
4525 // Location in constant area that the fixup refers to.
4526 int offset_into_constant_area_;
4527};
4528
4529Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4530 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4531 return Address::RIP(fixup);
4532}
4533
4534Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4535 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4536 return Address::RIP(fixup);
4537}
4538
4539Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4540 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4541 return Address::RIP(fixup);
4542}
4543
4544Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4545 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4546 return Address::RIP(fixup);
4547}
4548
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004549} // namespace x86_64
4550} // namespace art