blob: bfc827de1cd0295bfe7fe1326197c01415c585d1 [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
Roland Levillain62a46b22015-06-01 18:24:13 +010049#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050
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 __
Roland Levillain62a46b22015-06-01 18:24:13 +0100346#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100347
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,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100363 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800364 // 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()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100374 CpuRegister reg = temp.AsRegister<CpuRegister>();
Jeff Hao848f70a2014-01-15 13:49:50 -0800375 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100376 __ gs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000377 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100378 __ call(Address(reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000379 kX86_64WordSize).SizeValue()));
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100380 } else if (invoke->IsRecursive()) {
381 __ call(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000382 } else {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100383 CpuRegister reg = temp.AsRegister<CpuRegister>();
Nicolas Geoffrayae71a052015-06-09 14:12:28 +0100384 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
385 Register method_reg;
386 if (current_method.IsRegister()) {
387 method_reg = current_method.AsRegister<Register>();
388 } else {
389 DCHECK(invoke->GetLocations()->Intrinsified());
390 DCHECK(!current_method.IsValid());
391 method_reg = reg.AsRegister();
392 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
393 }
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100394 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +0100395 __ movl(reg, Address(CpuRegister(method_reg),
396 ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100397 // temp = temp[index_in_cache]
398 __ movq(reg, Address(
399 reg, CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
400 // (temp + offset_of_quick_compiled_code)()
401 __ call(Address(reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
402 kX86_64WordSize).SizeValue()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000403 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800404
405 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800406}
407
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100408void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100409 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100410}
411
412void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100413 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100414}
415
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100416size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
417 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
418 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100419}
420
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100421size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
422 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
423 return kX86_64WordSize;
424}
425
426size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
427 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
428 return kX86_64WordSize;
429}
430
431size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
432 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
433 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100434}
435
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000436static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000437// Use a fake return address register to mimic Quick.
438static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400439CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
440 const X86_64InstructionSetFeatures& isa_features,
441 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000442 : CodeGenerator(graph,
443 kNumberOfCpuRegisters,
444 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000445 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000446 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
447 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000448 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000449 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
450 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000451 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100452 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000454 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400455 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400456 isa_features_(isa_features),
457 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000458 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
459}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100460
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100461InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
462 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100463 : HGraphVisitor(graph),
464 assembler_(codegen->GetAssembler()),
465 codegen_(codegen) {}
466
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100468 switch (type) {
469 case Primitive::kPrimLong:
470 case Primitive::kPrimByte:
471 case Primitive::kPrimBoolean:
472 case Primitive::kPrimChar:
473 case Primitive::kPrimShort:
474 case Primitive::kPrimInt:
475 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100476 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100477 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478 }
479
480 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100481 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100482 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100483 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100485
486 case Primitive::kPrimVoid:
487 LOG(FATAL) << "Unreachable type " << type;
488 }
489
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100490 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100491}
492
Nicolas Geoffray98893962015-01-21 12:32:32 +0000493void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100494 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100495 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100496
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000497 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100498 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000499
Nicolas Geoffray98893962015-01-21 12:32:32 +0000500 if (is_baseline) {
501 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
502 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
503 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000504 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
505 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
506 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000507 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100508}
509
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100510static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100511 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100512}
David Srbecky9d8606d2015-04-12 09:35:32 +0100513
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100514static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100515 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100516}
517
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100518void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100519 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000520 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100521 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700522 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000523 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100524
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000525 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100526 __ testq(CpuRegister(RAX), Address(
527 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100528 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100529 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000530
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000531 if (HasEmptyFrame()) {
532 return;
533 }
534
Nicolas Geoffray98893962015-01-21 12:32:32 +0000535 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000536 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000537 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000538 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100539 __ cfi().AdjustCFAOffset(kX86_64WordSize);
540 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000541 }
542 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100543
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100544 int adjust = GetFrameSize() - GetCoreSpillSize();
545 __ subq(CpuRegister(RSP), Immediate(adjust));
546 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000547 uint32_t xmm_spill_location = GetFpuSpillStart();
548 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100549
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000550 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
551 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100552 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
553 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
554 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000555 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100556 }
557
Mathieu Chartiere401d142015-04-22 13:56:20 -0700558 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100559 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560}
561
562void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100563 __ cfi().RememberState();
564 if (!HasEmptyFrame()) {
565 uint32_t xmm_spill_location = GetFpuSpillStart();
566 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
567 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
568 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
569 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
570 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
571 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
572 }
573 }
574
575 int adjust = GetFrameSize() - GetCoreSpillSize();
576 __ addq(CpuRegister(RSP), Immediate(adjust));
577 __ cfi().AdjustCFAOffset(-adjust);
578
579 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
580 Register reg = kCoreCalleeSaves[i];
581 if (allocated_registers_.ContainsCoreRegister(reg)) {
582 __ popq(CpuRegister(reg));
583 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
584 __ cfi().Restore(DWARFReg(reg));
585 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000586 }
587 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100588 __ ret();
589 __ cfi().RestoreState();
590 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591}
592
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100593void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
594 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100595}
596
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100597Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
598 switch (load->GetType()) {
599 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100600 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100602
603 case Primitive::kPrimInt:
604 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100606 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100607
608 case Primitive::kPrimBoolean:
609 case Primitive::kPrimByte:
610 case Primitive::kPrimChar:
611 case Primitive::kPrimShort:
612 case Primitive::kPrimVoid:
613 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700614 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615 }
616
617 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700618 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100619}
620
621void CodeGeneratorX86_64::Move(Location destination, Location source) {
622 if (source.Equals(destination)) {
623 return;
624 }
625 if (destination.IsRegister()) {
626 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000629 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100630 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100633 } else {
634 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000635 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 Address(CpuRegister(RSP), source.GetStackIndex()));
637 }
638 } else if (destination.IsFpuRegister()) {
639 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000642 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100643 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000644 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100645 Address(CpuRegister(RSP), source.GetStackIndex()));
646 } else {
647 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000648 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100649 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100650 }
651 } else if (destination.IsStackSlot()) {
652 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100653 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000654 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 } else if (source.IsFpuRegister()) {
656 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000657 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500658 } else if (source.IsConstant()) {
659 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000660 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500661 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100662 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500663 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000664 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
665 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100666 }
667 } else {
668 DCHECK(destination.IsDoubleStackSlot());
669 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000671 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100672 } else if (source.IsFpuRegister()) {
673 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000674 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500675 } else if (source.IsConstant()) {
676 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800677 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500678 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000679 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500680 } else {
681 DCHECK(constant->IsLongConstant());
682 value = constant->AsLongConstant()->GetValue();
683 }
Mark Mendell92e83bf2015-05-07 11:25:03 -0400684 Load64BitValue(CpuRegister(TMP), value);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500685 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 } else {
687 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000688 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
689 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100690 }
691 }
692}
693
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100694void CodeGeneratorX86_64::Move(HInstruction* instruction,
695 Location location,
696 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000697 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100698 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700699 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100700 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000701 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100702 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000703 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000704 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
705 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000706 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000707 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000708 } else if (location.IsStackSlot()) {
709 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
710 } else {
711 DCHECK(location.IsConstant());
712 DCHECK_EQ(location.GetConstant(), const_to_move);
713 }
714 } else if (const_to_move->IsLongConstant()) {
715 int64_t value = const_to_move->AsLongConstant()->GetValue();
716 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400717 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000718 } else if (location.IsDoubleStackSlot()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400719 Load64BitValue(CpuRegister(TMP), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000720 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
721 } else {
722 DCHECK(location.IsConstant());
723 DCHECK_EQ(location.GetConstant(), const_to_move);
724 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 }
Roland Levillain476df552014-10-09 17:51:36 +0100726 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100727 switch (instruction->GetType()) {
728 case Primitive::kPrimBoolean:
729 case Primitive::kPrimByte:
730 case Primitive::kPrimChar:
731 case Primitive::kPrimShort:
732 case Primitive::kPrimInt:
733 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100735 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
736 break;
737
738 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100739 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000740 Move(location,
741 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742 break;
743
744 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100745 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000747 } else if (instruction->IsTemporary()) {
748 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
749 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100750 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100751 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752 switch (instruction->GetType()) {
753 case Primitive::kPrimBoolean:
754 case Primitive::kPrimByte:
755 case Primitive::kPrimChar:
756 case Primitive::kPrimShort:
757 case Primitive::kPrimInt:
758 case Primitive::kPrimNot:
759 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100760 case Primitive::kPrimFloat:
761 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000762 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100763 break;
764
765 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100766 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100767 }
768 }
769}
770
771void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
772 got->SetLocations(nullptr);
773}
774
775void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
776 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100777 DCHECK(!successor->IsExitBlock());
778
779 HBasicBlock* block = got->GetBlock();
780 HInstruction* previous = got->GetPrevious();
781
782 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000783 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100784 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
785 return;
786 }
787
788 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
789 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
790 }
791 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100792 __ jmp(codegen_->GetLabelOf(successor));
793 }
794}
795
796void LocationsBuilderX86_64::VisitExit(HExit* exit) {
797 exit->SetLocations(nullptr);
798}
799
800void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700801 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100802}
803
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700804void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
805 Label* true_target,
806 Label* false_target,
807 Label* always_true_target) {
808 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100809 if (cond->IsIntConstant()) {
810 // Constant condition, statically compared against 1.
811 int32_t cond_value = cond->AsIntConstant()->GetValue();
812 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700813 if (always_true_target != nullptr) {
814 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100815 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100817 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100818 DCHECK_EQ(cond_value, 0);
819 }
820 } else {
821 bool materialized =
822 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
823 // Moves do not affect the eflags register, so if the condition is
824 // evaluated just before the if, we don't need to evaluate it
825 // again.
826 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700827 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100828 if (materialized) {
829 if (!eflags_set) {
830 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700831 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000833 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100834 } else {
835 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
836 Immediate(0));
837 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700838 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700840 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 }
842 } else {
843 Location lhs = cond->GetLocations()->InAt(0);
844 Location rhs = cond->GetLocations()->InAt(1);
845 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000846 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000848 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000849 if (constant == 0) {
850 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
851 } else {
852 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
853 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100854 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000855 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100856 Address(CpuRegister(RSP), rhs.GetStackIndex()));
857 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700858 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700859 }
Dave Allison20dfc792014-06-16 20:44:29 -0700860 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700861 if (false_target != nullptr) {
862 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100863 }
864}
865
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700866void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
867 LocationSummary* locations =
868 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
869 HInstruction* cond = if_instr->InputAt(0);
870 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
871 locations->SetInAt(0, Location::Any());
872 }
873}
874
875void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
876 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
877 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
878 Label* always_true_target = true_target;
879 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
880 if_instr->IfTrueSuccessor())) {
881 always_true_target = nullptr;
882 }
883 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
884 if_instr->IfFalseSuccessor())) {
885 false_target = nullptr;
886 }
887 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
888}
889
890void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
891 LocationSummary* locations = new (GetGraph()->GetArena())
892 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
893 HInstruction* cond = deoptimize->InputAt(0);
894 DCHECK(cond->IsCondition());
895 if (cond->AsCondition()->NeedsMaterialization()) {
896 locations->SetInAt(0, Location::Any());
897 }
898}
899
900void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
901 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
902 DeoptimizationSlowPathX86_64(deoptimize);
903 codegen_->AddSlowPath(slow_path);
904 Label* slow_path_entry = slow_path->GetEntryLabel();
905 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
906}
907
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
909 local->SetLocations(nullptr);
910}
911
912void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
913 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
914}
915
916void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
917 local->SetLocations(nullptr);
918}
919
920void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
921 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700922 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923}
924
925void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100926 LocationSummary* locations =
927 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100928 switch (store->InputAt(1)->GetType()) {
929 case Primitive::kPrimBoolean:
930 case Primitive::kPrimByte:
931 case Primitive::kPrimChar:
932 case Primitive::kPrimShort:
933 case Primitive::kPrimInt:
934 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100935 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100936 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
937 break;
938
939 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100940 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100941 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
942 break;
943
944 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100946 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947}
948
949void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700950 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100951}
952
Roland Levillain0d37cd02015-05-27 16:39:19 +0100953void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100954 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +0100955 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100956 locations->SetInAt(0, Location::RequiresRegister());
957 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +0100958 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100959 locations->SetOut(Location::RequiresRegister());
960 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100961}
962
Roland Levillain0d37cd02015-05-27 16:39:19 +0100963void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
964 if (cond->NeedsMaterialization()) {
965 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000966 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100967 // Clear register: setcc only sets the low byte.
Mark Mendell92e83bf2015-05-07 11:25:03 -0400968 __ xorl(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000969 Location lhs = locations->InAt(0);
970 Location rhs = locations->InAt(1);
971 if (rhs.IsRegister()) {
972 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
973 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800974 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000975 if (constant == 0) {
976 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
977 } else {
978 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
979 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100980 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000981 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100982 }
Roland Levillain0d37cd02015-05-27 16:39:19 +0100983 __ setcc(X86_64Condition(cond->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700984 }
985}
986
987void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
988 VisitCondition(comp);
989}
990
991void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
992 VisitCondition(comp);
993}
994
995void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
996 VisitCondition(comp);
997}
998
999void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1000 VisitCondition(comp);
1001}
1002
1003void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1004 VisitCondition(comp);
1005}
1006
1007void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1008 VisitCondition(comp);
1009}
1010
1011void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1012 VisitCondition(comp);
1013}
1014
1015void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1016 VisitCondition(comp);
1017}
1018
1019void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1020 VisitCondition(comp);
1021}
1022
1023void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1024 VisitCondition(comp);
1025}
1026
1027void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1028 VisitCondition(comp);
1029}
1030
1031void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1032 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033}
1034
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001035void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001036 LocationSummary* locations =
1037 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001038 switch (compare->InputAt(0)->GetType()) {
1039 case Primitive::kPrimLong: {
1040 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001041 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001042 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1043 break;
1044 }
1045 case Primitive::kPrimFloat:
1046 case Primitive::kPrimDouble: {
1047 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001048 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001049 locations->SetOut(Location::RequiresRegister());
1050 break;
1051 }
1052 default:
1053 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1054 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001055}
1056
1057void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001058 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001059 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001060 Location left = locations->InAt(0);
1061 Location right = locations->InAt(1);
1062
1063 Label less, greater, done;
1064 Primitive::Type type = compare->InputAt(0)->GetType();
1065 switch (type) {
1066 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001067 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1068 if (right.IsConstant()) {
1069 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001070 if (IsInt<32>(value)) {
1071 if (value == 0) {
1072 __ testq(left_reg, left_reg);
1073 } else {
1074 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1075 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001076 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001077 // Value won't fit in an int.
1078 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001079 }
Mark Mendell40741f32015-04-20 22:10:34 -04001080 } else if (right.IsDoubleStackSlot()) {
1081 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001082 } else {
1083 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1084 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001085 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001086 }
1087 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001088 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1089 if (right.IsConstant()) {
1090 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1091 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1092 } else if (right.IsStackSlot()) {
1093 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1094 } else {
1095 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1096 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001097 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1098 break;
1099 }
1100 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001101 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1102 if (right.IsConstant()) {
1103 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1104 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1105 } else if (right.IsDoubleStackSlot()) {
1106 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1107 } else {
1108 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1109 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001110 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1111 break;
1112 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001113 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001114 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001115 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001116 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001117 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001118 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001119
Calin Juravle91debbc2014-11-26 19:01:09 +00001120 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001121 __ movl(out, Immediate(1));
1122 __ jmp(&done);
1123
1124 __ Bind(&less);
1125 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001126
1127 __ Bind(&done);
1128}
1129
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001131 LocationSummary* locations =
1132 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001133 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001134}
1135
1136void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001137 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139}
1140
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001141void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1144 locations->SetOut(Location::ConstantLocation(constant));
1145}
1146
1147void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1148 // Will be generated at use site.
1149 UNUSED(constant);
1150}
1151
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001153 LocationSummary* locations =
1154 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001155 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001156}
1157
1158void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001159 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001160 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001161}
1162
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001163void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1164 LocationSummary* locations =
1165 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1166 locations->SetOut(Location::ConstantLocation(constant));
1167}
1168
1169void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1170 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001171 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001172}
1173
1174void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1175 LocationSummary* locations =
1176 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1177 locations->SetOut(Location::ConstantLocation(constant));
1178}
1179
1180void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1181 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001182 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001183}
1184
Calin Juravle27df7582015-04-17 19:12:31 +01001185void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1186 memory_barrier->SetLocations(nullptr);
1187}
1188
1189void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1190 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1191}
1192
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001193void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1194 ret->SetLocations(nullptr);
1195}
1196
1197void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001198 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001199 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001200}
1201
1202void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001203 LocationSummary* locations =
1204 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001205 switch (ret->InputAt(0)->GetType()) {
1206 case Primitive::kPrimBoolean:
1207 case Primitive::kPrimByte:
1208 case Primitive::kPrimChar:
1209 case Primitive::kPrimShort:
1210 case Primitive::kPrimInt:
1211 case Primitive::kPrimNot:
1212 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001213 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001214 break;
1215
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001218 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001219 break;
1220
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001223 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224}
1225
1226void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1227 if (kIsDebugBuild) {
1228 switch (ret->InputAt(0)->GetType()) {
1229 case Primitive::kPrimBoolean:
1230 case Primitive::kPrimByte:
1231 case Primitive::kPrimChar:
1232 case Primitive::kPrimShort:
1233 case Primitive::kPrimInt:
1234 case Primitive::kPrimNot:
1235 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001237 break;
1238
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 case Primitive::kPrimFloat:
1240 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 XMM0);
1243 break;
1244
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001245 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247 }
1248 }
1249 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250}
1251
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001252Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1253 switch (type) {
1254 case Primitive::kPrimBoolean:
1255 case Primitive::kPrimByte:
1256 case Primitive::kPrimChar:
1257 case Primitive::kPrimShort:
1258 case Primitive::kPrimInt:
1259 case Primitive::kPrimNot:
1260 case Primitive::kPrimLong:
1261 return Location::RegisterLocation(RAX);
1262
1263 case Primitive::kPrimVoid:
1264 return Location::NoLocation();
1265
1266 case Primitive::kPrimDouble:
1267 case Primitive::kPrimFloat:
1268 return Location::FpuRegisterLocation(XMM0);
1269 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001270
1271 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001272}
1273
1274Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1275 return Location::RegisterLocation(kMethodRegisterArgument);
1276}
1277
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001278Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001279 switch (type) {
1280 case Primitive::kPrimBoolean:
1281 case Primitive::kPrimByte:
1282 case Primitive::kPrimChar:
1283 case Primitive::kPrimShort:
1284 case Primitive::kPrimInt:
1285 case Primitive::kPrimNot: {
1286 uint32_t index = gp_index_++;
1287 stack_index_++;
1288 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001289 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001290 } else {
1291 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1292 }
1293 }
1294
1295 case Primitive::kPrimLong: {
1296 uint32_t index = gp_index_;
1297 stack_index_ += 2;
1298 if (index < calling_convention.GetNumberOfRegisters()) {
1299 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001300 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001301 } else {
1302 gp_index_ += 2;
1303 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1304 }
1305 }
1306
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001307 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001308 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001309 stack_index_++;
1310 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001311 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001312 } else {
1313 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1314 }
1315 }
1316
1317 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001318 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001319 stack_index_ += 2;
1320 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001321 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001322 } else {
1323 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1324 }
1325 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001326
1327 case Primitive::kPrimVoid:
1328 LOG(FATAL) << "Unexpected parameter type " << type;
1329 break;
1330 }
1331 return Location();
1332}
1333
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001334void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001335 // When we do not run baseline, explicit clinit checks triggered by static
1336 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1337 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001338
Mark Mendellfb8d2792015-03-31 22:16:59 -04001339 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001340 if (intrinsic.TryDispatch(invoke)) {
1341 return;
1342 }
1343
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001344 HandleInvoke(invoke);
1345}
1346
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001347static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1348 if (invoke->GetLocations()->Intrinsified()) {
1349 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1350 intrinsic.Dispatch(invoke);
1351 return true;
1352 }
1353 return false;
1354}
1355
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001356void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001357 // When we do not run baseline, explicit clinit checks triggered by static
1358 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1359 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001360
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001361 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1362 return;
1363 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001364
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001365 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001366 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001367 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001368 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001369}
1370
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001371void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001372 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001373 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001374}
1375
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001376void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001377 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001378 if (intrinsic.TryDispatch(invoke)) {
1379 return;
1380 }
1381
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001382 HandleInvoke(invoke);
1383}
1384
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001385void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001386 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1387 return;
1388 }
1389
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001391 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1392 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001393 LocationSummary* locations = invoke->GetLocations();
1394 Location receiver = locations->InAt(0);
1395 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1396 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001397 DCHECK(receiver.IsRegister());
1398 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001399 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001400 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001401 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001402 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001403 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001404 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001405
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001406 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001407 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001408}
1409
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001410void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1411 HandleInvoke(invoke);
1412 // Add the hidden argument.
1413 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1414}
1415
1416void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1417 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001418 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001419 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1420 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001421 LocationSummary* locations = invoke->GetLocations();
1422 Location receiver = locations->InAt(0);
1423 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1424
1425 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001426 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1427 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001428
1429 // temp = object->GetClass();
1430 if (receiver.IsStackSlot()) {
1431 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1432 __ movl(temp, Address(temp, class_offset));
1433 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001434 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001435 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001436 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001437 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001438 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001439 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001440 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001441 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001442
1443 DCHECK(!codegen_->IsLeafMethod());
1444 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1445}
1446
Roland Levillain88cb1752014-10-20 16:36:47 +01001447void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1448 LocationSummary* locations =
1449 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1450 switch (neg->GetResultType()) {
1451 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001452 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001453 locations->SetInAt(0, Location::RequiresRegister());
1454 locations->SetOut(Location::SameAsFirstInput());
1455 break;
1456
Roland Levillain88cb1752014-10-20 16:36:47 +01001457 case Primitive::kPrimFloat:
1458 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001459 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001460 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001461 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001462 break;
1463
1464 default:
1465 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1466 }
1467}
1468
1469void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1470 LocationSummary* locations = neg->GetLocations();
1471 Location out = locations->Out();
1472 Location in = locations->InAt(0);
1473 switch (neg->GetResultType()) {
1474 case Primitive::kPrimInt:
1475 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001476 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001477 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001478 break;
1479
1480 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001481 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001482 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001483 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001484 break;
1485
Roland Levillain5368c212014-11-27 15:03:41 +00001486 case Primitive::kPrimFloat: {
1487 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001488 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001489 // Implement float negation with an exclusive or with value
1490 // 0x80000000 (mask for bit 31, representing the sign of a
1491 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001492 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001493 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001494 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001495 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001496
Roland Levillain5368c212014-11-27 15:03:41 +00001497 case Primitive::kPrimDouble: {
1498 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001499 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001500 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001501 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001502 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001503 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001504 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001505 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001506 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001507
1508 default:
1509 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1510 }
1511}
1512
Roland Levillaindff1f282014-11-05 14:15:05 +00001513void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1514 LocationSummary* locations =
1515 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1516 Primitive::Type result_type = conversion->GetResultType();
1517 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001518 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001519
David Brazdilb2bd1c52015-03-25 11:17:37 +00001520 // The Java language does not allow treating boolean as an integral type but
1521 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001522
Roland Levillaindff1f282014-11-05 14:15:05 +00001523 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001524 case Primitive::kPrimByte:
1525 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001526 case Primitive::kPrimBoolean:
1527 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001528 case Primitive::kPrimShort:
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001531 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001532 locations->SetInAt(0, Location::Any());
1533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1534 break;
1535
1536 default:
1537 LOG(FATAL) << "Unexpected type conversion from " << input_type
1538 << " to " << result_type;
1539 }
1540 break;
1541
Roland Levillain01a8d712014-11-14 16:27:39 +00001542 case Primitive::kPrimShort:
1543 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001544 case Primitive::kPrimBoolean:
1545 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001546 case Primitive::kPrimByte:
1547 case Primitive::kPrimInt:
1548 case Primitive::kPrimChar:
1549 // Processing a Dex `int-to-short' instruction.
1550 locations->SetInAt(0, Location::Any());
1551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1552 break;
1553
1554 default:
1555 LOG(FATAL) << "Unexpected type conversion from " << input_type
1556 << " to " << result_type;
1557 }
1558 break;
1559
Roland Levillain946e1432014-11-11 17:35:19 +00001560 case Primitive::kPrimInt:
1561 switch (input_type) {
1562 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001563 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001564 locations->SetInAt(0, Location::Any());
1565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1566 break;
1567
1568 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001569 // Processing a Dex `float-to-int' instruction.
1570 locations->SetInAt(0, Location::RequiresFpuRegister());
1571 locations->SetOut(Location::RequiresRegister());
1572 locations->AddTemp(Location::RequiresFpuRegister());
1573 break;
1574
Roland Levillain946e1432014-11-11 17:35:19 +00001575 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001576 // Processing a Dex `double-to-int' instruction.
1577 locations->SetInAt(0, Location::RequiresFpuRegister());
1578 locations->SetOut(Location::RequiresRegister());
1579 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001580 break;
1581
1582 default:
1583 LOG(FATAL) << "Unexpected type conversion from " << input_type
1584 << " to " << result_type;
1585 }
1586 break;
1587
Roland Levillaindff1f282014-11-05 14:15:05 +00001588 case Primitive::kPrimLong:
1589 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001590 case Primitive::kPrimBoolean:
1591 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001592 case Primitive::kPrimByte:
1593 case Primitive::kPrimShort:
1594 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001595 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001596 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001597 // TODO: We would benefit from a (to-be-implemented)
1598 // Location::RegisterOrStackSlot requirement for this input.
1599 locations->SetInAt(0, Location::RequiresRegister());
1600 locations->SetOut(Location::RequiresRegister());
1601 break;
1602
1603 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001604 // Processing a Dex `float-to-long' instruction.
1605 locations->SetInAt(0, Location::RequiresFpuRegister());
1606 locations->SetOut(Location::RequiresRegister());
1607 locations->AddTemp(Location::RequiresFpuRegister());
1608 break;
1609
Roland Levillaindff1f282014-11-05 14:15:05 +00001610 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001611 // Processing a Dex `double-to-long' instruction.
1612 locations->SetInAt(0, Location::RequiresFpuRegister());
1613 locations->SetOut(Location::RequiresRegister());
1614 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001615 break;
1616
1617 default:
1618 LOG(FATAL) << "Unexpected type conversion from " << input_type
1619 << " to " << result_type;
1620 }
1621 break;
1622
Roland Levillain981e4542014-11-14 11:47:14 +00001623 case Primitive::kPrimChar:
1624 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001625 case Primitive::kPrimBoolean:
1626 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001627 case Primitive::kPrimByte:
1628 case Primitive::kPrimShort:
1629 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001630 // Processing a Dex `int-to-char' instruction.
1631 locations->SetInAt(0, Location::Any());
1632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1633 break;
1634
1635 default:
1636 LOG(FATAL) << "Unexpected type conversion from " << input_type
1637 << " to " << result_type;
1638 }
1639 break;
1640
Roland Levillaindff1f282014-11-05 14:15:05 +00001641 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001642 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001643 case Primitive::kPrimBoolean:
1644 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001645 case Primitive::kPrimByte:
1646 case Primitive::kPrimShort:
1647 case Primitive::kPrimInt:
1648 case Primitive::kPrimChar:
1649 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001650 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001651 locations->SetOut(Location::RequiresFpuRegister());
1652 break;
1653
1654 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001655 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001656 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001657 locations->SetOut(Location::RequiresFpuRegister());
1658 break;
1659
Roland Levillaincff13742014-11-17 14:32:17 +00001660 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001661 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001662 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001663 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001664 break;
1665
1666 default:
1667 LOG(FATAL) << "Unexpected type conversion from " << input_type
1668 << " to " << result_type;
1669 };
1670 break;
1671
Roland Levillaindff1f282014-11-05 14:15:05 +00001672 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001673 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001674 case Primitive::kPrimBoolean:
1675 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001676 case Primitive::kPrimByte:
1677 case Primitive::kPrimShort:
1678 case Primitive::kPrimInt:
1679 case Primitive::kPrimChar:
1680 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001681 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001682 locations->SetOut(Location::RequiresFpuRegister());
1683 break;
1684
1685 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001686 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001687 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001688 locations->SetOut(Location::RequiresFpuRegister());
1689 break;
1690
Roland Levillaincff13742014-11-17 14:32:17 +00001691 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001692 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001693 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001694 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001695 break;
1696
1697 default:
1698 LOG(FATAL) << "Unexpected type conversion from " << input_type
1699 << " to " << result_type;
1700 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001701 break;
1702
1703 default:
1704 LOG(FATAL) << "Unexpected type conversion from " << input_type
1705 << " to " << result_type;
1706 }
1707}
1708
1709void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1710 LocationSummary* locations = conversion->GetLocations();
1711 Location out = locations->Out();
1712 Location in = locations->InAt(0);
1713 Primitive::Type result_type = conversion->GetResultType();
1714 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001715 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001716 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001717 case Primitive::kPrimByte:
1718 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001719 case Primitive::kPrimBoolean:
1720 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001721 case Primitive::kPrimShort:
1722 case Primitive::kPrimInt:
1723 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001724 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001725 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001726 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001727 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001729 Address(CpuRegister(RSP), in.GetStackIndex()));
1730 } else {
1731 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001732 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001733 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1734 }
1735 break;
1736
1737 default:
1738 LOG(FATAL) << "Unexpected type conversion from " << input_type
1739 << " to " << result_type;
1740 }
1741 break;
1742
Roland Levillain01a8d712014-11-14 16:27:39 +00001743 case Primitive::kPrimShort:
1744 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001745 case Primitive::kPrimBoolean:
1746 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001747 case Primitive::kPrimByte:
1748 case Primitive::kPrimInt:
1749 case Primitive::kPrimChar:
1750 // Processing a Dex `int-to-short' instruction.
1751 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001752 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001753 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001754 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001755 Address(CpuRegister(RSP), in.GetStackIndex()));
1756 } else {
1757 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001759 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1760 }
1761 break;
1762
1763 default:
1764 LOG(FATAL) << "Unexpected type conversion from " << input_type
1765 << " to " << result_type;
1766 }
1767 break;
1768
Roland Levillain946e1432014-11-11 17:35:19 +00001769 case Primitive::kPrimInt:
1770 switch (input_type) {
1771 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001772 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001773 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001774 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001775 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001777 Address(CpuRegister(RSP), in.GetStackIndex()));
1778 } else {
1779 DCHECK(in.IsConstant());
1780 DCHECK(in.GetConstant()->IsLongConstant());
1781 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001782 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001783 }
1784 break;
1785
Roland Levillain3f8f9362014-12-02 17:45:01 +00001786 case Primitive::kPrimFloat: {
1787 // Processing a Dex `float-to-int' instruction.
1788 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1789 CpuRegister output = out.AsRegister<CpuRegister>();
1790 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1791 Label done, nan;
1792
1793 __ movl(output, Immediate(kPrimIntMax));
1794 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001795 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001796 // if input >= temp goto done
1797 __ comiss(input, temp);
1798 __ j(kAboveEqual, &done);
1799 // if input == NaN goto nan
1800 __ j(kUnordered, &nan);
1801 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001802 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001803 __ jmp(&done);
1804 __ Bind(&nan);
1805 // output = 0
1806 __ xorl(output, output);
1807 __ Bind(&done);
1808 break;
1809 }
1810
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001811 case Primitive::kPrimDouble: {
1812 // Processing a Dex `double-to-int' instruction.
1813 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1814 CpuRegister output = out.AsRegister<CpuRegister>();
1815 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1816 Label done, nan;
1817
1818 __ movl(output, Immediate(kPrimIntMax));
1819 // temp = int-to-double(output)
1820 __ cvtsi2sd(temp, output);
1821 // if input >= temp goto done
1822 __ comisd(input, temp);
1823 __ j(kAboveEqual, &done);
1824 // if input == NaN goto nan
1825 __ j(kUnordered, &nan);
1826 // output = double-to-int-truncate(input)
1827 __ cvttsd2si(output, input);
1828 __ jmp(&done);
1829 __ Bind(&nan);
1830 // output = 0
1831 __ xorl(output, output);
1832 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001833 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001834 }
Roland Levillain946e1432014-11-11 17:35:19 +00001835
1836 default:
1837 LOG(FATAL) << "Unexpected type conversion from " << input_type
1838 << " to " << result_type;
1839 }
1840 break;
1841
Roland Levillaindff1f282014-11-05 14:15:05 +00001842 case Primitive::kPrimLong:
1843 switch (input_type) {
1844 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001845 case Primitive::kPrimBoolean:
1846 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001847 case Primitive::kPrimByte:
1848 case Primitive::kPrimShort:
1849 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001850 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001851 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001852 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001853 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001854 break;
1855
Roland Levillain624279f2014-12-04 11:54:28 +00001856 case Primitive::kPrimFloat: {
1857 // Processing a Dex `float-to-long' instruction.
1858 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1859 CpuRegister output = out.AsRegister<CpuRegister>();
1860 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1861 Label done, nan;
1862
Mark Mendell92e83bf2015-05-07 11:25:03 -04001863 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001864 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001865 __ cvtsi2ss(temp, output, true);
1866 // if input >= temp goto done
1867 __ comiss(input, temp);
1868 __ j(kAboveEqual, &done);
1869 // if input == NaN goto nan
1870 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001871 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001872 __ cvttss2si(output, input, true);
1873 __ jmp(&done);
1874 __ Bind(&nan);
1875 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001876 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00001877 __ Bind(&done);
1878 break;
1879 }
1880
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001881 case Primitive::kPrimDouble: {
1882 // Processing a Dex `double-to-long' instruction.
1883 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1884 CpuRegister output = out.AsRegister<CpuRegister>();
1885 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1886 Label done, nan;
1887
Mark Mendell92e83bf2015-05-07 11:25:03 -04001888 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001889 // temp = long-to-double(output)
1890 __ cvtsi2sd(temp, output, true);
1891 // if input >= temp goto done
1892 __ comisd(input, temp);
1893 __ j(kAboveEqual, &done);
1894 // if input == NaN goto nan
1895 __ j(kUnordered, &nan);
1896 // output = double-to-long-truncate(input)
1897 __ cvttsd2si(output, input, true);
1898 __ jmp(&done);
1899 __ Bind(&nan);
1900 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001901 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001902 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001903 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001904 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001905
1906 default:
1907 LOG(FATAL) << "Unexpected type conversion from " << input_type
1908 << " to " << result_type;
1909 }
1910 break;
1911
Roland Levillain981e4542014-11-14 11:47:14 +00001912 case Primitive::kPrimChar:
1913 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001914 case Primitive::kPrimBoolean:
1915 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001916 case Primitive::kPrimByte:
1917 case Primitive::kPrimShort:
1918 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001919 // Processing a Dex `int-to-char' instruction.
1920 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001921 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001922 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001924 Address(CpuRegister(RSP), in.GetStackIndex()));
1925 } else {
1926 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001927 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001928 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1929 }
1930 break;
1931
1932 default:
1933 LOG(FATAL) << "Unexpected type conversion from " << input_type
1934 << " to " << result_type;
1935 }
1936 break;
1937
Roland Levillaindff1f282014-11-05 14:15:05 +00001938 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001939 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001940 case Primitive::kPrimBoolean:
1941 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001942 case Primitive::kPrimByte:
1943 case Primitive::kPrimShort:
1944 case Primitive::kPrimInt:
1945 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001946 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001947 if (in.IsRegister()) {
1948 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1949 } else if (in.IsConstant()) {
1950 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
1951 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1952 if (v == 0) {
1953 __ xorps(dest, dest);
1954 } else {
1955 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1956 }
1957 } else {
1958 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1959 Address(CpuRegister(RSP), in.GetStackIndex()), false);
1960 }
Roland Levillaincff13742014-11-17 14:32:17 +00001961 break;
1962
1963 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001964 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001965 if (in.IsRegister()) {
1966 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1967 } else if (in.IsConstant()) {
1968 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
1969 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1970 if (v == 0) {
1971 __ xorps(dest, dest);
1972 } else {
1973 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1974 }
1975 } else {
1976 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1977 Address(CpuRegister(RSP), in.GetStackIndex()), true);
1978 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001979 break;
1980
Roland Levillaincff13742014-11-17 14:32:17 +00001981 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001982 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001983 if (in.IsFpuRegister()) {
1984 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
1985 } else if (in.IsConstant()) {
1986 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
1987 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1988 if (bit_cast<int64_t, double>(v) == 0) {
1989 __ xorps(dest, dest);
1990 } else {
1991 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1992 }
1993 } else {
1994 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
1995 Address(CpuRegister(RSP), in.GetStackIndex()));
1996 }
Roland Levillaincff13742014-11-17 14:32:17 +00001997 break;
1998
1999 default:
2000 LOG(FATAL) << "Unexpected type conversion from " << input_type
2001 << " to " << result_type;
2002 };
2003 break;
2004
Roland Levillaindff1f282014-11-05 14:15:05 +00002005 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002006 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002007 case Primitive::kPrimBoolean:
2008 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002009 case Primitive::kPrimByte:
2010 case Primitive::kPrimShort:
2011 case Primitive::kPrimInt:
2012 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002013 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002014 if (in.IsRegister()) {
2015 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2016 } else if (in.IsConstant()) {
2017 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2018 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2019 if (v == 0) {
2020 __ xorpd(dest, dest);
2021 } else {
2022 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2023 }
2024 } else {
2025 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2026 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2027 }
Roland Levillaincff13742014-11-17 14:32:17 +00002028 break;
2029
2030 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002031 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002032 if (in.IsRegister()) {
2033 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2034 } else if (in.IsConstant()) {
2035 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2036 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2037 if (v == 0) {
2038 __ xorpd(dest, dest);
2039 } else {
2040 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2041 }
2042 } else {
2043 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2044 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2045 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002046 break;
2047
Roland Levillaincff13742014-11-17 14:32:17 +00002048 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002049 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002050 if (in.IsFpuRegister()) {
2051 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2052 } else if (in.IsConstant()) {
2053 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2054 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2055 if (bit_cast<int32_t, float>(v) == 0) {
2056 __ xorpd(dest, dest);
2057 } else {
2058 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2059 }
2060 } else {
2061 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2062 Address(CpuRegister(RSP), in.GetStackIndex()));
2063 }
Roland Levillaincff13742014-11-17 14:32:17 +00002064 break;
2065
2066 default:
2067 LOG(FATAL) << "Unexpected type conversion from " << input_type
2068 << " to " << result_type;
2069 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002070 break;
2071
2072 default:
2073 LOG(FATAL) << "Unexpected type conversion from " << input_type
2074 << " to " << result_type;
2075 }
2076}
2077
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002078void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002079 LocationSummary* locations =
2080 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002081 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002082 case Primitive::kPrimInt: {
2083 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002084 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002086 break;
2087 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002088
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002089 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002090 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002091 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002092 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002093 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002094 break;
2095 }
2096
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002097 case Primitive::kPrimDouble:
2098 case Primitive::kPrimFloat: {
2099 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002100 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002101 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002102 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002103 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104
2105 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002106 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002107 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002108}
2109
2110void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2111 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002112 Location first = locations->InAt(0);
2113 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002114 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002115
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002116 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002117 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002119 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2120 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002121 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2122 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002123 } else {
2124 __ leal(out.AsRegister<CpuRegister>(), Address(
2125 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2126 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002128 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2129 __ addl(out.AsRegister<CpuRegister>(),
2130 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2131 } else {
2132 __ leal(out.AsRegister<CpuRegister>(), Address(
2133 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2134 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002135 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002136 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002138 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002139 break;
2140 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002141
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002142 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002143 if (second.IsRegister()) {
2144 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2145 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002146 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2147 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002148 } else {
2149 __ leaq(out.AsRegister<CpuRegister>(), Address(
2150 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2151 }
2152 } else {
2153 DCHECK(second.IsConstant());
2154 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2155 int32_t int32_value = Low32Bits(value);
2156 DCHECK_EQ(int32_value, value);
2157 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2158 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2159 } else {
2160 __ leaq(out.AsRegister<CpuRegister>(), Address(
2161 first.AsRegister<CpuRegister>(), int32_value));
2162 }
2163 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002164 break;
2165 }
2166
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002167 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002168 if (second.IsFpuRegister()) {
2169 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2170 } else if (second.IsConstant()) {
2171 __ addss(first.AsFpuRegister<XmmRegister>(),
2172 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2173 } else {
2174 DCHECK(second.IsStackSlot());
2175 __ addss(first.AsFpuRegister<XmmRegister>(),
2176 Address(CpuRegister(RSP), second.GetStackIndex()));
2177 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002178 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002179 }
2180
2181 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002182 if (second.IsFpuRegister()) {
2183 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2184 } else if (second.IsConstant()) {
2185 __ addsd(first.AsFpuRegister<XmmRegister>(),
2186 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2187 } else {
2188 DCHECK(second.IsDoubleStackSlot());
2189 __ addsd(first.AsFpuRegister<XmmRegister>(),
2190 Address(CpuRegister(RSP), second.GetStackIndex()));
2191 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002192 break;
2193 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002194
2195 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002196 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002197 }
2198}
2199
2200void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002201 LocationSummary* locations =
2202 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002203 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002204 case Primitive::kPrimInt: {
2205 locations->SetInAt(0, Location::RequiresRegister());
2206 locations->SetInAt(1, Location::Any());
2207 locations->SetOut(Location::SameAsFirstInput());
2208 break;
2209 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002210 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002211 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002212 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002213 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002214 break;
2215 }
Calin Juravle11351682014-10-23 15:38:15 +01002216 case Primitive::kPrimFloat:
2217 case Primitive::kPrimDouble: {
2218 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002219 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002220 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002221 break;
Calin Juravle11351682014-10-23 15:38:15 +01002222 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002223 default:
Calin Juravle11351682014-10-23 15:38:15 +01002224 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002225 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002226}
2227
2228void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2229 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002230 Location first = locations->InAt(0);
2231 Location second = locations->InAt(1);
2232 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002233 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002234 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002235 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002237 } else if (second.IsConstant()) {
2238 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002240 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002242 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002243 break;
2244 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002245 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002246 if (second.IsConstant()) {
2247 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2248 DCHECK(IsInt<32>(value));
2249 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2250 } else {
2251 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2252 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002253 break;
2254 }
2255
Calin Juravle11351682014-10-23 15:38:15 +01002256 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002257 if (second.IsFpuRegister()) {
2258 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2259 } else if (second.IsConstant()) {
2260 __ subss(first.AsFpuRegister<XmmRegister>(),
2261 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2262 } else {
2263 DCHECK(second.IsStackSlot());
2264 __ subss(first.AsFpuRegister<XmmRegister>(),
2265 Address(CpuRegister(RSP), second.GetStackIndex()));
2266 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002267 break;
Calin Juravle11351682014-10-23 15:38:15 +01002268 }
2269
2270 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002271 if (second.IsFpuRegister()) {
2272 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2273 } else if (second.IsConstant()) {
2274 __ subsd(first.AsFpuRegister<XmmRegister>(),
2275 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2276 } else {
2277 DCHECK(second.IsDoubleStackSlot());
2278 __ subsd(first.AsFpuRegister<XmmRegister>(),
2279 Address(CpuRegister(RSP), second.GetStackIndex()));
2280 }
Calin Juravle11351682014-10-23 15:38:15 +01002281 break;
2282 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002283
2284 default:
Calin Juravle11351682014-10-23 15:38:15 +01002285 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002286 }
2287}
2288
Calin Juravle34bacdf2014-10-07 20:23:36 +01002289void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2290 LocationSummary* locations =
2291 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2292 switch (mul->GetResultType()) {
2293 case Primitive::kPrimInt: {
2294 locations->SetInAt(0, Location::RequiresRegister());
2295 locations->SetInAt(1, Location::Any());
2296 locations->SetOut(Location::SameAsFirstInput());
2297 break;
2298 }
2299 case Primitive::kPrimLong: {
2300 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002301 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2302 if (locations->InAt(1).IsConstant()) {
2303 // Can use 3 operand multiply.
2304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2305 } else {
2306 locations->SetOut(Location::SameAsFirstInput());
2307 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002308 break;
2309 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002310 case Primitive::kPrimFloat:
2311 case Primitive::kPrimDouble: {
2312 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002313 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002314 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002315 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002316 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002317
2318 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002319 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002320 }
2321}
2322
2323void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2324 LocationSummary* locations = mul->GetLocations();
2325 Location first = locations->InAt(0);
2326 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002327 switch (mul->GetResultType()) {
2328 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002329 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002330 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002332 } else if (second.IsConstant()) {
2333 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002334 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002335 } else {
2336 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002337 __ imull(first.AsRegister<CpuRegister>(),
2338 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002339 }
2340 break;
2341 }
2342 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002343 if (second.IsConstant()) {
2344 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2345 DCHECK(IsInt<32>(value));
2346 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2347 first.AsRegister<CpuRegister>(),
2348 Immediate(static_cast<int32_t>(value)));
2349 } else {
2350 DCHECK(first.Equals(locations->Out()));
2351 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2352 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002353 break;
2354 }
2355
Calin Juravleb5bfa962014-10-21 18:02:24 +01002356 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002357 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002358 if (second.IsFpuRegister()) {
2359 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2360 } else if (second.IsConstant()) {
2361 __ mulss(first.AsFpuRegister<XmmRegister>(),
2362 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2363 } else {
2364 DCHECK(second.IsStackSlot());
2365 __ mulss(first.AsFpuRegister<XmmRegister>(),
2366 Address(CpuRegister(RSP), second.GetStackIndex()));
2367 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002368 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002369 }
2370
2371 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002372 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002373 if (second.IsFpuRegister()) {
2374 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2375 } else if (second.IsConstant()) {
2376 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2377 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2378 } else {
2379 DCHECK(second.IsDoubleStackSlot());
2380 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2381 Address(CpuRegister(RSP), second.GetStackIndex()));
2382 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002383 break;
2384 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002385
2386 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002387 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002388 }
2389}
2390
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002391void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2392 uint32_t stack_adjustment, bool is_float) {
2393 if (source.IsStackSlot()) {
2394 DCHECK(is_float);
2395 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2396 } else if (source.IsDoubleStackSlot()) {
2397 DCHECK(!is_float);
2398 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2399 } else {
2400 // Write the value to the temporary location on the stack and load to FP stack.
2401 if (is_float) {
2402 Location stack_temp = Location::StackSlot(temp_offset);
2403 codegen_->Move(stack_temp, source);
2404 __ flds(Address(CpuRegister(RSP), temp_offset));
2405 } else {
2406 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2407 codegen_->Move(stack_temp, source);
2408 __ fldl(Address(CpuRegister(RSP), temp_offset));
2409 }
2410 }
2411}
2412
2413void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2414 Primitive::Type type = rem->GetResultType();
2415 bool is_float = type == Primitive::kPrimFloat;
2416 size_t elem_size = Primitive::ComponentSize(type);
2417 LocationSummary* locations = rem->GetLocations();
2418 Location first = locations->InAt(0);
2419 Location second = locations->InAt(1);
2420 Location out = locations->Out();
2421
2422 // Create stack space for 2 elements.
2423 // TODO: enhance register allocator to ask for stack temporaries.
2424 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2425
2426 // Load the values to the FP stack in reverse order, using temporaries if needed.
2427 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2428 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2429
2430 // Loop doing FPREM until we stabilize.
2431 Label retry;
2432 __ Bind(&retry);
2433 __ fprem();
2434
2435 // Move FP status to AX.
2436 __ fstsw();
2437
2438 // And see if the argument reduction is complete. This is signaled by the
2439 // C2 FPU flag bit set to 0.
2440 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2441 __ j(kNotEqual, &retry);
2442
2443 // We have settled on the final value. Retrieve it into an XMM register.
2444 // Store FP top of stack to real stack.
2445 if (is_float) {
2446 __ fsts(Address(CpuRegister(RSP), 0));
2447 } else {
2448 __ fstl(Address(CpuRegister(RSP), 0));
2449 }
2450
2451 // Pop the 2 items from the FP stack.
2452 __ fucompp();
2453
2454 // Load the value from the stack into an XMM register.
2455 DCHECK(out.IsFpuRegister()) << out;
2456 if (is_float) {
2457 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2458 } else {
2459 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2460 }
2461
2462 // And remove the temporary stack space we allocated.
2463 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2464}
2465
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002466void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2467 DCHECK(instruction->IsDiv() || instruction->IsRem());
2468
2469 LocationSummary* locations = instruction->GetLocations();
2470 Location second = locations->InAt(1);
2471 DCHECK(second.IsConstant());
2472
2473 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2474 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002475 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002476
2477 DCHECK(imm == 1 || imm == -1);
2478
2479 switch (instruction->GetResultType()) {
2480 case Primitive::kPrimInt: {
2481 if (instruction->IsRem()) {
2482 __ xorl(output_register, output_register);
2483 } else {
2484 __ movl(output_register, input_register);
2485 if (imm == -1) {
2486 __ negl(output_register);
2487 }
2488 }
2489 break;
2490 }
2491
2492 case Primitive::kPrimLong: {
2493 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002494 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002495 } else {
2496 __ movq(output_register, input_register);
2497 if (imm == -1) {
2498 __ negq(output_register);
2499 }
2500 }
2501 break;
2502 }
2503
2504 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002505 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002506 }
2507}
2508
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002509void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002510 LocationSummary* locations = instruction->GetLocations();
2511 Location second = locations->InAt(1);
2512
2513 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2514 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2515
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002516 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002517
2518 DCHECK(IsPowerOfTwo(std::abs(imm)));
2519
2520 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2521
2522 if (instruction->GetResultType() == Primitive::kPrimInt) {
2523 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2524 __ testl(numerator, numerator);
2525 __ cmov(kGreaterEqual, tmp, numerator);
2526 int shift = CTZ(imm);
2527 __ sarl(tmp, Immediate(shift));
2528
2529 if (imm < 0) {
2530 __ negl(tmp);
2531 }
2532
2533 __ movl(output_register, tmp);
2534 } else {
2535 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2536 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2537
Mark Mendell92e83bf2015-05-07 11:25:03 -04002538 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002539 __ addq(rdx, numerator);
2540 __ testq(numerator, numerator);
2541 __ cmov(kGreaterEqual, rdx, numerator);
2542 int shift = CTZ(imm);
2543 __ sarq(rdx, Immediate(shift));
2544
2545 if (imm < 0) {
2546 __ negq(rdx);
2547 }
2548
2549 __ movq(output_register, rdx);
2550 }
2551}
2552
2553void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2554 DCHECK(instruction->IsDiv() || instruction->IsRem());
2555
2556 LocationSummary* locations = instruction->GetLocations();
2557 Location second = locations->InAt(1);
2558
2559 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2560 : locations->GetTemp(0).AsRegister<CpuRegister>();
2561 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2562 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2563 : locations->Out().AsRegister<CpuRegister>();
2564 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2565
2566 DCHECK_EQ(RAX, eax.AsRegister());
2567 DCHECK_EQ(RDX, edx.AsRegister());
2568 if (instruction->IsDiv()) {
2569 DCHECK_EQ(RAX, out.AsRegister());
2570 } else {
2571 DCHECK_EQ(RDX, out.AsRegister());
2572 }
2573
2574 int64_t magic;
2575 int shift;
2576
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002577 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002578 if (instruction->GetResultType() == Primitive::kPrimInt) {
2579 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2580
2581 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2582
2583 __ movl(numerator, eax);
2584
2585 Label no_div;
2586 Label end;
2587 __ testl(eax, eax);
2588 __ j(kNotEqual, &no_div);
2589
2590 __ xorl(out, out);
2591 __ jmp(&end);
2592
2593 __ Bind(&no_div);
2594
2595 __ movl(eax, Immediate(magic));
2596 __ imull(numerator);
2597
2598 if (imm > 0 && magic < 0) {
2599 __ addl(edx, numerator);
2600 } else if (imm < 0 && magic > 0) {
2601 __ subl(edx, numerator);
2602 }
2603
2604 if (shift != 0) {
2605 __ sarl(edx, Immediate(shift));
2606 }
2607
2608 __ movl(eax, edx);
2609 __ shrl(edx, Immediate(31));
2610 __ addl(edx, eax);
2611
2612 if (instruction->IsRem()) {
2613 __ movl(eax, numerator);
2614 __ imull(edx, Immediate(imm));
2615 __ subl(eax, edx);
2616 __ movl(edx, eax);
2617 } else {
2618 __ movl(eax, edx);
2619 }
2620 __ Bind(&end);
2621 } else {
2622 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2623
2624 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2625
2626 CpuRegister rax = eax;
2627 CpuRegister rdx = edx;
2628
2629 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2630
2631 // Save the numerator.
2632 __ movq(numerator, rax);
2633
2634 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002635 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002636
2637 // RDX:RAX = magic * numerator
2638 __ imulq(numerator);
2639
2640 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002641 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002642 __ addq(rdx, numerator);
2643 } else if (imm < 0 && magic > 0) {
2644 // RDX -= numerator
2645 __ subq(rdx, numerator);
2646 }
2647
2648 // Shift if needed.
2649 if (shift != 0) {
2650 __ sarq(rdx, Immediate(shift));
2651 }
2652
2653 // RDX += 1 if RDX < 0
2654 __ movq(rax, rdx);
2655 __ shrq(rdx, Immediate(63));
2656 __ addq(rdx, rax);
2657
2658 if (instruction->IsRem()) {
2659 __ movq(rax, numerator);
2660
2661 if (IsInt<32>(imm)) {
2662 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2663 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002664 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002665 }
2666
2667 __ subq(rax, rdx);
2668 __ movq(rdx, rax);
2669 } else {
2670 __ movq(rax, rdx);
2671 }
2672 }
2673}
2674
Calin Juravlebacfec32014-11-14 15:54:36 +00002675void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2676 DCHECK(instruction->IsDiv() || instruction->IsRem());
2677 Primitive::Type type = instruction->GetResultType();
2678 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2679
2680 bool is_div = instruction->IsDiv();
2681 LocationSummary* locations = instruction->GetLocations();
2682
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002683 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2684 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002685
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002687 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002688
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002689 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002690 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002691
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002692 if (imm == 0) {
2693 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2694 } else if (imm == 1 || imm == -1) {
2695 DivRemOneOrMinusOne(instruction);
2696 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002697 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002698 } else {
2699 DCHECK(imm <= -2 || imm >= 2);
2700 GenerateDivRemWithAnyConstant(instruction);
2701 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002702 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002703 SlowPathCodeX86_64* slow_path =
2704 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2705 out.AsRegister(), type, is_div);
2706 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002707
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002708 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2709 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2710 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2711 // so it's safe to just use negl instead of more complex comparisons.
2712 if (type == Primitive::kPrimInt) {
2713 __ cmpl(second_reg, Immediate(-1));
2714 __ j(kEqual, slow_path->GetEntryLabel());
2715 // edx:eax <- sign-extended of eax
2716 __ cdq();
2717 // eax = quotient, edx = remainder
2718 __ idivl(second_reg);
2719 } else {
2720 __ cmpq(second_reg, Immediate(-1));
2721 __ j(kEqual, slow_path->GetEntryLabel());
2722 // rdx:rax <- sign-extended of rax
2723 __ cqo();
2724 // rax = quotient, rdx = remainder
2725 __ idivq(second_reg);
2726 }
2727 __ Bind(slow_path->GetExitLabel());
2728 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002729}
2730
Calin Juravle7c4954d2014-10-28 16:57:40 +00002731void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2732 LocationSummary* locations =
2733 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2734 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002735 case Primitive::kPrimInt:
2736 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002737 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002738 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002739 locations->SetOut(Location::SameAsFirstInput());
2740 // Intel uses edx:eax as the dividend.
2741 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002742 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2743 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2744 // output and request another temp.
2745 if (div->InputAt(1)->IsConstant()) {
2746 locations->AddTemp(Location::RequiresRegister());
2747 }
Calin Juravled0d48522014-11-04 16:40:20 +00002748 break;
2749 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002750
Calin Juravle7c4954d2014-10-28 16:57:40 +00002751 case Primitive::kPrimFloat:
2752 case Primitive::kPrimDouble: {
2753 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002754 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002755 locations->SetOut(Location::SameAsFirstInput());
2756 break;
2757 }
2758
2759 default:
2760 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2761 }
2762}
2763
2764void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2765 LocationSummary* locations = div->GetLocations();
2766 Location first = locations->InAt(0);
2767 Location second = locations->InAt(1);
2768 DCHECK(first.Equals(locations->Out()));
2769
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002770 Primitive::Type type = div->GetResultType();
2771 switch (type) {
2772 case Primitive::kPrimInt:
2773 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002774 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002775 break;
2776 }
2777
Calin Juravle7c4954d2014-10-28 16:57:40 +00002778 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002779 if (second.IsFpuRegister()) {
2780 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2781 } else if (second.IsConstant()) {
2782 __ divss(first.AsFpuRegister<XmmRegister>(),
2783 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2784 } else {
2785 DCHECK(second.IsStackSlot());
2786 __ divss(first.AsFpuRegister<XmmRegister>(),
2787 Address(CpuRegister(RSP), second.GetStackIndex()));
2788 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002789 break;
2790 }
2791
2792 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002793 if (second.IsFpuRegister()) {
2794 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2795 } else if (second.IsConstant()) {
2796 __ divsd(first.AsFpuRegister<XmmRegister>(),
2797 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2798 } else {
2799 DCHECK(second.IsDoubleStackSlot());
2800 __ divsd(first.AsFpuRegister<XmmRegister>(),
2801 Address(CpuRegister(RSP), second.GetStackIndex()));
2802 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002803 break;
2804 }
2805
2806 default:
2807 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2808 }
2809}
2810
Calin Juravlebacfec32014-11-14 15:54:36 +00002811void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002812 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002813 LocationSummary* locations =
2814 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002815
2816 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002817 case Primitive::kPrimInt:
2818 case Primitive::kPrimLong: {
2819 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002820 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002821 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2822 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002823 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2824 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2825 // output and request another temp.
2826 if (rem->InputAt(1)->IsConstant()) {
2827 locations->AddTemp(Location::RequiresRegister());
2828 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002829 break;
2830 }
2831
2832 case Primitive::kPrimFloat:
2833 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002834 locations->SetInAt(0, Location::Any());
2835 locations->SetInAt(1, Location::Any());
2836 locations->SetOut(Location::RequiresFpuRegister());
2837 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002838 break;
2839 }
2840
2841 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002842 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002843 }
2844}
2845
2846void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2847 Primitive::Type type = rem->GetResultType();
2848 switch (type) {
2849 case Primitive::kPrimInt:
2850 case Primitive::kPrimLong: {
2851 GenerateDivRemIntegral(rem);
2852 break;
2853 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002854 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002855 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002856 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002857 break;
2858 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002859 default:
2860 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2861 }
2862}
2863
Calin Juravled0d48522014-11-04 16:40:20 +00002864void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2865 LocationSummary* locations =
2866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2867 locations->SetInAt(0, Location::Any());
2868 if (instruction->HasUses()) {
2869 locations->SetOut(Location::SameAsFirstInput());
2870 }
2871}
2872
2873void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2874 SlowPathCodeX86_64* slow_path =
2875 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2876 codegen_->AddSlowPath(slow_path);
2877
2878 LocationSummary* locations = instruction->GetLocations();
2879 Location value = locations->InAt(0);
2880
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002881 switch (instruction->GetType()) {
2882 case Primitive::kPrimInt: {
2883 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002885 __ j(kEqual, slow_path->GetEntryLabel());
2886 } else if (value.IsStackSlot()) {
2887 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2888 __ j(kEqual, slow_path->GetEntryLabel());
2889 } else {
2890 DCHECK(value.IsConstant()) << value;
2891 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2892 __ jmp(slow_path->GetEntryLabel());
2893 }
2894 }
2895 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002896 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002897 case Primitive::kPrimLong: {
2898 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002900 __ j(kEqual, slow_path->GetEntryLabel());
2901 } else if (value.IsDoubleStackSlot()) {
2902 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2903 __ j(kEqual, slow_path->GetEntryLabel());
2904 } else {
2905 DCHECK(value.IsConstant()) << value;
2906 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2907 __ jmp(slow_path->GetEntryLabel());
2908 }
2909 }
2910 break;
2911 }
2912 default:
2913 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002914 }
Calin Juravled0d48522014-11-04 16:40:20 +00002915}
2916
Calin Juravle9aec02f2014-11-18 23:06:35 +00002917void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2918 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2919
2920 LocationSummary* locations =
2921 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2922
2923 switch (op->GetResultType()) {
2924 case Primitive::kPrimInt:
2925 case Primitive::kPrimLong: {
2926 locations->SetInAt(0, Location::RequiresRegister());
2927 // The shift count needs to be in CL.
2928 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2929 locations->SetOut(Location::SameAsFirstInput());
2930 break;
2931 }
2932 default:
2933 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2934 }
2935}
2936
2937void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2938 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2939
2940 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002942 Location second = locations->InAt(1);
2943
2944 switch (op->GetResultType()) {
2945 case Primitive::kPrimInt: {
2946 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002947 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002948 if (op->IsShl()) {
2949 __ shll(first_reg, second_reg);
2950 } else if (op->IsShr()) {
2951 __ sarl(first_reg, second_reg);
2952 } else {
2953 __ shrl(first_reg, second_reg);
2954 }
2955 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002956 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002957 if (op->IsShl()) {
2958 __ shll(first_reg, imm);
2959 } else if (op->IsShr()) {
2960 __ sarl(first_reg, imm);
2961 } else {
2962 __ shrl(first_reg, imm);
2963 }
2964 }
2965 break;
2966 }
2967 case Primitive::kPrimLong: {
2968 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002970 if (op->IsShl()) {
2971 __ shlq(first_reg, second_reg);
2972 } else if (op->IsShr()) {
2973 __ sarq(first_reg, second_reg);
2974 } else {
2975 __ shrq(first_reg, second_reg);
2976 }
2977 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002978 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002979 if (op->IsShl()) {
2980 __ shlq(first_reg, imm);
2981 } else if (op->IsShr()) {
2982 __ sarq(first_reg, imm);
2983 } else {
2984 __ shrq(first_reg, imm);
2985 }
2986 }
2987 break;
2988 }
2989 default:
2990 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2991 }
2992}
2993
2994void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2995 HandleShift(shl);
2996}
2997
2998void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2999 HandleShift(shl);
3000}
3001
3002void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3003 HandleShift(shr);
3004}
3005
3006void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3007 HandleShift(shr);
3008}
3009
3010void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3011 HandleShift(ushr);
3012}
3013
3014void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3015 HandleShift(ushr);
3016}
3017
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003018void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003019 LocationSummary* locations =
3020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003021 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003022 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003023 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003024 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003025}
3026
3027void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3028 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003029 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3030 instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003031 __ gs()->call(
3032 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003033
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003034 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003035 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003036}
3037
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003038void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3039 LocationSummary* locations =
3040 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3041 InvokeRuntimeCallingConvention calling_convention;
3042 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003043 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003044 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003045 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003046}
3047
3048void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3049 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003050 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3051 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003052
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003053 __ gs()->call(
3054 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003055
3056 DCHECK(!codegen_->IsLeafMethod());
3057 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3058}
3059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003060void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003061 LocationSummary* locations =
3062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003063 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3064 if (location.IsStackSlot()) {
3065 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3066 } else if (location.IsDoubleStackSlot()) {
3067 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3068 }
3069 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070}
3071
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003072void InstructionCodeGeneratorX86_64::VisitParameterValue(
3073 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003075}
3076
3077void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3080 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3081}
3082
3083void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3084 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3085 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003086}
3087
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003088void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003089 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003090 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003091 locations->SetInAt(0, Location::RequiresRegister());
3092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093}
3094
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003095void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3096 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3098 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003099 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003100 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003101 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003103 break;
3104
3105 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003107 break;
3108
3109 default:
3110 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3111 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003112}
3113
David Brazdil66d126e2015-04-03 16:02:44 +01003114void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3115 LocationSummary* locations =
3116 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3117 locations->SetInAt(0, Location::RequiresRegister());
3118 locations->SetOut(Location::SameAsFirstInput());
3119}
3120
3121void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003122 LocationSummary* locations = bool_not->GetLocations();
3123 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3124 locations->Out().AsRegister<CpuRegister>().AsRegister());
3125 Location out = locations->Out();
3126 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3127}
3128
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003129void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003130 LocationSummary* locations =
3131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003132 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3133 locations->SetInAt(i, Location::Any());
3134 }
3135 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003136}
3137
3138void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003139 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003140 LOG(FATAL) << "Unimplemented";
3141}
3142
Calin Juravle52c48962014-12-16 17:02:57 +00003143void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3144 /*
3145 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3146 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3147 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3148 */
3149 switch (kind) {
3150 case MemBarrierKind::kAnyAny: {
3151 __ mfence();
3152 break;
3153 }
3154 case MemBarrierKind::kAnyStore:
3155 case MemBarrierKind::kLoadAny:
3156 case MemBarrierKind::kStoreStore: {
3157 // nop
3158 break;
3159 }
3160 default:
3161 LOG(FATAL) << "Unexpected memory barier " << kind;
3162 }
3163}
3164
3165void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3166 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3167
Nicolas Geoffray39468442014-09-02 15:17:15 +01003168 LocationSummary* locations =
3169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003170 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003171 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3172 locations->SetOut(Location::RequiresFpuRegister());
3173 } else {
3174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3175 }
Calin Juravle52c48962014-12-16 17:02:57 +00003176}
3177
3178void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3179 const FieldInfo& field_info) {
3180 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3181
3182 LocationSummary* locations = instruction->GetLocations();
3183 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3184 Location out = locations->Out();
3185 bool is_volatile = field_info.IsVolatile();
3186 Primitive::Type field_type = field_info.GetFieldType();
3187 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3188
3189 switch (field_type) {
3190 case Primitive::kPrimBoolean: {
3191 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3192 break;
3193 }
3194
3195 case Primitive::kPrimByte: {
3196 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3197 break;
3198 }
3199
3200 case Primitive::kPrimShort: {
3201 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3202 break;
3203 }
3204
3205 case Primitive::kPrimChar: {
3206 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3207 break;
3208 }
3209
3210 case Primitive::kPrimInt:
3211 case Primitive::kPrimNot: {
3212 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3213 break;
3214 }
3215
3216 case Primitive::kPrimLong: {
3217 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3218 break;
3219 }
3220
3221 case Primitive::kPrimFloat: {
3222 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3223 break;
3224 }
3225
3226 case Primitive::kPrimDouble: {
3227 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3228 break;
3229 }
3230
3231 case Primitive::kPrimVoid:
3232 LOG(FATAL) << "Unreachable type " << field_type;
3233 UNREACHABLE();
3234 }
3235
Calin Juravle77520bc2015-01-12 18:45:46 +00003236 codegen_->MaybeRecordImplicitNullCheck(instruction);
3237
Calin Juravle52c48962014-12-16 17:02:57 +00003238 if (is_volatile) {
3239 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3240 }
3241}
3242
3243void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3244 const FieldInfo& field_info) {
3245 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3246
3247 LocationSummary* locations =
3248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003249 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003250 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3251
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003252 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003253 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3254 locations->SetInAt(1, Location::RequiresFpuRegister());
3255 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003256 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003257 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003258 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003259 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003260 locations->AddTemp(Location::RequiresRegister());
3261 locations->AddTemp(Location::RequiresRegister());
3262 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003263}
3264
Calin Juravle52c48962014-12-16 17:02:57 +00003265void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003266 const FieldInfo& field_info,
3267 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003268 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3269
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003270 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003271 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3272 Location value = locations->InAt(1);
3273 bool is_volatile = field_info.IsVolatile();
3274 Primitive::Type field_type = field_info.GetFieldType();
3275 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3276
3277 if (is_volatile) {
3278 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3279 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280
3281 switch (field_type) {
3282 case Primitive::kPrimBoolean:
3283 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003284 if (value.IsConstant()) {
3285 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3286 __ movb(Address(base, offset), Immediate(v));
3287 } else {
3288 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3289 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003290 break;
3291 }
3292
3293 case Primitive::kPrimShort:
3294 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003295 if (value.IsConstant()) {
3296 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3297 __ movw(Address(base, offset), Immediate(v));
3298 } else {
3299 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3300 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003301 break;
3302 }
3303
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003304 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003305 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003306 if (value.IsConstant()) {
3307 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3308 __ movw(Address(base, offset), Immediate(v));
3309 } else {
3310 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3311 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003312 break;
3313 }
3314
3315 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003316 if (value.IsConstant()) {
3317 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3318 DCHECK(IsInt<32>(v));
3319 int32_t v_32 = v;
3320 __ movq(Address(base, offset), Immediate(v_32));
3321 } else {
3322 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3323 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003324 break;
3325 }
3326
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003327 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003328 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003329 break;
3330 }
3331
3332 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003333 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003334 break;
3335 }
3336
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003337 case Primitive::kPrimVoid:
3338 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003339 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340 }
Calin Juravle52c48962014-12-16 17:02:57 +00003341
Calin Juravle77520bc2015-01-12 18:45:46 +00003342 codegen_->MaybeRecordImplicitNullCheck(instruction);
3343
3344 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3345 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3346 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003347 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003348 }
3349
Calin Juravle52c48962014-12-16 17:02:57 +00003350 if (is_volatile) {
3351 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3352 }
3353}
3354
3355void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3356 HandleFieldSet(instruction, instruction->GetFieldInfo());
3357}
3358
3359void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003360 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003361}
3362
3363void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003364 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003365}
3366
3367void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003368 HandleFieldGet(instruction, instruction->GetFieldInfo());
3369}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003370
Calin Juravle52c48962014-12-16 17:02:57 +00003371void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3372 HandleFieldGet(instruction);
3373}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003374
Calin Juravle52c48962014-12-16 17:02:57 +00003375void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3376 HandleFieldGet(instruction, instruction->GetFieldInfo());
3377}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003378
Calin Juravle52c48962014-12-16 17:02:57 +00003379void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3380 HandleFieldSet(instruction, instruction->GetFieldInfo());
3381}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003382
Calin Juravle52c48962014-12-16 17:02:57 +00003383void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003384 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003385}
3386
3387void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003388 LocationSummary* locations =
3389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003390 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3391 ? Location::RequiresRegister()
3392 : Location::Any();
3393 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003394 if (instruction->HasUses()) {
3395 locations->SetOut(Location::SameAsFirstInput());
3396 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003397}
3398
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003399void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003400 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3401 return;
3402 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003403 LocationSummary* locations = instruction->GetLocations();
3404 Location obj = locations->InAt(0);
3405
3406 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3407 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3408}
3409
3410void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003411 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003412 codegen_->AddSlowPath(slow_path);
3413
3414 LocationSummary* locations = instruction->GetLocations();
3415 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003416
3417 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003418 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003419 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003421 } else {
3422 DCHECK(obj.IsConstant()) << obj;
3423 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3424 __ jmp(slow_path->GetEntryLabel());
3425 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003426 }
3427 __ j(kEqual, slow_path->GetEntryLabel());
3428}
3429
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003430void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3431 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3432 GenerateImplicitNullCheck(instruction);
3433 } else {
3434 GenerateExplicitNullCheck(instruction);
3435 }
3436}
3437
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003438void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003439 LocationSummary* locations =
3440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003441 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003442 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003443 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3444 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3445 } else {
3446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3447 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003448}
3449
3450void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3451 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003452 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 Location index = locations->InAt(1);
3454
3455 switch (instruction->GetType()) {
3456 case Primitive::kPrimBoolean: {
3457 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003458 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003459 if (index.IsConstant()) {
3460 __ movzxb(out, Address(obj,
3461 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3462 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 }
3465 break;
3466 }
3467
3468 case Primitive::kPrimByte: {
3469 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003470 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003471 if (index.IsConstant()) {
3472 __ movsxb(out, Address(obj,
3473 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3474 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003475 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003476 }
3477 break;
3478 }
3479
3480 case Primitive::kPrimShort: {
3481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003483 if (index.IsConstant()) {
3484 __ movsxw(out, Address(obj,
3485 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3486 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488 }
3489 break;
3490 }
3491
3492 case Primitive::kPrimChar: {
3493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003495 if (index.IsConstant()) {
3496 __ movzxw(out, Address(obj,
3497 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3498 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500 }
3501 break;
3502 }
3503
3504 case Primitive::kPrimInt:
3505 case Primitive::kPrimNot: {
3506 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3507 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003508 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003509 if (index.IsConstant()) {
3510 __ movl(out, Address(obj,
3511 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3512 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003513 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003514 }
3515 break;
3516 }
3517
3518 case Primitive::kPrimLong: {
3519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003520 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003521 if (index.IsConstant()) {
3522 __ movq(out, Address(obj,
3523 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3524 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003525 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003526 }
3527 break;
3528 }
3529
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003530 case Primitive::kPrimFloat: {
3531 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003532 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003533 if (index.IsConstant()) {
3534 __ movss(out, Address(obj,
3535 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3536 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003537 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003538 }
3539 break;
3540 }
3541
3542 case Primitive::kPrimDouble: {
3543 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003544 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003545 if (index.IsConstant()) {
3546 __ movsd(out, Address(obj,
3547 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3548 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003550 }
3551 break;
3552 }
3553
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003554 case Primitive::kPrimVoid:
3555 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003556 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003557 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003558 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003559}
3560
3561void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003562 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003563
3564 bool needs_write_barrier =
3565 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3566 bool needs_runtime_call = instruction->NeedsTypeCheck();
3567
Nicolas Geoffray39468442014-09-02 15:17:15 +01003568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003569 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3570 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003571 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003572 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3573 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3574 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003576 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003577 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003578 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3579 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003580 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003581 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003582 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3583 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003584 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003585 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003586 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003587
3588 if (needs_write_barrier) {
3589 // Temporary registers for the write barrier.
3590 locations->AddTemp(Location::RequiresRegister());
3591 locations->AddTemp(Location::RequiresRegister());
3592 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003594}
3595
3596void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3597 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003598 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003599 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003600 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003601 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003602 bool needs_runtime_call = locations->WillCall();
3603 bool needs_write_barrier =
3604 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003605
3606 switch (value_type) {
3607 case Primitive::kPrimBoolean:
3608 case Primitive::kPrimByte: {
3609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 if (index.IsConstant()) {
3611 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003612 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003613 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003614 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003615 __ movb(Address(obj, offset),
3616 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003617 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003618 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003619 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3621 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003622 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003624 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3625 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003626 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003627 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 break;
3629 }
3630
3631 case Primitive::kPrimShort:
3632 case Primitive::kPrimChar: {
3633 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 if (index.IsConstant()) {
3635 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003636 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003638 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003639 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003640 __ movw(Address(obj, offset),
3641 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003644 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003645 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3647 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003648 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003649 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003651 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3652 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003654 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003655 break;
3656 }
3657
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003658 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003660 if (!needs_runtime_call) {
3661 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3662 if (index.IsConstant()) {
3663 size_t offset =
3664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3665 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003667 } else {
3668 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003669 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3670 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003671 }
3672 } else {
3673 DCHECK(index.IsRegister()) << index;
3674 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003675 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3676 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003677 } else {
3678 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003679 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003681 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003682 }
3683 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003684 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003685 if (needs_write_barrier) {
3686 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3688 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003689 codegen_->MarkGCCard(
3690 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003691 }
3692 } else {
3693 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003694 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3695 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003696 DCHECK(!codegen_->IsLeafMethod());
3697 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3698 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003699 break;
3700 }
3701
3702 case Primitive::kPrimLong: {
3703 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003704 if (index.IsConstant()) {
3705 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04003706 if (value.IsRegister()) {
3707 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
3708 } else {
3709 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3710 DCHECK(IsInt<32>(v));
3711 int32_t v_32 = v;
3712 __ movq(Address(obj, offset), Immediate(v_32));
3713 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003714 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003715 if (value.IsRegister()) {
3716 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3717 value.AsRegister<CpuRegister>());
3718 } else {
3719 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3720 DCHECK(IsInt<32>(v));
3721 int32_t v_32 = v;
3722 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3723 Immediate(v_32));
3724 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003726 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 break;
3728 }
3729
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003730 case Primitive::kPrimFloat: {
3731 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3732 if (index.IsConstant()) {
3733 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3734 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003735 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003736 } else {
3737 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3739 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003740 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003741 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003742 break;
3743 }
3744
3745 case Primitive::kPrimDouble: {
3746 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3747 if (index.IsConstant()) {
3748 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3749 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003751 } else {
3752 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3754 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003755 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003756 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003757 break;
3758 }
3759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003760 case Primitive::kPrimVoid:
3761 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003762 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 }
3764}
3765
3766void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003767 LocationSummary* locations =
3768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003769 locations->SetInAt(0, Location::RequiresRegister());
3770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771}
3772
3773void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3774 LocationSummary* locations = instruction->GetLocations();
3775 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3777 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003780}
3781
3782void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003783 LocationSummary* locations =
3784 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003785 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003786 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003787 if (instruction->HasUses()) {
3788 locations->SetOut(Location::SameAsFirstInput());
3789 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003790}
3791
3792void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3793 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003794 Location index_loc = locations->InAt(0);
3795 Location length_loc = locations->InAt(1);
3796 SlowPathCodeX86_64* slow_path =
3797 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003798
Mark Mendell99dbd682015-04-22 16:18:52 -04003799 if (length_loc.IsConstant()) {
3800 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3801 if (index_loc.IsConstant()) {
3802 // BCE will remove the bounds check if we are guarenteed to pass.
3803 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3804 if (index < 0 || index >= length) {
3805 codegen_->AddSlowPath(slow_path);
3806 __ jmp(slow_path->GetEntryLabel());
3807 } else {
3808 // Some optimization after BCE may have generated this, and we should not
3809 // generate a bounds check if it is a valid range.
3810 }
3811 return;
3812 }
3813
3814 // We have to reverse the jump condition because the length is the constant.
3815 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
3816 __ cmpl(index_reg, Immediate(length));
3817 codegen_->AddSlowPath(slow_path);
3818 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003819 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003820 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3821 if (index_loc.IsConstant()) {
3822 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3823 __ cmpl(length, Immediate(value));
3824 } else {
3825 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3826 }
3827 codegen_->AddSlowPath(slow_path);
3828 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003829 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830}
3831
3832void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3833 CpuRegister card,
3834 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003835 CpuRegister value,
3836 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003837 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003838 if (value_can_be_null) {
3839 __ testl(value, value);
3840 __ j(kEqual, &is_null);
3841 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 __ gs()->movq(card, Address::Absolute(
3843 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3844 __ movq(temp, object);
3845 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3846 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003847 if (value_can_be_null) {
3848 __ Bind(&is_null);
3849 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003850}
3851
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003852void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3853 temp->SetLocations(nullptr);
3854}
3855
3856void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3857 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003858 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003859}
3860
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003861void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003862 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003863 LOG(FATAL) << "Unimplemented";
3864}
3865
3866void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003867 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3868}
3869
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003870void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3872}
3873
3874void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003875 HBasicBlock* block = instruction->GetBlock();
3876 if (block->GetLoopInformation() != nullptr) {
3877 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3878 // The back edge will generate the suspend check.
3879 return;
3880 }
3881 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3882 // The goto will generate the suspend check.
3883 return;
3884 }
3885 GenerateSuspendCheck(instruction, nullptr);
3886}
3887
3888void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3889 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003890 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003891 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
3892 if (slow_path == nullptr) {
3893 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
3894 instruction->SetSlowPath(slow_path);
3895 codegen_->AddSlowPath(slow_path);
3896 if (successor != nullptr) {
3897 DCHECK(successor->IsLoopHeader());
3898 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3899 }
3900 } else {
3901 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3902 }
3903
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003904 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003905 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003906 if (successor == nullptr) {
3907 __ j(kNotEqual, slow_path->GetEntryLabel());
3908 __ Bind(slow_path->GetReturnLabel());
3909 } else {
3910 __ j(kEqual, codegen_->GetLabelOf(successor));
3911 __ jmp(slow_path->GetEntryLabel());
3912 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003913}
3914
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003915X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3916 return codegen_->GetAssembler();
3917}
3918
3919void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3920 MoveOperands* move = moves_.Get(index);
3921 Location source = move->GetSource();
3922 Location destination = move->GetDestination();
3923
3924 if (source.IsRegister()) {
3925 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003926 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003927 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003928 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003929 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003930 } else {
3931 DCHECK(destination.IsDoubleStackSlot());
3932 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003934 }
3935 } else if (source.IsStackSlot()) {
3936 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003938 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003939 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003940 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003941 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003942 } else {
3943 DCHECK(destination.IsStackSlot());
3944 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3945 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3946 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003947 } else if (source.IsDoubleStackSlot()) {
3948 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003950 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003951 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003952 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3953 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003954 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003955 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003956 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3957 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3958 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003959 } else if (source.IsConstant()) {
3960 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003961 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3962 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003963 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003964 if (value == 0) {
3965 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3966 } else {
3967 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3968 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003969 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003970 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003971 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003972 }
3973 } else if (constant->IsLongConstant()) {
3974 int64_t value = constant->AsLongConstant()->GetValue();
3975 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003976 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003977 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003978 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003979 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003980 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3981 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003982 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003983 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003984 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003985 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003986 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3987 if (value == 0) {
3988 // easy FP 0.0.
3989 __ xorps(dest, dest);
3990 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003991 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003992 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003993 } else {
3994 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003995 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003996 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3997 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003998 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003999 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004000 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004001 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004002 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004003 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4004 if (value == 0) {
4005 __ xorpd(dest, dest);
4006 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004007 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004008 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004009 } else {
4010 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004011 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004012 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4013 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004014 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004015 } else if (source.IsFpuRegister()) {
4016 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004017 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004018 } else if (destination.IsStackSlot()) {
4019 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004020 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004021 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004022 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004023 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004025 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004026 }
4027}
4028
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004029void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004030 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004031 __ movl(Address(CpuRegister(RSP), mem), reg);
4032 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004033}
4034
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004035void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004036 ScratchRegisterScope ensure_scratch(
4037 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4038
4039 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4040 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4041 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4042 Address(CpuRegister(RSP), mem2 + stack_offset));
4043 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4044 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4045 CpuRegister(ensure_scratch.GetRegister()));
4046}
4047
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004048void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4049 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4050 __ movq(Address(CpuRegister(RSP), mem), reg);
4051 __ movq(reg, CpuRegister(TMP));
4052}
4053
4054void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4055 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004056 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004057
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004058 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4059 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4060 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4061 Address(CpuRegister(RSP), mem2 + stack_offset));
4062 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4063 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4064 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004065}
4066
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004067void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4068 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4069 __ movss(Address(CpuRegister(RSP), mem), reg);
4070 __ movd(reg, CpuRegister(TMP));
4071}
4072
4073void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4074 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4075 __ movsd(Address(CpuRegister(RSP), mem), reg);
4076 __ movd(reg, CpuRegister(TMP));
4077}
4078
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004079void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4080 MoveOperands* move = moves_.Get(index);
4081 Location source = move->GetSource();
4082 Location destination = move->GetDestination();
4083
4084 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004085 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004086 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004087 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004088 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004089 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004090 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004091 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4092 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004093 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004094 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004096 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4097 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004098 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4100 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4101 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004102 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004103 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004104 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004105 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004106 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004108 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004110 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004111 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004112 }
4113}
4114
4115
4116void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4117 __ pushq(CpuRegister(reg));
4118}
4119
4120
4121void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4122 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004123}
4124
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004125void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4126 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4127 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4128 Immediate(mirror::Class::kStatusInitialized));
4129 __ j(kLess, slow_path->GetEntryLabel());
4130 __ Bind(slow_path->GetExitLabel());
4131 // No need for memory fence, thanks to the X86_64 memory model.
4132}
4133
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004134void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004135 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4136 ? LocationSummary::kCallOnSlowPath
4137 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004138 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004139 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004140 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 locations->SetOut(Location::RequiresRegister());
4142}
4143
4144void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004145 LocationSummary* locations = cls->GetLocations();
4146 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4147 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004148 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004149 DCHECK(!cls->CanCallRuntime());
4150 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004151 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004152 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004153 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004154 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004155 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004156 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004157 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4158 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4159 codegen_->AddSlowPath(slow_path);
4160 __ testl(out, out);
4161 __ j(kEqual, slow_path->GetEntryLabel());
4162 if (cls->MustGenerateClinitCheck()) {
4163 GenerateClassInitializationCheck(slow_path, out);
4164 } else {
4165 __ Bind(slow_path->GetExitLabel());
4166 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004167 }
4168}
4169
4170void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4171 LocationSummary* locations =
4172 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4173 locations->SetInAt(0, Location::RequiresRegister());
4174 if (check->HasUses()) {
4175 locations->SetOut(Location::SameAsFirstInput());
4176 }
4177}
4178
4179void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004180 // We assume the class to not be null.
4181 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4182 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004183 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004184 GenerateClassInitializationCheck(slow_path,
4185 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004186}
4187
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004188void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4189 LocationSummary* locations =
4190 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004191 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004192 locations->SetOut(Location::RequiresRegister());
4193}
4194
4195void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4196 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4197 codegen_->AddSlowPath(slow_path);
4198
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004199 LocationSummary* locations = load->GetLocations();
4200 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4201 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004202 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004203 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004204 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4205 __ testl(out, out);
4206 __ j(kEqual, slow_path->GetEntryLabel());
4207 __ Bind(slow_path->GetExitLabel());
4208}
4209
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004210void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4211 LocationSummary* locations =
4212 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4213 locations->SetOut(Location::RequiresRegister());
4214}
4215
4216void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4217 Address address = Address::Absolute(
4218 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004219 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004220 __ gs()->movl(address, Immediate(0));
4221}
4222
4223void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4224 LocationSummary* locations =
4225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4226 InvokeRuntimeCallingConvention calling_convention;
4227 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4228}
4229
4230void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4231 __ gs()->call(
4232 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4233 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4234}
4235
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004236void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004237 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4238 ? LocationSummary::kNoCall
4239 : LocationSummary::kCallOnSlowPath;
4240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4241 locations->SetInAt(0, Location::RequiresRegister());
4242 locations->SetInAt(1, Location::Any());
4243 locations->SetOut(Location::RequiresRegister());
4244}
4245
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004246void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004247 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004248 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004249 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004250 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004251 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4252 Label done, zero;
4253 SlowPathCodeX86_64* slow_path = nullptr;
4254
4255 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004256 // Avoid null check if we know obj is not null.
4257 if (instruction->MustDoNullCheck()) {
4258 __ testl(obj, obj);
4259 __ j(kEqual, &zero);
4260 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004261 // Compare the class of `obj` with `cls`.
4262 __ movl(out, Address(obj, class_offset));
4263 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004264 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004265 } else {
4266 DCHECK(cls.IsStackSlot()) << cls;
4267 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4268 }
4269 if (instruction->IsClassFinal()) {
4270 // Classes must be equal for the instanceof to succeed.
4271 __ j(kNotEqual, &zero);
4272 __ movl(out, Immediate(1));
4273 __ jmp(&done);
4274 } else {
4275 // If the classes are not equal, we go into a slow path.
4276 DCHECK(locations->OnlyCallsOnSlowPath());
4277 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004278 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004279 codegen_->AddSlowPath(slow_path);
4280 __ j(kNotEqual, slow_path->GetEntryLabel());
4281 __ movl(out, Immediate(1));
4282 __ jmp(&done);
4283 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004284
4285 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4286 __ Bind(&zero);
4287 __ movl(out, Immediate(0));
4288 }
4289
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004290 if (slow_path != nullptr) {
4291 __ Bind(slow_path->GetExitLabel());
4292 }
4293 __ Bind(&done);
4294}
4295
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004296void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4298 instruction, LocationSummary::kCallOnSlowPath);
4299 locations->SetInAt(0, Location::RequiresRegister());
4300 locations->SetInAt(1, Location::Any());
4301 locations->AddTemp(Location::RequiresRegister());
4302}
4303
4304void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4305 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004306 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004307 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004308 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004309 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4310 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4311 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4312 codegen_->AddSlowPath(slow_path);
4313
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004314 // Avoid null check if we know obj is not null.
4315 if (instruction->MustDoNullCheck()) {
4316 __ testl(obj, obj);
4317 __ j(kEqual, slow_path->GetExitLabel());
4318 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004319 // Compare the class of `obj` with `cls`.
4320 __ movl(temp, Address(obj, class_offset));
4321 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004322 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004323 } else {
4324 DCHECK(cls.IsStackSlot()) << cls;
4325 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4326 }
4327 // Classes must be equal for the checkcast to succeed.
4328 __ j(kNotEqual, slow_path->GetEntryLabel());
4329 __ Bind(slow_path->GetExitLabel());
4330}
4331
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004332void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4333 LocationSummary* locations =
4334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4335 InvokeRuntimeCallingConvention calling_convention;
4336 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4337}
4338
4339void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4340 __ gs()->call(Address::Absolute(instruction->IsEnter()
4341 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4342 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4343 true));
4344 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4345}
4346
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004347void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4348void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4349void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4350
4351void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4352 LocationSummary* locations =
4353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4354 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4355 || instruction->GetResultType() == Primitive::kPrimLong);
4356 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004357 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004358 locations->SetOut(Location::SameAsFirstInput());
4359}
4360
4361void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4362 HandleBitwiseOperation(instruction);
4363}
4364
4365void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4366 HandleBitwiseOperation(instruction);
4367}
4368
4369void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4370 HandleBitwiseOperation(instruction);
4371}
4372
4373void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4374 LocationSummary* locations = instruction->GetLocations();
4375 Location first = locations->InAt(0);
4376 Location second = locations->InAt(1);
4377 DCHECK(first.Equals(locations->Out()));
4378
4379 if (instruction->GetResultType() == Primitive::kPrimInt) {
4380 if (second.IsRegister()) {
4381 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004383 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004384 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004385 } else {
4386 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004387 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004388 }
4389 } else if (second.IsConstant()) {
4390 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4391 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004392 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004393 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004394 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004395 } else {
4396 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004397 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004398 }
4399 } else {
4400 Address address(CpuRegister(RSP), second.GetStackIndex());
4401 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004402 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004403 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004404 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004405 } else {
4406 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004408 }
4409 }
4410 } else {
4411 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004412 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4413 bool second_is_constant = false;
4414 int64_t value = 0;
4415 if (second.IsConstant()) {
4416 second_is_constant = true;
4417 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004418 }
Mark Mendell40741f32015-04-20 22:10:34 -04004419 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004420
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004421 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004422 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004423 if (is_int32_value) {
4424 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4425 } else {
4426 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4427 }
4428 } else if (second.IsDoubleStackSlot()) {
4429 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004430 } else {
4431 __ andq(first_reg, second.AsRegister<CpuRegister>());
4432 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004433 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004434 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004435 if (is_int32_value) {
4436 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4437 } else {
4438 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4439 }
4440 } else if (second.IsDoubleStackSlot()) {
4441 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004442 } else {
4443 __ orq(first_reg, second.AsRegister<CpuRegister>());
4444 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004445 } else {
4446 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004447 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004448 if (is_int32_value) {
4449 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4450 } else {
4451 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4452 }
4453 } else if (second.IsDoubleStackSlot()) {
4454 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004455 } else {
4456 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4457 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004458 }
4459 }
4460}
4461
Calin Juravleb1498f62015-02-16 13:13:29 +00004462void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4463 // Nothing to do, this should be removed during prepare for register allocator.
4464 UNUSED(instruction);
4465 LOG(FATAL) << "Unreachable";
4466}
4467
4468void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4469 // Nothing to do, this should be removed during prepare for register allocator.
4470 UNUSED(instruction);
4471 LOG(FATAL) << "Unreachable";
4472}
4473
Mark Mendell92e83bf2015-05-07 11:25:03 -04004474void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4475 if (value == 0) {
4476 __ xorl(dest, dest);
4477 } else if (value > 0 && IsInt<32>(value)) {
4478 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4479 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4480 } else {
4481 __ movq(dest, Immediate(value));
4482 }
4483}
4484
Mark Mendellf55c3e02015-03-26 21:07:46 -04004485void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4486 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004487 X86_64Assembler* assembler = GetAssembler();
4488 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004489 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4490 // byte values. If used for vectors at a later time, this will need to be
4491 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004492 assembler->Align(4, 0);
4493 constant_area_start_ = assembler->CodeSize();
4494 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004495 }
4496
4497 // And finish up.
4498 CodeGenerator::Finalize(allocator);
4499}
4500
4501/**
4502 * Class to handle late fixup of offsets into constant area.
4503 */
4504class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4505 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004506 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004507 : codegen_(codegen), offset_into_constant_area_(offset) {}
4508
4509 private:
4510 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4511 // Patch the correct offset for the instruction. We use the address of the
4512 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4513 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4514 int relative_position = constant_offset - pos;
4515
4516 // Patch in the right value.
4517 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4518 }
4519
Mark Mendell39dcf552015-04-09 20:42:42 -04004520 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004521
4522 // Location in constant area that the fixup refers to.
4523 int offset_into_constant_area_;
4524};
4525
4526Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4527 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4528 return Address::RIP(fixup);
4529}
4530
4531Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4532 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4533 return Address::RIP(fixup);
4534}
4535
4536Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4537 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4538 return Address::RIP(fixup);
4539}
4540
4541Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4542 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4543 return Address::RIP(fixup);
4544}
4545
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004546} // namespace x86_64
4547} // namespace art