blob: c9fe813f2020128380f05d444520c09880861c89 [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 LocationSummary* locations = invoke->GetLocations();
384 CpuRegister reg = temp.AsRegister<CpuRegister>();
385 CpuRegister current_method =
386 locations->InAt(invoke->GetCurrentMethodInputIndex()).AsRegister<CpuRegister>();
387 // temp = temp->dex_cache_resolved_methods_;
388 __ movl(reg, Address(
389 current_method, ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
390 // temp = temp[index_in_cache]
391 __ movq(reg, Address(
392 reg, CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
393 // (temp + offset_of_quick_compiled_code)()
394 __ call(Address(reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
395 kX86_64WordSize).SizeValue()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000396 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800397
398 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800399}
400
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100402 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100403}
404
405void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100406 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100407}
408
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100409size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
410 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
411 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100412}
413
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100414size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
415 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
416 return kX86_64WordSize;
417}
418
419size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
420 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
421 return kX86_64WordSize;
422}
423
424size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
425 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
426 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100427}
428
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000429static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000430// Use a fake return address register to mimic Quick.
431static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400432CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
433 const X86_64InstructionSetFeatures& isa_features,
434 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000435 : CodeGenerator(graph,
436 kNumberOfCpuRegisters,
437 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000438 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000439 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
440 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000441 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000442 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
443 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000444 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100445 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100446 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000447 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400448 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400449 isa_features_(isa_features),
450 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000451 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
452}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100454InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
455 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100456 : HGraphVisitor(graph),
457 assembler_(codegen->GetAssembler()),
458 codegen_(codegen) {}
459
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100460Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100461 switch (type) {
462 case Primitive::kPrimLong:
463 case Primitive::kPrimByte:
464 case Primitive::kPrimBoolean:
465 case Primitive::kPrimChar:
466 case Primitive::kPrimShort:
467 case Primitive::kPrimInt:
468 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100469 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100470 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100471 }
472
473 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100474 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100475 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100476 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100477 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478
479 case Primitive::kPrimVoid:
480 LOG(FATAL) << "Unreachable type " << type;
481 }
482
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100483 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100484}
485
Nicolas Geoffray98893962015-01-21 12:32:32 +0000486void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100487 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100488 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100489
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000490 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100491 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000492
Nicolas Geoffray98893962015-01-21 12:32:32 +0000493 if (is_baseline) {
494 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
495 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
496 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000497 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
498 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
499 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000500 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100501}
502
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100503static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100504 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100505}
David Srbecky9d8606d2015-04-12 09:35:32 +0100506
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100507static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100508 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100509}
510
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100512 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000513 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100514 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700515 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000516 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100517
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000518 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100519 __ testq(CpuRegister(RAX), Address(
520 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100521 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100522 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000523
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000524 if (HasEmptyFrame()) {
525 return;
526 }
527
Nicolas Geoffray98893962015-01-21 12:32:32 +0000528 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000529 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000530 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100532 __ cfi().AdjustCFAOffset(kX86_64WordSize);
533 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000534 }
535 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100536
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100537 int adjust = GetFrameSize() - GetCoreSpillSize();
538 __ subq(CpuRegister(RSP), Immediate(adjust));
539 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000540 uint32_t xmm_spill_location = GetFpuSpillStart();
541 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100542
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000543 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
544 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100545 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
546 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
547 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000548 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100549 }
550
Mathieu Chartiere401d142015-04-22 13:56:20 -0700551 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100552 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100553}
554
555void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100556 __ cfi().RememberState();
557 if (!HasEmptyFrame()) {
558 uint32_t xmm_spill_location = GetFpuSpillStart();
559 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
560 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
561 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
562 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
563 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
564 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
565 }
566 }
567
568 int adjust = GetFrameSize() - GetCoreSpillSize();
569 __ addq(CpuRegister(RSP), Immediate(adjust));
570 __ cfi().AdjustCFAOffset(-adjust);
571
572 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
573 Register reg = kCoreCalleeSaves[i];
574 if (allocated_registers_.ContainsCoreRegister(reg)) {
575 __ popq(CpuRegister(reg));
576 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
577 __ cfi().Restore(DWARFReg(reg));
578 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000579 }
580 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100581 __ ret();
582 __ cfi().RestoreState();
583 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584}
585
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100586void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
587 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100588}
589
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100590void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000591 DCHECK(RequiresCurrentMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100593}
594
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100595Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
596 switch (load->GetType()) {
597 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100598 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100599 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600
601 case Primitive::kPrimInt:
602 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100603 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100604 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605
606 case Primitive::kPrimBoolean:
607 case Primitive::kPrimByte:
608 case Primitive::kPrimChar:
609 case Primitive::kPrimShort:
610 case Primitive::kPrimVoid:
611 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700612 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100613 }
614
615 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700616 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100617}
618
619void CodeGeneratorX86_64::Move(Location destination, Location source) {
620 if (source.Equals(destination)) {
621 return;
622 }
623 if (destination.IsRegister()) {
624 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000625 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100628 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000629 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100630 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100631 } else {
632 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 Address(CpuRegister(RSP), source.GetStackIndex()));
635 }
636 } else if (destination.IsFpuRegister()) {
637 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000642 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100643 Address(CpuRegister(RSP), source.GetStackIndex()));
644 } else {
645 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000646 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100647 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648 }
649 } else if (destination.IsStackSlot()) {
650 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000652 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100653 } else if (source.IsFpuRegister()) {
654 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000655 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500656 } else if (source.IsConstant()) {
657 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000658 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500659 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100660 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500661 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000662 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
663 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100664 }
665 } else {
666 DCHECK(destination.IsDoubleStackSlot());
667 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000669 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 } else if (source.IsFpuRegister()) {
671 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000672 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500673 } else if (source.IsConstant()) {
674 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800675 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500676 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000677 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500678 } else {
679 DCHECK(constant->IsLongConstant());
680 value = constant->AsLongConstant()->GetValue();
681 }
Mark Mendell92e83bf2015-05-07 11:25:03 -0400682 Load64BitValue(CpuRegister(TMP), value);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500683 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100684 } else {
685 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000686 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
687 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100688 }
689 }
690}
691
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100692void CodeGeneratorX86_64::Move(HInstruction* instruction,
693 Location location,
694 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000695 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100696 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700697 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100698 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000699 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100700 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000701 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000702 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
703 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000704 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000705 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000706 } else if (location.IsStackSlot()) {
707 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
708 } else {
709 DCHECK(location.IsConstant());
710 DCHECK_EQ(location.GetConstant(), const_to_move);
711 }
712 } else if (const_to_move->IsLongConstant()) {
713 int64_t value = const_to_move->AsLongConstant()->GetValue();
714 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400715 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000716 } else if (location.IsDoubleStackSlot()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400717 Load64BitValue(CpuRegister(TMP), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000718 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
719 } else {
720 DCHECK(location.IsConstant());
721 DCHECK_EQ(location.GetConstant(), const_to_move);
722 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100723 }
Roland Levillain476df552014-10-09 17:51:36 +0100724 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 switch (instruction->GetType()) {
726 case Primitive::kPrimBoolean:
727 case Primitive::kPrimByte:
728 case Primitive::kPrimChar:
729 case Primitive::kPrimShort:
730 case Primitive::kPrimInt:
731 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100732 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100733 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
734 break;
735
736 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000738 Move(location,
739 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100740 break;
741
742 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100744 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000745 } else if (instruction->IsTemporary()) {
746 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
747 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100748 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100749 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100750 switch (instruction->GetType()) {
751 case Primitive::kPrimBoolean:
752 case Primitive::kPrimByte:
753 case Primitive::kPrimChar:
754 case Primitive::kPrimShort:
755 case Primitive::kPrimInt:
756 case Primitive::kPrimNot:
757 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100758 case Primitive::kPrimFloat:
759 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000760 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100761 break;
762
763 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100765 }
766 }
767}
768
769void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
770 got->SetLocations(nullptr);
771}
772
773void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
774 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100775 DCHECK(!successor->IsExitBlock());
776
777 HBasicBlock* block = got->GetBlock();
778 HInstruction* previous = got->GetPrevious();
779
780 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000781 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100782 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
783 return;
784 }
785
786 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
787 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
788 }
789 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100790 __ jmp(codegen_->GetLabelOf(successor));
791 }
792}
793
794void LocationsBuilderX86_64::VisitExit(HExit* exit) {
795 exit->SetLocations(nullptr);
796}
797
798void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700799 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100800}
801
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700802void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
803 Label* true_target,
804 Label* false_target,
805 Label* always_true_target) {
806 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 if (cond->IsIntConstant()) {
808 // Constant condition, statically compared against 1.
809 int32_t cond_value = cond->AsIntConstant()->GetValue();
810 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700811 if (always_true_target != nullptr) {
812 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100813 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100815 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 DCHECK_EQ(cond_value, 0);
817 }
818 } else {
819 bool materialized =
820 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
821 // Moves do not affect the eflags register, so if the condition is
822 // evaluated just before the if, we don't need to evaluate it
823 // again.
824 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700825 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100826 if (materialized) {
827 if (!eflags_set) {
828 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700829 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000831 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 } else {
833 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
834 Immediate(0));
835 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100837 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700838 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 }
840 } else {
841 Location lhs = cond->GetLocations()->InAt(0);
842 Location rhs = cond->GetLocations()->InAt(1);
843 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000844 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100845 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000846 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000847 if (constant == 0) {
848 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
849 } else {
850 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
851 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100852 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000853 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100854 Address(CpuRegister(RSP), rhs.GetStackIndex()));
855 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700856 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700857 }
Dave Allison20dfc792014-06-16 20:44:29 -0700858 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700859 if (false_target != nullptr) {
860 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861 }
862}
863
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700864void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
865 LocationSummary* locations =
866 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
867 HInstruction* cond = if_instr->InputAt(0);
868 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
869 locations->SetInAt(0, Location::Any());
870 }
871}
872
873void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
874 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
875 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
876 Label* always_true_target = true_target;
877 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
878 if_instr->IfTrueSuccessor())) {
879 always_true_target = nullptr;
880 }
881 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
882 if_instr->IfFalseSuccessor())) {
883 false_target = nullptr;
884 }
885 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
886}
887
888void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
889 LocationSummary* locations = new (GetGraph()->GetArena())
890 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
891 HInstruction* cond = deoptimize->InputAt(0);
892 DCHECK(cond->IsCondition());
893 if (cond->AsCondition()->NeedsMaterialization()) {
894 locations->SetInAt(0, Location::Any());
895 }
896}
897
898void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
899 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
900 DeoptimizationSlowPathX86_64(deoptimize);
901 codegen_->AddSlowPath(slow_path);
902 Label* slow_path_entry = slow_path->GetEntryLabel();
903 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
904}
905
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100906void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
907 local->SetLocations(nullptr);
908}
909
910void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
911 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
912}
913
914void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
915 local->SetLocations(nullptr);
916}
917
918void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
919 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700920 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921}
922
923void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100924 LocationSummary* locations =
925 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926 switch (store->InputAt(1)->GetType()) {
927 case Primitive::kPrimBoolean:
928 case Primitive::kPrimByte:
929 case Primitive::kPrimChar:
930 case Primitive::kPrimShort:
931 case Primitive::kPrimInt:
932 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100933 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100934 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
935 break;
936
937 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100938 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100939 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
940 break;
941
942 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100943 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100945}
946
947void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700948 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100949}
950
Roland Levillain0d37cd02015-05-27 16:39:19 +0100951void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100952 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +0100953 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100954 locations->SetInAt(0, Location::RequiresRegister());
955 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +0100956 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100957 locations->SetOut(Location::RequiresRegister());
958 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100959}
960
Roland Levillain0d37cd02015-05-27 16:39:19 +0100961void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
962 if (cond->NeedsMaterialization()) {
963 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000964 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100965 // Clear register: setcc only sets the low byte.
Mark Mendell92e83bf2015-05-07 11:25:03 -0400966 __ xorl(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000967 Location lhs = locations->InAt(0);
968 Location rhs = locations->InAt(1);
969 if (rhs.IsRegister()) {
970 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
971 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800972 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000973 if (constant == 0) {
974 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
975 } else {
976 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
977 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100978 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000979 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100980 }
Roland Levillain0d37cd02015-05-27 16:39:19 +0100981 __ setcc(X86_64Condition(cond->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700982 }
983}
984
985void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
986 VisitCondition(comp);
987}
988
989void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
990 VisitCondition(comp);
991}
992
993void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
994 VisitCondition(comp);
995}
996
997void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
998 VisitCondition(comp);
999}
1000
1001void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1002 VisitCondition(comp);
1003}
1004
1005void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1006 VisitCondition(comp);
1007}
1008
1009void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1010 VisitCondition(comp);
1011}
1012
1013void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1014 VisitCondition(comp);
1015}
1016
1017void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1018 VisitCondition(comp);
1019}
1020
1021void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1022 VisitCondition(comp);
1023}
1024
1025void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1026 VisitCondition(comp);
1027}
1028
1029void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1030 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031}
1032
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001033void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001034 LocationSummary* locations =
1035 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001036 switch (compare->InputAt(0)->GetType()) {
1037 case Primitive::kPrimLong: {
1038 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001039 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1041 break;
1042 }
1043 case Primitive::kPrimFloat:
1044 case Primitive::kPrimDouble: {
1045 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001046 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001047 locations->SetOut(Location::RequiresRegister());
1048 break;
1049 }
1050 default:
1051 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1052 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001053}
1054
1055void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001056 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001057 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001058 Location left = locations->InAt(0);
1059 Location right = locations->InAt(1);
1060
1061 Label less, greater, done;
1062 Primitive::Type type = compare->InputAt(0)->GetType();
1063 switch (type) {
1064 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001065 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1066 if (right.IsConstant()) {
1067 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001068 if (IsInt<32>(value)) {
1069 if (value == 0) {
1070 __ testq(left_reg, left_reg);
1071 } else {
1072 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1073 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001074 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001075 // Value won't fit in an int.
1076 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001077 }
Mark Mendell40741f32015-04-20 22:10:34 -04001078 } else if (right.IsDoubleStackSlot()) {
1079 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001080 } else {
1081 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1082 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001083 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001084 }
1085 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001086 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1087 if (right.IsConstant()) {
1088 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1089 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1090 } else if (right.IsStackSlot()) {
1091 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1092 } else {
1093 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1094 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001095 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1096 break;
1097 }
1098 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001099 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1100 if (right.IsConstant()) {
1101 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1102 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1103 } else if (right.IsDoubleStackSlot()) {
1104 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1105 } else {
1106 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1107 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001108 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1109 break;
1110 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001111 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001112 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001113 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001114 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001115 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001116 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001117
Calin Juravle91debbc2014-11-26 19:01:09 +00001118 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001119 __ movl(out, Immediate(1));
1120 __ jmp(&done);
1121
1122 __ Bind(&less);
1123 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001124
1125 __ Bind(&done);
1126}
1127
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001128void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001129 LocationSummary* locations =
1130 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001131 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001132}
1133
1134void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001135 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001136 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137}
1138
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001139void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1140 LocationSummary* locations =
1141 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1142 locations->SetOut(Location::ConstantLocation(constant));
1143}
1144
1145void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1146 // Will be generated at use site.
1147 UNUSED(constant);
1148}
1149
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001151 LocationSummary* locations =
1152 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001153 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001154}
1155
1156void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001157 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001158 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001159}
1160
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001161void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1162 LocationSummary* locations =
1163 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1164 locations->SetOut(Location::ConstantLocation(constant));
1165}
1166
1167void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1168 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001169 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001170}
1171
1172void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1173 LocationSummary* locations =
1174 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1175 locations->SetOut(Location::ConstantLocation(constant));
1176}
1177
1178void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1179 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001180 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001181}
1182
Calin Juravle27df7582015-04-17 19:12:31 +01001183void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1184 memory_barrier->SetLocations(nullptr);
1185}
1186
1187void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1188 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1189}
1190
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1192 ret->SetLocations(nullptr);
1193}
1194
1195void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001196 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001197 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001198}
1199
1200void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001201 LocationSummary* locations =
1202 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001203 switch (ret->InputAt(0)->GetType()) {
1204 case Primitive::kPrimBoolean:
1205 case Primitive::kPrimByte:
1206 case Primitive::kPrimChar:
1207 case Primitive::kPrimShort:
1208 case Primitive::kPrimInt:
1209 case Primitive::kPrimNot:
1210 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001211 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001212 break;
1213
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 case Primitive::kPrimFloat:
1215 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001216 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 break;
1218
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001222}
1223
1224void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1225 if (kIsDebugBuild) {
1226 switch (ret->InputAt(0)->GetType()) {
1227 case Primitive::kPrimBoolean:
1228 case Primitive::kPrimByte:
1229 case Primitive::kPrimChar:
1230 case Primitive::kPrimShort:
1231 case Primitive::kPrimInt:
1232 case Primitive::kPrimNot:
1233 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001235 break;
1236
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 case Primitive::kPrimFloat:
1238 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 XMM0);
1241 break;
1242
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001245 }
1246 }
1247 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001248}
1249
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001250Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1251 switch (type) {
1252 case Primitive::kPrimBoolean:
1253 case Primitive::kPrimByte:
1254 case Primitive::kPrimChar:
1255 case Primitive::kPrimShort:
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimNot:
1258 case Primitive::kPrimLong:
1259 return Location::RegisterLocation(RAX);
1260
1261 case Primitive::kPrimVoid:
1262 return Location::NoLocation();
1263
1264 case Primitive::kPrimDouble:
1265 case Primitive::kPrimFloat:
1266 return Location::FpuRegisterLocation(XMM0);
1267 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001268
1269 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001270}
1271
1272Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1273 return Location::RegisterLocation(kMethodRegisterArgument);
1274}
1275
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001276Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277 switch (type) {
1278 case Primitive::kPrimBoolean:
1279 case Primitive::kPrimByte:
1280 case Primitive::kPrimChar:
1281 case Primitive::kPrimShort:
1282 case Primitive::kPrimInt:
1283 case Primitive::kPrimNot: {
1284 uint32_t index = gp_index_++;
1285 stack_index_++;
1286 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001287 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288 } else {
1289 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1290 }
1291 }
1292
1293 case Primitive::kPrimLong: {
1294 uint32_t index = gp_index_;
1295 stack_index_ += 2;
1296 if (index < calling_convention.GetNumberOfRegisters()) {
1297 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001298 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001299 } else {
1300 gp_index_ += 2;
1301 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1302 }
1303 }
1304
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001305 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001306 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001307 stack_index_++;
1308 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001309 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001310 } else {
1311 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1312 }
1313 }
1314
1315 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001316 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001317 stack_index_ += 2;
1318 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001319 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001320 } else {
1321 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1322 }
1323 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001324
1325 case Primitive::kPrimVoid:
1326 LOG(FATAL) << "Unexpected parameter type " << type;
1327 break;
1328 }
1329 return Location();
1330}
1331
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001332void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001333 // When we do not run baseline, explicit clinit checks triggered by static
1334 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1335 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001336
Mark Mendellfb8d2792015-03-31 22:16:59 -04001337 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001338 if (intrinsic.TryDispatch(invoke)) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001339 LocationSummary* locations = invoke->GetLocations();
1340 if (locations->CanCall()) {
1341 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::RequiresRegister());
1342 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001343 return;
1344 }
1345
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001346 HandleInvoke(invoke);
1347}
1348
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001349static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1350 if (invoke->GetLocations()->Intrinsified()) {
1351 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1352 intrinsic.Dispatch(invoke);
1353 return true;
1354 }
1355 return false;
1356}
1357
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001358void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001359 // When we do not run baseline, explicit clinit checks triggered by static
1360 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1361 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001362
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001363 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1364 return;
1365 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001366
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001367 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001368 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001369 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001370 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001371}
1372
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001373void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001374 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001375 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001376}
1377
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001378void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001379 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001380 if (intrinsic.TryDispatch(invoke)) {
1381 return;
1382 }
1383
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001384 HandleInvoke(invoke);
1385}
1386
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001387void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001388 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1389 return;
1390 }
1391
Roland Levillain271ab9c2014-11-27 15:23:57 +00001392 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001393 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1394 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001395 LocationSummary* locations = invoke->GetLocations();
1396 Location receiver = locations->InAt(0);
1397 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1398 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001399 DCHECK(receiver.IsRegister());
1400 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001401 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001402 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001403 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001404 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001405 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001406 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001407
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001408 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001409 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001410}
1411
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001412void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1413 HandleInvoke(invoke);
1414 // Add the hidden argument.
1415 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1416}
1417
1418void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1419 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001420 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001421 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1422 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001423 LocationSummary* locations = invoke->GetLocations();
1424 Location receiver = locations->InAt(0);
1425 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1426
1427 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001428 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1429 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001430
1431 // temp = object->GetClass();
1432 if (receiver.IsStackSlot()) {
1433 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1434 __ movl(temp, Address(temp, class_offset));
1435 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001436 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001437 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001438 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001439 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001440 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001441 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001442 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001443 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001444
1445 DCHECK(!codegen_->IsLeafMethod());
1446 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1447}
1448
Roland Levillain88cb1752014-10-20 16:36:47 +01001449void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1450 LocationSummary* locations =
1451 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1452 switch (neg->GetResultType()) {
1453 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001454 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001455 locations->SetInAt(0, Location::RequiresRegister());
1456 locations->SetOut(Location::SameAsFirstInput());
1457 break;
1458
Roland Levillain88cb1752014-10-20 16:36:47 +01001459 case Primitive::kPrimFloat:
1460 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001461 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001462 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001463 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001464 break;
1465
1466 default:
1467 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1468 }
1469}
1470
1471void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1472 LocationSummary* locations = neg->GetLocations();
1473 Location out = locations->Out();
1474 Location in = locations->InAt(0);
1475 switch (neg->GetResultType()) {
1476 case Primitive::kPrimInt:
1477 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001478 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001479 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001480 break;
1481
1482 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001483 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001484 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001485 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001486 break;
1487
Roland Levillain5368c212014-11-27 15:03:41 +00001488 case Primitive::kPrimFloat: {
1489 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001490 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001491 // Implement float negation with an exclusive or with value
1492 // 0x80000000 (mask for bit 31, representing the sign of a
1493 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001494 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001495 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001496 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001497 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001498
Roland Levillain5368c212014-11-27 15:03:41 +00001499 case Primitive::kPrimDouble: {
1500 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001501 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001502 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001503 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001504 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001505 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001506 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001507 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001508 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001509
1510 default:
1511 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1512 }
1513}
1514
Roland Levillaindff1f282014-11-05 14:15:05 +00001515void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1516 LocationSummary* locations =
1517 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1518 Primitive::Type result_type = conversion->GetResultType();
1519 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001520 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001521
David Brazdilb2bd1c52015-03-25 11:17:37 +00001522 // The Java language does not allow treating boolean as an integral type but
1523 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001524
Roland Levillaindff1f282014-11-05 14:15:05 +00001525 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001526 case Primitive::kPrimByte:
1527 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001528 case Primitive::kPrimBoolean:
1529 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001530 case Primitive::kPrimShort:
1531 case Primitive::kPrimInt:
1532 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001533 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001534 locations->SetInAt(0, Location::Any());
1535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 }
1542 break;
1543
Roland Levillain01a8d712014-11-14 16:27:39 +00001544 case Primitive::kPrimShort:
1545 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001546 case Primitive::kPrimBoolean:
1547 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001548 case Primitive::kPrimByte:
1549 case Primitive::kPrimInt:
1550 case Primitive::kPrimChar:
1551 // Processing a Dex `int-to-short' instruction.
1552 locations->SetInAt(0, Location::Any());
1553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1554 break;
1555
1556 default:
1557 LOG(FATAL) << "Unexpected type conversion from " << input_type
1558 << " to " << result_type;
1559 }
1560 break;
1561
Roland Levillain946e1432014-11-11 17:35:19 +00001562 case Primitive::kPrimInt:
1563 switch (input_type) {
1564 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001565 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001566 locations->SetInAt(0, Location::Any());
1567 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1568 break;
1569
1570 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001571 // Processing a Dex `float-to-int' instruction.
1572 locations->SetInAt(0, Location::RequiresFpuRegister());
1573 locations->SetOut(Location::RequiresRegister());
1574 locations->AddTemp(Location::RequiresFpuRegister());
1575 break;
1576
Roland Levillain946e1432014-11-11 17:35:19 +00001577 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001578 // Processing a Dex `double-to-int' instruction.
1579 locations->SetInAt(0, Location::RequiresFpuRegister());
1580 locations->SetOut(Location::RequiresRegister());
1581 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001582 break;
1583
1584 default:
1585 LOG(FATAL) << "Unexpected type conversion from " << input_type
1586 << " to " << result_type;
1587 }
1588 break;
1589
Roland Levillaindff1f282014-11-05 14:15:05 +00001590 case Primitive::kPrimLong:
1591 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001592 case Primitive::kPrimBoolean:
1593 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001594 case Primitive::kPrimByte:
1595 case Primitive::kPrimShort:
1596 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001597 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001598 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001599 // TODO: We would benefit from a (to-be-implemented)
1600 // Location::RegisterOrStackSlot requirement for this input.
1601 locations->SetInAt(0, Location::RequiresRegister());
1602 locations->SetOut(Location::RequiresRegister());
1603 break;
1604
1605 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001606 // Processing a Dex `float-to-long' instruction.
1607 locations->SetInAt(0, Location::RequiresFpuRegister());
1608 locations->SetOut(Location::RequiresRegister());
1609 locations->AddTemp(Location::RequiresFpuRegister());
1610 break;
1611
Roland Levillaindff1f282014-11-05 14:15:05 +00001612 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001613 // Processing a Dex `double-to-long' instruction.
1614 locations->SetInAt(0, Location::RequiresFpuRegister());
1615 locations->SetOut(Location::RequiresRegister());
1616 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001617 break;
1618
1619 default:
1620 LOG(FATAL) << "Unexpected type conversion from " << input_type
1621 << " to " << result_type;
1622 }
1623 break;
1624
Roland Levillain981e4542014-11-14 11:47:14 +00001625 case Primitive::kPrimChar:
1626 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001627 case Primitive::kPrimBoolean:
1628 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001629 case Primitive::kPrimByte:
1630 case Primitive::kPrimShort:
1631 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001632 // Processing a Dex `int-to-char' instruction.
1633 locations->SetInAt(0, Location::Any());
1634 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1635 break;
1636
1637 default:
1638 LOG(FATAL) << "Unexpected type conversion from " << input_type
1639 << " to " << result_type;
1640 }
1641 break;
1642
Roland Levillaindff1f282014-11-05 14:15:05 +00001643 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001644 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001645 case Primitive::kPrimBoolean:
1646 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001647 case Primitive::kPrimByte:
1648 case Primitive::kPrimShort:
1649 case Primitive::kPrimInt:
1650 case Primitive::kPrimChar:
1651 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001652 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001653 locations->SetOut(Location::RequiresFpuRegister());
1654 break;
1655
1656 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001657 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001658 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001659 locations->SetOut(Location::RequiresFpuRegister());
1660 break;
1661
Roland Levillaincff13742014-11-17 14:32:17 +00001662 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001663 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001664 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001665 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001666 break;
1667
1668 default:
1669 LOG(FATAL) << "Unexpected type conversion from " << input_type
1670 << " to " << result_type;
1671 };
1672 break;
1673
Roland Levillaindff1f282014-11-05 14:15:05 +00001674 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001675 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001676 case Primitive::kPrimBoolean:
1677 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001678 case Primitive::kPrimByte:
1679 case Primitive::kPrimShort:
1680 case Primitive::kPrimInt:
1681 case Primitive::kPrimChar:
1682 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001683 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001684 locations->SetOut(Location::RequiresFpuRegister());
1685 break;
1686
1687 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001688 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001689 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001690 locations->SetOut(Location::RequiresFpuRegister());
1691 break;
1692
Roland Levillaincff13742014-11-17 14:32:17 +00001693 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001694 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001695 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001696 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001697 break;
1698
1699 default:
1700 LOG(FATAL) << "Unexpected type conversion from " << input_type
1701 << " to " << result_type;
1702 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001703 break;
1704
1705 default:
1706 LOG(FATAL) << "Unexpected type conversion from " << input_type
1707 << " to " << result_type;
1708 }
1709}
1710
1711void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1712 LocationSummary* locations = conversion->GetLocations();
1713 Location out = locations->Out();
1714 Location in = locations->InAt(0);
1715 Primitive::Type result_type = conversion->GetResultType();
1716 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001717 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001718 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001719 case Primitive::kPrimByte:
1720 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001721 case Primitive::kPrimBoolean:
1722 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001723 case Primitive::kPrimShort:
1724 case Primitive::kPrimInt:
1725 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001726 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001727 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001729 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001730 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001731 Address(CpuRegister(RSP), in.GetStackIndex()));
1732 } else {
1733 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001734 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001735 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1736 }
1737 break;
1738
1739 default:
1740 LOG(FATAL) << "Unexpected type conversion from " << input_type
1741 << " to " << result_type;
1742 }
1743 break;
1744
Roland Levillain01a8d712014-11-14 16:27:39 +00001745 case Primitive::kPrimShort:
1746 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001747 case Primitive::kPrimBoolean:
1748 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001749 case Primitive::kPrimByte:
1750 case Primitive::kPrimInt:
1751 case Primitive::kPrimChar:
1752 // Processing a Dex `int-to-short' instruction.
1753 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001754 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001755 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001757 Address(CpuRegister(RSP), in.GetStackIndex()));
1758 } else {
1759 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001760 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001761 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1762 }
1763 break;
1764
1765 default:
1766 LOG(FATAL) << "Unexpected type conversion from " << input_type
1767 << " to " << result_type;
1768 }
1769 break;
1770
Roland Levillain946e1432014-11-11 17:35:19 +00001771 case Primitive::kPrimInt:
1772 switch (input_type) {
1773 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001774 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001775 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001777 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001778 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001779 Address(CpuRegister(RSP), in.GetStackIndex()));
1780 } else {
1781 DCHECK(in.IsConstant());
1782 DCHECK(in.GetConstant()->IsLongConstant());
1783 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001784 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001785 }
1786 break;
1787
Roland Levillain3f8f9362014-12-02 17:45:01 +00001788 case Primitive::kPrimFloat: {
1789 // Processing a Dex `float-to-int' instruction.
1790 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1791 CpuRegister output = out.AsRegister<CpuRegister>();
1792 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1793 Label done, nan;
1794
1795 __ movl(output, Immediate(kPrimIntMax));
1796 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001797 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001798 // if input >= temp goto done
1799 __ comiss(input, temp);
1800 __ j(kAboveEqual, &done);
1801 // if input == NaN goto nan
1802 __ j(kUnordered, &nan);
1803 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001804 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001805 __ jmp(&done);
1806 __ Bind(&nan);
1807 // output = 0
1808 __ xorl(output, output);
1809 __ Bind(&done);
1810 break;
1811 }
1812
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001813 case Primitive::kPrimDouble: {
1814 // Processing a Dex `double-to-int' instruction.
1815 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1816 CpuRegister output = out.AsRegister<CpuRegister>();
1817 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1818 Label done, nan;
1819
1820 __ movl(output, Immediate(kPrimIntMax));
1821 // temp = int-to-double(output)
1822 __ cvtsi2sd(temp, output);
1823 // if input >= temp goto done
1824 __ comisd(input, temp);
1825 __ j(kAboveEqual, &done);
1826 // if input == NaN goto nan
1827 __ j(kUnordered, &nan);
1828 // output = double-to-int-truncate(input)
1829 __ cvttsd2si(output, input);
1830 __ jmp(&done);
1831 __ Bind(&nan);
1832 // output = 0
1833 __ xorl(output, output);
1834 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001835 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001836 }
Roland Levillain946e1432014-11-11 17:35:19 +00001837
1838 default:
1839 LOG(FATAL) << "Unexpected type conversion from " << input_type
1840 << " to " << result_type;
1841 }
1842 break;
1843
Roland Levillaindff1f282014-11-05 14:15:05 +00001844 case Primitive::kPrimLong:
1845 switch (input_type) {
1846 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001847 case Primitive::kPrimBoolean:
1848 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001849 case Primitive::kPrimByte:
1850 case Primitive::kPrimShort:
1851 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001852 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001853 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001854 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001855 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001856 break;
1857
Roland Levillain624279f2014-12-04 11:54:28 +00001858 case Primitive::kPrimFloat: {
1859 // Processing a Dex `float-to-long' instruction.
1860 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1861 CpuRegister output = out.AsRegister<CpuRegister>();
1862 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1863 Label done, nan;
1864
Mark Mendell92e83bf2015-05-07 11:25:03 -04001865 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001866 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001867 __ cvtsi2ss(temp, output, true);
1868 // if input >= temp goto done
1869 __ comiss(input, temp);
1870 __ j(kAboveEqual, &done);
1871 // if input == NaN goto nan
1872 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001873 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001874 __ cvttss2si(output, input, true);
1875 __ jmp(&done);
1876 __ Bind(&nan);
1877 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001878 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00001879 __ Bind(&done);
1880 break;
1881 }
1882
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001883 case Primitive::kPrimDouble: {
1884 // Processing a Dex `double-to-long' instruction.
1885 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1886 CpuRegister output = out.AsRegister<CpuRegister>();
1887 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1888 Label done, nan;
1889
Mark Mendell92e83bf2015-05-07 11:25:03 -04001890 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001891 // temp = long-to-double(output)
1892 __ cvtsi2sd(temp, output, true);
1893 // if input >= temp goto done
1894 __ comisd(input, temp);
1895 __ j(kAboveEqual, &done);
1896 // if input == NaN goto nan
1897 __ j(kUnordered, &nan);
1898 // output = double-to-long-truncate(input)
1899 __ cvttsd2si(output, input, true);
1900 __ jmp(&done);
1901 __ Bind(&nan);
1902 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001903 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001904 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001905 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001906 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001907
1908 default:
1909 LOG(FATAL) << "Unexpected type conversion from " << input_type
1910 << " to " << result_type;
1911 }
1912 break;
1913
Roland Levillain981e4542014-11-14 11:47:14 +00001914 case Primitive::kPrimChar:
1915 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001916 case Primitive::kPrimBoolean:
1917 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001918 case Primitive::kPrimByte:
1919 case Primitive::kPrimShort:
1920 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001921 // Processing a Dex `int-to-char' instruction.
1922 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001924 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001925 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001926 Address(CpuRegister(RSP), in.GetStackIndex()));
1927 } else {
1928 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001929 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001930 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1931 }
1932 break;
1933
1934 default:
1935 LOG(FATAL) << "Unexpected type conversion from " << input_type
1936 << " to " << result_type;
1937 }
1938 break;
1939
Roland Levillaindff1f282014-11-05 14:15:05 +00001940 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001941 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001942 case Primitive::kPrimBoolean:
1943 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001944 case Primitive::kPrimByte:
1945 case Primitive::kPrimShort:
1946 case Primitive::kPrimInt:
1947 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001948 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001949 if (in.IsRegister()) {
1950 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1951 } else if (in.IsConstant()) {
1952 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
1953 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1954 if (v == 0) {
1955 __ xorps(dest, dest);
1956 } else {
1957 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1958 }
1959 } else {
1960 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1961 Address(CpuRegister(RSP), in.GetStackIndex()), false);
1962 }
Roland Levillaincff13742014-11-17 14:32:17 +00001963 break;
1964
1965 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001966 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001967 if (in.IsRegister()) {
1968 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1969 } else if (in.IsConstant()) {
1970 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
1971 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1972 if (v == 0) {
1973 __ xorps(dest, dest);
1974 } else {
1975 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1976 }
1977 } else {
1978 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1979 Address(CpuRegister(RSP), in.GetStackIndex()), true);
1980 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001981 break;
1982
Roland Levillaincff13742014-11-17 14:32:17 +00001983 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001984 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001985 if (in.IsFpuRegister()) {
1986 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
1987 } else if (in.IsConstant()) {
1988 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
1989 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1990 if (bit_cast<int64_t, double>(v) == 0) {
1991 __ xorps(dest, dest);
1992 } else {
1993 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1994 }
1995 } else {
1996 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
1997 Address(CpuRegister(RSP), in.GetStackIndex()));
1998 }
Roland Levillaincff13742014-11-17 14:32:17 +00001999 break;
2000
2001 default:
2002 LOG(FATAL) << "Unexpected type conversion from " << input_type
2003 << " to " << result_type;
2004 };
2005 break;
2006
Roland Levillaindff1f282014-11-05 14:15:05 +00002007 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002008 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002009 case Primitive::kPrimBoolean:
2010 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002011 case Primitive::kPrimByte:
2012 case Primitive::kPrimShort:
2013 case Primitive::kPrimInt:
2014 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002015 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002016 if (in.IsRegister()) {
2017 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2018 } else if (in.IsConstant()) {
2019 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2020 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2021 if (v == 0) {
2022 __ xorpd(dest, dest);
2023 } else {
2024 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2025 }
2026 } else {
2027 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2028 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2029 }
Roland Levillaincff13742014-11-17 14:32:17 +00002030 break;
2031
2032 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002033 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002034 if (in.IsRegister()) {
2035 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2036 } else if (in.IsConstant()) {
2037 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2038 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2039 if (v == 0) {
2040 __ xorpd(dest, dest);
2041 } else {
2042 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2043 }
2044 } else {
2045 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2046 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2047 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002048 break;
2049
Roland Levillaincff13742014-11-17 14:32:17 +00002050 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002051 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002052 if (in.IsFpuRegister()) {
2053 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2054 } else if (in.IsConstant()) {
2055 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2056 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2057 if (bit_cast<int32_t, float>(v) == 0) {
2058 __ xorpd(dest, dest);
2059 } else {
2060 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2061 }
2062 } else {
2063 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2064 Address(CpuRegister(RSP), in.GetStackIndex()));
2065 }
Roland Levillaincff13742014-11-17 14:32:17 +00002066 break;
2067
2068 default:
2069 LOG(FATAL) << "Unexpected type conversion from " << input_type
2070 << " to " << result_type;
2071 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002072 break;
2073
2074 default:
2075 LOG(FATAL) << "Unexpected type conversion from " << input_type
2076 << " to " << result_type;
2077 }
2078}
2079
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002080void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002081 LocationSummary* locations =
2082 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002083 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002084 case Primitive::kPrimInt: {
2085 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002086 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2087 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002088 break;
2089 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002090
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002091 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002092 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002093 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002094 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002095 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002096 break;
2097 }
2098
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002099 case Primitive::kPrimDouble:
2100 case Primitive::kPrimFloat: {
2101 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002102 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002103 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002105 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002106
2107 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002108 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002109 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002110}
2111
2112void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2113 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002114 Location first = locations->InAt(0);
2115 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002116 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002117
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002118 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002119 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002120 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002121 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2122 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002123 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2124 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002125 } else {
2126 __ leal(out.AsRegister<CpuRegister>(), Address(
2127 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2128 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002129 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002130 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2131 __ addl(out.AsRegister<CpuRegister>(),
2132 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2133 } else {
2134 __ leal(out.AsRegister<CpuRegister>(), Address(
2135 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2136 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002137 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002138 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002140 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002141 break;
2142 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002144 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002145 if (second.IsRegister()) {
2146 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2147 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002148 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2149 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002150 } else {
2151 __ leaq(out.AsRegister<CpuRegister>(), Address(
2152 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2153 }
2154 } else {
2155 DCHECK(second.IsConstant());
2156 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2157 int32_t int32_value = Low32Bits(value);
2158 DCHECK_EQ(int32_value, value);
2159 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2160 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2161 } else {
2162 __ leaq(out.AsRegister<CpuRegister>(), Address(
2163 first.AsRegister<CpuRegister>(), int32_value));
2164 }
2165 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002166 break;
2167 }
2168
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002169 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002170 if (second.IsFpuRegister()) {
2171 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2172 } else if (second.IsConstant()) {
2173 __ addss(first.AsFpuRegister<XmmRegister>(),
2174 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2175 } else {
2176 DCHECK(second.IsStackSlot());
2177 __ addss(first.AsFpuRegister<XmmRegister>(),
2178 Address(CpuRegister(RSP), second.GetStackIndex()));
2179 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002180 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002181 }
2182
2183 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002184 if (second.IsFpuRegister()) {
2185 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2186 } else if (second.IsConstant()) {
2187 __ addsd(first.AsFpuRegister<XmmRegister>(),
2188 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2189 } else {
2190 DCHECK(second.IsDoubleStackSlot());
2191 __ addsd(first.AsFpuRegister<XmmRegister>(),
2192 Address(CpuRegister(RSP), second.GetStackIndex()));
2193 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002194 break;
2195 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002196
2197 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002198 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199 }
2200}
2201
2202void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002203 LocationSummary* locations =
2204 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002206 case Primitive::kPrimInt: {
2207 locations->SetInAt(0, Location::RequiresRegister());
2208 locations->SetInAt(1, Location::Any());
2209 locations->SetOut(Location::SameAsFirstInput());
2210 break;
2211 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002213 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002214 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002215 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216 break;
2217 }
Calin Juravle11351682014-10-23 15:38:15 +01002218 case Primitive::kPrimFloat:
2219 case Primitive::kPrimDouble: {
2220 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002221 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002222 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002223 break;
Calin Juravle11351682014-10-23 15:38:15 +01002224 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002225 default:
Calin Juravle11351682014-10-23 15:38:15 +01002226 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002227 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002228}
2229
2230void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2231 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002232 Location first = locations->InAt(0);
2233 Location second = locations->InAt(1);
2234 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002235 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002236 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002237 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002238 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002239 } else if (second.IsConstant()) {
2240 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002242 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002243 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002244 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002245 break;
2246 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002247 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002248 if (second.IsConstant()) {
2249 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2250 DCHECK(IsInt<32>(value));
2251 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2252 } else {
2253 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2254 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002255 break;
2256 }
2257
Calin Juravle11351682014-10-23 15:38:15 +01002258 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002259 if (second.IsFpuRegister()) {
2260 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2261 } else if (second.IsConstant()) {
2262 __ subss(first.AsFpuRegister<XmmRegister>(),
2263 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2264 } else {
2265 DCHECK(second.IsStackSlot());
2266 __ subss(first.AsFpuRegister<XmmRegister>(),
2267 Address(CpuRegister(RSP), second.GetStackIndex()));
2268 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002269 break;
Calin Juravle11351682014-10-23 15:38:15 +01002270 }
2271
2272 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002273 if (second.IsFpuRegister()) {
2274 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2275 } else if (second.IsConstant()) {
2276 __ subsd(first.AsFpuRegister<XmmRegister>(),
2277 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2278 } else {
2279 DCHECK(second.IsDoubleStackSlot());
2280 __ subsd(first.AsFpuRegister<XmmRegister>(),
2281 Address(CpuRegister(RSP), second.GetStackIndex()));
2282 }
Calin Juravle11351682014-10-23 15:38:15 +01002283 break;
2284 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002285
2286 default:
Calin Juravle11351682014-10-23 15:38:15 +01002287 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002288 }
2289}
2290
Calin Juravle34bacdf2014-10-07 20:23:36 +01002291void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2292 LocationSummary* locations =
2293 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2294 switch (mul->GetResultType()) {
2295 case Primitive::kPrimInt: {
2296 locations->SetInAt(0, Location::RequiresRegister());
2297 locations->SetInAt(1, Location::Any());
2298 locations->SetOut(Location::SameAsFirstInput());
2299 break;
2300 }
2301 case Primitive::kPrimLong: {
2302 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002303 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2304 if (locations->InAt(1).IsConstant()) {
2305 // Can use 3 operand multiply.
2306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2307 } else {
2308 locations->SetOut(Location::SameAsFirstInput());
2309 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002310 break;
2311 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002312 case Primitive::kPrimFloat:
2313 case Primitive::kPrimDouble: {
2314 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002315 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002316 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002317 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002318 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002319
2320 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002321 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002322 }
2323}
2324
2325void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2326 LocationSummary* locations = mul->GetLocations();
2327 Location first = locations->InAt(0);
2328 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002329 switch (mul->GetResultType()) {
2330 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002331 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002332 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002333 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002334 } else if (second.IsConstant()) {
2335 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002336 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002337 } else {
2338 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002339 __ imull(first.AsRegister<CpuRegister>(),
2340 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002341 }
2342 break;
2343 }
2344 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002345 if (second.IsConstant()) {
2346 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2347 DCHECK(IsInt<32>(value));
2348 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2349 first.AsRegister<CpuRegister>(),
2350 Immediate(static_cast<int32_t>(value)));
2351 } else {
2352 DCHECK(first.Equals(locations->Out()));
2353 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2354 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002355 break;
2356 }
2357
Calin Juravleb5bfa962014-10-21 18:02:24 +01002358 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002359 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002360 if (second.IsFpuRegister()) {
2361 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2362 } else if (second.IsConstant()) {
2363 __ mulss(first.AsFpuRegister<XmmRegister>(),
2364 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2365 } else {
2366 DCHECK(second.IsStackSlot());
2367 __ mulss(first.AsFpuRegister<XmmRegister>(),
2368 Address(CpuRegister(RSP), second.GetStackIndex()));
2369 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002370 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002371 }
2372
2373 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002374 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002375 if (second.IsFpuRegister()) {
2376 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2377 } else if (second.IsConstant()) {
2378 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2379 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2380 } else {
2381 DCHECK(second.IsDoubleStackSlot());
2382 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2383 Address(CpuRegister(RSP), second.GetStackIndex()));
2384 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002385 break;
2386 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002387
2388 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002389 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002390 }
2391}
2392
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002393void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2394 uint32_t stack_adjustment, bool is_float) {
2395 if (source.IsStackSlot()) {
2396 DCHECK(is_float);
2397 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2398 } else if (source.IsDoubleStackSlot()) {
2399 DCHECK(!is_float);
2400 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2401 } else {
2402 // Write the value to the temporary location on the stack and load to FP stack.
2403 if (is_float) {
2404 Location stack_temp = Location::StackSlot(temp_offset);
2405 codegen_->Move(stack_temp, source);
2406 __ flds(Address(CpuRegister(RSP), temp_offset));
2407 } else {
2408 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2409 codegen_->Move(stack_temp, source);
2410 __ fldl(Address(CpuRegister(RSP), temp_offset));
2411 }
2412 }
2413}
2414
2415void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2416 Primitive::Type type = rem->GetResultType();
2417 bool is_float = type == Primitive::kPrimFloat;
2418 size_t elem_size = Primitive::ComponentSize(type);
2419 LocationSummary* locations = rem->GetLocations();
2420 Location first = locations->InAt(0);
2421 Location second = locations->InAt(1);
2422 Location out = locations->Out();
2423
2424 // Create stack space for 2 elements.
2425 // TODO: enhance register allocator to ask for stack temporaries.
2426 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2427
2428 // Load the values to the FP stack in reverse order, using temporaries if needed.
2429 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2430 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2431
2432 // Loop doing FPREM until we stabilize.
2433 Label retry;
2434 __ Bind(&retry);
2435 __ fprem();
2436
2437 // Move FP status to AX.
2438 __ fstsw();
2439
2440 // And see if the argument reduction is complete. This is signaled by the
2441 // C2 FPU flag bit set to 0.
2442 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2443 __ j(kNotEqual, &retry);
2444
2445 // We have settled on the final value. Retrieve it into an XMM register.
2446 // Store FP top of stack to real stack.
2447 if (is_float) {
2448 __ fsts(Address(CpuRegister(RSP), 0));
2449 } else {
2450 __ fstl(Address(CpuRegister(RSP), 0));
2451 }
2452
2453 // Pop the 2 items from the FP stack.
2454 __ fucompp();
2455
2456 // Load the value from the stack into an XMM register.
2457 DCHECK(out.IsFpuRegister()) << out;
2458 if (is_float) {
2459 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2460 } else {
2461 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2462 }
2463
2464 // And remove the temporary stack space we allocated.
2465 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2466}
2467
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002468void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2469 DCHECK(instruction->IsDiv() || instruction->IsRem());
2470
2471 LocationSummary* locations = instruction->GetLocations();
2472 Location second = locations->InAt(1);
2473 DCHECK(second.IsConstant());
2474
2475 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2476 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002477 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002478
2479 DCHECK(imm == 1 || imm == -1);
2480
2481 switch (instruction->GetResultType()) {
2482 case Primitive::kPrimInt: {
2483 if (instruction->IsRem()) {
2484 __ xorl(output_register, output_register);
2485 } else {
2486 __ movl(output_register, input_register);
2487 if (imm == -1) {
2488 __ negl(output_register);
2489 }
2490 }
2491 break;
2492 }
2493
2494 case Primitive::kPrimLong: {
2495 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002496 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002497 } else {
2498 __ movq(output_register, input_register);
2499 if (imm == -1) {
2500 __ negq(output_register);
2501 }
2502 }
2503 break;
2504 }
2505
2506 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002507 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002508 }
2509}
2510
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002511void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002512 LocationSummary* locations = instruction->GetLocations();
2513 Location second = locations->InAt(1);
2514
2515 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2516 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2517
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002518 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002519
2520 DCHECK(IsPowerOfTwo(std::abs(imm)));
2521
2522 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2523
2524 if (instruction->GetResultType() == Primitive::kPrimInt) {
2525 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2526 __ testl(numerator, numerator);
2527 __ cmov(kGreaterEqual, tmp, numerator);
2528 int shift = CTZ(imm);
2529 __ sarl(tmp, Immediate(shift));
2530
2531 if (imm < 0) {
2532 __ negl(tmp);
2533 }
2534
2535 __ movl(output_register, tmp);
2536 } else {
2537 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2538 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2539
Mark Mendell92e83bf2015-05-07 11:25:03 -04002540 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002541 __ addq(rdx, numerator);
2542 __ testq(numerator, numerator);
2543 __ cmov(kGreaterEqual, rdx, numerator);
2544 int shift = CTZ(imm);
2545 __ sarq(rdx, Immediate(shift));
2546
2547 if (imm < 0) {
2548 __ negq(rdx);
2549 }
2550
2551 __ movq(output_register, rdx);
2552 }
2553}
2554
2555void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2556 DCHECK(instruction->IsDiv() || instruction->IsRem());
2557
2558 LocationSummary* locations = instruction->GetLocations();
2559 Location second = locations->InAt(1);
2560
2561 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2562 : locations->GetTemp(0).AsRegister<CpuRegister>();
2563 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2564 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2565 : locations->Out().AsRegister<CpuRegister>();
2566 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2567
2568 DCHECK_EQ(RAX, eax.AsRegister());
2569 DCHECK_EQ(RDX, edx.AsRegister());
2570 if (instruction->IsDiv()) {
2571 DCHECK_EQ(RAX, out.AsRegister());
2572 } else {
2573 DCHECK_EQ(RDX, out.AsRegister());
2574 }
2575
2576 int64_t magic;
2577 int shift;
2578
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002579 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002580 if (instruction->GetResultType() == Primitive::kPrimInt) {
2581 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2582
2583 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2584
2585 __ movl(numerator, eax);
2586
2587 Label no_div;
2588 Label end;
2589 __ testl(eax, eax);
2590 __ j(kNotEqual, &no_div);
2591
2592 __ xorl(out, out);
2593 __ jmp(&end);
2594
2595 __ Bind(&no_div);
2596
2597 __ movl(eax, Immediate(magic));
2598 __ imull(numerator);
2599
2600 if (imm > 0 && magic < 0) {
2601 __ addl(edx, numerator);
2602 } else if (imm < 0 && magic > 0) {
2603 __ subl(edx, numerator);
2604 }
2605
2606 if (shift != 0) {
2607 __ sarl(edx, Immediate(shift));
2608 }
2609
2610 __ movl(eax, edx);
2611 __ shrl(edx, Immediate(31));
2612 __ addl(edx, eax);
2613
2614 if (instruction->IsRem()) {
2615 __ movl(eax, numerator);
2616 __ imull(edx, Immediate(imm));
2617 __ subl(eax, edx);
2618 __ movl(edx, eax);
2619 } else {
2620 __ movl(eax, edx);
2621 }
2622 __ Bind(&end);
2623 } else {
2624 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2625
2626 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2627
2628 CpuRegister rax = eax;
2629 CpuRegister rdx = edx;
2630
2631 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2632
2633 // Save the numerator.
2634 __ movq(numerator, rax);
2635
2636 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002637 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002638
2639 // RDX:RAX = magic * numerator
2640 __ imulq(numerator);
2641
2642 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002643 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002644 __ addq(rdx, numerator);
2645 } else if (imm < 0 && magic > 0) {
2646 // RDX -= numerator
2647 __ subq(rdx, numerator);
2648 }
2649
2650 // Shift if needed.
2651 if (shift != 0) {
2652 __ sarq(rdx, Immediate(shift));
2653 }
2654
2655 // RDX += 1 if RDX < 0
2656 __ movq(rax, rdx);
2657 __ shrq(rdx, Immediate(63));
2658 __ addq(rdx, rax);
2659
2660 if (instruction->IsRem()) {
2661 __ movq(rax, numerator);
2662
2663 if (IsInt<32>(imm)) {
2664 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2665 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002666 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002667 }
2668
2669 __ subq(rax, rdx);
2670 __ movq(rdx, rax);
2671 } else {
2672 __ movq(rax, rdx);
2673 }
2674 }
2675}
2676
Calin Juravlebacfec32014-11-14 15:54:36 +00002677void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2678 DCHECK(instruction->IsDiv() || instruction->IsRem());
2679 Primitive::Type type = instruction->GetResultType();
2680 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2681
2682 bool is_div = instruction->IsDiv();
2683 LocationSummary* locations = instruction->GetLocations();
2684
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002685 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2686 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002687
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002689 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002690
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002691 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002692 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002693
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002694 if (imm == 0) {
2695 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2696 } else if (imm == 1 || imm == -1) {
2697 DivRemOneOrMinusOne(instruction);
2698 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002699 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002700 } else {
2701 DCHECK(imm <= -2 || imm >= 2);
2702 GenerateDivRemWithAnyConstant(instruction);
2703 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002704 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002705 SlowPathCodeX86_64* slow_path =
2706 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2707 out.AsRegister(), type, is_div);
2708 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002709
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002710 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2711 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2712 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2713 // so it's safe to just use negl instead of more complex comparisons.
2714 if (type == Primitive::kPrimInt) {
2715 __ cmpl(second_reg, Immediate(-1));
2716 __ j(kEqual, slow_path->GetEntryLabel());
2717 // edx:eax <- sign-extended of eax
2718 __ cdq();
2719 // eax = quotient, edx = remainder
2720 __ idivl(second_reg);
2721 } else {
2722 __ cmpq(second_reg, Immediate(-1));
2723 __ j(kEqual, slow_path->GetEntryLabel());
2724 // rdx:rax <- sign-extended of rax
2725 __ cqo();
2726 // rax = quotient, rdx = remainder
2727 __ idivq(second_reg);
2728 }
2729 __ Bind(slow_path->GetExitLabel());
2730 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002731}
2732
Calin Juravle7c4954d2014-10-28 16:57:40 +00002733void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2734 LocationSummary* locations =
2735 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2736 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002737 case Primitive::kPrimInt:
2738 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002739 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002740 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002741 locations->SetOut(Location::SameAsFirstInput());
2742 // Intel uses edx:eax as the dividend.
2743 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002744 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2745 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2746 // output and request another temp.
2747 if (div->InputAt(1)->IsConstant()) {
2748 locations->AddTemp(Location::RequiresRegister());
2749 }
Calin Juravled0d48522014-11-04 16:40:20 +00002750 break;
2751 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002752
Calin Juravle7c4954d2014-10-28 16:57:40 +00002753 case Primitive::kPrimFloat:
2754 case Primitive::kPrimDouble: {
2755 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002756 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002757 locations->SetOut(Location::SameAsFirstInput());
2758 break;
2759 }
2760
2761 default:
2762 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2763 }
2764}
2765
2766void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2767 LocationSummary* locations = div->GetLocations();
2768 Location first = locations->InAt(0);
2769 Location second = locations->InAt(1);
2770 DCHECK(first.Equals(locations->Out()));
2771
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002772 Primitive::Type type = div->GetResultType();
2773 switch (type) {
2774 case Primitive::kPrimInt:
2775 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002776 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002777 break;
2778 }
2779
Calin Juravle7c4954d2014-10-28 16:57:40 +00002780 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002781 if (second.IsFpuRegister()) {
2782 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2783 } else if (second.IsConstant()) {
2784 __ divss(first.AsFpuRegister<XmmRegister>(),
2785 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2786 } else {
2787 DCHECK(second.IsStackSlot());
2788 __ divss(first.AsFpuRegister<XmmRegister>(),
2789 Address(CpuRegister(RSP), second.GetStackIndex()));
2790 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002791 break;
2792 }
2793
2794 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002795 if (second.IsFpuRegister()) {
2796 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2797 } else if (second.IsConstant()) {
2798 __ divsd(first.AsFpuRegister<XmmRegister>(),
2799 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2800 } else {
2801 DCHECK(second.IsDoubleStackSlot());
2802 __ divsd(first.AsFpuRegister<XmmRegister>(),
2803 Address(CpuRegister(RSP), second.GetStackIndex()));
2804 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002805 break;
2806 }
2807
2808 default:
2809 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2810 }
2811}
2812
Calin Juravlebacfec32014-11-14 15:54:36 +00002813void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002814 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002815 LocationSummary* locations =
2816 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002817
2818 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002819 case Primitive::kPrimInt:
2820 case Primitive::kPrimLong: {
2821 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002822 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002823 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2824 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002825 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2826 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2827 // output and request another temp.
2828 if (rem->InputAt(1)->IsConstant()) {
2829 locations->AddTemp(Location::RequiresRegister());
2830 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002831 break;
2832 }
2833
2834 case Primitive::kPrimFloat:
2835 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002836 locations->SetInAt(0, Location::Any());
2837 locations->SetInAt(1, Location::Any());
2838 locations->SetOut(Location::RequiresFpuRegister());
2839 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002840 break;
2841 }
2842
2843 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002844 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002845 }
2846}
2847
2848void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2849 Primitive::Type type = rem->GetResultType();
2850 switch (type) {
2851 case Primitive::kPrimInt:
2852 case Primitive::kPrimLong: {
2853 GenerateDivRemIntegral(rem);
2854 break;
2855 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002856 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002857 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002858 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002859 break;
2860 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002861 default:
2862 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2863 }
2864}
2865
Calin Juravled0d48522014-11-04 16:40:20 +00002866void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2867 LocationSummary* locations =
2868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2869 locations->SetInAt(0, Location::Any());
2870 if (instruction->HasUses()) {
2871 locations->SetOut(Location::SameAsFirstInput());
2872 }
2873}
2874
2875void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2876 SlowPathCodeX86_64* slow_path =
2877 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2878 codegen_->AddSlowPath(slow_path);
2879
2880 LocationSummary* locations = instruction->GetLocations();
2881 Location value = locations->InAt(0);
2882
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002883 switch (instruction->GetType()) {
2884 case Primitive::kPrimInt: {
2885 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002886 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002887 __ j(kEqual, slow_path->GetEntryLabel());
2888 } else if (value.IsStackSlot()) {
2889 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2890 __ j(kEqual, slow_path->GetEntryLabel());
2891 } else {
2892 DCHECK(value.IsConstant()) << value;
2893 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2894 __ jmp(slow_path->GetEntryLabel());
2895 }
2896 }
2897 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002898 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002899 case Primitive::kPrimLong: {
2900 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002901 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002902 __ j(kEqual, slow_path->GetEntryLabel());
2903 } else if (value.IsDoubleStackSlot()) {
2904 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2905 __ j(kEqual, slow_path->GetEntryLabel());
2906 } else {
2907 DCHECK(value.IsConstant()) << value;
2908 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2909 __ jmp(slow_path->GetEntryLabel());
2910 }
2911 }
2912 break;
2913 }
2914 default:
2915 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002916 }
Calin Juravled0d48522014-11-04 16:40:20 +00002917}
2918
Calin Juravle9aec02f2014-11-18 23:06:35 +00002919void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2920 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2921
2922 LocationSummary* locations =
2923 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2924
2925 switch (op->GetResultType()) {
2926 case Primitive::kPrimInt:
2927 case Primitive::kPrimLong: {
2928 locations->SetInAt(0, Location::RequiresRegister());
2929 // The shift count needs to be in CL.
2930 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2931 locations->SetOut(Location::SameAsFirstInput());
2932 break;
2933 }
2934 default:
2935 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2936 }
2937}
2938
2939void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2940 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2941
2942 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002943 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002944 Location second = locations->InAt(1);
2945
2946 switch (op->GetResultType()) {
2947 case Primitive::kPrimInt: {
2948 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002949 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002950 if (op->IsShl()) {
2951 __ shll(first_reg, second_reg);
2952 } else if (op->IsShr()) {
2953 __ sarl(first_reg, second_reg);
2954 } else {
2955 __ shrl(first_reg, second_reg);
2956 }
2957 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002958 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002959 if (op->IsShl()) {
2960 __ shll(first_reg, imm);
2961 } else if (op->IsShr()) {
2962 __ sarl(first_reg, imm);
2963 } else {
2964 __ shrl(first_reg, imm);
2965 }
2966 }
2967 break;
2968 }
2969 case Primitive::kPrimLong: {
2970 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002971 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002972 if (op->IsShl()) {
2973 __ shlq(first_reg, second_reg);
2974 } else if (op->IsShr()) {
2975 __ sarq(first_reg, second_reg);
2976 } else {
2977 __ shrq(first_reg, second_reg);
2978 }
2979 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002980 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002981 if (op->IsShl()) {
2982 __ shlq(first_reg, imm);
2983 } else if (op->IsShr()) {
2984 __ sarq(first_reg, imm);
2985 } else {
2986 __ shrq(first_reg, imm);
2987 }
2988 }
2989 break;
2990 }
2991 default:
2992 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2993 }
2994}
2995
2996void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2997 HandleShift(shl);
2998}
2999
3000void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3001 HandleShift(shl);
3002}
3003
3004void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3005 HandleShift(shr);
3006}
3007
3008void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3009 HandleShift(shr);
3010}
3011
3012void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3013 HandleShift(ushr);
3014}
3015
3016void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3017 HandleShift(ushr);
3018}
3019
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003020void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003021 LocationSummary* locations =
3022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003023 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003024 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3025 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3026 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003027}
3028
3029void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3030 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003031 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Mark Mendell92e83bf2015-05-07 11:25:03 -04003032 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3033 instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003034 __ gs()->call(
3035 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003036
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003037 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003038 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039}
3040
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003041void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3042 LocationSummary* locations =
3043 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3044 InvokeRuntimeCallingConvention calling_convention;
3045 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003046 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003047 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003048 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003049}
3050
3051void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3052 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003053 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Mark Mendell92e83bf2015-05-07 11:25:03 -04003054 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3055 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003056
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003057 __ gs()->call(
3058 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003059
3060 DCHECK(!codegen_->IsLeafMethod());
3061 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3062}
3063
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003064void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003065 LocationSummary* locations =
3066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003067 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3068 if (location.IsStackSlot()) {
3069 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3070 } else if (location.IsDoubleStackSlot()) {
3071 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3072 }
3073 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074}
3075
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003076void InstructionCodeGeneratorX86_64::VisitParameterValue(
3077 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003078 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003079}
3080
3081void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3082 LocationSummary* locations =
3083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3084 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3085}
3086
3087void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3088 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3089 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003090}
3091
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003092void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003093 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003094 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003095 locations->SetInAt(0, Location::RequiresRegister());
3096 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003097}
3098
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003099void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3100 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3102 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003103 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003104 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003105 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003107 break;
3108
3109 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003110 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003111 break;
3112
3113 default:
3114 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3115 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003116}
3117
David Brazdil66d126e2015-04-03 16:02:44 +01003118void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3119 LocationSummary* locations =
3120 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3121 locations->SetInAt(0, Location::RequiresRegister());
3122 locations->SetOut(Location::SameAsFirstInput());
3123}
3124
3125void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003126 LocationSummary* locations = bool_not->GetLocations();
3127 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3128 locations->Out().AsRegister<CpuRegister>().AsRegister());
3129 Location out = locations->Out();
3130 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3131}
3132
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003133void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003134 LocationSummary* locations =
3135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003136 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3137 locations->SetInAt(i, Location::Any());
3138 }
3139 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003140}
3141
3142void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003143 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003144 LOG(FATAL) << "Unimplemented";
3145}
3146
Calin Juravle52c48962014-12-16 17:02:57 +00003147void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3148 /*
3149 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3150 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3151 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3152 */
3153 switch (kind) {
3154 case MemBarrierKind::kAnyAny: {
3155 __ mfence();
3156 break;
3157 }
3158 case MemBarrierKind::kAnyStore:
3159 case MemBarrierKind::kLoadAny:
3160 case MemBarrierKind::kStoreStore: {
3161 // nop
3162 break;
3163 }
3164 default:
3165 LOG(FATAL) << "Unexpected memory barier " << kind;
3166 }
3167}
3168
3169void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3170 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3171
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 LocationSummary* locations =
3173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003174 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003175 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3176 locations->SetOut(Location::RequiresFpuRegister());
3177 } else {
3178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3179 }
Calin Juravle52c48962014-12-16 17:02:57 +00003180}
3181
3182void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3183 const FieldInfo& field_info) {
3184 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3185
3186 LocationSummary* locations = instruction->GetLocations();
3187 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3188 Location out = locations->Out();
3189 bool is_volatile = field_info.IsVolatile();
3190 Primitive::Type field_type = field_info.GetFieldType();
3191 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3192
3193 switch (field_type) {
3194 case Primitive::kPrimBoolean: {
3195 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3196 break;
3197 }
3198
3199 case Primitive::kPrimByte: {
3200 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3201 break;
3202 }
3203
3204 case Primitive::kPrimShort: {
3205 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3206 break;
3207 }
3208
3209 case Primitive::kPrimChar: {
3210 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3211 break;
3212 }
3213
3214 case Primitive::kPrimInt:
3215 case Primitive::kPrimNot: {
3216 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3217 break;
3218 }
3219
3220 case Primitive::kPrimLong: {
3221 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3222 break;
3223 }
3224
3225 case Primitive::kPrimFloat: {
3226 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3227 break;
3228 }
3229
3230 case Primitive::kPrimDouble: {
3231 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3232 break;
3233 }
3234
3235 case Primitive::kPrimVoid:
3236 LOG(FATAL) << "Unreachable type " << field_type;
3237 UNREACHABLE();
3238 }
3239
Calin Juravle77520bc2015-01-12 18:45:46 +00003240 codegen_->MaybeRecordImplicitNullCheck(instruction);
3241
Calin Juravle52c48962014-12-16 17:02:57 +00003242 if (is_volatile) {
3243 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3244 }
3245}
3246
3247void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3248 const FieldInfo& field_info) {
3249 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3250
3251 LocationSummary* locations =
3252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003253 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003254 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3255
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003256 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003257 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3258 locations->SetInAt(1, Location::RequiresFpuRegister());
3259 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003260 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003261 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003262 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003263 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003264 locations->AddTemp(Location::RequiresRegister());
3265 locations->AddTemp(Location::RequiresRegister());
3266 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003267}
3268
Calin Juravle52c48962014-12-16 17:02:57 +00003269void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003270 const FieldInfo& field_info,
3271 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003272 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3273
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003274 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003275 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3276 Location value = locations->InAt(1);
3277 bool is_volatile = field_info.IsVolatile();
3278 Primitive::Type field_type = field_info.GetFieldType();
3279 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3280
3281 if (is_volatile) {
3282 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3283 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003284
3285 switch (field_type) {
3286 case Primitive::kPrimBoolean:
3287 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003288 if (value.IsConstant()) {
3289 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3290 __ movb(Address(base, offset), Immediate(v));
3291 } else {
3292 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3293 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003294 break;
3295 }
3296
3297 case Primitive::kPrimShort:
3298 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003299 if (value.IsConstant()) {
3300 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3301 __ movw(Address(base, offset), Immediate(v));
3302 } else {
3303 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3304 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003305 break;
3306 }
3307
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003308 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003309 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003310 if (value.IsConstant()) {
3311 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3312 __ movw(Address(base, offset), Immediate(v));
3313 } else {
3314 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3315 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003316 break;
3317 }
3318
3319 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003320 if (value.IsConstant()) {
3321 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3322 DCHECK(IsInt<32>(v));
3323 int32_t v_32 = v;
3324 __ movq(Address(base, offset), Immediate(v_32));
3325 } else {
3326 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3327 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003328 break;
3329 }
3330
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003331 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003332 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003333 break;
3334 }
3335
3336 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003337 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003338 break;
3339 }
3340
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003341 case Primitive::kPrimVoid:
3342 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003343 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003344 }
Calin Juravle52c48962014-12-16 17:02:57 +00003345
Calin Juravle77520bc2015-01-12 18:45:46 +00003346 codegen_->MaybeRecordImplicitNullCheck(instruction);
3347
3348 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3349 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3350 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003351 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003352 }
3353
Calin Juravle52c48962014-12-16 17:02:57 +00003354 if (is_volatile) {
3355 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3356 }
3357}
3358
3359void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3360 HandleFieldSet(instruction, instruction->GetFieldInfo());
3361}
3362
3363void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003364 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003365}
3366
3367void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003368 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003369}
3370
3371void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003372 HandleFieldGet(instruction, instruction->GetFieldInfo());
3373}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003374
Calin Juravle52c48962014-12-16 17:02:57 +00003375void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3376 HandleFieldGet(instruction);
3377}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003378
Calin Juravle52c48962014-12-16 17:02:57 +00003379void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3380 HandleFieldGet(instruction, instruction->GetFieldInfo());
3381}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003382
Calin Juravle52c48962014-12-16 17:02:57 +00003383void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3384 HandleFieldSet(instruction, instruction->GetFieldInfo());
3385}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003386
Calin Juravle52c48962014-12-16 17:02:57 +00003387void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003388 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003389}
3390
3391void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003394 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3395 ? Location::RequiresRegister()
3396 : Location::Any();
3397 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003398 if (instruction->HasUses()) {
3399 locations->SetOut(Location::SameAsFirstInput());
3400 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003401}
3402
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003403void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003404 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3405 return;
3406 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003407 LocationSummary* locations = instruction->GetLocations();
3408 Location obj = locations->InAt(0);
3409
3410 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3411 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3412}
3413
3414void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003415 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003416 codegen_->AddSlowPath(slow_path);
3417
3418 LocationSummary* locations = instruction->GetLocations();
3419 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420
3421 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003422 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003423 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003424 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003425 } else {
3426 DCHECK(obj.IsConstant()) << obj;
3427 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3428 __ jmp(slow_path->GetEntryLabel());
3429 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003430 }
3431 __ j(kEqual, slow_path->GetEntryLabel());
3432}
3433
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003434void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3435 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3436 GenerateImplicitNullCheck(instruction);
3437 } else {
3438 GenerateExplicitNullCheck(instruction);
3439 }
3440}
3441
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003442void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003443 LocationSummary* locations =
3444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003445 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003446 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003447 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3448 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3449 } else {
3450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3451 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003452}
3453
3454void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3455 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003457 Location index = locations->InAt(1);
3458
3459 switch (instruction->GetType()) {
3460 case Primitive::kPrimBoolean: {
3461 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003462 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003463 if (index.IsConstant()) {
3464 __ movzxb(out, Address(obj,
3465 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3466 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003467 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003468 }
3469 break;
3470 }
3471
3472 case Primitive::kPrimByte: {
3473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003474 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003475 if (index.IsConstant()) {
3476 __ movsxb(out, Address(obj,
3477 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3478 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003480 }
3481 break;
3482 }
3483
3484 case Primitive::kPrimShort: {
3485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003487 if (index.IsConstant()) {
3488 __ movsxw(out, Address(obj,
3489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3490 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003491 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003492 }
3493 break;
3494 }
3495
3496 case Primitive::kPrimChar: {
3497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003498 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003499 if (index.IsConstant()) {
3500 __ movzxw(out, Address(obj,
3501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3502 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003504 }
3505 break;
3506 }
3507
3508 case Primitive::kPrimInt:
3509 case Primitive::kPrimNot: {
3510 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003512 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003513 if (index.IsConstant()) {
3514 __ movl(out, Address(obj,
3515 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3516 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003517 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003518 }
3519 break;
3520 }
3521
3522 case Primitive::kPrimLong: {
3523 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003524 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003525 if (index.IsConstant()) {
3526 __ movq(out, Address(obj,
3527 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3528 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003529 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003530 }
3531 break;
3532 }
3533
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003534 case Primitive::kPrimFloat: {
3535 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003537 if (index.IsConstant()) {
3538 __ movss(out, Address(obj,
3539 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3540 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003541 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003542 }
3543 break;
3544 }
3545
3546 case Primitive::kPrimDouble: {
3547 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003548 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003549 if (index.IsConstant()) {
3550 __ movsd(out, Address(obj,
3551 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3552 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003553 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003554 }
3555 break;
3556 }
3557
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003558 case Primitive::kPrimVoid:
3559 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003560 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003561 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003562 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003563}
3564
3565void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003566 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003567
3568 bool needs_write_barrier =
3569 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3570 bool needs_runtime_call = instruction->NeedsTypeCheck();
3571
Nicolas Geoffray39468442014-09-02 15:17:15 +01003572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003573 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3574 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003576 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3577 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3578 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003580 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003581 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003582 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3583 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003584 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003585 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003586 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3587 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003588 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003589 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003590 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003591
3592 if (needs_write_barrier) {
3593 // Temporary registers for the write barrier.
3594 locations->AddTemp(Location::RequiresRegister());
3595 locations->AddTemp(Location::RequiresRegister());
3596 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003597 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003598}
3599
3600void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3601 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003604 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003605 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003606 bool needs_runtime_call = locations->WillCall();
3607 bool needs_write_barrier =
3608 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003609
3610 switch (value_type) {
3611 case Primitive::kPrimBoolean:
3612 case Primitive::kPrimByte: {
3613 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003614 if (index.IsConstant()) {
3615 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003616 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003617 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003618 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003619 __ movb(Address(obj, offset),
3620 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003623 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3625 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003626 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003628 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3629 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003630 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003631 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 break;
3633 }
3634
3635 case Primitive::kPrimShort:
3636 case Primitive::kPrimChar: {
3637 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003638 if (index.IsConstant()) {
3639 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003640 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003641 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003642 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003643 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003644 __ movw(Address(obj, offset),
3645 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003646 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003647 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003648 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003649 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3651 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003652 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003653 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003655 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3656 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003657 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003658 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 break;
3660 }
3661
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003662 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003664 if (!needs_runtime_call) {
3665 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3666 if (index.IsConstant()) {
3667 size_t offset =
3668 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3669 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003670 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003671 } else {
3672 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003673 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3674 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003675 }
3676 } else {
3677 DCHECK(index.IsRegister()) << index;
3678 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003679 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3680 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003681 } else {
3682 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003683 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003685 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003686 }
3687 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003688 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003689 if (needs_write_barrier) {
3690 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3692 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003693 codegen_->MarkGCCard(
3694 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003695 }
3696 } else {
3697 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003698 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3699 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003700 DCHECK(!codegen_->IsLeafMethod());
3701 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3702 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003703 break;
3704 }
3705
3706 case Primitive::kPrimLong: {
3707 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003708 if (index.IsConstant()) {
3709 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04003710 if (value.IsRegister()) {
3711 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
3712 } else {
3713 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3714 DCHECK(IsInt<32>(v));
3715 int32_t v_32 = v;
3716 __ movq(Address(obj, offset), Immediate(v_32));
3717 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003718 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003719 if (value.IsRegister()) {
3720 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3721 value.AsRegister<CpuRegister>());
3722 } else {
3723 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3724 DCHECK(IsInt<32>(v));
3725 int32_t v_32 = v;
3726 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3727 Immediate(v_32));
3728 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003729 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003730 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003731 break;
3732 }
3733
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003734 case Primitive::kPrimFloat: {
3735 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3736 if (index.IsConstant()) {
3737 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3738 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003739 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003740 } else {
3741 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3743 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003744 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003745 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003746 break;
3747 }
3748
3749 case Primitive::kPrimDouble: {
3750 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3751 if (index.IsConstant()) {
3752 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3753 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003754 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003755 } else {
3756 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003757 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3758 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003759 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003760 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003761 break;
3762 }
3763
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003764 case Primitive::kPrimVoid:
3765 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003766 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767 }
3768}
3769
3770void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003771 LocationSummary* locations =
3772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003773 locations->SetInAt(0, Location::RequiresRegister());
3774 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775}
3776
3777void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3778 LocationSummary* locations = instruction->GetLocations();
3779 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3781 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003783 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003784}
3785
3786void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003787 LocationSummary* locations =
3788 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003789 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003790 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003791 if (instruction->HasUses()) {
3792 locations->SetOut(Location::SameAsFirstInput());
3793 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794}
3795
3796void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3797 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003798 Location index_loc = locations->InAt(0);
3799 Location length_loc = locations->InAt(1);
3800 SlowPathCodeX86_64* slow_path =
3801 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802
Mark Mendell99dbd682015-04-22 16:18:52 -04003803 if (length_loc.IsConstant()) {
3804 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3805 if (index_loc.IsConstant()) {
3806 // BCE will remove the bounds check if we are guarenteed to pass.
3807 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3808 if (index < 0 || index >= length) {
3809 codegen_->AddSlowPath(slow_path);
3810 __ jmp(slow_path->GetEntryLabel());
3811 } else {
3812 // Some optimization after BCE may have generated this, and we should not
3813 // generate a bounds check if it is a valid range.
3814 }
3815 return;
3816 }
3817
3818 // We have to reverse the jump condition because the length is the constant.
3819 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
3820 __ cmpl(index_reg, Immediate(length));
3821 codegen_->AddSlowPath(slow_path);
3822 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003823 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003824 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3825 if (index_loc.IsConstant()) {
3826 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3827 __ cmpl(length, Immediate(value));
3828 } else {
3829 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3830 }
3831 codegen_->AddSlowPath(slow_path);
3832 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003833 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834}
3835
3836void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3837 CpuRegister card,
3838 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003839 CpuRegister value,
3840 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003841 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003842 if (value_can_be_null) {
3843 __ testl(value, value);
3844 __ j(kEqual, &is_null);
3845 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003846 __ gs()->movq(card, Address::Absolute(
3847 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3848 __ movq(temp, object);
3849 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3850 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003851 if (value_can_be_null) {
3852 __ Bind(&is_null);
3853 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003854}
3855
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003856void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3857 temp->SetLocations(nullptr);
3858}
3859
3860void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3861 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003862 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003863}
3864
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003865void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003866 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003867 LOG(FATAL) << "Unimplemented";
3868}
3869
3870void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003871 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3872}
3873
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003874void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3876}
3877
3878void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003879 HBasicBlock* block = instruction->GetBlock();
3880 if (block->GetLoopInformation() != nullptr) {
3881 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3882 // The back edge will generate the suspend check.
3883 return;
3884 }
3885 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3886 // The goto will generate the suspend check.
3887 return;
3888 }
3889 GenerateSuspendCheck(instruction, nullptr);
3890}
3891
3892void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3893 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003894 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003895 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
3896 if (slow_path == nullptr) {
3897 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
3898 instruction->SetSlowPath(slow_path);
3899 codegen_->AddSlowPath(slow_path);
3900 if (successor != nullptr) {
3901 DCHECK(successor->IsLoopHeader());
3902 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3903 }
3904 } else {
3905 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3906 }
3907
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003908 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003909 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003910 if (successor == nullptr) {
3911 __ j(kNotEqual, slow_path->GetEntryLabel());
3912 __ Bind(slow_path->GetReturnLabel());
3913 } else {
3914 __ j(kEqual, codegen_->GetLabelOf(successor));
3915 __ jmp(slow_path->GetEntryLabel());
3916 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003917}
3918
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003919X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3920 return codegen_->GetAssembler();
3921}
3922
3923void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3924 MoveOperands* move = moves_.Get(index);
3925 Location source = move->GetSource();
3926 Location destination = move->GetDestination();
3927
3928 if (source.IsRegister()) {
3929 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003931 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003932 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003934 } else {
3935 DCHECK(destination.IsDoubleStackSlot());
3936 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003938 }
3939 } else if (source.IsStackSlot()) {
3940 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003941 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003942 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003943 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003944 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003945 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003946 } else {
3947 DCHECK(destination.IsStackSlot());
3948 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3949 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3950 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003951 } else if (source.IsDoubleStackSlot()) {
3952 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003953 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003954 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003955 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003956 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3957 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003958 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003959 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003960 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3961 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3962 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003963 } else if (source.IsConstant()) {
3964 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003965 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3966 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003967 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003968 if (value == 0) {
3969 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3970 } else {
3971 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3972 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003973 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003974 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003975 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003976 }
3977 } else if (constant->IsLongConstant()) {
3978 int64_t value = constant->AsLongConstant()->GetValue();
3979 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003980 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003981 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003982 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003983 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003984 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3985 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003986 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003987 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003988 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003989 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003990 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3991 if (value == 0) {
3992 // easy FP 0.0.
3993 __ xorps(dest, dest);
3994 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003995 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003996 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003997 } else {
3998 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003999 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004000 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4001 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004002 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004003 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004004 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004005 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004006 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004007 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4008 if (value == 0) {
4009 __ xorpd(dest, dest);
4010 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004011 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004012 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004013 } else {
4014 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004015 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004016 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4017 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004018 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004019 } else if (source.IsFpuRegister()) {
4020 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004021 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004022 } else if (destination.IsStackSlot()) {
4023 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004025 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004026 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004027 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004028 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004029 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004030 }
4031}
4032
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004033void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004034 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004035 __ movl(Address(CpuRegister(RSP), mem), reg);
4036 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004037}
4038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004039void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004040 ScratchRegisterScope ensure_scratch(
4041 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4042
4043 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4044 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4045 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4046 Address(CpuRegister(RSP), mem2 + stack_offset));
4047 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4048 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4049 CpuRegister(ensure_scratch.GetRegister()));
4050}
4051
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004052void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4053 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4054 __ movq(Address(CpuRegister(RSP), mem), reg);
4055 __ movq(reg, CpuRegister(TMP));
4056}
4057
4058void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4059 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004060 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004061
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004062 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4063 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4064 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4065 Address(CpuRegister(RSP), mem2 + stack_offset));
4066 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4067 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4068 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004069}
4070
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004071void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4072 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4073 __ movss(Address(CpuRegister(RSP), mem), reg);
4074 __ movd(reg, CpuRegister(TMP));
4075}
4076
4077void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4078 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4079 __ movsd(Address(CpuRegister(RSP), mem), reg);
4080 __ movd(reg, CpuRegister(TMP));
4081}
4082
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004083void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4084 MoveOperands* move = moves_.Get(index);
4085 Location source = move->GetSource();
4086 Location destination = move->GetDestination();
4087
4088 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004089 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004090 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004092 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004093 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004094 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004095 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4096 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004098 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004100 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4101 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004102 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004103 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4104 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4105 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004106 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004108 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004110 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004111 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004112 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004113 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004114 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004115 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004116 }
4117}
4118
4119
4120void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4121 __ pushq(CpuRegister(reg));
4122}
4123
4124
4125void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4126 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004127}
4128
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004129void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4130 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4131 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4132 Immediate(mirror::Class::kStatusInitialized));
4133 __ j(kLess, slow_path->GetEntryLabel());
4134 __ Bind(slow_path->GetExitLabel());
4135 // No need for memory fence, thanks to the X86_64 memory model.
4136}
4137
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004138void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004139 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4140 ? LocationSummary::kCallOnSlowPath
4141 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004142 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004143 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004144 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004145 locations->SetOut(Location::RequiresRegister());
4146}
4147
4148void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004149 LocationSummary* locations = cls->GetLocations();
4150 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4151 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004152 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004153 DCHECK(!cls->CanCallRuntime());
4154 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004155 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004156 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004157 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004158 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004159 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004160 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004161 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4162 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4163 codegen_->AddSlowPath(slow_path);
4164 __ testl(out, out);
4165 __ j(kEqual, slow_path->GetEntryLabel());
4166 if (cls->MustGenerateClinitCheck()) {
4167 GenerateClassInitializationCheck(slow_path, out);
4168 } else {
4169 __ Bind(slow_path->GetExitLabel());
4170 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004171 }
4172}
4173
4174void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4175 LocationSummary* locations =
4176 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4177 locations->SetInAt(0, Location::RequiresRegister());
4178 if (check->HasUses()) {
4179 locations->SetOut(Location::SameAsFirstInput());
4180 }
4181}
4182
4183void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004184 // We assume the class to not be null.
4185 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4186 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004187 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004188 GenerateClassInitializationCheck(slow_path,
4189 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004190}
4191
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004192void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4193 LocationSummary* locations =
4194 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004195 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004196 locations->SetOut(Location::RequiresRegister());
4197}
4198
4199void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4200 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4201 codegen_->AddSlowPath(slow_path);
4202
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004203 LocationSummary* locations = load->GetLocations();
4204 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4205 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004206 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004207 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004208 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4209 __ testl(out, out);
4210 __ j(kEqual, slow_path->GetEntryLabel());
4211 __ Bind(slow_path->GetExitLabel());
4212}
4213
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004214void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4215 LocationSummary* locations =
4216 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4217 locations->SetOut(Location::RequiresRegister());
4218}
4219
4220void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4221 Address address = Address::Absolute(
4222 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004223 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004224 __ gs()->movl(address, Immediate(0));
4225}
4226
4227void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4228 LocationSummary* locations =
4229 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4230 InvokeRuntimeCallingConvention calling_convention;
4231 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4232}
4233
4234void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4235 __ gs()->call(
4236 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4237 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4238}
4239
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004240void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004241 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4242 ? LocationSummary::kNoCall
4243 : LocationSummary::kCallOnSlowPath;
4244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4245 locations->SetInAt(0, Location::RequiresRegister());
4246 locations->SetInAt(1, Location::Any());
4247 locations->SetOut(Location::RequiresRegister());
4248}
4249
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004250void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004251 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004252 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004253 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004254 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004255 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4256 Label done, zero;
4257 SlowPathCodeX86_64* slow_path = nullptr;
4258
4259 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004260 // Avoid null check if we know obj is not null.
4261 if (instruction->MustDoNullCheck()) {
4262 __ testl(obj, obj);
4263 __ j(kEqual, &zero);
4264 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004265 // Compare the class of `obj` with `cls`.
4266 __ movl(out, Address(obj, class_offset));
4267 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004268 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004269 } else {
4270 DCHECK(cls.IsStackSlot()) << cls;
4271 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4272 }
4273 if (instruction->IsClassFinal()) {
4274 // Classes must be equal for the instanceof to succeed.
4275 __ j(kNotEqual, &zero);
4276 __ movl(out, Immediate(1));
4277 __ jmp(&done);
4278 } else {
4279 // If the classes are not equal, we go into a slow path.
4280 DCHECK(locations->OnlyCallsOnSlowPath());
4281 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004282 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004283 codegen_->AddSlowPath(slow_path);
4284 __ j(kNotEqual, slow_path->GetEntryLabel());
4285 __ movl(out, Immediate(1));
4286 __ jmp(&done);
4287 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004288
4289 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4290 __ Bind(&zero);
4291 __ movl(out, Immediate(0));
4292 }
4293
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004294 if (slow_path != nullptr) {
4295 __ Bind(slow_path->GetExitLabel());
4296 }
4297 __ Bind(&done);
4298}
4299
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004300void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4301 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4302 instruction, LocationSummary::kCallOnSlowPath);
4303 locations->SetInAt(0, Location::RequiresRegister());
4304 locations->SetInAt(1, Location::Any());
4305 locations->AddTemp(Location::RequiresRegister());
4306}
4307
4308void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4309 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004310 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004311 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004312 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004313 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4314 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4315 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4316 codegen_->AddSlowPath(slow_path);
4317
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004318 // Avoid null check if we know obj is not null.
4319 if (instruction->MustDoNullCheck()) {
4320 __ testl(obj, obj);
4321 __ j(kEqual, slow_path->GetExitLabel());
4322 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004323 // Compare the class of `obj` with `cls`.
4324 __ movl(temp, Address(obj, class_offset));
4325 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004326 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004327 } else {
4328 DCHECK(cls.IsStackSlot()) << cls;
4329 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4330 }
4331 // Classes must be equal for the checkcast to succeed.
4332 __ j(kNotEqual, slow_path->GetEntryLabel());
4333 __ Bind(slow_path->GetExitLabel());
4334}
4335
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004336void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4337 LocationSummary* locations =
4338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4339 InvokeRuntimeCallingConvention calling_convention;
4340 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4341}
4342
4343void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4344 __ gs()->call(Address::Absolute(instruction->IsEnter()
4345 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4346 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4347 true));
4348 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4349}
4350
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004351void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4352void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4353void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4354
4355void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4356 LocationSummary* locations =
4357 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4358 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4359 || instruction->GetResultType() == Primitive::kPrimLong);
4360 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004361 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004362 locations->SetOut(Location::SameAsFirstInput());
4363}
4364
4365void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4366 HandleBitwiseOperation(instruction);
4367}
4368
4369void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4370 HandleBitwiseOperation(instruction);
4371}
4372
4373void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4374 HandleBitwiseOperation(instruction);
4375}
4376
4377void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4378 LocationSummary* locations = instruction->GetLocations();
4379 Location first = locations->InAt(0);
4380 Location second = locations->InAt(1);
4381 DCHECK(first.Equals(locations->Out()));
4382
4383 if (instruction->GetResultType() == Primitive::kPrimInt) {
4384 if (second.IsRegister()) {
4385 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004386 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004387 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004388 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004389 } else {
4390 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004391 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004392 }
4393 } else if (second.IsConstant()) {
4394 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4395 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004397 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004399 } else {
4400 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004401 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004402 }
4403 } else {
4404 Address address(CpuRegister(RSP), second.GetStackIndex());
4405 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004406 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004407 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004408 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004409 } else {
4410 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004411 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004412 }
4413 }
4414 } else {
4415 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004416 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4417 bool second_is_constant = false;
4418 int64_t value = 0;
4419 if (second.IsConstant()) {
4420 second_is_constant = true;
4421 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004422 }
Mark Mendell40741f32015-04-20 22:10:34 -04004423 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004424
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004425 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004426 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004427 if (is_int32_value) {
4428 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4429 } else {
4430 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4431 }
4432 } else if (second.IsDoubleStackSlot()) {
4433 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004434 } else {
4435 __ andq(first_reg, second.AsRegister<CpuRegister>());
4436 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004437 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004438 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004439 if (is_int32_value) {
4440 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4441 } else {
4442 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4443 }
4444 } else if (second.IsDoubleStackSlot()) {
4445 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004446 } else {
4447 __ orq(first_reg, second.AsRegister<CpuRegister>());
4448 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004449 } else {
4450 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004451 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004452 if (is_int32_value) {
4453 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4454 } else {
4455 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4456 }
4457 } else if (second.IsDoubleStackSlot()) {
4458 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004459 } else {
4460 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4461 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004462 }
4463 }
4464}
4465
Calin Juravleb1498f62015-02-16 13:13:29 +00004466void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4467 // Nothing to do, this should be removed during prepare for register allocator.
4468 UNUSED(instruction);
4469 LOG(FATAL) << "Unreachable";
4470}
4471
4472void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4473 // Nothing to do, this should be removed during prepare for register allocator.
4474 UNUSED(instruction);
4475 LOG(FATAL) << "Unreachable";
4476}
4477
Mark Mendell92e83bf2015-05-07 11:25:03 -04004478void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4479 if (value == 0) {
4480 __ xorl(dest, dest);
4481 } else if (value > 0 && IsInt<32>(value)) {
4482 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4483 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4484 } else {
4485 __ movq(dest, Immediate(value));
4486 }
4487}
4488
Mark Mendellf55c3e02015-03-26 21:07:46 -04004489void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4490 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004491 X86_64Assembler* assembler = GetAssembler();
4492 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004493 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4494 // byte values. If used for vectors at a later time, this will need to be
4495 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004496 assembler->Align(4, 0);
4497 constant_area_start_ = assembler->CodeSize();
4498 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004499 }
4500
4501 // And finish up.
4502 CodeGenerator::Finalize(allocator);
4503}
4504
4505/**
4506 * Class to handle late fixup of offsets into constant area.
4507 */
4508class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4509 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004510 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004511 : codegen_(codegen), offset_into_constant_area_(offset) {}
4512
4513 private:
4514 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4515 // Patch the correct offset for the instruction. We use the address of the
4516 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4517 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4518 int relative_position = constant_offset - pos;
4519
4520 // Patch in the right value.
4521 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4522 }
4523
Mark Mendell39dcf552015-04-09 20:42:42 -04004524 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004525
4526 // Location in constant area that the fixup refers to.
4527 int offset_into_constant_area_;
4528};
4529
4530Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4531 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4532 return Address::RIP(fixup);
4533}
4534
4535Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4536 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4537 return Address::RIP(fixup);
4538}
4539
4540Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4541 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4542 return Address::RIP(fixup);
4543}
4544
4545Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4546 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4547 return Address::RIP(fixup);
4548}
4549
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004550} // namespace x86_64
4551} // namespace art