blob: febe2bcd7bcb16860190dad369147eda4fd60390 [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,
363 CpuRegister temp) {
364 // All registers are assumed to be correctly set up.
365
366 // TODO: Implement all kinds of calls:
367 // 1) boot -> boot
368 // 2) app -> boot
369 // 3) app -> app
370 //
371 // Currently we implement the app -> app logic, which looks up in the resolve cache.
372
Jeff Hao848f70a2014-01-15 13:49:50 -0800373 if (invoke->IsStringInit()) {
374 // temp = thread->string_init_entrypoint
375 __ gs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000376 // (temp + offset_of_quick_compiled_code)()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000378 kX86_64WordSize).SizeValue()));
379 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -0800380 // temp = method;
381 LoadCurrentMethod(temp);
382 if (!invoke->IsRecursive()) {
383 // temp = temp->dex_cache_resolved_methods_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 __ movl(temp, Address(temp, ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800385 // temp = temp[index_in_cache]
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 __ movq(temp, Address(
387 temp, CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
Jeff Hao848f70a2014-01-15 13:49:50 -0800388 // (temp + offset_of_quick_compiled_code)()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Jeff Hao848f70a2014-01-15 13:49:50 -0800390 kX86_64WordSize).SizeValue()));
391 } else {
392 __ call(&frame_entry_label_);
393 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000394 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800395
396 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800397}
398
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100399void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100400 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401}
402
403void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100404 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100405}
406
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100407size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
408 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
409 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100410}
411
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100412size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
413 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
414 return kX86_64WordSize;
415}
416
417size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
418 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
419 return kX86_64WordSize;
420}
421
422size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
423 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
424 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100425}
426
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000427static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000428// Use a fake return address register to mimic Quick.
429static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400430CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
431 const X86_64InstructionSetFeatures& isa_features,
432 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000433 : CodeGenerator(graph,
434 kNumberOfCpuRegisters,
435 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000436 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000437 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
438 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000439 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000440 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
441 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000442 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100443 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100444 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000445 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400446 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400447 isa_features_(isa_features),
448 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000449 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
450}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100451
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100452InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
453 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454 : HGraphVisitor(graph),
455 assembler_(codegen->GetAssembler()),
456 codegen_(codegen) {}
457
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459 switch (type) {
460 case Primitive::kPrimLong:
461 case Primitive::kPrimByte:
462 case Primitive::kPrimBoolean:
463 case Primitive::kPrimChar:
464 case Primitive::kPrimShort:
465 case Primitive::kPrimInt:
466 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100468 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100469 }
470
471 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100472 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100473 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100474 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100476
477 case Primitive::kPrimVoid:
478 LOG(FATAL) << "Unreachable type " << type;
479 }
480
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100481 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100482}
483
Nicolas Geoffray98893962015-01-21 12:32:32 +0000484void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100485 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100486 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100487
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000488 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100489 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000490
Nicolas Geoffray98893962015-01-21 12:32:32 +0000491 if (is_baseline) {
492 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
493 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
494 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000495 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
496 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
497 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000498 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499}
500
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100501static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100502 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100503}
David Srbecky9d8606d2015-04-12 09:35:32 +0100504
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100505static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100506 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100507}
508
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100510 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000511 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100512 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700513 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000514 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100515
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000516 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100517 __ testq(CpuRegister(RAX), Address(
518 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100519 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100520 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000521
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000522 if (HasEmptyFrame()) {
523 return;
524 }
525
Nicolas Geoffray98893962015-01-21 12:32:32 +0000526 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000527 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000528 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000529 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100530 __ cfi().AdjustCFAOffset(kX86_64WordSize);
531 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000532 }
533 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100534
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100535 int adjust = GetFrameSize() - GetCoreSpillSize();
536 __ subq(CpuRegister(RSP), Immediate(adjust));
537 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000538 uint32_t xmm_spill_location = GetFpuSpillStart();
539 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100540
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000541 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
542 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100543 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
544 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
545 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000546 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100547 }
548
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100550 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551}
552
553void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100554 __ cfi().RememberState();
555 if (!HasEmptyFrame()) {
556 uint32_t xmm_spill_location = GetFpuSpillStart();
557 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
558 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
559 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
560 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
561 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
562 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
563 }
564 }
565
566 int adjust = GetFrameSize() - GetCoreSpillSize();
567 __ addq(CpuRegister(RSP), Immediate(adjust));
568 __ cfi().AdjustCFAOffset(-adjust);
569
570 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
571 Register reg = kCoreCalleeSaves[i];
572 if (allocated_registers_.ContainsCoreRegister(reg)) {
573 __ popq(CpuRegister(reg));
574 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
575 __ cfi().Restore(DWARFReg(reg));
576 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000577 }
578 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100579 __ ret();
580 __ cfi().RestoreState();
581 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100582}
583
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100584void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
585 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586}
587
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100588void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000589 DCHECK(RequiresCurrentMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591}
592
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100593Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
594 switch (load->GetType()) {
595 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100597 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100598
599 case Primitive::kPrimInt:
600 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100602 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100603
604 case Primitive::kPrimBoolean:
605 case Primitive::kPrimByte:
606 case Primitive::kPrimChar:
607 case Primitive::kPrimShort:
608 case Primitive::kPrimVoid:
609 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700610 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100611 }
612
613 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700614 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615}
616
617void CodeGeneratorX86_64::Move(Location destination, Location source) {
618 if (source.Equals(destination)) {
619 return;
620 }
621 if (destination.IsRegister()) {
622 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000623 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000625 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100626 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100629 } else {
630 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 Address(CpuRegister(RSP), source.GetStackIndex()));
633 }
634 } else if (destination.IsFpuRegister()) {
635 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 Address(CpuRegister(RSP), source.GetStackIndex()));
642 } else {
643 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000644 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100645 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100646 }
647 } else if (destination.IsStackSlot()) {
648 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100649 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (source.IsFpuRegister()) {
652 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500654 } else if (source.IsConstant()) {
655 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000656 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500657 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100658 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500659 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000660 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
661 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100662 }
663 } else {
664 DCHECK(destination.IsDoubleStackSlot());
665 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000667 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 } else if (source.IsFpuRegister()) {
669 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000670 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500671 } else if (source.IsConstant()) {
672 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800673 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500674 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000675 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500676 } else {
677 DCHECK(constant->IsLongConstant());
678 value = constant->AsLongConstant()->GetValue();
679 }
Mark Mendell92e83bf2015-05-07 11:25:03 -0400680 Load64BitValue(CpuRegister(TMP), value);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500681 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682 } else {
683 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000684 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
685 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 }
687 }
688}
689
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100690void CodeGeneratorX86_64::Move(HInstruction* instruction,
691 Location location,
692 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000693 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100694 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700695 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100696 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000697 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100698 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000699 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000700 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
701 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000702 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000703 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000704 } else if (location.IsStackSlot()) {
705 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
706 } else {
707 DCHECK(location.IsConstant());
708 DCHECK_EQ(location.GetConstant(), const_to_move);
709 }
710 } else if (const_to_move->IsLongConstant()) {
711 int64_t value = const_to_move->AsLongConstant()->GetValue();
712 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400713 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000714 } else if (location.IsDoubleStackSlot()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400715 Load64BitValue(CpuRegister(TMP), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000716 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
717 } else {
718 DCHECK(location.IsConstant());
719 DCHECK_EQ(location.GetConstant(), const_to_move);
720 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100721 }
Roland Levillain476df552014-10-09 17:51:36 +0100722 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100723 switch (instruction->GetType()) {
724 case Primitive::kPrimBoolean:
725 case Primitive::kPrimByte:
726 case Primitive::kPrimChar:
727 case Primitive::kPrimShort:
728 case Primitive::kPrimInt:
729 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100731 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
732 break;
733
734 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000736 Move(location,
737 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100738 break;
739
740 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000743 } else if (instruction->IsTemporary()) {
744 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
745 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100747 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100748 switch (instruction->GetType()) {
749 case Primitive::kPrimBoolean:
750 case Primitive::kPrimByte:
751 case Primitive::kPrimChar:
752 case Primitive::kPrimShort:
753 case Primitive::kPrimInt:
754 case Primitive::kPrimNot:
755 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100756 case Primitive::kPrimFloat:
757 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000758 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100759 break;
760
761 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100763 }
764 }
765}
766
767void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
768 got->SetLocations(nullptr);
769}
770
771void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
772 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100773 DCHECK(!successor->IsExitBlock());
774
775 HBasicBlock* block = got->GetBlock();
776 HInstruction* previous = got->GetPrevious();
777
778 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000779 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100780 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
781 return;
782 }
783
784 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
785 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
786 }
787 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100788 __ jmp(codegen_->GetLabelOf(successor));
789 }
790}
791
792void LocationsBuilderX86_64::VisitExit(HExit* exit) {
793 exit->SetLocations(nullptr);
794}
795
796void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700797 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100798}
799
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700800void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
801 Label* true_target,
802 Label* false_target,
803 Label* always_true_target) {
804 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 if (cond->IsIntConstant()) {
806 // Constant condition, statically compared against 1.
807 int32_t cond_value = cond->AsIntConstant()->GetValue();
808 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700809 if (always_true_target != nullptr) {
810 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100811 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100812 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100813 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 DCHECK_EQ(cond_value, 0);
815 }
816 } else {
817 bool materialized =
818 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
819 // Moves do not affect the eflags register, so if the condition is
820 // evaluated just before the if, we don't need to evaluate it
821 // again.
822 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700823 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 if (materialized) {
825 if (!eflags_set) {
826 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700827 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100828 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000829 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 } else {
831 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
832 Immediate(0));
833 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700834 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100835 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100837 }
838 } else {
839 Location lhs = cond->GetLocations()->InAt(0);
840 Location rhs = cond->GetLocations()->InAt(1);
841 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000842 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100843 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000844 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000845 if (constant == 0) {
846 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
847 } else {
848 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
849 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000851 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100852 Address(CpuRegister(RSP), rhs.GetStackIndex()));
853 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700854 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700855 }
Dave Allison20dfc792014-06-16 20:44:29 -0700856 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700857 if (false_target != nullptr) {
858 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 }
860}
861
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700862void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
863 LocationSummary* locations =
864 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
865 HInstruction* cond = if_instr->InputAt(0);
866 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
867 locations->SetInAt(0, Location::Any());
868 }
869}
870
871void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
872 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
873 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
874 Label* always_true_target = true_target;
875 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
876 if_instr->IfTrueSuccessor())) {
877 always_true_target = nullptr;
878 }
879 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
880 if_instr->IfFalseSuccessor())) {
881 false_target = nullptr;
882 }
883 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
884}
885
886void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
887 LocationSummary* locations = new (GetGraph()->GetArena())
888 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
889 HInstruction* cond = deoptimize->InputAt(0);
890 DCHECK(cond->IsCondition());
891 if (cond->AsCondition()->NeedsMaterialization()) {
892 locations->SetInAt(0, Location::Any());
893 }
894}
895
896void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
897 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
898 DeoptimizationSlowPathX86_64(deoptimize);
899 codegen_->AddSlowPath(slow_path);
900 Label* slow_path_entry = slow_path->GetEntryLabel();
901 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
902}
903
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100904void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
905 local->SetLocations(nullptr);
906}
907
908void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
909 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
910}
911
912void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
913 local->SetLocations(nullptr);
914}
915
916void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
917 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700918 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100919}
920
921void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100922 LocationSummary* locations =
923 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924 switch (store->InputAt(1)->GetType()) {
925 case Primitive::kPrimBoolean:
926 case Primitive::kPrimByte:
927 case Primitive::kPrimChar:
928 case Primitive::kPrimShort:
929 case Primitive::kPrimInt:
930 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100931 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100932 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
933 break;
934
935 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100936 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100937 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
938 break;
939
940 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100943}
944
945void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700946 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947}
948
Roland Levillain0d37cd02015-05-27 16:39:19 +0100949void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100950 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +0100951 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100952 locations->SetInAt(0, Location::RequiresRegister());
953 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +0100954 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100955 locations->SetOut(Location::RequiresRegister());
956 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100957}
958
Roland Levillain0d37cd02015-05-27 16:39:19 +0100959void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
960 if (cond->NeedsMaterialization()) {
961 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000962 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100963 // Clear register: setcc only sets the low byte.
Mark Mendell92e83bf2015-05-07 11:25:03 -0400964 __ xorl(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000965 Location lhs = locations->InAt(0);
966 Location rhs = locations->InAt(1);
967 if (rhs.IsRegister()) {
968 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
969 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800970 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000971 if (constant == 0) {
972 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
973 } else {
974 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
975 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100976 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000977 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100978 }
Roland Levillain0d37cd02015-05-27 16:39:19 +0100979 __ setcc(X86_64Condition(cond->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700980 }
981}
982
983void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
984 VisitCondition(comp);
985}
986
987void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
988 VisitCondition(comp);
989}
990
991void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
992 VisitCondition(comp);
993}
994
995void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
996 VisitCondition(comp);
997}
998
999void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1000 VisitCondition(comp);
1001}
1002
1003void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1004 VisitCondition(comp);
1005}
1006
1007void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1008 VisitCondition(comp);
1009}
1010
1011void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1012 VisitCondition(comp);
1013}
1014
1015void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1016 VisitCondition(comp);
1017}
1018
1019void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1020 VisitCondition(comp);
1021}
1022
1023void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1024 VisitCondition(comp);
1025}
1026
1027void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1028 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029}
1030
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001031void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001034 switch (compare->InputAt(0)->GetType()) {
1035 case Primitive::kPrimLong: {
1036 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001037 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001038 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1039 break;
1040 }
1041 case Primitive::kPrimFloat:
1042 case Primitive::kPrimDouble: {
1043 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001044 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001045 locations->SetOut(Location::RequiresRegister());
1046 break;
1047 }
1048 default:
1049 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1050 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001051}
1052
1053void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001054 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001055 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001056 Location left = locations->InAt(0);
1057 Location right = locations->InAt(1);
1058
1059 Label less, greater, done;
1060 Primitive::Type type = compare->InputAt(0)->GetType();
1061 switch (type) {
1062 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001063 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1064 if (right.IsConstant()) {
1065 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001066 if (IsInt<32>(value)) {
1067 if (value == 0) {
1068 __ testq(left_reg, left_reg);
1069 } else {
1070 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1071 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001072 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001073 // Value won't fit in an int.
1074 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001075 }
Mark Mendell40741f32015-04-20 22:10:34 -04001076 } else if (right.IsDoubleStackSlot()) {
1077 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001078 } else {
1079 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1080 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001081 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001082 }
1083 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001084 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1085 if (right.IsConstant()) {
1086 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1087 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1088 } else if (right.IsStackSlot()) {
1089 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1090 } else {
1091 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1092 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001093 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1094 break;
1095 }
1096 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001097 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1098 if (right.IsConstant()) {
1099 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1100 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1101 } else if (right.IsDoubleStackSlot()) {
1102 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1103 } else {
1104 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1105 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001106 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1107 break;
1108 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001109 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001110 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001111 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001112 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001113 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001114 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001115
Calin Juravle91debbc2014-11-26 19:01:09 +00001116 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001117 __ movl(out, Immediate(1));
1118 __ jmp(&done);
1119
1120 __ Bind(&less);
1121 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001122
1123 __ Bind(&done);
1124}
1125
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001126void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001127 LocationSummary* locations =
1128 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001129 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130}
1131
1132void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001133 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135}
1136
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001137void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1138 LocationSummary* locations =
1139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1140 locations->SetOut(Location::ConstantLocation(constant));
1141}
1142
1143void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1144 // Will be generated at use site.
1145 UNUSED(constant);
1146}
1147
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001149 LocationSummary* locations =
1150 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001151 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152}
1153
1154void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001155 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001156 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001157}
1158
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001159void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1160 LocationSummary* locations =
1161 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1162 locations->SetOut(Location::ConstantLocation(constant));
1163}
1164
1165void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1166 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001167 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001168}
1169
1170void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1171 LocationSummary* locations =
1172 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1173 locations->SetOut(Location::ConstantLocation(constant));
1174}
1175
1176void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1177 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001178 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001179}
1180
Calin Juravle27df7582015-04-17 19:12:31 +01001181void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1182 memory_barrier->SetLocations(nullptr);
1183}
1184
1185void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1186 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1187}
1188
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001189void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1190 ret->SetLocations(nullptr);
1191}
1192
1193void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001194 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196}
1197
1198void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001199 LocationSummary* locations =
1200 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201 switch (ret->InputAt(0)->GetType()) {
1202 case Primitive::kPrimBoolean:
1203 case Primitive::kPrimByte:
1204 case Primitive::kPrimChar:
1205 case Primitive::kPrimShort:
1206 case Primitive::kPrimInt:
1207 case Primitive::kPrimNot:
1208 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001209 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001210 break;
1211
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 case Primitive::kPrimFloat:
1213 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001214 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 break;
1216
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001217 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001220}
1221
1222void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1223 if (kIsDebugBuild) {
1224 switch (ret->InputAt(0)->GetType()) {
1225 case Primitive::kPrimBoolean:
1226 case Primitive::kPrimByte:
1227 case Primitive::kPrimChar:
1228 case Primitive::kPrimShort:
1229 case Primitive::kPrimInt:
1230 case Primitive::kPrimNot:
1231 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001233 break;
1234
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 case Primitive::kPrimFloat:
1236 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 XMM0);
1239 break;
1240
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243 }
1244 }
1245 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001246}
1247
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001248Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1249 switch (type) {
1250 case Primitive::kPrimBoolean:
1251 case Primitive::kPrimByte:
1252 case Primitive::kPrimChar:
1253 case Primitive::kPrimShort:
1254 case Primitive::kPrimInt:
1255 case Primitive::kPrimNot:
1256 case Primitive::kPrimLong:
1257 return Location::RegisterLocation(RAX);
1258
1259 case Primitive::kPrimVoid:
1260 return Location::NoLocation();
1261
1262 case Primitive::kPrimDouble:
1263 case Primitive::kPrimFloat:
1264 return Location::FpuRegisterLocation(XMM0);
1265 }
1266}
1267
1268Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1269 return Location::RegisterLocation(kMethodRegisterArgument);
1270}
1271
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001272Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273 switch (type) {
1274 case Primitive::kPrimBoolean:
1275 case Primitive::kPrimByte:
1276 case Primitive::kPrimChar:
1277 case Primitive::kPrimShort:
1278 case Primitive::kPrimInt:
1279 case Primitive::kPrimNot: {
1280 uint32_t index = gp_index_++;
1281 stack_index_++;
1282 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001283 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001284 } else {
1285 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1286 }
1287 }
1288
1289 case Primitive::kPrimLong: {
1290 uint32_t index = gp_index_;
1291 stack_index_ += 2;
1292 if (index < calling_convention.GetNumberOfRegisters()) {
1293 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001294 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001295 } else {
1296 gp_index_ += 2;
1297 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1298 }
1299 }
1300
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001301 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001302 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001303 stack_index_++;
1304 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001305 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 } else {
1307 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1308 }
1309 }
1310
1311 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001312 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001313 stack_index_ += 2;
1314 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001315 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001316 } else {
1317 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1318 }
1319 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001320
1321 case Primitive::kPrimVoid:
1322 LOG(FATAL) << "Unexpected parameter type " << type;
1323 break;
1324 }
1325 return Location();
1326}
1327
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001328void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001329 // When we do not run baseline, explicit clinit checks triggered by static
1330 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1331 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001332
Mark Mendellfb8d2792015-03-31 22:16:59 -04001333 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001334 if (intrinsic.TryDispatch(invoke)) {
1335 return;
1336 }
1337
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001338 HandleInvoke(invoke);
1339}
1340
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001341static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1342 if (invoke->GetLocations()->Intrinsified()) {
1343 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1344 intrinsic.Dispatch(invoke);
1345 return true;
1346 }
1347 return false;
1348}
1349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001350void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001351 // When we do not run baseline, explicit clinit checks triggered by static
1352 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1353 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001354
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001355 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1356 return;
1357 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001358
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001359 codegen_->GenerateStaticOrDirectCall(
1360 invoke,
1361 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001362 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001363}
1364
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001365void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001366 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001367 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001368}
1369
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001370void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001371 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001372 if (intrinsic.TryDispatch(invoke)) {
1373 return;
1374 }
1375
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001376 HandleInvoke(invoke);
1377}
1378
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001379void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001380 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1381 return;
1382 }
1383
Roland Levillain271ab9c2014-11-27 15:23:57 +00001384 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001385 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1386 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001387 LocationSummary* locations = invoke->GetLocations();
1388 Location receiver = locations->InAt(0);
1389 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1390 // temp = object->GetClass();
1391 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001392 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1393 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001394 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001395 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001396 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001397 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001398 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001399 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001400 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001401 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001402 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001403
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001404 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001405 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001406}
1407
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001408void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1409 HandleInvoke(invoke);
1410 // Add the hidden argument.
1411 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1412}
1413
1414void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1415 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001416 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001417 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1418 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001419 LocationSummary* locations = invoke->GetLocations();
1420 Location receiver = locations->InAt(0);
1421 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1422
1423 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001424 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1425 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001426
1427 // temp = object->GetClass();
1428 if (receiver.IsStackSlot()) {
1429 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1430 __ movl(temp, Address(temp, class_offset));
1431 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001432 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001433 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001434 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001435 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001436 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001437 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001438 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001439 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001440
1441 DCHECK(!codegen_->IsLeafMethod());
1442 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1443}
1444
Roland Levillain88cb1752014-10-20 16:36:47 +01001445void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1446 LocationSummary* locations =
1447 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1448 switch (neg->GetResultType()) {
1449 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001450 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001451 locations->SetInAt(0, Location::RequiresRegister());
1452 locations->SetOut(Location::SameAsFirstInput());
1453 break;
1454
Roland Levillain88cb1752014-10-20 16:36:47 +01001455 case Primitive::kPrimFloat:
1456 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001457 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001458 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001459 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001460 break;
1461
1462 default:
1463 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1464 }
1465}
1466
1467void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1468 LocationSummary* locations = neg->GetLocations();
1469 Location out = locations->Out();
1470 Location in = locations->InAt(0);
1471 switch (neg->GetResultType()) {
1472 case Primitive::kPrimInt:
1473 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001474 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001475 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001476 break;
1477
1478 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001479 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001480 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001481 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001482 break;
1483
Roland Levillain5368c212014-11-27 15:03:41 +00001484 case Primitive::kPrimFloat: {
1485 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001486 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001487 // Implement float negation with an exclusive or with value
1488 // 0x80000000 (mask for bit 31, representing the sign of a
1489 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001490 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001491 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001492 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001493 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001494
Roland Levillain5368c212014-11-27 15:03:41 +00001495 case Primitive::kPrimDouble: {
1496 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001497 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001498 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001499 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001500 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001501 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001502 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001503 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001504 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001505
1506 default:
1507 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1508 }
1509}
1510
Roland Levillaindff1f282014-11-05 14:15:05 +00001511void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1512 LocationSummary* locations =
1513 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1514 Primitive::Type result_type = conversion->GetResultType();
1515 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001516 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001517
David Brazdilb2bd1c52015-03-25 11:17:37 +00001518 // The Java language does not allow treating boolean as an integral type but
1519 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001520
Roland Levillaindff1f282014-11-05 14:15:05 +00001521 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001522 case Primitive::kPrimByte:
1523 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001524 case Primitive::kPrimBoolean:
1525 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001526 case Primitive::kPrimShort:
1527 case Primitive::kPrimInt:
1528 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001529 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001530 locations->SetInAt(0, Location::Any());
1531 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1532 break;
1533
1534 default:
1535 LOG(FATAL) << "Unexpected type conversion from " << input_type
1536 << " to " << result_type;
1537 }
1538 break;
1539
Roland Levillain01a8d712014-11-14 16:27:39 +00001540 case Primitive::kPrimShort:
1541 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001542 case Primitive::kPrimBoolean:
1543 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001544 case Primitive::kPrimByte:
1545 case Primitive::kPrimInt:
1546 case Primitive::kPrimChar:
1547 // Processing a Dex `int-to-short' instruction.
1548 locations->SetInAt(0, Location::Any());
1549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1550 break;
1551
1552 default:
1553 LOG(FATAL) << "Unexpected type conversion from " << input_type
1554 << " to " << result_type;
1555 }
1556 break;
1557
Roland Levillain946e1432014-11-11 17:35:19 +00001558 case Primitive::kPrimInt:
1559 switch (input_type) {
1560 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001561 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001562 locations->SetInAt(0, Location::Any());
1563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1564 break;
1565
1566 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001567 // Processing a Dex `float-to-int' instruction.
1568 locations->SetInAt(0, Location::RequiresFpuRegister());
1569 locations->SetOut(Location::RequiresRegister());
1570 locations->AddTemp(Location::RequiresFpuRegister());
1571 break;
1572
Roland Levillain946e1432014-11-11 17:35:19 +00001573 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001574 // Processing a Dex `double-to-int' instruction.
1575 locations->SetInAt(0, Location::RequiresFpuRegister());
1576 locations->SetOut(Location::RequiresRegister());
1577 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001578 break;
1579
1580 default:
1581 LOG(FATAL) << "Unexpected type conversion from " << input_type
1582 << " to " << result_type;
1583 }
1584 break;
1585
Roland Levillaindff1f282014-11-05 14:15:05 +00001586 case Primitive::kPrimLong:
1587 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001588 case Primitive::kPrimBoolean:
1589 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001590 case Primitive::kPrimByte:
1591 case Primitive::kPrimShort:
1592 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001593 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001594 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001595 // TODO: We would benefit from a (to-be-implemented)
1596 // Location::RegisterOrStackSlot requirement for this input.
1597 locations->SetInAt(0, Location::RequiresRegister());
1598 locations->SetOut(Location::RequiresRegister());
1599 break;
1600
1601 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001602 // Processing a Dex `float-to-long' instruction.
1603 locations->SetInAt(0, Location::RequiresFpuRegister());
1604 locations->SetOut(Location::RequiresRegister());
1605 locations->AddTemp(Location::RequiresFpuRegister());
1606 break;
1607
Roland Levillaindff1f282014-11-05 14:15:05 +00001608 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001609 // Processing a Dex `double-to-long' instruction.
1610 locations->SetInAt(0, Location::RequiresFpuRegister());
1611 locations->SetOut(Location::RequiresRegister());
1612 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001613 break;
1614
1615 default:
1616 LOG(FATAL) << "Unexpected type conversion from " << input_type
1617 << " to " << result_type;
1618 }
1619 break;
1620
Roland Levillain981e4542014-11-14 11:47:14 +00001621 case Primitive::kPrimChar:
1622 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001623 case Primitive::kPrimBoolean:
1624 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001625 case Primitive::kPrimByte:
1626 case Primitive::kPrimShort:
1627 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001628 // Processing a Dex `int-to-char' instruction.
1629 locations->SetInAt(0, Location::Any());
1630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1631 break;
1632
1633 default:
1634 LOG(FATAL) << "Unexpected type conversion from " << input_type
1635 << " to " << result_type;
1636 }
1637 break;
1638
Roland Levillaindff1f282014-11-05 14:15:05 +00001639 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001640 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001641 case Primitive::kPrimBoolean:
1642 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001643 case Primitive::kPrimByte:
1644 case Primitive::kPrimShort:
1645 case Primitive::kPrimInt:
1646 case Primitive::kPrimChar:
1647 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001648 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001649 locations->SetOut(Location::RequiresFpuRegister());
1650 break;
1651
1652 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001653 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001654 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001655 locations->SetOut(Location::RequiresFpuRegister());
1656 break;
1657
Roland Levillaincff13742014-11-17 14:32:17 +00001658 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001659 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001660 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001661 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001662 break;
1663
1664 default:
1665 LOG(FATAL) << "Unexpected type conversion from " << input_type
1666 << " to " << result_type;
1667 };
1668 break;
1669
Roland Levillaindff1f282014-11-05 14:15:05 +00001670 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001671 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001672 case Primitive::kPrimBoolean:
1673 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001674 case Primitive::kPrimByte:
1675 case Primitive::kPrimShort:
1676 case Primitive::kPrimInt:
1677 case Primitive::kPrimChar:
1678 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001679 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001680 locations->SetOut(Location::RequiresFpuRegister());
1681 break;
1682
1683 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001684 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001685 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001686 locations->SetOut(Location::RequiresFpuRegister());
1687 break;
1688
Roland Levillaincff13742014-11-17 14:32:17 +00001689 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001690 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001691 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001692 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001693 break;
1694
1695 default:
1696 LOG(FATAL) << "Unexpected type conversion from " << input_type
1697 << " to " << result_type;
1698 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001699 break;
1700
1701 default:
1702 LOG(FATAL) << "Unexpected type conversion from " << input_type
1703 << " to " << result_type;
1704 }
1705}
1706
1707void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1708 LocationSummary* locations = conversion->GetLocations();
1709 Location out = locations->Out();
1710 Location in = locations->InAt(0);
1711 Primitive::Type result_type = conversion->GetResultType();
1712 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001713 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001714 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001715 case Primitive::kPrimByte:
1716 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001717 case Primitive::kPrimBoolean:
1718 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001719 case Primitive::kPrimShort:
1720 case Primitive::kPrimInt:
1721 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001722 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001723 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001724 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001725 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001726 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001727 Address(CpuRegister(RSP), in.GetStackIndex()));
1728 } else {
1729 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001730 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001731 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1732 }
1733 break;
1734
1735 default:
1736 LOG(FATAL) << "Unexpected type conversion from " << input_type
1737 << " to " << result_type;
1738 }
1739 break;
1740
Roland Levillain01a8d712014-11-14 16:27:39 +00001741 case Primitive::kPrimShort:
1742 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001743 case Primitive::kPrimBoolean:
1744 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001745 case Primitive::kPrimByte:
1746 case Primitive::kPrimInt:
1747 case Primitive::kPrimChar:
1748 // Processing a Dex `int-to-short' instruction.
1749 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001751 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001752 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001753 Address(CpuRegister(RSP), in.GetStackIndex()));
1754 } else {
1755 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001757 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1758 }
1759 break;
1760
1761 default:
1762 LOG(FATAL) << "Unexpected type conversion from " << input_type
1763 << " to " << result_type;
1764 }
1765 break;
1766
Roland Levillain946e1432014-11-11 17:35:19 +00001767 case Primitive::kPrimInt:
1768 switch (input_type) {
1769 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001770 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001771 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001772 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001773 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001774 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001775 Address(CpuRegister(RSP), in.GetStackIndex()));
1776 } else {
1777 DCHECK(in.IsConstant());
1778 DCHECK(in.GetConstant()->IsLongConstant());
1779 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001780 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001781 }
1782 break;
1783
Roland Levillain3f8f9362014-12-02 17:45:01 +00001784 case Primitive::kPrimFloat: {
1785 // Processing a Dex `float-to-int' instruction.
1786 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1787 CpuRegister output = out.AsRegister<CpuRegister>();
1788 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1789 Label done, nan;
1790
1791 __ movl(output, Immediate(kPrimIntMax));
1792 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001793 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001794 // if input >= temp goto done
1795 __ comiss(input, temp);
1796 __ j(kAboveEqual, &done);
1797 // if input == NaN goto nan
1798 __ j(kUnordered, &nan);
1799 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001800 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001801 __ jmp(&done);
1802 __ Bind(&nan);
1803 // output = 0
1804 __ xorl(output, output);
1805 __ Bind(&done);
1806 break;
1807 }
1808
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001809 case Primitive::kPrimDouble: {
1810 // Processing a Dex `double-to-int' instruction.
1811 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1812 CpuRegister output = out.AsRegister<CpuRegister>();
1813 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1814 Label done, nan;
1815
1816 __ movl(output, Immediate(kPrimIntMax));
1817 // temp = int-to-double(output)
1818 __ cvtsi2sd(temp, output);
1819 // if input >= temp goto done
1820 __ comisd(input, temp);
1821 __ j(kAboveEqual, &done);
1822 // if input == NaN goto nan
1823 __ j(kUnordered, &nan);
1824 // output = double-to-int-truncate(input)
1825 __ cvttsd2si(output, input);
1826 __ jmp(&done);
1827 __ Bind(&nan);
1828 // output = 0
1829 __ xorl(output, output);
1830 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001831 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001832 }
Roland Levillain946e1432014-11-11 17:35:19 +00001833
1834 default:
1835 LOG(FATAL) << "Unexpected type conversion from " << input_type
1836 << " to " << result_type;
1837 }
1838 break;
1839
Roland Levillaindff1f282014-11-05 14:15:05 +00001840 case Primitive::kPrimLong:
1841 switch (input_type) {
1842 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001843 case Primitive::kPrimBoolean:
1844 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001845 case Primitive::kPrimByte:
1846 case Primitive::kPrimShort:
1847 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001848 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001849 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001850 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001851 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001852 break;
1853
Roland Levillain624279f2014-12-04 11:54:28 +00001854 case Primitive::kPrimFloat: {
1855 // Processing a Dex `float-to-long' instruction.
1856 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1857 CpuRegister output = out.AsRegister<CpuRegister>();
1858 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1859 Label done, nan;
1860
Mark Mendell92e83bf2015-05-07 11:25:03 -04001861 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001862 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001863 __ cvtsi2ss(temp, output, true);
1864 // if input >= temp goto done
1865 __ comiss(input, temp);
1866 __ j(kAboveEqual, &done);
1867 // if input == NaN goto nan
1868 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001869 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001870 __ cvttss2si(output, input, true);
1871 __ jmp(&done);
1872 __ Bind(&nan);
1873 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001874 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00001875 __ Bind(&done);
1876 break;
1877 }
1878
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001879 case Primitive::kPrimDouble: {
1880 // Processing a Dex `double-to-long' instruction.
1881 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1882 CpuRegister output = out.AsRegister<CpuRegister>();
1883 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1884 Label done, nan;
1885
Mark Mendell92e83bf2015-05-07 11:25:03 -04001886 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001887 // temp = long-to-double(output)
1888 __ cvtsi2sd(temp, output, true);
1889 // if input >= temp goto done
1890 __ comisd(input, temp);
1891 __ j(kAboveEqual, &done);
1892 // if input == NaN goto nan
1893 __ j(kUnordered, &nan);
1894 // output = double-to-long-truncate(input)
1895 __ cvttsd2si(output, input, true);
1896 __ jmp(&done);
1897 __ Bind(&nan);
1898 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04001899 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001900 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001901 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001902 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001903
1904 default:
1905 LOG(FATAL) << "Unexpected type conversion from " << input_type
1906 << " to " << result_type;
1907 }
1908 break;
1909
Roland Levillain981e4542014-11-14 11:47:14 +00001910 case Primitive::kPrimChar:
1911 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001912 case Primitive::kPrimBoolean:
1913 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001914 case Primitive::kPrimByte:
1915 case Primitive::kPrimShort:
1916 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001917 // Processing a Dex `int-to-char' instruction.
1918 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001919 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001920 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001921 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001922 Address(CpuRegister(RSP), in.GetStackIndex()));
1923 } else {
1924 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001925 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001926 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1927 }
1928 break;
1929
1930 default:
1931 LOG(FATAL) << "Unexpected type conversion from " << input_type
1932 << " to " << result_type;
1933 }
1934 break;
1935
Roland Levillaindff1f282014-11-05 14:15:05 +00001936 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001937 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001938 case Primitive::kPrimBoolean:
1939 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001940 case Primitive::kPrimByte:
1941 case Primitive::kPrimShort:
1942 case Primitive::kPrimInt:
1943 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001944 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001945 if (in.IsRegister()) {
1946 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
1947 } else if (in.IsConstant()) {
1948 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
1949 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1950 if (v == 0) {
1951 __ xorps(dest, dest);
1952 } else {
1953 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1954 }
1955 } else {
1956 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1957 Address(CpuRegister(RSP), in.GetStackIndex()), false);
1958 }
Roland Levillaincff13742014-11-17 14:32:17 +00001959 break;
1960
1961 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001962 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001963 if (in.IsRegister()) {
1964 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1965 } else if (in.IsConstant()) {
1966 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
1967 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1968 if (v == 0) {
1969 __ xorps(dest, dest);
1970 } else {
1971 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1972 }
1973 } else {
1974 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
1975 Address(CpuRegister(RSP), in.GetStackIndex()), true);
1976 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001977 break;
1978
Roland Levillaincff13742014-11-17 14:32:17 +00001979 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001980 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001981 if (in.IsFpuRegister()) {
1982 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
1983 } else if (in.IsConstant()) {
1984 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
1985 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
1986 if (bit_cast<int64_t, double>(v) == 0) {
1987 __ xorps(dest, dest);
1988 } else {
1989 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
1990 }
1991 } else {
1992 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
1993 Address(CpuRegister(RSP), in.GetStackIndex()));
1994 }
Roland Levillaincff13742014-11-17 14:32:17 +00001995 break;
1996
1997 default:
1998 LOG(FATAL) << "Unexpected type conversion from " << input_type
1999 << " to " << result_type;
2000 };
2001 break;
2002
Roland Levillaindff1f282014-11-05 14:15:05 +00002003 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002004 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002005 case Primitive::kPrimBoolean:
2006 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002007 case Primitive::kPrimByte:
2008 case Primitive::kPrimShort:
2009 case Primitive::kPrimInt:
2010 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002011 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002012 if (in.IsRegister()) {
2013 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2014 } else if (in.IsConstant()) {
2015 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2016 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2017 if (v == 0) {
2018 __ xorpd(dest, dest);
2019 } else {
2020 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2021 }
2022 } else {
2023 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2024 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2025 }
Roland Levillaincff13742014-11-17 14:32:17 +00002026 break;
2027
2028 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002029 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002030 if (in.IsRegister()) {
2031 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2032 } else if (in.IsConstant()) {
2033 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2034 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2035 if (v == 0) {
2036 __ xorpd(dest, dest);
2037 } else {
2038 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2039 }
2040 } else {
2041 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2042 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2043 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002044 break;
2045
Roland Levillaincff13742014-11-17 14:32:17 +00002046 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002047 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002048 if (in.IsFpuRegister()) {
2049 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2050 } else if (in.IsConstant()) {
2051 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2052 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2053 if (bit_cast<int32_t, float>(v) == 0) {
2054 __ xorpd(dest, dest);
2055 } else {
2056 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2057 }
2058 } else {
2059 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2060 Address(CpuRegister(RSP), in.GetStackIndex()));
2061 }
Roland Levillaincff13742014-11-17 14:32:17 +00002062 break;
2063
2064 default:
2065 LOG(FATAL) << "Unexpected type conversion from " << input_type
2066 << " to " << result_type;
2067 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002068 break;
2069
2070 default:
2071 LOG(FATAL) << "Unexpected type conversion from " << input_type
2072 << " to " << result_type;
2073 }
2074}
2075
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002076void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002077 LocationSummary* locations =
2078 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002079 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002080 case Primitive::kPrimInt: {
2081 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002082 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002084 break;
2085 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002086
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002087 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002088 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002089 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002090 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002092 break;
2093 }
2094
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002095 case Primitive::kPrimDouble:
2096 case Primitive::kPrimFloat: {
2097 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002098 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002099 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002100 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002101 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002102
2103 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002104 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002105 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002106}
2107
2108void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2109 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002110 Location first = locations->InAt(0);
2111 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002112 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002113
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002114 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002115 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002116 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002117 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2118 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002119 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2120 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002121 } else {
2122 __ leal(out.AsRegister<CpuRegister>(), Address(
2123 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2124 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002125 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002126 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2127 __ addl(out.AsRegister<CpuRegister>(),
2128 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2129 } else {
2130 __ leal(out.AsRegister<CpuRegister>(), Address(
2131 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2132 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002133 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002134 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002136 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002137 break;
2138 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002139
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002140 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002141 if (second.IsRegister()) {
2142 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2143 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002144 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2145 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002146 } else {
2147 __ leaq(out.AsRegister<CpuRegister>(), Address(
2148 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2149 }
2150 } else {
2151 DCHECK(second.IsConstant());
2152 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2153 int32_t int32_value = Low32Bits(value);
2154 DCHECK_EQ(int32_value, value);
2155 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2156 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2157 } else {
2158 __ leaq(out.AsRegister<CpuRegister>(), Address(
2159 first.AsRegister<CpuRegister>(), int32_value));
2160 }
2161 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002162 break;
2163 }
2164
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002165 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002166 if (second.IsFpuRegister()) {
2167 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2168 } else if (second.IsConstant()) {
2169 __ addss(first.AsFpuRegister<XmmRegister>(),
2170 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2171 } else {
2172 DCHECK(second.IsStackSlot());
2173 __ addss(first.AsFpuRegister<XmmRegister>(),
2174 Address(CpuRegister(RSP), second.GetStackIndex()));
2175 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002176 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002177 }
2178
2179 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002180 if (second.IsFpuRegister()) {
2181 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2182 } else if (second.IsConstant()) {
2183 __ addsd(first.AsFpuRegister<XmmRegister>(),
2184 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2185 } else {
2186 DCHECK(second.IsDoubleStackSlot());
2187 __ addsd(first.AsFpuRegister<XmmRegister>(),
2188 Address(CpuRegister(RSP), second.GetStackIndex()));
2189 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002190 break;
2191 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002192
2193 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002194 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002195 }
2196}
2197
2198void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002199 LocationSummary* locations =
2200 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002201 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002202 case Primitive::kPrimInt: {
2203 locations->SetInAt(0, Location::RequiresRegister());
2204 locations->SetInAt(1, Location::Any());
2205 locations->SetOut(Location::SameAsFirstInput());
2206 break;
2207 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002208 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002209 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002210 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002211 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212 break;
2213 }
Calin Juravle11351682014-10-23 15:38:15 +01002214 case Primitive::kPrimFloat:
2215 case Primitive::kPrimDouble: {
2216 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002217 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002218 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002219 break;
Calin Juravle11351682014-10-23 15:38:15 +01002220 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002221 default:
Calin Juravle11351682014-10-23 15:38:15 +01002222 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002223 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002224}
2225
2226void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2227 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002228 Location first = locations->InAt(0);
2229 Location second = locations->InAt(1);
2230 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002231 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002232 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002233 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002235 } else if (second.IsConstant()) {
2236 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002237 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002238 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002240 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002241 break;
2242 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002243 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002244 if (second.IsConstant()) {
2245 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2246 DCHECK(IsInt<32>(value));
2247 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2248 } else {
2249 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2250 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002251 break;
2252 }
2253
Calin Juravle11351682014-10-23 15:38:15 +01002254 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002255 if (second.IsFpuRegister()) {
2256 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2257 } else if (second.IsConstant()) {
2258 __ subss(first.AsFpuRegister<XmmRegister>(),
2259 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2260 } else {
2261 DCHECK(second.IsStackSlot());
2262 __ subss(first.AsFpuRegister<XmmRegister>(),
2263 Address(CpuRegister(RSP), second.GetStackIndex()));
2264 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002265 break;
Calin Juravle11351682014-10-23 15:38:15 +01002266 }
2267
2268 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002269 if (second.IsFpuRegister()) {
2270 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2271 } else if (second.IsConstant()) {
2272 __ subsd(first.AsFpuRegister<XmmRegister>(),
2273 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2274 } else {
2275 DCHECK(second.IsDoubleStackSlot());
2276 __ subsd(first.AsFpuRegister<XmmRegister>(),
2277 Address(CpuRegister(RSP), second.GetStackIndex()));
2278 }
Calin Juravle11351682014-10-23 15:38:15 +01002279 break;
2280 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002281
2282 default:
Calin Juravle11351682014-10-23 15:38:15 +01002283 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002284 }
2285}
2286
Calin Juravle34bacdf2014-10-07 20:23:36 +01002287void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2288 LocationSummary* locations =
2289 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2290 switch (mul->GetResultType()) {
2291 case Primitive::kPrimInt: {
2292 locations->SetInAt(0, Location::RequiresRegister());
2293 locations->SetInAt(1, Location::Any());
2294 locations->SetOut(Location::SameAsFirstInput());
2295 break;
2296 }
2297 case Primitive::kPrimLong: {
2298 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002299 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2300 if (locations->InAt(1).IsConstant()) {
2301 // Can use 3 operand multiply.
2302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2303 } else {
2304 locations->SetOut(Location::SameAsFirstInput());
2305 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002306 break;
2307 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002308 case Primitive::kPrimFloat:
2309 case Primitive::kPrimDouble: {
2310 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002311 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002312 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002313 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002314 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002315
2316 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002317 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002318 }
2319}
2320
2321void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2322 LocationSummary* locations = mul->GetLocations();
2323 Location first = locations->InAt(0);
2324 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002325 switch (mul->GetResultType()) {
2326 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002327 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002328 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002329 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002330 } else if (second.IsConstant()) {
2331 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002333 } else {
2334 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002335 __ imull(first.AsRegister<CpuRegister>(),
2336 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002337 }
2338 break;
2339 }
2340 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002341 if (second.IsConstant()) {
2342 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2343 DCHECK(IsInt<32>(value));
2344 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2345 first.AsRegister<CpuRegister>(),
2346 Immediate(static_cast<int32_t>(value)));
2347 } else {
2348 DCHECK(first.Equals(locations->Out()));
2349 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2350 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002351 break;
2352 }
2353
Calin Juravleb5bfa962014-10-21 18:02:24 +01002354 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002355 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002356 if (second.IsFpuRegister()) {
2357 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2358 } else if (second.IsConstant()) {
2359 __ mulss(first.AsFpuRegister<XmmRegister>(),
2360 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2361 } else {
2362 DCHECK(second.IsStackSlot());
2363 __ mulss(first.AsFpuRegister<XmmRegister>(),
2364 Address(CpuRegister(RSP), second.GetStackIndex()));
2365 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002366 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002367 }
2368
2369 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002370 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002371 if (second.IsFpuRegister()) {
2372 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2373 } else if (second.IsConstant()) {
2374 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2375 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2376 } else {
2377 DCHECK(second.IsDoubleStackSlot());
2378 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2379 Address(CpuRegister(RSP), second.GetStackIndex()));
2380 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002381 break;
2382 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002383
2384 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002385 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002386 }
2387}
2388
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002389void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2390 uint32_t stack_adjustment, bool is_float) {
2391 if (source.IsStackSlot()) {
2392 DCHECK(is_float);
2393 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2394 } else if (source.IsDoubleStackSlot()) {
2395 DCHECK(!is_float);
2396 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2397 } else {
2398 // Write the value to the temporary location on the stack and load to FP stack.
2399 if (is_float) {
2400 Location stack_temp = Location::StackSlot(temp_offset);
2401 codegen_->Move(stack_temp, source);
2402 __ flds(Address(CpuRegister(RSP), temp_offset));
2403 } else {
2404 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2405 codegen_->Move(stack_temp, source);
2406 __ fldl(Address(CpuRegister(RSP), temp_offset));
2407 }
2408 }
2409}
2410
2411void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2412 Primitive::Type type = rem->GetResultType();
2413 bool is_float = type == Primitive::kPrimFloat;
2414 size_t elem_size = Primitive::ComponentSize(type);
2415 LocationSummary* locations = rem->GetLocations();
2416 Location first = locations->InAt(0);
2417 Location second = locations->InAt(1);
2418 Location out = locations->Out();
2419
2420 // Create stack space for 2 elements.
2421 // TODO: enhance register allocator to ask for stack temporaries.
2422 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2423
2424 // Load the values to the FP stack in reverse order, using temporaries if needed.
2425 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2426 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2427
2428 // Loop doing FPREM until we stabilize.
2429 Label retry;
2430 __ Bind(&retry);
2431 __ fprem();
2432
2433 // Move FP status to AX.
2434 __ fstsw();
2435
2436 // And see if the argument reduction is complete. This is signaled by the
2437 // C2 FPU flag bit set to 0.
2438 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2439 __ j(kNotEqual, &retry);
2440
2441 // We have settled on the final value. Retrieve it into an XMM register.
2442 // Store FP top of stack to real stack.
2443 if (is_float) {
2444 __ fsts(Address(CpuRegister(RSP), 0));
2445 } else {
2446 __ fstl(Address(CpuRegister(RSP), 0));
2447 }
2448
2449 // Pop the 2 items from the FP stack.
2450 __ fucompp();
2451
2452 // Load the value from the stack into an XMM register.
2453 DCHECK(out.IsFpuRegister()) << out;
2454 if (is_float) {
2455 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2456 } else {
2457 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2458 }
2459
2460 // And remove the temporary stack space we allocated.
2461 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2462}
2463
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002464void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2465 DCHECK(instruction->IsDiv() || instruction->IsRem());
2466
2467 LocationSummary* locations = instruction->GetLocations();
2468 Location second = locations->InAt(1);
2469 DCHECK(second.IsConstant());
2470
2471 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2472 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002473 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002474
2475 DCHECK(imm == 1 || imm == -1);
2476
2477 switch (instruction->GetResultType()) {
2478 case Primitive::kPrimInt: {
2479 if (instruction->IsRem()) {
2480 __ xorl(output_register, output_register);
2481 } else {
2482 __ movl(output_register, input_register);
2483 if (imm == -1) {
2484 __ negl(output_register);
2485 }
2486 }
2487 break;
2488 }
2489
2490 case Primitive::kPrimLong: {
2491 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002492 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002493 } else {
2494 __ movq(output_register, input_register);
2495 if (imm == -1) {
2496 __ negq(output_register);
2497 }
2498 }
2499 break;
2500 }
2501
2502 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002503 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002504 }
2505}
2506
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002507void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002508 LocationSummary* locations = instruction->GetLocations();
2509 Location second = locations->InAt(1);
2510
2511 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2512 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2513
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002514 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002515
2516 DCHECK(IsPowerOfTwo(std::abs(imm)));
2517
2518 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2519
2520 if (instruction->GetResultType() == Primitive::kPrimInt) {
2521 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2522 __ testl(numerator, numerator);
2523 __ cmov(kGreaterEqual, tmp, numerator);
2524 int shift = CTZ(imm);
2525 __ sarl(tmp, Immediate(shift));
2526
2527 if (imm < 0) {
2528 __ negl(tmp);
2529 }
2530
2531 __ movl(output_register, tmp);
2532 } else {
2533 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2534 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2535
Mark Mendell92e83bf2015-05-07 11:25:03 -04002536 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002537 __ addq(rdx, numerator);
2538 __ testq(numerator, numerator);
2539 __ cmov(kGreaterEqual, rdx, numerator);
2540 int shift = CTZ(imm);
2541 __ sarq(rdx, Immediate(shift));
2542
2543 if (imm < 0) {
2544 __ negq(rdx);
2545 }
2546
2547 __ movq(output_register, rdx);
2548 }
2549}
2550
2551void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2552 DCHECK(instruction->IsDiv() || instruction->IsRem());
2553
2554 LocationSummary* locations = instruction->GetLocations();
2555 Location second = locations->InAt(1);
2556
2557 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2558 : locations->GetTemp(0).AsRegister<CpuRegister>();
2559 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2560 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2561 : locations->Out().AsRegister<CpuRegister>();
2562 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2563
2564 DCHECK_EQ(RAX, eax.AsRegister());
2565 DCHECK_EQ(RDX, edx.AsRegister());
2566 if (instruction->IsDiv()) {
2567 DCHECK_EQ(RAX, out.AsRegister());
2568 } else {
2569 DCHECK_EQ(RDX, out.AsRegister());
2570 }
2571
2572 int64_t magic;
2573 int shift;
2574
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002575 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002576 if (instruction->GetResultType() == Primitive::kPrimInt) {
2577 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2578
2579 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2580
2581 __ movl(numerator, eax);
2582
2583 Label no_div;
2584 Label end;
2585 __ testl(eax, eax);
2586 __ j(kNotEqual, &no_div);
2587
2588 __ xorl(out, out);
2589 __ jmp(&end);
2590
2591 __ Bind(&no_div);
2592
2593 __ movl(eax, Immediate(magic));
2594 __ imull(numerator);
2595
2596 if (imm > 0 && magic < 0) {
2597 __ addl(edx, numerator);
2598 } else if (imm < 0 && magic > 0) {
2599 __ subl(edx, numerator);
2600 }
2601
2602 if (shift != 0) {
2603 __ sarl(edx, Immediate(shift));
2604 }
2605
2606 __ movl(eax, edx);
2607 __ shrl(edx, Immediate(31));
2608 __ addl(edx, eax);
2609
2610 if (instruction->IsRem()) {
2611 __ movl(eax, numerator);
2612 __ imull(edx, Immediate(imm));
2613 __ subl(eax, edx);
2614 __ movl(edx, eax);
2615 } else {
2616 __ movl(eax, edx);
2617 }
2618 __ Bind(&end);
2619 } else {
2620 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2621
2622 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2623
2624 CpuRegister rax = eax;
2625 CpuRegister rdx = edx;
2626
2627 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2628
2629 // Save the numerator.
2630 __ movq(numerator, rax);
2631
2632 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002633 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002634
2635 // RDX:RAX = magic * numerator
2636 __ imulq(numerator);
2637
2638 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002639 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002640 __ addq(rdx, numerator);
2641 } else if (imm < 0 && magic > 0) {
2642 // RDX -= numerator
2643 __ subq(rdx, numerator);
2644 }
2645
2646 // Shift if needed.
2647 if (shift != 0) {
2648 __ sarq(rdx, Immediate(shift));
2649 }
2650
2651 // RDX += 1 if RDX < 0
2652 __ movq(rax, rdx);
2653 __ shrq(rdx, Immediate(63));
2654 __ addq(rdx, rax);
2655
2656 if (instruction->IsRem()) {
2657 __ movq(rax, numerator);
2658
2659 if (IsInt<32>(imm)) {
2660 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2661 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002662 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002663 }
2664
2665 __ subq(rax, rdx);
2666 __ movq(rdx, rax);
2667 } else {
2668 __ movq(rax, rdx);
2669 }
2670 }
2671}
2672
Calin Juravlebacfec32014-11-14 15:54:36 +00002673void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2674 DCHECK(instruction->IsDiv() || instruction->IsRem());
2675 Primitive::Type type = instruction->GetResultType();
2676 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2677
2678 bool is_div = instruction->IsDiv();
2679 LocationSummary* locations = instruction->GetLocations();
2680
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002681 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2682 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002683
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002685 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002686
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002687 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002688 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002689
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002690 if (imm == 0) {
2691 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2692 } else if (imm == 1 || imm == -1) {
2693 DivRemOneOrMinusOne(instruction);
2694 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002695 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002696 } else {
2697 DCHECK(imm <= -2 || imm >= 2);
2698 GenerateDivRemWithAnyConstant(instruction);
2699 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002700 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002701 SlowPathCodeX86_64* slow_path =
2702 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2703 out.AsRegister(), type, is_div);
2704 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002705
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002706 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2707 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2708 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2709 // so it's safe to just use negl instead of more complex comparisons.
2710 if (type == Primitive::kPrimInt) {
2711 __ cmpl(second_reg, Immediate(-1));
2712 __ j(kEqual, slow_path->GetEntryLabel());
2713 // edx:eax <- sign-extended of eax
2714 __ cdq();
2715 // eax = quotient, edx = remainder
2716 __ idivl(second_reg);
2717 } else {
2718 __ cmpq(second_reg, Immediate(-1));
2719 __ j(kEqual, slow_path->GetEntryLabel());
2720 // rdx:rax <- sign-extended of rax
2721 __ cqo();
2722 // rax = quotient, rdx = remainder
2723 __ idivq(second_reg);
2724 }
2725 __ Bind(slow_path->GetExitLabel());
2726 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002727}
2728
Calin Juravle7c4954d2014-10-28 16:57:40 +00002729void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2730 LocationSummary* locations =
2731 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2732 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002733 case Primitive::kPrimInt:
2734 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002735 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002736 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002737 locations->SetOut(Location::SameAsFirstInput());
2738 // Intel uses edx:eax as the dividend.
2739 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002740 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
2741 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
2742 // output and request another temp.
2743 if (div->InputAt(1)->IsConstant()) {
2744 locations->AddTemp(Location::RequiresRegister());
2745 }
Calin Juravled0d48522014-11-04 16:40:20 +00002746 break;
2747 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002748
Calin Juravle7c4954d2014-10-28 16:57:40 +00002749 case Primitive::kPrimFloat:
2750 case Primitive::kPrimDouble: {
2751 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002752 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002753 locations->SetOut(Location::SameAsFirstInput());
2754 break;
2755 }
2756
2757 default:
2758 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2759 }
2760}
2761
2762void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2763 LocationSummary* locations = div->GetLocations();
2764 Location first = locations->InAt(0);
2765 Location second = locations->InAt(1);
2766 DCHECK(first.Equals(locations->Out()));
2767
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002768 Primitive::Type type = div->GetResultType();
2769 switch (type) {
2770 case Primitive::kPrimInt:
2771 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002772 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002773 break;
2774 }
2775
Calin Juravle7c4954d2014-10-28 16:57:40 +00002776 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002777 if (second.IsFpuRegister()) {
2778 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2779 } else if (second.IsConstant()) {
2780 __ divss(first.AsFpuRegister<XmmRegister>(),
2781 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2782 } else {
2783 DCHECK(second.IsStackSlot());
2784 __ divss(first.AsFpuRegister<XmmRegister>(),
2785 Address(CpuRegister(RSP), second.GetStackIndex()));
2786 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002787 break;
2788 }
2789
2790 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002791 if (second.IsFpuRegister()) {
2792 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2793 } else if (second.IsConstant()) {
2794 __ divsd(first.AsFpuRegister<XmmRegister>(),
2795 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2796 } else {
2797 DCHECK(second.IsDoubleStackSlot());
2798 __ divsd(first.AsFpuRegister<XmmRegister>(),
2799 Address(CpuRegister(RSP), second.GetStackIndex()));
2800 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002801 break;
2802 }
2803
2804 default:
2805 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2806 }
2807}
2808
Calin Juravlebacfec32014-11-14 15:54:36 +00002809void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002810 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002811 LocationSummary* locations =
2812 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002813
2814 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002815 case Primitive::kPrimInt:
2816 case Primitive::kPrimLong: {
2817 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002818 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002819 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2820 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002821 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2822 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
2823 // output and request another temp.
2824 if (rem->InputAt(1)->IsConstant()) {
2825 locations->AddTemp(Location::RequiresRegister());
2826 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002827 break;
2828 }
2829
2830 case Primitive::kPrimFloat:
2831 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002832 locations->SetInAt(0, Location::Any());
2833 locations->SetInAt(1, Location::Any());
2834 locations->SetOut(Location::RequiresFpuRegister());
2835 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002836 break;
2837 }
2838
2839 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002840 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002841 }
2842}
2843
2844void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2845 Primitive::Type type = rem->GetResultType();
2846 switch (type) {
2847 case Primitive::kPrimInt:
2848 case Primitive::kPrimLong: {
2849 GenerateDivRemIntegral(rem);
2850 break;
2851 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002852 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002853 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002854 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002855 break;
2856 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002857 default:
2858 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2859 }
2860}
2861
Calin Juravled0d48522014-11-04 16:40:20 +00002862void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2863 LocationSummary* locations =
2864 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2865 locations->SetInAt(0, Location::Any());
2866 if (instruction->HasUses()) {
2867 locations->SetOut(Location::SameAsFirstInput());
2868 }
2869}
2870
2871void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2872 SlowPathCodeX86_64* slow_path =
2873 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2874 codegen_->AddSlowPath(slow_path);
2875
2876 LocationSummary* locations = instruction->GetLocations();
2877 Location value = locations->InAt(0);
2878
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002879 switch (instruction->GetType()) {
2880 case Primitive::kPrimInt: {
2881 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002883 __ j(kEqual, slow_path->GetEntryLabel());
2884 } else if (value.IsStackSlot()) {
2885 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2886 __ j(kEqual, slow_path->GetEntryLabel());
2887 } else {
2888 DCHECK(value.IsConstant()) << value;
2889 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2890 __ jmp(slow_path->GetEntryLabel());
2891 }
2892 }
2893 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002894 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002895 case Primitive::kPrimLong: {
2896 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002898 __ j(kEqual, slow_path->GetEntryLabel());
2899 } else if (value.IsDoubleStackSlot()) {
2900 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2901 __ j(kEqual, slow_path->GetEntryLabel());
2902 } else {
2903 DCHECK(value.IsConstant()) << value;
2904 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2905 __ jmp(slow_path->GetEntryLabel());
2906 }
2907 }
2908 break;
2909 }
2910 default:
2911 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002912 }
Calin Juravled0d48522014-11-04 16:40:20 +00002913}
2914
Calin Juravle9aec02f2014-11-18 23:06:35 +00002915void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2916 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2917
2918 LocationSummary* locations =
2919 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2920
2921 switch (op->GetResultType()) {
2922 case Primitive::kPrimInt:
2923 case Primitive::kPrimLong: {
2924 locations->SetInAt(0, Location::RequiresRegister());
2925 // The shift count needs to be in CL.
2926 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2927 locations->SetOut(Location::SameAsFirstInput());
2928 break;
2929 }
2930 default:
2931 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2932 }
2933}
2934
2935void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2936 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2937
2938 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002940 Location second = locations->InAt(1);
2941
2942 switch (op->GetResultType()) {
2943 case Primitive::kPrimInt: {
2944 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002945 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002946 if (op->IsShl()) {
2947 __ shll(first_reg, second_reg);
2948 } else if (op->IsShr()) {
2949 __ sarl(first_reg, second_reg);
2950 } else {
2951 __ shrl(first_reg, second_reg);
2952 }
2953 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002954 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002955 if (op->IsShl()) {
2956 __ shll(first_reg, imm);
2957 } else if (op->IsShr()) {
2958 __ sarl(first_reg, imm);
2959 } else {
2960 __ shrl(first_reg, imm);
2961 }
2962 }
2963 break;
2964 }
2965 case Primitive::kPrimLong: {
2966 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002967 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002968 if (op->IsShl()) {
2969 __ shlq(first_reg, second_reg);
2970 } else if (op->IsShr()) {
2971 __ sarq(first_reg, second_reg);
2972 } else {
2973 __ shrq(first_reg, second_reg);
2974 }
2975 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002976 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002977 if (op->IsShl()) {
2978 __ shlq(first_reg, imm);
2979 } else if (op->IsShr()) {
2980 __ sarq(first_reg, imm);
2981 } else {
2982 __ shrq(first_reg, imm);
2983 }
2984 }
2985 break;
2986 }
2987 default:
2988 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2989 }
2990}
2991
2992void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2993 HandleShift(shl);
2994}
2995
2996void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2997 HandleShift(shl);
2998}
2999
3000void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3001 HandleShift(shr);
3002}
3003
3004void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3005 HandleShift(shr);
3006}
3007
3008void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3009 HandleShift(ushr);
3010}
3011
3012void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3013 HandleShift(ushr);
3014}
3015
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003016void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003019 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003020 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3021 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3022 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003023}
3024
3025void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3026 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003027 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Mark Mendell92e83bf2015-05-07 11:25:03 -04003028 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3029 instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003030 __ gs()->call(
3031 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003032
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003033 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003034 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035}
3036
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003037void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3038 LocationSummary* locations =
3039 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3040 InvokeRuntimeCallingConvention calling_convention;
3041 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003042 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003043 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003044 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003045}
3046
3047void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3048 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003049 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Mark Mendell92e83bf2015-05-07 11:25:03 -04003050 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3051 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003052
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003053 __ gs()->call(
3054 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003055
3056 DCHECK(!codegen_->IsLeafMethod());
3057 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3058}
3059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003060void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003061 LocationSummary* locations =
3062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003063 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3064 if (location.IsStackSlot()) {
3065 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3066 } else if (location.IsDoubleStackSlot()) {
3067 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3068 }
3069 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070}
3071
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003072void InstructionCodeGeneratorX86_64::VisitParameterValue(
3073 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003075}
3076
3077void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3080 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3081}
3082
3083void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3084 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3085 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003086}
3087
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003088void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003089 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003090 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003091 locations->SetInAt(0, Location::RequiresRegister());
3092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093}
3094
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003095void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3096 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3098 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003099 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003100 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003101 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003103 break;
3104
3105 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003107 break;
3108
3109 default:
3110 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3111 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003112}
3113
David Brazdil66d126e2015-04-03 16:02:44 +01003114void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3115 LocationSummary* locations =
3116 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3117 locations->SetInAt(0, Location::RequiresRegister());
3118 locations->SetOut(Location::SameAsFirstInput());
3119}
3120
3121void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003122 LocationSummary* locations = bool_not->GetLocations();
3123 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3124 locations->Out().AsRegister<CpuRegister>().AsRegister());
3125 Location out = locations->Out();
3126 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3127}
3128
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003129void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003130 LocationSummary* locations =
3131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003132 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3133 locations->SetInAt(i, Location::Any());
3134 }
3135 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003136}
3137
3138void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003139 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003140 LOG(FATAL) << "Unimplemented";
3141}
3142
Calin Juravle52c48962014-12-16 17:02:57 +00003143void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3144 /*
3145 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3146 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3147 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3148 */
3149 switch (kind) {
3150 case MemBarrierKind::kAnyAny: {
3151 __ mfence();
3152 break;
3153 }
3154 case MemBarrierKind::kAnyStore:
3155 case MemBarrierKind::kLoadAny:
3156 case MemBarrierKind::kStoreStore: {
3157 // nop
3158 break;
3159 }
3160 default:
3161 LOG(FATAL) << "Unexpected memory barier " << kind;
3162 }
3163}
3164
3165void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3166 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3167
Nicolas Geoffray39468442014-09-02 15:17:15 +01003168 LocationSummary* locations =
3169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003170 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003171 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3172 locations->SetOut(Location::RequiresFpuRegister());
3173 } else {
3174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3175 }
Calin Juravle52c48962014-12-16 17:02:57 +00003176}
3177
3178void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3179 const FieldInfo& field_info) {
3180 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3181
3182 LocationSummary* locations = instruction->GetLocations();
3183 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3184 Location out = locations->Out();
3185 bool is_volatile = field_info.IsVolatile();
3186 Primitive::Type field_type = field_info.GetFieldType();
3187 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3188
3189 switch (field_type) {
3190 case Primitive::kPrimBoolean: {
3191 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3192 break;
3193 }
3194
3195 case Primitive::kPrimByte: {
3196 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3197 break;
3198 }
3199
3200 case Primitive::kPrimShort: {
3201 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3202 break;
3203 }
3204
3205 case Primitive::kPrimChar: {
3206 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3207 break;
3208 }
3209
3210 case Primitive::kPrimInt:
3211 case Primitive::kPrimNot: {
3212 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3213 break;
3214 }
3215
3216 case Primitive::kPrimLong: {
3217 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3218 break;
3219 }
3220
3221 case Primitive::kPrimFloat: {
3222 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3223 break;
3224 }
3225
3226 case Primitive::kPrimDouble: {
3227 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3228 break;
3229 }
3230
3231 case Primitive::kPrimVoid:
3232 LOG(FATAL) << "Unreachable type " << field_type;
3233 UNREACHABLE();
3234 }
3235
Calin Juravle77520bc2015-01-12 18:45:46 +00003236 codegen_->MaybeRecordImplicitNullCheck(instruction);
3237
Calin Juravle52c48962014-12-16 17:02:57 +00003238 if (is_volatile) {
3239 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3240 }
3241}
3242
3243void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3244 const FieldInfo& field_info) {
3245 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3246
3247 LocationSummary* locations =
3248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003249 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00003250 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
3251
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003252 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003253 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3254 locations->SetInAt(1, Location::RequiresFpuRegister());
3255 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003256 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003257 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003258 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003259 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003260 locations->AddTemp(Location::RequiresRegister());
3261 locations->AddTemp(Location::RequiresRegister());
3262 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003263}
3264
Calin Juravle52c48962014-12-16 17:02:57 +00003265void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003266 const FieldInfo& field_info,
3267 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003268 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3269
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003270 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003271 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3272 Location value = locations->InAt(1);
3273 bool is_volatile = field_info.IsVolatile();
3274 Primitive::Type field_type = field_info.GetFieldType();
3275 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3276
3277 if (is_volatile) {
3278 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3279 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280
3281 switch (field_type) {
3282 case Primitive::kPrimBoolean:
3283 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003284 if (value.IsConstant()) {
3285 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3286 __ movb(Address(base, offset), Immediate(v));
3287 } else {
3288 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3289 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003290 break;
3291 }
3292
3293 case Primitive::kPrimShort:
3294 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003295 if (value.IsConstant()) {
3296 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3297 __ movw(Address(base, offset), Immediate(v));
3298 } else {
3299 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3300 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003301 break;
3302 }
3303
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003304 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003305 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003306 if (value.IsConstant()) {
3307 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3308 __ movw(Address(base, offset), Immediate(v));
3309 } else {
3310 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3311 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003312 break;
3313 }
3314
3315 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003316 if (value.IsConstant()) {
3317 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3318 DCHECK(IsInt<32>(v));
3319 int32_t v_32 = v;
3320 __ movq(Address(base, offset), Immediate(v_32));
3321 } else {
3322 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3323 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003324 break;
3325 }
3326
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003327 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003328 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003329 break;
3330 }
3331
3332 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003333 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003334 break;
3335 }
3336
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003337 case Primitive::kPrimVoid:
3338 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003339 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340 }
Calin Juravle52c48962014-12-16 17:02:57 +00003341
Calin Juravle77520bc2015-01-12 18:45:46 +00003342 codegen_->MaybeRecordImplicitNullCheck(instruction);
3343
3344 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3345 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3346 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003347 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003348 }
3349
Calin Juravle52c48962014-12-16 17:02:57 +00003350 if (is_volatile) {
3351 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3352 }
3353}
3354
3355void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3356 HandleFieldSet(instruction, instruction->GetFieldInfo());
3357}
3358
3359void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003360 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003361}
3362
3363void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003364 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003365}
3366
3367void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003368 HandleFieldGet(instruction, instruction->GetFieldInfo());
3369}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003370
Calin Juravle52c48962014-12-16 17:02:57 +00003371void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3372 HandleFieldGet(instruction);
3373}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003374
Calin Juravle52c48962014-12-16 17:02:57 +00003375void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3376 HandleFieldGet(instruction, instruction->GetFieldInfo());
3377}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003378
Calin Juravle52c48962014-12-16 17:02:57 +00003379void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3380 HandleFieldSet(instruction, instruction->GetFieldInfo());
3381}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003382
Calin Juravle52c48962014-12-16 17:02:57 +00003383void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003384 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003385}
3386
3387void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003388 LocationSummary* locations =
3389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003390 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3391 ? Location::RequiresRegister()
3392 : Location::Any();
3393 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003394 if (instruction->HasUses()) {
3395 locations->SetOut(Location::SameAsFirstInput());
3396 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003397}
3398
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003399void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003400 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3401 return;
3402 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003403 LocationSummary* locations = instruction->GetLocations();
3404 Location obj = locations->InAt(0);
3405
3406 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3407 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3408}
3409
3410void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003411 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003412 codegen_->AddSlowPath(slow_path);
3413
3414 LocationSummary* locations = instruction->GetLocations();
3415 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003416
3417 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003418 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003419 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003421 } else {
3422 DCHECK(obj.IsConstant()) << obj;
3423 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3424 __ jmp(slow_path->GetEntryLabel());
3425 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003426 }
3427 __ j(kEqual, slow_path->GetEntryLabel());
3428}
3429
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003430void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3431 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3432 GenerateImplicitNullCheck(instruction);
3433 } else {
3434 GenerateExplicitNullCheck(instruction);
3435 }
3436}
3437
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003438void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003439 LocationSummary* locations =
3440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003441 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003442 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003443 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3444 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3445 } else {
3446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3447 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003448}
3449
3450void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3451 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003452 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 Location index = locations->InAt(1);
3454
3455 switch (instruction->GetType()) {
3456 case Primitive::kPrimBoolean: {
3457 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003458 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003459 if (index.IsConstant()) {
3460 __ movzxb(out, Address(obj,
3461 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3462 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 }
3465 break;
3466 }
3467
3468 case Primitive::kPrimByte: {
3469 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003470 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003471 if (index.IsConstant()) {
3472 __ movsxb(out, Address(obj,
3473 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3474 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003475 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003476 }
3477 break;
3478 }
3479
3480 case Primitive::kPrimShort: {
3481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003483 if (index.IsConstant()) {
3484 __ movsxw(out, Address(obj,
3485 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3486 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488 }
3489 break;
3490 }
3491
3492 case Primitive::kPrimChar: {
3493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003495 if (index.IsConstant()) {
3496 __ movzxw(out, Address(obj,
3497 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3498 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500 }
3501 break;
3502 }
3503
3504 case Primitive::kPrimInt:
3505 case Primitive::kPrimNot: {
3506 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3507 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003508 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003509 if (index.IsConstant()) {
3510 __ movl(out, Address(obj,
3511 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3512 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003513 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003514 }
3515 break;
3516 }
3517
3518 case Primitive::kPrimLong: {
3519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003520 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003521 if (index.IsConstant()) {
3522 __ movq(out, Address(obj,
3523 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3524 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003525 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003526 }
3527 break;
3528 }
3529
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003530 case Primitive::kPrimFloat: {
3531 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003532 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003533 if (index.IsConstant()) {
3534 __ movss(out, Address(obj,
3535 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3536 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003537 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003538 }
3539 break;
3540 }
3541
3542 case Primitive::kPrimDouble: {
3543 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003544 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003545 if (index.IsConstant()) {
3546 __ movsd(out, Address(obj,
3547 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3548 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003550 }
3551 break;
3552 }
3553
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003554 case Primitive::kPrimVoid:
3555 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003556 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003557 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003558 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003559}
3560
3561void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003562 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003563
3564 bool needs_write_barrier =
3565 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3566 bool needs_runtime_call = instruction->NeedsTypeCheck();
3567
Nicolas Geoffray39468442014-09-02 15:17:15 +01003568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003569 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3570 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003571 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003572 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3573 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3574 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003576 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003577 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003578 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3579 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003580 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003581 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003582 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3583 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003584 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003585 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003586 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003587
3588 if (needs_write_barrier) {
3589 // Temporary registers for the write barrier.
3590 locations->AddTemp(Location::RequiresRegister());
3591 locations->AddTemp(Location::RequiresRegister());
3592 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003594}
3595
3596void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3597 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003598 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003599 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003600 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003601 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003602 bool needs_runtime_call = locations->WillCall();
3603 bool needs_write_barrier =
3604 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003605
3606 switch (value_type) {
3607 case Primitive::kPrimBoolean:
3608 case Primitive::kPrimByte: {
3609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 if (index.IsConstant()) {
3611 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003612 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003613 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003614 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003615 __ movb(Address(obj, offset),
3616 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003617 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003618 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003619 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3621 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003622 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003624 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3625 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003626 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003627 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 break;
3629 }
3630
3631 case Primitive::kPrimShort:
3632 case Primitive::kPrimChar: {
3633 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 if (index.IsConstant()) {
3635 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003636 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003638 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003639 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003640 __ movw(Address(obj, offset),
3641 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003644 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003645 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3647 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003648 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003649 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003651 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3652 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003654 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003655 break;
3656 }
3657
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003658 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003660 if (!needs_runtime_call) {
3661 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3662 if (index.IsConstant()) {
3663 size_t offset =
3664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3665 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003667 } else {
3668 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003669 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3670 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003671 }
3672 } else {
3673 DCHECK(index.IsRegister()) << index;
3674 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003675 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3676 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003677 } else {
3678 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003679 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003681 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003682 }
3683 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003684 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003685 if (needs_write_barrier) {
3686 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3688 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003689 codegen_->MarkGCCard(
3690 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003691 }
3692 } else {
3693 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003694 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3695 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003696 DCHECK(!codegen_->IsLeafMethod());
3697 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3698 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003699 break;
3700 }
3701
3702 case Primitive::kPrimLong: {
3703 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003704 if (index.IsConstant()) {
3705 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04003706 if (value.IsRegister()) {
3707 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
3708 } else {
3709 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3710 DCHECK(IsInt<32>(v));
3711 int32_t v_32 = v;
3712 __ movq(Address(obj, offset), Immediate(v_32));
3713 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003714 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003715 if (value.IsRegister()) {
3716 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3717 value.AsRegister<CpuRegister>());
3718 } else {
3719 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3720 DCHECK(IsInt<32>(v));
3721 int32_t v_32 = v;
3722 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3723 Immediate(v_32));
3724 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003726 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 break;
3728 }
3729
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003730 case Primitive::kPrimFloat: {
3731 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3732 if (index.IsConstant()) {
3733 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3734 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003735 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003736 } else {
3737 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3739 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003740 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003741 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003742 break;
3743 }
3744
3745 case Primitive::kPrimDouble: {
3746 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3747 if (index.IsConstant()) {
3748 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3749 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003751 } else {
3752 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3754 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003755 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003756 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003757 break;
3758 }
3759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003760 case Primitive::kPrimVoid:
3761 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003762 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 }
3764}
3765
3766void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003767 LocationSummary* locations =
3768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003769 locations->SetInAt(0, Location::RequiresRegister());
3770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771}
3772
3773void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3774 LocationSummary* locations = instruction->GetLocations();
3775 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3777 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003780}
3781
3782void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003783 LocationSummary* locations =
3784 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003785 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003786 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003787 if (instruction->HasUses()) {
3788 locations->SetOut(Location::SameAsFirstInput());
3789 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003790}
3791
3792void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3793 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003794 Location index_loc = locations->InAt(0);
3795 Location length_loc = locations->InAt(1);
3796 SlowPathCodeX86_64* slow_path =
3797 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003798
Mark Mendell99dbd682015-04-22 16:18:52 -04003799 if (length_loc.IsConstant()) {
3800 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3801 if (index_loc.IsConstant()) {
3802 // BCE will remove the bounds check if we are guarenteed to pass.
3803 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3804 if (index < 0 || index >= length) {
3805 codegen_->AddSlowPath(slow_path);
3806 __ jmp(slow_path->GetEntryLabel());
3807 } else {
3808 // Some optimization after BCE may have generated this, and we should not
3809 // generate a bounds check if it is a valid range.
3810 }
3811 return;
3812 }
3813
3814 // We have to reverse the jump condition because the length is the constant.
3815 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
3816 __ cmpl(index_reg, Immediate(length));
3817 codegen_->AddSlowPath(slow_path);
3818 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003819 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003820 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3821 if (index_loc.IsConstant()) {
3822 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3823 __ cmpl(length, Immediate(value));
3824 } else {
3825 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3826 }
3827 codegen_->AddSlowPath(slow_path);
3828 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003829 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830}
3831
3832void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3833 CpuRegister card,
3834 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003835 CpuRegister value,
3836 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003837 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003838 if (value_can_be_null) {
3839 __ testl(value, value);
3840 __ j(kEqual, &is_null);
3841 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 __ gs()->movq(card, Address::Absolute(
3843 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3844 __ movq(temp, object);
3845 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3846 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003847 if (value_can_be_null) {
3848 __ Bind(&is_null);
3849 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003850}
3851
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003852void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3853 temp->SetLocations(nullptr);
3854}
3855
3856void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3857 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003858 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003859}
3860
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003861void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003862 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003863 LOG(FATAL) << "Unimplemented";
3864}
3865
3866void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003867 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3868}
3869
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003870void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3872}
3873
3874void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003875 HBasicBlock* block = instruction->GetBlock();
3876 if (block->GetLoopInformation() != nullptr) {
3877 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3878 // The back edge will generate the suspend check.
3879 return;
3880 }
3881 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3882 // The goto will generate the suspend check.
3883 return;
3884 }
3885 GenerateSuspendCheck(instruction, nullptr);
3886}
3887
3888void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3889 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003890 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003891 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
3892 if (slow_path == nullptr) {
3893 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
3894 instruction->SetSlowPath(slow_path);
3895 codegen_->AddSlowPath(slow_path);
3896 if (successor != nullptr) {
3897 DCHECK(successor->IsLoopHeader());
3898 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3899 }
3900 } else {
3901 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3902 }
3903
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003904 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003905 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003906 if (successor == nullptr) {
3907 __ j(kNotEqual, slow_path->GetEntryLabel());
3908 __ Bind(slow_path->GetReturnLabel());
3909 } else {
3910 __ j(kEqual, codegen_->GetLabelOf(successor));
3911 __ jmp(slow_path->GetEntryLabel());
3912 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003913}
3914
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003915X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3916 return codegen_->GetAssembler();
3917}
3918
3919void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3920 MoveOperands* move = moves_.Get(index);
3921 Location source = move->GetSource();
3922 Location destination = move->GetDestination();
3923
3924 if (source.IsRegister()) {
3925 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003926 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003927 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003928 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003929 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003930 } else {
3931 DCHECK(destination.IsDoubleStackSlot());
3932 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003934 }
3935 } else if (source.IsStackSlot()) {
3936 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003938 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003939 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003940 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003941 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003942 } else {
3943 DCHECK(destination.IsStackSlot());
3944 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3945 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3946 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003947 } else if (source.IsDoubleStackSlot()) {
3948 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003950 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003951 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003952 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3953 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003954 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003955 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003956 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3957 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3958 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003959 } else if (source.IsConstant()) {
3960 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003961 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3962 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003963 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003964 if (value == 0) {
3965 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3966 } else {
3967 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3968 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003969 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003970 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003971 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003972 }
3973 } else if (constant->IsLongConstant()) {
3974 int64_t value = constant->AsLongConstant()->GetValue();
3975 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003976 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003977 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003978 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003979 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003980 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3981 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003982 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003983 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003984 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003985 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003986 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3987 if (value == 0) {
3988 // easy FP 0.0.
3989 __ xorps(dest, dest);
3990 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003991 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003992 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003993 } else {
3994 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003995 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003996 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3997 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003998 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003999 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004000 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004001 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004002 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004003 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4004 if (value == 0) {
4005 __ xorpd(dest, dest);
4006 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004007 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004008 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004009 } else {
4010 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004011 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004012 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4013 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004014 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004015 } else if (source.IsFpuRegister()) {
4016 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004017 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004018 } else if (destination.IsStackSlot()) {
4019 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004020 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004021 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004022 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004023 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004025 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004026 }
4027}
4028
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004029void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004030 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004031 __ movl(Address(CpuRegister(RSP), mem), reg);
4032 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004033}
4034
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004035void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004036 ScratchRegisterScope ensure_scratch(
4037 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4038
4039 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4040 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4041 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4042 Address(CpuRegister(RSP), mem2 + stack_offset));
4043 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4044 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4045 CpuRegister(ensure_scratch.GetRegister()));
4046}
4047
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004048void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4049 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4050 __ movq(Address(CpuRegister(RSP), mem), reg);
4051 __ movq(reg, CpuRegister(TMP));
4052}
4053
4054void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4055 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004056 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004057
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004058 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4059 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4060 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4061 Address(CpuRegister(RSP), mem2 + stack_offset));
4062 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4063 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4064 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004065}
4066
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004067void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4068 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4069 __ movss(Address(CpuRegister(RSP), mem), reg);
4070 __ movd(reg, CpuRegister(TMP));
4071}
4072
4073void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4074 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4075 __ movsd(Address(CpuRegister(RSP), mem), reg);
4076 __ movd(reg, CpuRegister(TMP));
4077}
4078
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004079void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4080 MoveOperands* move = moves_.Get(index);
4081 Location source = move->GetSource();
4082 Location destination = move->GetDestination();
4083
4084 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004085 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004086 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004087 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004088 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004089 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004090 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004091 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4092 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004093 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004094 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004096 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4097 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004098 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4100 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4101 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004102 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004103 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004104 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004105 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004106 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004108 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004110 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004111 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004112 }
4113}
4114
4115
4116void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4117 __ pushq(CpuRegister(reg));
4118}
4119
4120
4121void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4122 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004123}
4124
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004125void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4126 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4127 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4128 Immediate(mirror::Class::kStatusInitialized));
4129 __ j(kLess, slow_path->GetEntryLabel());
4130 __ Bind(slow_path->GetExitLabel());
4131 // No need for memory fence, thanks to the X86_64 memory model.
4132}
4133
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004134void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004135 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4136 ? LocationSummary::kCallOnSlowPath
4137 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004138 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004139 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004140 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 locations->SetOut(Location::RequiresRegister());
4142}
4143
4144void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004145 LocationSummary* locations = cls->GetLocations();
4146 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4147 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004148 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004149 DCHECK(!cls->CanCallRuntime());
4150 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004151 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004152 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004153 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004154 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004155 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004156 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004157 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4158 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4159 codegen_->AddSlowPath(slow_path);
4160 __ testl(out, out);
4161 __ j(kEqual, slow_path->GetEntryLabel());
4162 if (cls->MustGenerateClinitCheck()) {
4163 GenerateClassInitializationCheck(slow_path, out);
4164 } else {
4165 __ Bind(slow_path->GetExitLabel());
4166 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004167 }
4168}
4169
4170void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4171 LocationSummary* locations =
4172 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4173 locations->SetInAt(0, Location::RequiresRegister());
4174 if (check->HasUses()) {
4175 locations->SetOut(Location::SameAsFirstInput());
4176 }
4177}
4178
4179void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004180 // We assume the class to not be null.
4181 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4182 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004183 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004184 GenerateClassInitializationCheck(slow_path,
4185 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004186}
4187
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004188void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4189 LocationSummary* locations =
4190 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004191 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004192 locations->SetOut(Location::RequiresRegister());
4193}
4194
4195void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4196 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4197 codegen_->AddSlowPath(slow_path);
4198
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004199 LocationSummary* locations = load->GetLocations();
4200 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4201 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004202 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004203 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004204 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4205 __ testl(out, out);
4206 __ j(kEqual, slow_path->GetEntryLabel());
4207 __ Bind(slow_path->GetExitLabel());
4208}
4209
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004210void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4211 LocationSummary* locations =
4212 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4213 locations->SetOut(Location::RequiresRegister());
4214}
4215
4216void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4217 Address address = Address::Absolute(
4218 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004219 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004220 __ gs()->movl(address, Immediate(0));
4221}
4222
4223void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4224 LocationSummary* locations =
4225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4226 InvokeRuntimeCallingConvention calling_convention;
4227 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4228}
4229
4230void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4231 __ gs()->call(
4232 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4233 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4234}
4235
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004236void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004237 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4238 ? LocationSummary::kNoCall
4239 : LocationSummary::kCallOnSlowPath;
4240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4241 locations->SetInAt(0, Location::RequiresRegister());
4242 locations->SetInAt(1, Location::Any());
4243 locations->SetOut(Location::RequiresRegister());
4244}
4245
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004246void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004247 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004248 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004249 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004250 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004251 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4252 Label done, zero;
4253 SlowPathCodeX86_64* slow_path = nullptr;
4254
4255 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004256 // Avoid null check if we know obj is not null.
4257 if (instruction->MustDoNullCheck()) {
4258 __ testl(obj, obj);
4259 __ j(kEqual, &zero);
4260 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004261 // Compare the class of `obj` with `cls`.
4262 __ movl(out, Address(obj, class_offset));
4263 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004264 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004265 } else {
4266 DCHECK(cls.IsStackSlot()) << cls;
4267 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4268 }
4269 if (instruction->IsClassFinal()) {
4270 // Classes must be equal for the instanceof to succeed.
4271 __ j(kNotEqual, &zero);
4272 __ movl(out, Immediate(1));
4273 __ jmp(&done);
4274 } else {
4275 // If the classes are not equal, we go into a slow path.
4276 DCHECK(locations->OnlyCallsOnSlowPath());
4277 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004278 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004279 codegen_->AddSlowPath(slow_path);
4280 __ j(kNotEqual, slow_path->GetEntryLabel());
4281 __ movl(out, Immediate(1));
4282 __ jmp(&done);
4283 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004284
4285 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4286 __ Bind(&zero);
4287 __ movl(out, Immediate(0));
4288 }
4289
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004290 if (slow_path != nullptr) {
4291 __ Bind(slow_path->GetExitLabel());
4292 }
4293 __ Bind(&done);
4294}
4295
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004296void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4298 instruction, LocationSummary::kCallOnSlowPath);
4299 locations->SetInAt(0, Location::RequiresRegister());
4300 locations->SetInAt(1, Location::Any());
4301 locations->AddTemp(Location::RequiresRegister());
4302}
4303
4304void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4305 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004306 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004307 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004308 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004309 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4310 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4311 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4312 codegen_->AddSlowPath(slow_path);
4313
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004314 // Avoid null check if we know obj is not null.
4315 if (instruction->MustDoNullCheck()) {
4316 __ testl(obj, obj);
4317 __ j(kEqual, slow_path->GetExitLabel());
4318 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004319 // Compare the class of `obj` with `cls`.
4320 __ movl(temp, Address(obj, class_offset));
4321 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004322 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004323 } else {
4324 DCHECK(cls.IsStackSlot()) << cls;
4325 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4326 }
4327 // Classes must be equal for the checkcast to succeed.
4328 __ j(kNotEqual, slow_path->GetEntryLabel());
4329 __ Bind(slow_path->GetExitLabel());
4330}
4331
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004332void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4333 LocationSummary* locations =
4334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4335 InvokeRuntimeCallingConvention calling_convention;
4336 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4337}
4338
4339void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4340 __ gs()->call(Address::Absolute(instruction->IsEnter()
4341 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4342 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4343 true));
4344 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4345}
4346
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004347void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4348void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4349void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4350
4351void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4352 LocationSummary* locations =
4353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4354 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4355 || instruction->GetResultType() == Primitive::kPrimLong);
4356 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004357 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004358 locations->SetOut(Location::SameAsFirstInput());
4359}
4360
4361void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4362 HandleBitwiseOperation(instruction);
4363}
4364
4365void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4366 HandleBitwiseOperation(instruction);
4367}
4368
4369void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4370 HandleBitwiseOperation(instruction);
4371}
4372
4373void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4374 LocationSummary* locations = instruction->GetLocations();
4375 Location first = locations->InAt(0);
4376 Location second = locations->InAt(1);
4377 DCHECK(first.Equals(locations->Out()));
4378
4379 if (instruction->GetResultType() == Primitive::kPrimInt) {
4380 if (second.IsRegister()) {
4381 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004383 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004384 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004385 } else {
4386 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004387 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004388 }
4389 } else if (second.IsConstant()) {
4390 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4391 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004392 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004393 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004394 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004395 } else {
4396 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004397 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004398 }
4399 } else {
4400 Address address(CpuRegister(RSP), second.GetStackIndex());
4401 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004402 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004403 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004404 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004405 } else {
4406 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004408 }
4409 }
4410 } else {
4411 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004412 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4413 bool second_is_constant = false;
4414 int64_t value = 0;
4415 if (second.IsConstant()) {
4416 second_is_constant = true;
4417 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004418 }
Mark Mendell40741f32015-04-20 22:10:34 -04004419 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004420
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004421 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004422 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004423 if (is_int32_value) {
4424 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4425 } else {
4426 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4427 }
4428 } else if (second.IsDoubleStackSlot()) {
4429 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004430 } else {
4431 __ andq(first_reg, second.AsRegister<CpuRegister>());
4432 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004433 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004434 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004435 if (is_int32_value) {
4436 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4437 } else {
4438 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4439 }
4440 } else if (second.IsDoubleStackSlot()) {
4441 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004442 } else {
4443 __ orq(first_reg, second.AsRegister<CpuRegister>());
4444 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004445 } else {
4446 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004447 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004448 if (is_int32_value) {
4449 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4450 } else {
4451 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4452 }
4453 } else if (second.IsDoubleStackSlot()) {
4454 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004455 } else {
4456 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4457 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004458 }
4459 }
4460}
4461
Calin Juravleb1498f62015-02-16 13:13:29 +00004462void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4463 // Nothing to do, this should be removed during prepare for register allocator.
4464 UNUSED(instruction);
4465 LOG(FATAL) << "Unreachable";
4466}
4467
4468void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4469 // Nothing to do, this should be removed during prepare for register allocator.
4470 UNUSED(instruction);
4471 LOG(FATAL) << "Unreachable";
4472}
4473
Mark Mendell92e83bf2015-05-07 11:25:03 -04004474void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4475 if (value == 0) {
4476 __ xorl(dest, dest);
4477 } else if (value > 0 && IsInt<32>(value)) {
4478 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4479 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4480 } else {
4481 __ movq(dest, Immediate(value));
4482 }
4483}
4484
Mark Mendellf55c3e02015-03-26 21:07:46 -04004485void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4486 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004487 X86_64Assembler* assembler = GetAssembler();
4488 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004489 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4490 // byte values. If used for vectors at a later time, this will need to be
4491 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004492 assembler->Align(4, 0);
4493 constant_area_start_ = assembler->CodeSize();
4494 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004495 }
4496
4497 // And finish up.
4498 CodeGenerator::Finalize(allocator);
4499}
4500
4501/**
4502 * Class to handle late fixup of offsets into constant area.
4503 */
4504class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4505 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004506 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004507 : codegen_(codegen), offset_into_constant_area_(offset) {}
4508
4509 private:
4510 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4511 // Patch the correct offset for the instruction. We use the address of the
4512 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4513 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4514 int relative_position = constant_offset - pos;
4515
4516 // Patch in the right value.
4517 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4518 }
4519
Mark Mendell39dcf552015-04-09 20:42:42 -04004520 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004521
4522 // Location in constant area that the fixup refers to.
4523 int offset_into_constant_area_;
4524};
4525
4526Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4527 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4528 return Address::RIP(fixup);
4529}
4530
4531Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4532 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4533 return Address::RIP(fixup);
4534}
4535
4536Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4537 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4538 return Address::RIP(fixup);
4539}
4540
4541Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4542 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4543 return Address::RIP(fixup);
4544}
4545
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004546} // namespace x86_64
4547} // namespace art