blob: b5e038fb0564b673dab1fda6ae975f71f5deb3f8 [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
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "intrinsics.h"
22#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070023#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010025#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "mirror/object_reference.h"
27#include "thread.h"
28#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/x86_64/assembler_x86_64.h"
31#include "utils/x86_64/managed_register_x86_64.h"
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace art {
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace x86_64 {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr int kCurrentMethodStackOffset = 0;
41
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000042static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000043static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Mark Mendell24f2dfa2015-01-14 19:51:45 -050045static constexpr int kC2ConditionMask = 0x400;
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
49
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010050class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010052 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Alexandre Rames2ed20af2015-03-06 13:55:35 +000054 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010056 __ gs()->call(
57 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000058 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 }
60
61 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010062 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
64};
65
Calin Juravled0d48522014-11-04 16:40:20 +000066class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
67 public:
68 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
69
Alexandre Rames2ed20af2015-03-06 13:55:35 +000070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000071 __ Bind(GetEntryLabel());
72 __ gs()->call(
73 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000074 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000075 }
76
77 private:
78 HDivZeroCheck* const instruction_;
79 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
80};
81
Calin Juravlebacfec32014-11-14 15:54:36 +000082class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000083 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000084 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
85 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000086
Alexandre Rames2ed20af2015-03-06 13:55:35 +000087 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +000089 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +000090 if (is_div_) {
91 __ negl(cpu_reg_);
92 } else {
93 __ movl(cpu_reg_, Immediate(0));
94 }
95
Calin Juravled6fb6cf2014-11-11 19:07:44 +000096 } else {
97 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +000098 if (is_div_) {
99 __ negq(cpu_reg_);
100 } else {
101 __ movq(cpu_reg_, Immediate(0));
102 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000103 }
Calin Juravled0d48522014-11-04 16:40:20 +0000104 __ jmp(GetExitLabel());
105 }
106
107 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000109 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 const bool is_div_;
111 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000112};
113
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100114class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000115 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100116 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
117 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000119 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100120 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000122 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000123 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000124 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
125 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100126 if (successor_ == nullptr) {
127 __ jmp(GetReturnLabel());
128 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100129 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100130 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 }
132
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 Label* GetReturnLabel() {
134 DCHECK(successor_ == nullptr);
135 return &return_label_;
136 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137
138 private:
139 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100140 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 Label return_label_;
142
143 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
144};
145
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100148 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
149 Location index_location,
150 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 : instruction_(instruction),
152 index_location_(index_location),
153 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000155 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000157 // We're moving two locations to locations that could overlap, so we need a parallel
158 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000160 codegen->EmitParallelMoves(
161 index_location_,
162 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
163 length_location_,
164 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165 __ gs()->call(Address::Absolute(
166 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000167 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 }
169
170 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100171 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 const Location index_location_;
173 const Location length_location_;
174
175 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
176};
177
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000178class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100179 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000180 LoadClassSlowPathX86_64(HLoadClass* cls,
181 HInstruction* at,
182 uint32_t dex_pc,
183 bool do_clinit)
184 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
185 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
186 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100187
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000189 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
191 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100192
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000193 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000194
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100195 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100197 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000198 __ gs()->call(Address::Absolute((do_clinit_
199 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
200 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000201 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000203 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000205 if (out.IsValid()) {
206 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
207 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 }
209
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 __ jmp(GetExitLabel());
212 }
213
214 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 // The class this slow path will load.
216 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100217
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000218 // The instruction where this slow path is happening.
219 // (Might be the load class or an initialization check).
220 HInstruction* const at_;
221
222 // The dex PC of `at_`.
223 const uint32_t dex_pc_;
224
225 // Whether to initialize the class.
226 const bool do_clinit_;
227
228 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100229};
230
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000231class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
232 public:
233 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
234
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000235 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000236 LocationSummary* locations = instruction_->GetLocations();
237 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
238
239 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
240 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000241 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000242
243 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800244 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
245 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000246 Immediate(instruction_->GetStringIndex()));
247 __ gs()->call(Address::Absolute(
248 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000249 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000250 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000251 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000252 __ jmp(GetExitLabel());
253 }
254
255 private:
256 HLoadString* const instruction_;
257
258 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
259};
260
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000261class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
262 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000263 TypeCheckSlowPathX86_64(HInstruction* instruction,
264 Location class_to_check,
265 Location object_class,
266 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000268 class_to_check_(class_to_check),
269 object_class_(object_class),
270 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000272 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000274 DCHECK(instruction_->IsCheckCast()
275 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
277 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
278 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280
281 // We're moving two locations to locations that could overlap, so we need a parallel
282 // move resolver.
283 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000284 codegen->EmitParallelMoves(
285 class_to_check_,
286 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
287 object_class_,
288 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000290 if (instruction_->IsInstanceOf()) {
291 __ gs()->call(
292 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
293 } else {
294 DCHECK(instruction_->IsCheckCast());
295 __ gs()->call(
296 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
297 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000298 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299
300 if (instruction_->IsInstanceOf()) {
301 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
302 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000304 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 __ jmp(GetExitLabel());
306 }
307
308 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 HInstruction* const instruction_;
310 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000312 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
314 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
315};
316
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700317class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
318 public:
319 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
320 : instruction_(instruction) {}
321
322 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
323 __ Bind(GetEntryLabel());
324 SaveLiveRegisters(codegen, instruction_->GetLocations());
325 __ gs()->call(
326 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeoptimize), true));
327 DCHECK(instruction_->IsDeoptimize());
328 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
329 uint32_t dex_pc = deoptimize->GetDexPc();
330 codegen->RecordPcInfo(instruction_, dex_pc, this);
331 }
332
333 private:
334 HInstruction* const instruction_;
335 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
336};
337
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100338#undef __
339#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
340
Dave Allison20dfc792014-06-16 20:44:29 -0700341inline Condition X86_64Condition(IfCondition cond) {
342 switch (cond) {
343 case kCondEQ: return kEqual;
344 case kCondNE: return kNotEqual;
345 case kCondLT: return kLess;
346 case kCondLE: return kLessEqual;
347 case kCondGT: return kGreater;
348 case kCondGE: return kGreaterEqual;
349 default:
350 LOG(FATAL) << "Unknown if condition";
351 }
352 return kEqual;
353}
354
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800355void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
356 CpuRegister temp) {
357 // All registers are assumed to be correctly set up.
358
359 // TODO: Implement all kinds of calls:
360 // 1) boot -> boot
361 // 2) app -> boot
362 // 3) app -> app
363 //
364 // Currently we implement the app -> app logic, which looks up in the resolve cache.
365
366 // temp = method;
367 LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000368 if (!invoke->IsRecursive()) {
369 // temp = temp->dex_cache_resolved_methods_;
370 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
371 // temp = temp[index_in_cache]
372 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
373 // (temp + offset_of_quick_compiled_code)()
374 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
375 kX86_64WordSize).SizeValue()));
376 } else {
377 __ call(&frame_entry_label_);
378 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800379
380 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800381}
382
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100383void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
384 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
385}
386
387void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
388 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
389}
390
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100391size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
392 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
393 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100394}
395
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100396size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
397 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
398 return kX86_64WordSize;
399}
400
401size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
402 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
403 return kX86_64WordSize;
404}
405
406size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
407 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
408 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100409}
410
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000411static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000412// Use a fake return address register to mimic Quick.
413static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400414CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
415 const X86_64InstructionSetFeatures& isa_features,
416 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000417 : CodeGenerator(graph,
418 kNumberOfCpuRegisters,
419 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000420 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000421 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
422 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000423 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000424 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
425 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000426 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100427 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100428 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000429 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400430 move_resolver_(graph->GetArena(), this),
431 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000432 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
433}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100434
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100435InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
436 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100437 : HGraphVisitor(graph),
438 assembler_(codegen->GetAssembler()),
439 codegen_(codegen) {}
440
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100441Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100442 switch (type) {
443 case Primitive::kPrimLong:
444 case Primitive::kPrimByte:
445 case Primitive::kPrimBoolean:
446 case Primitive::kPrimChar:
447 case Primitive::kPrimShort:
448 case Primitive::kPrimInt:
449 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100451 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100452 }
453
454 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100455 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100457 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100458 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459
460 case Primitive::kPrimVoid:
461 LOG(FATAL) << "Unreachable type " << type;
462 }
463
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100464 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100465}
466
Nicolas Geoffray98893962015-01-21 12:32:32 +0000467void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100468 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100469 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100470
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000471 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100472 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000473
Nicolas Geoffray98893962015-01-21 12:32:32 +0000474 if (is_baseline) {
475 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
476 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
477 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
479 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
480 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000481 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100482}
483
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100484static dwarf::Reg DWARFReg(Register reg) {
485 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
486}
487static dwarf::Reg DWARFReg(FloatRegister reg) {
488 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
489}
490
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100491void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100492 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000493 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100494 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700495 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000496 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100497
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000498 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100499 __ testq(CpuRegister(RAX), Address(
500 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100501 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100502 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000503
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000504 if (HasEmptyFrame()) {
505 return;
506 }
507
Nicolas Geoffray98893962015-01-21 12:32:32 +0000508 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000509 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000510 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000511 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100512 __ cfi().AdjustCFAOffset(kX86_64WordSize);
513 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000514 }
515 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100516
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100517 int adjust = GetFrameSize() - GetCoreSpillSize();
518 __ subq(CpuRegister(RSP), Immediate(adjust));
519 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000520 uint32_t xmm_spill_location = GetFpuSpillStart();
521 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100522
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000523 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
524 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100525 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
526 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
527 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000528 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100529 }
530
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
532}
533
534void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000535 if (HasEmptyFrame()) {
536 return;
537 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000538 uint32_t xmm_spill_location = GetFpuSpillStart();
539 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
540 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
541 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100542 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
543 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
544 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000545 }
546 }
547
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100548 int adjust = GetFrameSize() - GetCoreSpillSize();
549 __ addq(CpuRegister(RSP), Immediate(adjust));
550 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000551
552 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000553 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000554 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000555 __ popq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100556 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
557 __ cfi().Restore(DWARFReg(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000558 }
559 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560}
561
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100562void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
563 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564}
565
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100566void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000567 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100568 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
569}
570
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100571Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
572 switch (load->GetType()) {
573 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100575 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576
577 case Primitive::kPrimInt:
578 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100581
582 case Primitive::kPrimBoolean:
583 case Primitive::kPrimByte:
584 case Primitive::kPrimChar:
585 case Primitive::kPrimShort:
586 case Primitive::kPrimVoid:
587 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700588 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100589 }
590
591 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700592 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100593}
594
595void CodeGeneratorX86_64::Move(Location destination, Location source) {
596 if (source.Equals(destination)) {
597 return;
598 }
599 if (destination.IsRegister()) {
600 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000601 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100602 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000603 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100604 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000605 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100606 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100607 } else {
608 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000609 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100610 Address(CpuRegister(RSP), source.GetStackIndex()));
611 }
612 } else if (destination.IsFpuRegister()) {
613 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000614 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100615 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000616 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100617 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000618 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 Address(CpuRegister(RSP), source.GetStackIndex()));
620 } else {
621 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624 }
625 } else if (destination.IsStackSlot()) {
626 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else if (source.IsFpuRegister()) {
630 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500632 } else if (source.IsConstant()) {
633 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000634 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500635 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500637 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000638 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
639 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100640 }
641 } else {
642 DCHECK(destination.IsDoubleStackSlot());
643 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000645 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 } else if (source.IsFpuRegister()) {
647 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000648 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500649 } else if (source.IsConstant()) {
650 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800651 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500652 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000653 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500654 } else {
655 DCHECK(constant->IsLongConstant());
656 value = constant->AsLongConstant()->GetValue();
657 }
658 __ movq(CpuRegister(TMP), Immediate(value));
659 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100660 } else {
661 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000662 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
663 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100664 }
665 }
666}
667
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100668void CodeGeneratorX86_64::Move(HInstruction* instruction,
669 Location location,
670 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000671 LocationSummary* locations = instruction->GetLocations();
672 if (locations != nullptr && locations->Out().Equals(location)) {
673 return;
674 }
675
676 if (locations != nullptr && locations->Out().IsConstant()) {
677 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000678 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
679 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000680 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000681 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000682 } else if (location.IsStackSlot()) {
683 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
684 } else {
685 DCHECK(location.IsConstant());
686 DCHECK_EQ(location.GetConstant(), const_to_move);
687 }
688 } else if (const_to_move->IsLongConstant()) {
689 int64_t value = const_to_move->AsLongConstant()->GetValue();
690 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000691 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000692 } else if (location.IsDoubleStackSlot()) {
693 __ movq(CpuRegister(TMP), Immediate(value));
694 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
695 } else {
696 DCHECK(location.IsConstant());
697 DCHECK_EQ(location.GetConstant(), const_to_move);
698 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100699 }
Roland Levillain476df552014-10-09 17:51:36 +0100700 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100701 switch (instruction->GetType()) {
702 case Primitive::kPrimBoolean:
703 case Primitive::kPrimByte:
704 case Primitive::kPrimChar:
705 case Primitive::kPrimShort:
706 case Primitive::kPrimInt:
707 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100708 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100709 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
710 break;
711
712 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000714 Move(location,
715 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100716 break;
717
718 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100719 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100720 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000721 } else if (instruction->IsTemporary()) {
722 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
723 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100724 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100725 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726 switch (instruction->GetType()) {
727 case Primitive::kPrimBoolean:
728 case Primitive::kPrimByte:
729 case Primitive::kPrimChar:
730 case Primitive::kPrimShort:
731 case Primitive::kPrimInt:
732 case Primitive::kPrimNot:
733 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 case Primitive::kPrimFloat:
735 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000736 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100737 break;
738
739 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100741 }
742 }
743}
744
745void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
746 got->SetLocations(nullptr);
747}
748
749void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
750 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100751 DCHECK(!successor->IsExitBlock());
752
753 HBasicBlock* block = got->GetBlock();
754 HInstruction* previous = got->GetPrevious();
755
756 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000757 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100758 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
759 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
760 return;
761 }
762
763 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
764 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
765 }
766 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100767 __ jmp(codegen_->GetLabelOf(successor));
768 }
769}
770
771void LocationsBuilderX86_64::VisitExit(HExit* exit) {
772 exit->SetLocations(nullptr);
773}
774
775void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700776 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100777}
778
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700779void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
780 Label* true_target,
781 Label* false_target,
782 Label* always_true_target) {
783 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100784 if (cond->IsIntConstant()) {
785 // Constant condition, statically compared against 1.
786 int32_t cond_value = cond->AsIntConstant()->GetValue();
787 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700788 if (always_true_target != nullptr) {
789 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100790 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100791 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100792 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100793 DCHECK_EQ(cond_value, 0);
794 }
795 } else {
796 bool materialized =
797 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
798 // Moves do not affect the eflags register, so if the condition is
799 // evaluated just before the if, we don't need to evaluate it
800 // again.
801 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700802 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100803 if (materialized) {
804 if (!eflags_set) {
805 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700806 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000808 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100809 } else {
810 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
811 Immediate(0));
812 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700813 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700815 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 }
817 } else {
818 Location lhs = cond->GetLocations()->InAt(0);
819 Location rhs = cond->GetLocations()->InAt(1);
820 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000821 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000823 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000824 if (constant == 0) {
825 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
826 } else {
827 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
828 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100829 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000830 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100831 Address(CpuRegister(RSP), rhs.GetStackIndex()));
832 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700833 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700834 }
Dave Allison20dfc792014-06-16 20:44:29 -0700835 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836 if (false_target != nullptr) {
837 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100838 }
839}
840
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700841void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
842 LocationSummary* locations =
843 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
844 HInstruction* cond = if_instr->InputAt(0);
845 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
846 locations->SetInAt(0, Location::Any());
847 }
848}
849
850void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
851 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
852 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
853 Label* always_true_target = true_target;
854 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
855 if_instr->IfTrueSuccessor())) {
856 always_true_target = nullptr;
857 }
858 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
859 if_instr->IfFalseSuccessor())) {
860 false_target = nullptr;
861 }
862 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
863}
864
865void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
866 LocationSummary* locations = new (GetGraph()->GetArena())
867 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
868 HInstruction* cond = deoptimize->InputAt(0);
869 DCHECK(cond->IsCondition());
870 if (cond->AsCondition()->NeedsMaterialization()) {
871 locations->SetInAt(0, Location::Any());
872 }
873}
874
875void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
876 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
877 DeoptimizationSlowPathX86_64(deoptimize);
878 codegen_->AddSlowPath(slow_path);
879 Label* slow_path_entry = slow_path->GetEntryLabel();
880 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
881}
882
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100883void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
884 local->SetLocations(nullptr);
885}
886
887void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
888 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
889}
890
891void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
892 local->SetLocations(nullptr);
893}
894
895void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
896 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700897 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100898}
899
900void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100901 LocationSummary* locations =
902 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100903 switch (store->InputAt(1)->GetType()) {
904 case Primitive::kPrimBoolean:
905 case Primitive::kPrimByte:
906 case Primitive::kPrimChar:
907 case Primitive::kPrimShort:
908 case Primitive::kPrimInt:
909 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100910 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100911 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
912 break;
913
914 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100916 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
917 break;
918
919 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922}
923
924void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700925 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926}
927
Dave Allison20dfc792014-06-16 20:44:29 -0700928void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100929 LocationSummary* locations =
930 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100931 locations->SetInAt(0, Location::RequiresRegister());
932 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100933 if (comp->NeedsMaterialization()) {
934 locations->SetOut(Location::RequiresRegister());
935 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100936}
937
Dave Allison20dfc792014-06-16 20:44:29 -0700938void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
939 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100940 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000941 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100942 // Clear register: setcc only sets the low byte.
943 __ xorq(reg, reg);
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000944 Location lhs = locations->InAt(0);
945 Location rhs = locations->InAt(1);
946 if (rhs.IsRegister()) {
947 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
948 } else if (rhs.IsConstant()) {
Mingyao Yangdc5ac732015-02-25 11:28:05 -0800949 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000950 if (constant == 0) {
951 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
952 } else {
953 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
954 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100955 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000956 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100957 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100958 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700959 }
960}
961
962void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
963 VisitCondition(comp);
964}
965
966void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
967 VisitCondition(comp);
968}
969
970void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
971 VisitCondition(comp);
972}
973
974void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
975 VisitCondition(comp);
976}
977
978void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
979 VisitCondition(comp);
980}
981
982void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
983 VisitCondition(comp);
984}
985
986void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
987 VisitCondition(comp);
988}
989
990void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
991 VisitCondition(comp);
992}
993
994void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
995 VisitCondition(comp);
996}
997
998void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
999 VisitCondition(comp);
1000}
1001
1002void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1003 VisitCondition(comp);
1004}
1005
1006void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1007 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001008}
1009
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001010void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001011 LocationSummary* locations =
1012 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001013 switch (compare->InputAt(0)->GetType()) {
1014 case Primitive::kPrimLong: {
1015 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001016 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00001017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1018 break;
1019 }
1020 case Primitive::kPrimFloat:
1021 case Primitive::kPrimDouble: {
1022 locations->SetInAt(0, Location::RequiresFpuRegister());
1023 locations->SetInAt(1, Location::RequiresFpuRegister());
1024 locations->SetOut(Location::RequiresRegister());
1025 break;
1026 }
1027 default:
1028 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1029 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001030}
1031
1032void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001033 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001034 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001035 Location left = locations->InAt(0);
1036 Location right = locations->InAt(1);
1037
1038 Label less, greater, done;
1039 Primitive::Type type = compare->InputAt(0)->GetType();
1040 switch (type) {
1041 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001042 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1043 if (right.IsConstant()) {
1044 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1045 DCHECK(IsInt<32>(value));
1046 if (value == 0) {
1047 __ testq(left_reg, left_reg);
1048 } else {
1049 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1050 }
1051 } else {
1052 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001054 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001055 }
1056 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001057 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001058 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1059 break;
1060 }
1061 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001062 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001063 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1064 break;
1065 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001066 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001067 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001068 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001069 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001070 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001071 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001072
Calin Juravle91debbc2014-11-26 19:01:09 +00001073 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001074 __ movl(out, Immediate(1));
1075 __ jmp(&done);
1076
1077 __ Bind(&less);
1078 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001079
1080 __ Bind(&done);
1081}
1082
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001083void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001084 LocationSummary* locations =
1085 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001086 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001087}
1088
1089void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001090 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001091 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092}
1093
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001094void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1095 LocationSummary* locations =
1096 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1097 locations->SetOut(Location::ConstantLocation(constant));
1098}
1099
1100void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1101 // Will be generated at use site.
1102 UNUSED(constant);
1103}
1104
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001106 LocationSummary* locations =
1107 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001108 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001109}
1110
1111void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001112 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001113 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114}
1115
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001116void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1117 LocationSummary* locations =
1118 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1119 locations->SetOut(Location::ConstantLocation(constant));
1120}
1121
1122void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1123 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001124 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001125}
1126
1127void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1128 LocationSummary* locations =
1129 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1130 locations->SetOut(Location::ConstantLocation(constant));
1131}
1132
1133void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1134 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001135 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001136}
1137
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001138void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1139 ret->SetLocations(nullptr);
1140}
1141
1142void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001143 UNUSED(ret);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001144 __ cfi().RememberState();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001145 codegen_->GenerateFrameExit();
1146 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001147 __ cfi().RestoreState();
1148 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149}
1150
1151void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001152 LocationSummary* locations =
1153 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001154 switch (ret->InputAt(0)->GetType()) {
1155 case Primitive::kPrimBoolean:
1156 case Primitive::kPrimByte:
1157 case Primitive::kPrimChar:
1158 case Primitive::kPrimShort:
1159 case Primitive::kPrimInt:
1160 case Primitive::kPrimNot:
1161 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001163 break;
1164
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 case Primitive::kPrimFloat:
1166 case Primitive::kPrimDouble:
1167 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001168 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001169 break;
1170
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001171 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001173 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001174}
1175
1176void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1177 if (kIsDebugBuild) {
1178 switch (ret->InputAt(0)->GetType()) {
1179 case Primitive::kPrimBoolean:
1180 case Primitive::kPrimByte:
1181 case Primitive::kPrimChar:
1182 case Primitive::kPrimShort:
1183 case Primitive::kPrimInt:
1184 case Primitive::kPrimNot:
1185 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001186 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001187 break;
1188
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 case Primitive::kPrimFloat:
1190 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001191 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 XMM0);
1193 break;
1194
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001196 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001197 }
1198 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001199 __ cfi().RememberState();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001200 codegen_->GenerateFrameExit();
1201 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001202 __ cfi().RestoreState();
1203 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001204}
1205
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1207 switch (type) {
1208 case Primitive::kPrimBoolean:
1209 case Primitive::kPrimByte:
1210 case Primitive::kPrimChar:
1211 case Primitive::kPrimShort:
1212 case Primitive::kPrimInt:
1213 case Primitive::kPrimNot: {
1214 uint32_t index = gp_index_++;
1215 stack_index_++;
1216 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001217 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218 } else {
1219 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1220 }
1221 }
1222
1223 case Primitive::kPrimLong: {
1224 uint32_t index = gp_index_;
1225 stack_index_ += 2;
1226 if (index < calling_convention.GetNumberOfRegisters()) {
1227 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001228 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001229 } else {
1230 gp_index_ += 2;
1231 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1232 }
1233 }
1234
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 case Primitive::kPrimFloat: {
1236 uint32_t index = fp_index_++;
1237 stack_index_++;
1238 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001239 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else {
1241 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1242 }
1243 }
1244
1245 case Primitive::kPrimDouble: {
1246 uint32_t index = fp_index_++;
1247 stack_index_ += 2;
1248 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001249 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001250 } else {
1251 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1252 }
1253 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001254
1255 case Primitive::kPrimVoid:
1256 LOG(FATAL) << "Unexpected parameter type " << type;
1257 break;
1258 }
1259 return Location();
1260}
1261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001262void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001263 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001264 if (intrinsic.TryDispatch(invoke)) {
1265 return;
1266 }
1267
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 HandleInvoke(invoke);
1269}
1270
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001271static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1272 if (invoke->GetLocations()->Intrinsified()) {
1273 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1274 intrinsic.Dispatch(invoke);
1275 return true;
1276 }
1277 return false;
1278}
1279
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001280void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001281 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1282 return;
1283 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001284
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001285 codegen_->GenerateStaticOrDirectCall(
1286 invoke,
1287 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001288 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001289}
1290
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001291void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001292 LocationSummary* locations =
1293 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001294 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001295
1296 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001297 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001298 HInstruction* input = invoke->InputAt(i);
1299 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1300 }
1301
1302 switch (invoke->GetType()) {
1303 case Primitive::kPrimBoolean:
1304 case Primitive::kPrimByte:
1305 case Primitive::kPrimChar:
1306 case Primitive::kPrimShort:
1307 case Primitive::kPrimInt:
1308 case Primitive::kPrimNot:
1309 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001310 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001311 break;
1312
1313 case Primitive::kPrimVoid:
1314 break;
1315
1316 case Primitive::kPrimDouble:
1317 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001318 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319 break;
1320 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001321}
1322
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001323void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001324 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001325 if (intrinsic.TryDispatch(invoke)) {
1326 return;
1327 }
1328
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329 HandleInvoke(invoke);
1330}
1331
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001332void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001333 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1334 return;
1335 }
1336
Roland Levillain271ab9c2014-11-27 15:23:57 +00001337 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001338 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1339 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1340 LocationSummary* locations = invoke->GetLocations();
1341 Location receiver = locations->InAt(0);
1342 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1343 // temp = object->GetClass();
1344 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001345 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1346 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001347 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001348 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001349 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001350 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001351 // temp = temp->GetMethodAt(method_offset);
1352 __ movl(temp, Address(temp, method_offset));
1353 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001354 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001355 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001356
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001357 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001358 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001359}
1360
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001361void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1362 HandleInvoke(invoke);
1363 // Add the hidden argument.
1364 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1365}
1366
1367void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1368 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001369 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001370 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1371 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1372 LocationSummary* locations = invoke->GetLocations();
1373 Location receiver = locations->InAt(0);
1374 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1375
1376 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001378 Immediate(invoke->GetDexMethodIndex()));
1379
1380 // temp = object->GetClass();
1381 if (receiver.IsStackSlot()) {
1382 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1383 __ movl(temp, Address(temp, class_offset));
1384 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001385 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001386 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001387 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001388 // temp = temp->GetImtEntryAt(method_offset);
1389 __ movl(temp, Address(temp, method_offset));
1390 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001391 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001392 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001393
1394 DCHECK(!codegen_->IsLeafMethod());
1395 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1396}
1397
Roland Levillain88cb1752014-10-20 16:36:47 +01001398void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1399 LocationSummary* locations =
1400 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1401 switch (neg->GetResultType()) {
1402 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001403 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001404 locations->SetInAt(0, Location::RequiresRegister());
1405 locations->SetOut(Location::SameAsFirstInput());
1406 break;
1407
Roland Levillain88cb1752014-10-20 16:36:47 +01001408 case Primitive::kPrimFloat:
1409 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001410 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001411 locations->SetOut(Location::SameAsFirstInput());
1412 locations->AddTemp(Location::RequiresRegister());
1413 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001414 break;
1415
1416 default:
1417 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1418 }
1419}
1420
1421void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1422 LocationSummary* locations = neg->GetLocations();
1423 Location out = locations->Out();
1424 Location in = locations->InAt(0);
1425 switch (neg->GetResultType()) {
1426 case Primitive::kPrimInt:
1427 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001428 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001429 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001430 break;
1431
1432 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001433 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001434 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001435 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001436 break;
1437
Roland Levillain5368c212014-11-27 15:03:41 +00001438 case Primitive::kPrimFloat: {
1439 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001440 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1441 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001442 // Implement float negation with an exclusive or with value
1443 // 0x80000000 (mask for bit 31, representing the sign of a
1444 // single-precision floating-point number).
1445 __ movq(constant, Immediate(INT64_C(0x80000000)));
1446 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001447 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001448 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001449 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001450
Roland Levillain5368c212014-11-27 15:03:41 +00001451 case Primitive::kPrimDouble: {
1452 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001453 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1454 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001455 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001456 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001457 // a double-precision floating-point number).
1458 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1459 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001460 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001461 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001462 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001463
1464 default:
1465 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1466 }
1467}
1468
Roland Levillaindff1f282014-11-05 14:15:05 +00001469void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1470 LocationSummary* locations =
1471 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1472 Primitive::Type result_type = conversion->GetResultType();
1473 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001474 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001475
David Brazdilb2bd1c52015-03-25 11:17:37 +00001476 // The Java language does not allow treating boolean as an integral type but
1477 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001478
Roland Levillaindff1f282014-11-05 14:15:05 +00001479 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001480 case Primitive::kPrimByte:
1481 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001482 case Primitive::kPrimBoolean:
1483 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001484 case Primitive::kPrimShort:
1485 case Primitive::kPrimInt:
1486 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001487 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001488 locations->SetInAt(0, Location::Any());
1489 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1490 break;
1491
1492 default:
1493 LOG(FATAL) << "Unexpected type conversion from " << input_type
1494 << " to " << result_type;
1495 }
1496 break;
1497
Roland Levillain01a8d712014-11-14 16:27:39 +00001498 case Primitive::kPrimShort:
1499 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001500 case Primitive::kPrimBoolean:
1501 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001502 case Primitive::kPrimByte:
1503 case Primitive::kPrimInt:
1504 case Primitive::kPrimChar:
1505 // Processing a Dex `int-to-short' instruction.
1506 locations->SetInAt(0, Location::Any());
1507 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1508 break;
1509
1510 default:
1511 LOG(FATAL) << "Unexpected type conversion from " << input_type
1512 << " to " << result_type;
1513 }
1514 break;
1515
Roland Levillain946e1432014-11-11 17:35:19 +00001516 case Primitive::kPrimInt:
1517 switch (input_type) {
1518 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001519 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001520 locations->SetInAt(0, Location::Any());
1521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1522 break;
1523
1524 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001525 // Processing a Dex `float-to-int' instruction.
1526 locations->SetInAt(0, Location::RequiresFpuRegister());
1527 locations->SetOut(Location::RequiresRegister());
1528 locations->AddTemp(Location::RequiresFpuRegister());
1529 break;
1530
Roland Levillain946e1432014-11-11 17:35:19 +00001531 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001532 // Processing a Dex `double-to-int' instruction.
1533 locations->SetInAt(0, Location::RequiresFpuRegister());
1534 locations->SetOut(Location::RequiresRegister());
1535 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 }
1542 break;
1543
Roland Levillaindff1f282014-11-05 14:15:05 +00001544 case Primitive::kPrimLong:
1545 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001546 case Primitive::kPrimBoolean:
1547 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001548 case Primitive::kPrimByte:
1549 case Primitive::kPrimShort:
1550 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001551 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001552 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001553 // TODO: We would benefit from a (to-be-implemented)
1554 // Location::RegisterOrStackSlot requirement for this input.
1555 locations->SetInAt(0, Location::RequiresRegister());
1556 locations->SetOut(Location::RequiresRegister());
1557 break;
1558
1559 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001560 // Processing a Dex `float-to-long' instruction.
1561 locations->SetInAt(0, Location::RequiresFpuRegister());
1562 locations->SetOut(Location::RequiresRegister());
1563 locations->AddTemp(Location::RequiresFpuRegister());
1564 break;
1565
Roland Levillaindff1f282014-11-05 14:15:05 +00001566 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001567 // Processing a Dex `double-to-long' instruction.
1568 locations->SetInAt(0, Location::RequiresFpuRegister());
1569 locations->SetOut(Location::RequiresRegister());
1570 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001571 break;
1572
1573 default:
1574 LOG(FATAL) << "Unexpected type conversion from " << input_type
1575 << " to " << result_type;
1576 }
1577 break;
1578
Roland Levillain981e4542014-11-14 11:47:14 +00001579 case Primitive::kPrimChar:
1580 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001581 case Primitive::kPrimBoolean:
1582 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001583 case Primitive::kPrimByte:
1584 case Primitive::kPrimShort:
1585 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001586 // Processing a Dex `int-to-char' instruction.
1587 locations->SetInAt(0, Location::Any());
1588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1589 break;
1590
1591 default:
1592 LOG(FATAL) << "Unexpected type conversion from " << input_type
1593 << " to " << result_type;
1594 }
1595 break;
1596
Roland Levillaindff1f282014-11-05 14:15:05 +00001597 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001598 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001599 case Primitive::kPrimBoolean:
1600 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001601 case Primitive::kPrimByte:
1602 case Primitive::kPrimShort:
1603 case Primitive::kPrimInt:
1604 case Primitive::kPrimChar:
1605 // Processing a Dex `int-to-float' instruction.
1606 locations->SetInAt(0, Location::RequiresRegister());
1607 locations->SetOut(Location::RequiresFpuRegister());
1608 break;
1609
1610 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001611 // Processing a Dex `long-to-float' instruction.
1612 locations->SetInAt(0, Location::RequiresRegister());
1613 locations->SetOut(Location::RequiresFpuRegister());
1614 break;
1615
Roland Levillaincff13742014-11-17 14:32:17 +00001616 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001617 // Processing a Dex `double-to-float' instruction.
1618 locations->SetInAt(0, Location::RequiresFpuRegister());
1619 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001620 break;
1621
1622 default:
1623 LOG(FATAL) << "Unexpected type conversion from " << input_type
1624 << " to " << result_type;
1625 };
1626 break;
1627
Roland Levillaindff1f282014-11-05 14:15:05 +00001628 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001629 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001630 case Primitive::kPrimBoolean:
1631 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001632 case Primitive::kPrimByte:
1633 case Primitive::kPrimShort:
1634 case Primitive::kPrimInt:
1635 case Primitive::kPrimChar:
1636 // Processing a Dex `int-to-double' instruction.
1637 locations->SetInAt(0, Location::RequiresRegister());
1638 locations->SetOut(Location::RequiresFpuRegister());
1639 break;
1640
1641 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001642 // Processing a Dex `long-to-double' instruction.
1643 locations->SetInAt(0, Location::RequiresRegister());
1644 locations->SetOut(Location::RequiresFpuRegister());
1645 break;
1646
Roland Levillaincff13742014-11-17 14:32:17 +00001647 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001648 // Processing a Dex `float-to-double' instruction.
1649 locations->SetInAt(0, Location::RequiresFpuRegister());
1650 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001651 break;
1652
1653 default:
1654 LOG(FATAL) << "Unexpected type conversion from " << input_type
1655 << " to " << result_type;
1656 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001657 break;
1658
1659 default:
1660 LOG(FATAL) << "Unexpected type conversion from " << input_type
1661 << " to " << result_type;
1662 }
1663}
1664
1665void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1666 LocationSummary* locations = conversion->GetLocations();
1667 Location out = locations->Out();
1668 Location in = locations->InAt(0);
1669 Primitive::Type result_type = conversion->GetResultType();
1670 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001671 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001672 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001673 case Primitive::kPrimByte:
1674 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001675 case Primitive::kPrimBoolean:
1676 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001677 case Primitive::kPrimShort:
1678 case Primitive::kPrimInt:
1679 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001680 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001681 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001683 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001685 Address(CpuRegister(RSP), in.GetStackIndex()));
1686 } else {
1687 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001688 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001689 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1690 }
1691 break;
1692
1693 default:
1694 LOG(FATAL) << "Unexpected type conversion from " << input_type
1695 << " to " << result_type;
1696 }
1697 break;
1698
Roland Levillain01a8d712014-11-14 16:27:39 +00001699 case Primitive::kPrimShort:
1700 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001701 case Primitive::kPrimBoolean:
1702 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001703 case Primitive::kPrimByte:
1704 case Primitive::kPrimInt:
1705 case Primitive::kPrimChar:
1706 // Processing a Dex `int-to-short' instruction.
1707 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001708 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001709 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001710 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001711 Address(CpuRegister(RSP), in.GetStackIndex()));
1712 } else {
1713 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001714 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001715 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1716 }
1717 break;
1718
1719 default:
1720 LOG(FATAL) << "Unexpected type conversion from " << input_type
1721 << " to " << result_type;
1722 }
1723 break;
1724
Roland Levillain946e1432014-11-11 17:35:19 +00001725 case Primitive::kPrimInt:
1726 switch (input_type) {
1727 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001728 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001729 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001730 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001731 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001732 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001733 Address(CpuRegister(RSP), in.GetStackIndex()));
1734 } else {
1735 DCHECK(in.IsConstant());
1736 DCHECK(in.GetConstant()->IsLongConstant());
1737 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001738 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001739 }
1740 break;
1741
Roland Levillain3f8f9362014-12-02 17:45:01 +00001742 case Primitive::kPrimFloat: {
1743 // Processing a Dex `float-to-int' instruction.
1744 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1745 CpuRegister output = out.AsRegister<CpuRegister>();
1746 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1747 Label done, nan;
1748
1749 __ movl(output, Immediate(kPrimIntMax));
1750 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001751 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001752 // if input >= temp goto done
1753 __ comiss(input, temp);
1754 __ j(kAboveEqual, &done);
1755 // if input == NaN goto nan
1756 __ j(kUnordered, &nan);
1757 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001758 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001759 __ jmp(&done);
1760 __ Bind(&nan);
1761 // output = 0
1762 __ xorl(output, output);
1763 __ Bind(&done);
1764 break;
1765 }
1766
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001767 case Primitive::kPrimDouble: {
1768 // Processing a Dex `double-to-int' instruction.
1769 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1770 CpuRegister output = out.AsRegister<CpuRegister>();
1771 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1772 Label done, nan;
1773
1774 __ movl(output, Immediate(kPrimIntMax));
1775 // temp = int-to-double(output)
1776 __ cvtsi2sd(temp, output);
1777 // if input >= temp goto done
1778 __ comisd(input, temp);
1779 __ j(kAboveEqual, &done);
1780 // if input == NaN goto nan
1781 __ j(kUnordered, &nan);
1782 // output = double-to-int-truncate(input)
1783 __ cvttsd2si(output, input);
1784 __ jmp(&done);
1785 __ Bind(&nan);
1786 // output = 0
1787 __ xorl(output, output);
1788 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001789 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001790 }
Roland Levillain946e1432014-11-11 17:35:19 +00001791
1792 default:
1793 LOG(FATAL) << "Unexpected type conversion from " << input_type
1794 << " to " << result_type;
1795 }
1796 break;
1797
Roland Levillaindff1f282014-11-05 14:15:05 +00001798 case Primitive::kPrimLong:
1799 switch (input_type) {
1800 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00001801 case Primitive::kPrimBoolean:
1802 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001803 case Primitive::kPrimByte:
1804 case Primitive::kPrimShort:
1805 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001806 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001807 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001809 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001810 break;
1811
Roland Levillain624279f2014-12-04 11:54:28 +00001812 case Primitive::kPrimFloat: {
1813 // Processing a Dex `float-to-long' instruction.
1814 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1815 CpuRegister output = out.AsRegister<CpuRegister>();
1816 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1817 Label done, nan;
1818
1819 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001820 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001821 __ cvtsi2ss(temp, output, true);
1822 // if input >= temp goto done
1823 __ comiss(input, temp);
1824 __ j(kAboveEqual, &done);
1825 // if input == NaN goto nan
1826 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001827 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001828 __ cvttss2si(output, input, true);
1829 __ jmp(&done);
1830 __ Bind(&nan);
1831 // output = 0
1832 __ xorq(output, output);
1833 __ Bind(&done);
1834 break;
1835 }
1836
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001837 case Primitive::kPrimDouble: {
1838 // Processing a Dex `double-to-long' instruction.
1839 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1840 CpuRegister output = out.AsRegister<CpuRegister>();
1841 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1842 Label done, nan;
1843
1844 __ movq(output, Immediate(kPrimLongMax));
1845 // temp = long-to-double(output)
1846 __ cvtsi2sd(temp, output, true);
1847 // if input >= temp goto done
1848 __ comisd(input, temp);
1849 __ j(kAboveEqual, &done);
1850 // if input == NaN goto nan
1851 __ j(kUnordered, &nan);
1852 // output = double-to-long-truncate(input)
1853 __ cvttsd2si(output, input, true);
1854 __ jmp(&done);
1855 __ Bind(&nan);
1856 // output = 0
1857 __ xorq(output, output);
1858 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001859 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001860 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001861
1862 default:
1863 LOG(FATAL) << "Unexpected type conversion from " << input_type
1864 << " to " << result_type;
1865 }
1866 break;
1867
Roland Levillain981e4542014-11-14 11:47:14 +00001868 case Primitive::kPrimChar:
1869 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001870 case Primitive::kPrimBoolean:
1871 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001872 case Primitive::kPrimByte:
1873 case Primitive::kPrimShort:
1874 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001875 // Processing a Dex `int-to-char' instruction.
1876 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001877 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001878 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001879 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001880 Address(CpuRegister(RSP), in.GetStackIndex()));
1881 } else {
1882 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001883 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001884 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1885 }
1886 break;
1887
1888 default:
1889 LOG(FATAL) << "Unexpected type conversion from " << input_type
1890 << " to " << result_type;
1891 }
1892 break;
1893
Roland Levillaindff1f282014-11-05 14:15:05 +00001894 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001895 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001896 case Primitive::kPrimBoolean:
1897 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001898 case Primitive::kPrimByte:
1899 case Primitive::kPrimShort:
1900 case Primitive::kPrimInt:
1901 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001902 // Processing a Dex `int-to-float' instruction.
1903 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001904 break;
1905
1906 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001907 // Processing a Dex `long-to-float' instruction.
1908 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1909 break;
1910
Roland Levillaincff13742014-11-17 14:32:17 +00001911 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001912 // Processing a Dex `double-to-float' instruction.
1913 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001914 break;
1915
1916 default:
1917 LOG(FATAL) << "Unexpected type conversion from " << input_type
1918 << " to " << result_type;
1919 };
1920 break;
1921
Roland Levillaindff1f282014-11-05 14:15:05 +00001922 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001923 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001924 case Primitive::kPrimBoolean:
1925 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001926 case Primitive::kPrimByte:
1927 case Primitive::kPrimShort:
1928 case Primitive::kPrimInt:
1929 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001930 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001931 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001932 break;
1933
1934 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001935 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001937 break;
1938
Roland Levillaincff13742014-11-17 14:32:17 +00001939 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001940 // Processing a Dex `float-to-double' instruction.
1941 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001942 break;
1943
1944 default:
1945 LOG(FATAL) << "Unexpected type conversion from " << input_type
1946 << " to " << result_type;
1947 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001948 break;
1949
1950 default:
1951 LOG(FATAL) << "Unexpected type conversion from " << input_type
1952 << " to " << result_type;
1953 }
1954}
1955
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001956void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001957 LocationSummary* locations =
1958 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001959 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001960 case Primitive::kPrimInt: {
1961 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001962 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001964 break;
1965 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001968 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05001969 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001970 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05001971 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001972 break;
1973 }
1974
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001975 case Primitive::kPrimDouble:
1976 case Primitive::kPrimFloat: {
1977 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001978 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001979 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001980 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001981 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001982
1983 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001985 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001986}
1987
1988void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1989 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001990 Location first = locations->InAt(0);
1991 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001992 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01001993
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001994 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001995 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001996 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001997 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1998 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
1999 } else {
2000 __ leal(out.AsRegister<CpuRegister>(), Address(
2001 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2002 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002003 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002004 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2005 __ addl(out.AsRegister<CpuRegister>(),
2006 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2007 } else {
2008 __ leal(out.AsRegister<CpuRegister>(), Address(
2009 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2010 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002011 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002012 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002013 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002014 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002015 break;
2016 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002017
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002018 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002019 if (second.IsRegister()) {
2020 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2021 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2022 } else {
2023 __ leaq(out.AsRegister<CpuRegister>(), Address(
2024 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2025 }
2026 } else {
2027 DCHECK(second.IsConstant());
2028 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2029 int32_t int32_value = Low32Bits(value);
2030 DCHECK_EQ(int32_value, value);
2031 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2032 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2033 } else {
2034 __ leaq(out.AsRegister<CpuRegister>(), Address(
2035 first.AsRegister<CpuRegister>(), int32_value));
2036 }
2037 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002038 break;
2039 }
2040
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002041 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002042 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002043 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002044 }
2045
2046 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002047 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002048 break;
2049 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002050
2051 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002052 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002053 }
2054}
2055
2056void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002057 LocationSummary* locations =
2058 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002059 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002060 case Primitive::kPrimInt: {
2061 locations->SetInAt(0, Location::RequiresRegister());
2062 locations->SetInAt(1, Location::Any());
2063 locations->SetOut(Location::SameAsFirstInput());
2064 break;
2065 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002066 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002067 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002068 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002069 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002070 break;
2071 }
Calin Juravle11351682014-10-23 15:38:15 +01002072 case Primitive::kPrimFloat:
2073 case Primitive::kPrimDouble: {
2074 locations->SetInAt(0, Location::RequiresFpuRegister());
2075 locations->SetInAt(1, Location::RequiresFpuRegister());
2076 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002077 break;
Calin Juravle11351682014-10-23 15:38:15 +01002078 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002079 default:
Calin Juravle11351682014-10-23 15:38:15 +01002080 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002081 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002082}
2083
2084void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2085 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002086 Location first = locations->InAt(0);
2087 Location second = locations->InAt(1);
2088 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002089 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002090 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002091 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002093 } else if (second.IsConstant()) {
2094 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002095 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002096 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002097 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002098 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002099 break;
2100 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002101 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002102 if (second.IsConstant()) {
2103 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2104 DCHECK(IsInt<32>(value));
2105 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2106 } else {
2107 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2108 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002109 break;
2110 }
2111
Calin Juravle11351682014-10-23 15:38:15 +01002112 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002114 break;
Calin Juravle11351682014-10-23 15:38:15 +01002115 }
2116
2117 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002119 break;
2120 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002121
2122 default:
Calin Juravle11351682014-10-23 15:38:15 +01002123 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002124 }
2125}
2126
Calin Juravle34bacdf2014-10-07 20:23:36 +01002127void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2128 LocationSummary* locations =
2129 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2130 switch (mul->GetResultType()) {
2131 case Primitive::kPrimInt: {
2132 locations->SetInAt(0, Location::RequiresRegister());
2133 locations->SetInAt(1, Location::Any());
2134 locations->SetOut(Location::SameAsFirstInput());
2135 break;
2136 }
2137 case Primitive::kPrimLong: {
2138 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002139 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2140 if (locations->InAt(1).IsConstant()) {
2141 // Can use 3 operand multiply.
2142 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2143 } else {
2144 locations->SetOut(Location::SameAsFirstInput());
2145 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002146 break;
2147 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002148 case Primitive::kPrimFloat:
2149 case Primitive::kPrimDouble: {
2150 locations->SetInAt(0, Location::RequiresFpuRegister());
2151 locations->SetInAt(1, Location::RequiresFpuRegister());
2152 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002153 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002154 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002155
2156 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002157 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002158 }
2159}
2160
2161void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2162 LocationSummary* locations = mul->GetLocations();
2163 Location first = locations->InAt(0);
2164 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002165 switch (mul->GetResultType()) {
2166 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002167 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002168 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002170 } else if (second.IsConstant()) {
2171 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002172 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002173 } else {
2174 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002175 __ imull(first.AsRegister<CpuRegister>(),
2176 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002177 }
2178 break;
2179 }
2180 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002181 if (second.IsConstant()) {
2182 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2183 DCHECK(IsInt<32>(value));
2184 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2185 first.AsRegister<CpuRegister>(),
2186 Immediate(static_cast<int32_t>(value)));
2187 } else {
2188 DCHECK(first.Equals(locations->Out()));
2189 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2190 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002191 break;
2192 }
2193
Calin Juravleb5bfa962014-10-21 18:02:24 +01002194 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002195 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002196 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002197 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002198 }
2199
2200 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002201 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002202 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002203 break;
2204 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002205
2206 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002207 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002208 }
2209}
2210
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002211void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2212 uint32_t stack_adjustment, bool is_float) {
2213 if (source.IsStackSlot()) {
2214 DCHECK(is_float);
2215 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2216 } else if (source.IsDoubleStackSlot()) {
2217 DCHECK(!is_float);
2218 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2219 } else {
2220 // Write the value to the temporary location on the stack and load to FP stack.
2221 if (is_float) {
2222 Location stack_temp = Location::StackSlot(temp_offset);
2223 codegen_->Move(stack_temp, source);
2224 __ flds(Address(CpuRegister(RSP), temp_offset));
2225 } else {
2226 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2227 codegen_->Move(stack_temp, source);
2228 __ fldl(Address(CpuRegister(RSP), temp_offset));
2229 }
2230 }
2231}
2232
2233void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2234 Primitive::Type type = rem->GetResultType();
2235 bool is_float = type == Primitive::kPrimFloat;
2236 size_t elem_size = Primitive::ComponentSize(type);
2237 LocationSummary* locations = rem->GetLocations();
2238 Location first = locations->InAt(0);
2239 Location second = locations->InAt(1);
2240 Location out = locations->Out();
2241
2242 // Create stack space for 2 elements.
2243 // TODO: enhance register allocator to ask for stack temporaries.
2244 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2245
2246 // Load the values to the FP stack in reverse order, using temporaries if needed.
2247 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2248 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2249
2250 // Loop doing FPREM until we stabilize.
2251 Label retry;
2252 __ Bind(&retry);
2253 __ fprem();
2254
2255 // Move FP status to AX.
2256 __ fstsw();
2257
2258 // And see if the argument reduction is complete. This is signaled by the
2259 // C2 FPU flag bit set to 0.
2260 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2261 __ j(kNotEqual, &retry);
2262
2263 // We have settled on the final value. Retrieve it into an XMM register.
2264 // Store FP top of stack to real stack.
2265 if (is_float) {
2266 __ fsts(Address(CpuRegister(RSP), 0));
2267 } else {
2268 __ fstl(Address(CpuRegister(RSP), 0));
2269 }
2270
2271 // Pop the 2 items from the FP stack.
2272 __ fucompp();
2273
2274 // Load the value from the stack into an XMM register.
2275 DCHECK(out.IsFpuRegister()) << out;
2276 if (is_float) {
2277 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2278 } else {
2279 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2280 }
2281
2282 // And remove the temporary stack space we allocated.
2283 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2284}
2285
Calin Juravlebacfec32014-11-14 15:54:36 +00002286void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2287 DCHECK(instruction->IsDiv() || instruction->IsRem());
2288 Primitive::Type type = instruction->GetResultType();
2289 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2290
2291 bool is_div = instruction->IsDiv();
2292 LocationSummary* locations = instruction->GetLocations();
2293
Roland Levillain271ab9c2014-11-27 15:23:57 +00002294 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2295 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002296
Roland Levillain271ab9c2014-11-27 15:23:57 +00002297 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002298 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2299
2300 SlowPathCodeX86_64* slow_path =
2301 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2302 out_reg.AsRegister(), type, is_div);
2303 codegen_->AddSlowPath(slow_path);
2304
2305 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2306 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2307 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002308 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002309 __ cmpl(second_reg, Immediate(-1));
2310 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002311 // edx:eax <- sign-extended of eax
2312 __ cdq();
2313 // eax = quotient, edx = remainder
2314 __ idivl(second_reg);
2315 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002316 __ cmpq(second_reg, Immediate(-1));
2317 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002318 // rdx:rax <- sign-extended of rax
2319 __ cqo();
2320 // rax = quotient, rdx = remainder
2321 __ idivq(second_reg);
2322 }
2323
2324 __ Bind(slow_path->GetExitLabel());
2325}
2326
Calin Juravle7c4954d2014-10-28 16:57:40 +00002327void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2328 LocationSummary* locations =
2329 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2330 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002331 case Primitive::kPrimInt:
2332 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002333 locations->SetInAt(0, Location::RegisterLocation(RAX));
2334 locations->SetInAt(1, Location::RequiresRegister());
2335 locations->SetOut(Location::SameAsFirstInput());
2336 // Intel uses edx:eax as the dividend.
2337 locations->AddTemp(Location::RegisterLocation(RDX));
2338 break;
2339 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002340
Calin Juravle7c4954d2014-10-28 16:57:40 +00002341 case Primitive::kPrimFloat:
2342 case Primitive::kPrimDouble: {
2343 locations->SetInAt(0, Location::RequiresFpuRegister());
2344 locations->SetInAt(1, Location::RequiresFpuRegister());
2345 locations->SetOut(Location::SameAsFirstInput());
2346 break;
2347 }
2348
2349 default:
2350 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2351 }
2352}
2353
2354void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2355 LocationSummary* locations = div->GetLocations();
2356 Location first = locations->InAt(0);
2357 Location second = locations->InAt(1);
2358 DCHECK(first.Equals(locations->Out()));
2359
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002360 Primitive::Type type = div->GetResultType();
2361 switch (type) {
2362 case Primitive::kPrimInt:
2363 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002364 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002365 break;
2366 }
2367
Calin Juravle7c4954d2014-10-28 16:57:40 +00002368 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002370 break;
2371 }
2372
2373 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002374 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002375 break;
2376 }
2377
2378 default:
2379 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2380 }
2381}
2382
Calin Juravlebacfec32014-11-14 15:54:36 +00002383void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002384 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002385 LocationSummary* locations =
2386 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002387
2388 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002389 case Primitive::kPrimInt:
2390 case Primitive::kPrimLong: {
2391 locations->SetInAt(0, Location::RegisterLocation(RAX));
2392 locations->SetInAt(1, Location::RequiresRegister());
2393 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2394 locations->SetOut(Location::RegisterLocation(RDX));
2395 break;
2396 }
2397
2398 case Primitive::kPrimFloat:
2399 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002400 locations->SetInAt(0, Location::Any());
2401 locations->SetInAt(1, Location::Any());
2402 locations->SetOut(Location::RequiresFpuRegister());
2403 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002404 break;
2405 }
2406
2407 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002408 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002409 }
2410}
2411
2412void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2413 Primitive::Type type = rem->GetResultType();
2414 switch (type) {
2415 case Primitive::kPrimInt:
2416 case Primitive::kPrimLong: {
2417 GenerateDivRemIntegral(rem);
2418 break;
2419 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002420 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002421 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002422 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002423 break;
2424 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002425 default:
2426 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2427 }
2428}
2429
Calin Juravled0d48522014-11-04 16:40:20 +00002430void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2431 LocationSummary* locations =
2432 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2433 locations->SetInAt(0, Location::Any());
2434 if (instruction->HasUses()) {
2435 locations->SetOut(Location::SameAsFirstInput());
2436 }
2437}
2438
2439void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2440 SlowPathCodeX86_64* slow_path =
2441 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2442 codegen_->AddSlowPath(slow_path);
2443
2444 LocationSummary* locations = instruction->GetLocations();
2445 Location value = locations->InAt(0);
2446
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002447 switch (instruction->GetType()) {
2448 case Primitive::kPrimInt: {
2449 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002451 __ j(kEqual, slow_path->GetEntryLabel());
2452 } else if (value.IsStackSlot()) {
2453 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2454 __ j(kEqual, slow_path->GetEntryLabel());
2455 } else {
2456 DCHECK(value.IsConstant()) << value;
2457 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2458 __ jmp(slow_path->GetEntryLabel());
2459 }
2460 }
2461 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002462 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002463 case Primitive::kPrimLong: {
2464 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002465 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002466 __ j(kEqual, slow_path->GetEntryLabel());
2467 } else if (value.IsDoubleStackSlot()) {
2468 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2469 __ j(kEqual, slow_path->GetEntryLabel());
2470 } else {
2471 DCHECK(value.IsConstant()) << value;
2472 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2473 __ jmp(slow_path->GetEntryLabel());
2474 }
2475 }
2476 break;
2477 }
2478 default:
2479 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002480 }
Calin Juravled0d48522014-11-04 16:40:20 +00002481}
2482
Calin Juravle9aec02f2014-11-18 23:06:35 +00002483void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2484 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2485
2486 LocationSummary* locations =
2487 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2488
2489 switch (op->GetResultType()) {
2490 case Primitive::kPrimInt:
2491 case Primitive::kPrimLong: {
2492 locations->SetInAt(0, Location::RequiresRegister());
2493 // The shift count needs to be in CL.
2494 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2495 locations->SetOut(Location::SameAsFirstInput());
2496 break;
2497 }
2498 default:
2499 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2500 }
2501}
2502
2503void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2504 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2505
2506 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002507 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002508 Location second = locations->InAt(1);
2509
2510 switch (op->GetResultType()) {
2511 case Primitive::kPrimInt: {
2512 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002513 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002514 if (op->IsShl()) {
2515 __ shll(first_reg, second_reg);
2516 } else if (op->IsShr()) {
2517 __ sarl(first_reg, second_reg);
2518 } else {
2519 __ shrl(first_reg, second_reg);
2520 }
2521 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002522 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002523 if (op->IsShl()) {
2524 __ shll(first_reg, imm);
2525 } else if (op->IsShr()) {
2526 __ sarl(first_reg, imm);
2527 } else {
2528 __ shrl(first_reg, imm);
2529 }
2530 }
2531 break;
2532 }
2533 case Primitive::kPrimLong: {
2534 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002535 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002536 if (op->IsShl()) {
2537 __ shlq(first_reg, second_reg);
2538 } else if (op->IsShr()) {
2539 __ sarq(first_reg, second_reg);
2540 } else {
2541 __ shrq(first_reg, second_reg);
2542 }
2543 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002544 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002545 if (op->IsShl()) {
2546 __ shlq(first_reg, imm);
2547 } else if (op->IsShr()) {
2548 __ sarq(first_reg, imm);
2549 } else {
2550 __ shrq(first_reg, imm);
2551 }
2552 }
2553 break;
2554 }
2555 default:
2556 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2557 }
2558}
2559
2560void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2561 HandleShift(shl);
2562}
2563
2564void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2565 HandleShift(shl);
2566}
2567
2568void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2569 HandleShift(shr);
2570}
2571
2572void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2573 HandleShift(shr);
2574}
2575
2576void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2577 HandleShift(ushr);
2578}
2579
2580void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2581 HandleShift(ushr);
2582}
2583
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002584void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002585 LocationSummary* locations =
2586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002587 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002588 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2589 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2590 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002591}
2592
2593void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2594 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002595 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002596 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2597
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002598 __ gs()->call(
2599 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002600
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002601 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002602 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002603}
2604
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002605void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2606 LocationSummary* locations =
2607 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2608 InvokeRuntimeCallingConvention calling_convention;
2609 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002610 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002611 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002612 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002613}
2614
2615void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2616 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002617 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002618 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2619
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002620 __ gs()->call(
2621 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002622
2623 DCHECK(!codegen_->IsLeafMethod());
2624 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2625}
2626
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002627void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002628 LocationSummary* locations =
2629 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002630 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2631 if (location.IsStackSlot()) {
2632 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2633 } else if (location.IsDoubleStackSlot()) {
2634 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2635 }
2636 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002637}
2638
2639void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2640 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002641 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002642}
2643
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002644void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002645 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002646 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002649}
2650
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002651void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2652 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002653 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2654 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002655 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002656 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002657 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002659 break;
2660
2661 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002662 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002663 break;
2664
2665 default:
2666 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2667 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002668}
2669
2670void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002671 LocationSummary* locations =
2672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002673 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2674 locations->SetInAt(i, Location::Any());
2675 }
2676 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002677}
2678
2679void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002680 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002681 LOG(FATAL) << "Unimplemented";
2682}
2683
Calin Juravle52c48962014-12-16 17:02:57 +00002684void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2685 /*
2686 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2687 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2688 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2689 */
2690 switch (kind) {
2691 case MemBarrierKind::kAnyAny: {
2692 __ mfence();
2693 break;
2694 }
2695 case MemBarrierKind::kAnyStore:
2696 case MemBarrierKind::kLoadAny:
2697 case MemBarrierKind::kStoreStore: {
2698 // nop
2699 break;
2700 }
2701 default:
2702 LOG(FATAL) << "Unexpected memory barier " << kind;
2703 }
2704}
2705
2706void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2707 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2708
Nicolas Geoffray39468442014-09-02 15:17:15 +01002709 LocationSummary* locations =
2710 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002711 locations->SetInAt(0, Location::RequiresRegister());
2712 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2713}
2714
2715void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2716 const FieldInfo& field_info) {
2717 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2718
2719 LocationSummary* locations = instruction->GetLocations();
2720 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2721 Location out = locations->Out();
2722 bool is_volatile = field_info.IsVolatile();
2723 Primitive::Type field_type = field_info.GetFieldType();
2724 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2725
2726 switch (field_type) {
2727 case Primitive::kPrimBoolean: {
2728 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2729 break;
2730 }
2731
2732 case Primitive::kPrimByte: {
2733 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2734 break;
2735 }
2736
2737 case Primitive::kPrimShort: {
2738 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2739 break;
2740 }
2741
2742 case Primitive::kPrimChar: {
2743 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2744 break;
2745 }
2746
2747 case Primitive::kPrimInt:
2748 case Primitive::kPrimNot: {
2749 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2750 break;
2751 }
2752
2753 case Primitive::kPrimLong: {
2754 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2755 break;
2756 }
2757
2758 case Primitive::kPrimFloat: {
2759 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2760 break;
2761 }
2762
2763 case Primitive::kPrimDouble: {
2764 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2765 break;
2766 }
2767
2768 case Primitive::kPrimVoid:
2769 LOG(FATAL) << "Unreachable type " << field_type;
2770 UNREACHABLE();
2771 }
2772
Calin Juravle77520bc2015-01-12 18:45:46 +00002773 codegen_->MaybeRecordImplicitNullCheck(instruction);
2774
Calin Juravle52c48962014-12-16 17:02:57 +00002775 if (is_volatile) {
2776 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2777 }
2778}
2779
2780void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2781 const FieldInfo& field_info) {
2782 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2783
2784 LocationSummary* locations =
2785 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002786 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002787 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2788
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002789 locations->SetInAt(0, Location::RequiresRegister());
2790 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002791 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002792 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002793 locations->AddTemp(Location::RequiresRegister());
2794 locations->AddTemp(Location::RequiresRegister());
2795 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002796}
2797
Calin Juravle52c48962014-12-16 17:02:57 +00002798void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2799 const FieldInfo& field_info) {
2800 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2801
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002802 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002803 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2804 Location value = locations->InAt(1);
2805 bool is_volatile = field_info.IsVolatile();
2806 Primitive::Type field_type = field_info.GetFieldType();
2807 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2808
2809 if (is_volatile) {
2810 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2811 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002812
2813 switch (field_type) {
2814 case Primitive::kPrimBoolean:
2815 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002816 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002817 break;
2818 }
2819
2820 case Primitive::kPrimShort:
2821 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002822 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002823 break;
2824 }
2825
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002827 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002828 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002829 break;
2830 }
2831
2832 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002833 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002834 break;
2835 }
2836
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002837 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002838 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002839 break;
2840 }
2841
2842 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002843 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002844 break;
2845 }
2846
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002847 case Primitive::kPrimVoid:
2848 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002849 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002850 }
Calin Juravle52c48962014-12-16 17:02:57 +00002851
Calin Juravle77520bc2015-01-12 18:45:46 +00002852 codegen_->MaybeRecordImplicitNullCheck(instruction);
2853
2854 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2855 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2856 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2857 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2858 }
2859
Calin Juravle52c48962014-12-16 17:02:57 +00002860 if (is_volatile) {
2861 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2862 }
2863}
2864
2865void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2866 HandleFieldSet(instruction, instruction->GetFieldInfo());
2867}
2868
2869void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2870 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002871}
2872
2873void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002874 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002875}
2876
2877void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002878 HandleFieldGet(instruction, instruction->GetFieldInfo());
2879}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002880
Calin Juravle52c48962014-12-16 17:02:57 +00002881void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2882 HandleFieldGet(instruction);
2883}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002884
Calin Juravle52c48962014-12-16 17:02:57 +00002885void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2886 HandleFieldGet(instruction, instruction->GetFieldInfo());
2887}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002888
Calin Juravle52c48962014-12-16 17:02:57 +00002889void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2890 HandleFieldSet(instruction, instruction->GetFieldInfo());
2891}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002892
Calin Juravle52c48962014-12-16 17:02:57 +00002893void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2894 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002895}
2896
2897void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002898 LocationSummary* locations =
2899 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002900 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2901 ? Location::RequiresRegister()
2902 : Location::Any();
2903 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002904 if (instruction->HasUses()) {
2905 locations->SetOut(Location::SameAsFirstInput());
2906 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002907}
2908
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002909void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002910 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2911 return;
2912 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002913 LocationSummary* locations = instruction->GetLocations();
2914 Location obj = locations->InAt(0);
2915
2916 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2917 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2918}
2919
2920void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002921 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002922 codegen_->AddSlowPath(slow_path);
2923
2924 LocationSummary* locations = instruction->GetLocations();
2925 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002926
2927 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002928 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002929 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002930 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002931 } else {
2932 DCHECK(obj.IsConstant()) << obj;
2933 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2934 __ jmp(slow_path->GetEntryLabel());
2935 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002936 }
2937 __ j(kEqual, slow_path->GetEntryLabel());
2938}
2939
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002940void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2941 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2942 GenerateImplicitNullCheck(instruction);
2943 } else {
2944 GenerateExplicitNullCheck(instruction);
2945 }
2946}
2947
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002948void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002949 LocationSummary* locations =
2950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002951 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002952 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002953 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002955}
2956
2957void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2958 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002959 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002960 Location index = locations->InAt(1);
2961
2962 switch (instruction->GetType()) {
2963 case Primitive::kPrimBoolean: {
2964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002966 if (index.IsConstant()) {
2967 __ movzxb(out, Address(obj,
2968 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2969 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002971 }
2972 break;
2973 }
2974
2975 case Primitive::kPrimByte: {
2976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002977 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 if (index.IsConstant()) {
2979 __ movsxb(out, Address(obj,
2980 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2981 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002982 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002983 }
2984 break;
2985 }
2986
2987 case Primitive::kPrimShort: {
2988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002989 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002990 if (index.IsConstant()) {
2991 __ movsxw(out, Address(obj,
2992 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2993 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002994 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002995 }
2996 break;
2997 }
2998
2999 case Primitive::kPrimChar: {
3000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003002 if (index.IsConstant()) {
3003 __ movzxw(out, Address(obj,
3004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3005 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003007 }
3008 break;
3009 }
3010
3011 case Primitive::kPrimInt:
3012 case Primitive::kPrimNot: {
3013 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3014 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003016 if (index.IsConstant()) {
3017 __ movl(out, Address(obj,
3018 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3019 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003021 }
3022 break;
3023 }
3024
3025 case Primitive::kPrimLong: {
3026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003027 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003028 if (index.IsConstant()) {
3029 __ movq(out, Address(obj,
3030 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3031 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003032 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003033 }
3034 break;
3035 }
3036
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003037 case Primitive::kPrimFloat: {
3038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003040 if (index.IsConstant()) {
3041 __ movss(out, Address(obj,
3042 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3043 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003044 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003045 }
3046 break;
3047 }
3048
3049 case Primitive::kPrimDouble: {
3050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003052 if (index.IsConstant()) {
3053 __ movsd(out, Address(obj,
3054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3055 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003057 }
3058 break;
3059 }
3060
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003061 case Primitive::kPrimVoid:
3062 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003063 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003064 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003065 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003066}
3067
3068void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003069 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003070
3071 bool needs_write_barrier =
3072 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3073 bool needs_runtime_call = instruction->NeedsTypeCheck();
3074
Nicolas Geoffray39468442014-09-02 15:17:15 +01003075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003076 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3077 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003079 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3080 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3081 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003083 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003084 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003085 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3086 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003087 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003088 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003089 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3090 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003091 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003092 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003093 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003094
3095 if (needs_write_barrier) {
3096 // Temporary registers for the write barrier.
3097 locations->AddTemp(Location::RequiresRegister());
3098 locations->AddTemp(Location::RequiresRegister());
3099 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003101}
3102
3103void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3104 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003107 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003108 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003109 bool needs_runtime_call = locations->WillCall();
3110 bool needs_write_barrier =
3111 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003112
3113 switch (value_type) {
3114 case Primitive::kPrimBoolean:
3115 case Primitive::kPrimByte: {
3116 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 if (index.IsConstant()) {
3118 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003119 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003120 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003121 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003122 __ movb(Address(obj, offset),
3123 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003124 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003126 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003127 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3128 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003129 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003130 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003131 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3132 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003134 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003135 break;
3136 }
3137
3138 case Primitive::kPrimShort:
3139 case Primitive::kPrimChar: {
3140 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003141 if (index.IsConstant()) {
3142 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003143 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003144 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003145 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003146 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003147 __ movw(Address(obj, offset),
3148 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003149 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003150 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003151 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003152 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003153 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3154 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003155 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003156 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003158 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3159 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003160 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003161 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003162 break;
3163 }
3164
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003165 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003166 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003167 if (!needs_runtime_call) {
3168 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3169 if (index.IsConstant()) {
3170 size_t offset =
3171 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3172 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003173 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003174 } else {
3175 DCHECK(value.IsConstant()) << value;
3176 __ movl(Address(obj, offset),
3177 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3178 }
3179 } else {
3180 DCHECK(index.IsRegister()) << index;
3181 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3183 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003184 } else {
3185 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003187 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3188 }
3189 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003190 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003191 if (needs_write_barrier) {
3192 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3194 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3195 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003196 }
3197 } else {
3198 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003199 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3200 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003201 DCHECK(!codegen_->IsLeafMethod());
3202 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003204 break;
3205 }
3206
3207 case Primitive::kPrimLong: {
3208 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 if (index.IsConstant()) {
3210 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003211 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003212 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003213 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003214 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003215 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3216 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003217 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003218 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003219 break;
3220 }
3221
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003222 case Primitive::kPrimFloat: {
3223 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3224 if (index.IsConstant()) {
3225 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3226 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003227 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003228 } else {
3229 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003230 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3231 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003232 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003234 break;
3235 }
3236
3237 case Primitive::kPrimDouble: {
3238 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3239 if (index.IsConstant()) {
3240 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3241 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003243 } else {
3244 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3246 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003247 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003248 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003249 break;
3250 }
3251
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003252 case Primitive::kPrimVoid:
3253 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003254 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003255 }
3256}
3257
3258void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003259 LocationSummary* locations =
3260 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003261 locations->SetInAt(0, Location::RequiresRegister());
3262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003263}
3264
3265void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3266 LocationSummary* locations = instruction->GetLocations();
3267 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003268 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3269 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003270 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003271 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272}
3273
3274void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003275 LocationSummary* locations =
3276 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003277 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003279 if (instruction->HasUses()) {
3280 locations->SetOut(Location::SameAsFirstInput());
3281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282}
3283
3284void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3285 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003286 Location index_loc = locations->InAt(0);
3287 Location length_loc = locations->InAt(1);
3288 SlowPathCodeX86_64* slow_path =
3289 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003290 codegen_->AddSlowPath(slow_path);
3291
Mark Mendellf60c90b2015-03-04 15:12:59 -05003292 CpuRegister length = length_loc.AsRegister<CpuRegister>();
3293 if (index_loc.IsConstant()) {
3294 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3295 __ cmpl(length, Immediate(value));
3296 } else {
3297 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
3298 }
3299 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003300}
3301
3302void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3303 CpuRegister card,
3304 CpuRegister object,
3305 CpuRegister value) {
3306 Label is_null;
3307 __ testl(value, value);
3308 __ j(kEqual, &is_null);
3309 __ gs()->movq(card, Address::Absolute(
3310 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3311 __ movq(temp, object);
3312 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3313 __ movb(Address(temp, card, TIMES_1, 0), card);
3314 __ Bind(&is_null);
3315}
3316
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003317void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3318 temp->SetLocations(nullptr);
3319}
3320
3321void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3322 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003323 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003324}
3325
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003326void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003327 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003328 LOG(FATAL) << "Unimplemented";
3329}
3330
3331void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003332 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3333}
3334
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003335void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3337}
3338
3339void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003340 HBasicBlock* block = instruction->GetBlock();
3341 if (block->GetLoopInformation() != nullptr) {
3342 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3343 // The back edge will generate the suspend check.
3344 return;
3345 }
3346 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3347 // The goto will generate the suspend check.
3348 return;
3349 }
3350 GenerateSuspendCheck(instruction, nullptr);
3351}
3352
3353void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3354 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003355 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003356 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003357 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003358 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003359 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003360 if (successor == nullptr) {
3361 __ j(kNotEqual, slow_path->GetEntryLabel());
3362 __ Bind(slow_path->GetReturnLabel());
3363 } else {
3364 __ j(kEqual, codegen_->GetLabelOf(successor));
3365 __ jmp(slow_path->GetEntryLabel());
3366 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003367}
3368
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003369X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3370 return codegen_->GetAssembler();
3371}
3372
3373void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3374 MoveOperands* move = moves_.Get(index);
3375 Location source = move->GetSource();
3376 Location destination = move->GetDestination();
3377
3378 if (source.IsRegister()) {
3379 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003380 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003381 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003382 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003383 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003384 } else {
3385 DCHECK(destination.IsDoubleStackSlot());
3386 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003387 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003388 }
3389 } else if (source.IsStackSlot()) {
3390 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003392 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003393 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003394 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003395 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003396 } else {
3397 DCHECK(destination.IsStackSlot());
3398 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3399 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3400 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003401 } else if (source.IsDoubleStackSlot()) {
3402 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003403 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003404 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003405 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003406 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3407 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003408 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003409 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003410 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3411 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3412 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003413 } else if (source.IsConstant()) {
3414 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003415 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3416 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003417 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003418 if (value == 0) {
3419 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
3420 } else {
3421 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
3422 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003423 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003424 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003425 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003426 }
3427 } else if (constant->IsLongConstant()) {
3428 int64_t value = constant->AsLongConstant()->GetValue();
3429 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003430 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003431 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003432 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003433 __ movq(CpuRegister(TMP), Immediate(value));
3434 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3435 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003436 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003437 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003438 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003439 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003440 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003441 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3442 if (value == 0) {
3443 // easy FP 0.0.
3444 __ xorps(dest, dest);
3445 } else {
3446 __ movl(CpuRegister(TMP), imm);
3447 __ movd(dest, CpuRegister(TMP));
3448 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003449 } else {
3450 DCHECK(destination.IsStackSlot()) << destination;
3451 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3452 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003453 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003454 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003455 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003456 int64_t value = bit_cast<int64_t, double>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003457 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003458 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003459 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3460 if (value == 0) {
3461 __ xorpd(dest, dest);
3462 } else {
3463 __ movq(CpuRegister(TMP), imm);
3464 __ movd(dest, CpuRegister(TMP));
3465 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003466 } else {
3467 DCHECK(destination.IsDoubleStackSlot()) << destination;
3468 __ movq(CpuRegister(TMP), imm);
3469 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3470 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003471 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003472 } else if (source.IsFpuRegister()) {
3473 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003474 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003475 } else if (destination.IsStackSlot()) {
3476 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003477 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003478 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003479 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003480 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003482 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003483 }
3484}
3485
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003486void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003487 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003488 __ movl(Address(CpuRegister(RSP), mem), reg);
3489 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003490}
3491
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003492void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003493 ScratchRegisterScope ensure_scratch(
3494 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3495
3496 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3497 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3498 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3499 Address(CpuRegister(RSP), mem2 + stack_offset));
3500 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3501 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3502 CpuRegister(ensure_scratch.GetRegister()));
3503}
3504
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003505void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3506 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3507 __ movq(Address(CpuRegister(RSP), mem), reg);
3508 __ movq(reg, CpuRegister(TMP));
3509}
3510
3511void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3512 ScratchRegisterScope ensure_scratch(
3513 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3514
3515 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3516 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3517 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3518 Address(CpuRegister(RSP), mem2 + stack_offset));
3519 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3520 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3521 CpuRegister(ensure_scratch.GetRegister()));
3522}
3523
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003524void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3525 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3526 __ movss(Address(CpuRegister(RSP), mem), reg);
3527 __ movd(reg, CpuRegister(TMP));
3528}
3529
3530void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3531 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3532 __ movsd(Address(CpuRegister(RSP), mem), reg);
3533 __ movd(reg, CpuRegister(TMP));
3534}
3535
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003536void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3537 MoveOperands* move = moves_.Get(index);
3538 Location source = move->GetSource();
3539 Location destination = move->GetDestination();
3540
3541 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003542 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003543 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003544 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003545 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003546 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003547 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003548 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3549 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003551 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003553 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3554 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003555 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003556 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3557 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3558 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003559 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003560 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003561 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003562 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003563 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003564 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003565 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003567 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003568 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003569 }
3570}
3571
3572
3573void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3574 __ pushq(CpuRegister(reg));
3575}
3576
3577
3578void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3579 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003580}
3581
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003582void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3583 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3584 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3585 Immediate(mirror::Class::kStatusInitialized));
3586 __ j(kLess, slow_path->GetEntryLabel());
3587 __ Bind(slow_path->GetExitLabel());
3588 // No need for memory fence, thanks to the X86_64 memory model.
3589}
3590
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003591void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003592 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3593 ? LocationSummary::kCallOnSlowPath
3594 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003595 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003596 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003597 locations->SetOut(Location::RequiresRegister());
3598}
3599
3600void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003602 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003603 DCHECK(!cls->CanCallRuntime());
3604 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003605 codegen_->LoadCurrentMethod(out);
3606 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3607 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003608 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003609 codegen_->LoadCurrentMethod(out);
3610 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3611 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003612 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3613 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3614 codegen_->AddSlowPath(slow_path);
3615 __ testl(out, out);
3616 __ j(kEqual, slow_path->GetEntryLabel());
3617 if (cls->MustGenerateClinitCheck()) {
3618 GenerateClassInitializationCheck(slow_path, out);
3619 } else {
3620 __ Bind(slow_path->GetExitLabel());
3621 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003622 }
3623}
3624
3625void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3626 LocationSummary* locations =
3627 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3628 locations->SetInAt(0, Location::RequiresRegister());
3629 if (check->HasUses()) {
3630 locations->SetOut(Location::SameAsFirstInput());
3631 }
3632}
3633
3634void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003635 // We assume the class to not be null.
3636 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3637 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003638 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003639 GenerateClassInitializationCheck(slow_path,
3640 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003641}
3642
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003643void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3644 LocationSummary* locations =
3645 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3646 locations->SetOut(Location::RequiresRegister());
3647}
3648
3649void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3650 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3651 codegen_->AddSlowPath(slow_path);
3652
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003654 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003655 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3656 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003657 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3658 __ testl(out, out);
3659 __ j(kEqual, slow_path->GetEntryLabel());
3660 __ Bind(slow_path->GetExitLabel());
3661}
3662
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003663void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3664 LocationSummary* locations =
3665 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3666 locations->SetOut(Location::RequiresRegister());
3667}
3668
3669void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3670 Address address = Address::Absolute(
3671 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003673 __ gs()->movl(address, Immediate(0));
3674}
3675
3676void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3677 LocationSummary* locations =
3678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3679 InvokeRuntimeCallingConvention calling_convention;
3680 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3681}
3682
3683void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3684 __ gs()->call(
3685 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3686 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3687}
3688
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003689void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003690 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3691 ? LocationSummary::kNoCall
3692 : LocationSummary::kCallOnSlowPath;
3693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3694 locations->SetInAt(0, Location::RequiresRegister());
3695 locations->SetInAt(1, Location::Any());
3696 locations->SetOut(Location::RequiresRegister());
3697}
3698
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003699void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003700 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003702 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003703 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003704 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3705 Label done, zero;
3706 SlowPathCodeX86_64* slow_path = nullptr;
3707
3708 // Return 0 if `obj` is null.
3709 // TODO: avoid this check if we know obj is not null.
3710 __ testl(obj, obj);
3711 __ j(kEqual, &zero);
3712 // Compare the class of `obj` with `cls`.
3713 __ movl(out, Address(obj, class_offset));
3714 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003715 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003716 } else {
3717 DCHECK(cls.IsStackSlot()) << cls;
3718 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3719 }
3720 if (instruction->IsClassFinal()) {
3721 // Classes must be equal for the instanceof to succeed.
3722 __ j(kNotEqual, &zero);
3723 __ movl(out, Immediate(1));
3724 __ jmp(&done);
3725 } else {
3726 // If the classes are not equal, we go into a slow path.
3727 DCHECK(locations->OnlyCallsOnSlowPath());
3728 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003729 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003730 codegen_->AddSlowPath(slow_path);
3731 __ j(kNotEqual, slow_path->GetEntryLabel());
3732 __ movl(out, Immediate(1));
3733 __ jmp(&done);
3734 }
3735 __ Bind(&zero);
3736 __ movl(out, Immediate(0));
3737 if (slow_path != nullptr) {
3738 __ Bind(slow_path->GetExitLabel());
3739 }
3740 __ Bind(&done);
3741}
3742
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003743void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3744 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3745 instruction, LocationSummary::kCallOnSlowPath);
3746 locations->SetInAt(0, Location::RequiresRegister());
3747 locations->SetInAt(1, Location::Any());
3748 locations->AddTemp(Location::RequiresRegister());
3749}
3750
3751void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3752 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003754 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003755 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003756 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3757 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3758 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3759 codegen_->AddSlowPath(slow_path);
3760
3761 // TODO: avoid this check if we know obj is not null.
3762 __ testl(obj, obj);
3763 __ j(kEqual, slow_path->GetExitLabel());
3764 // Compare the class of `obj` with `cls`.
3765 __ movl(temp, Address(obj, class_offset));
3766 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003767 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003768 } else {
3769 DCHECK(cls.IsStackSlot()) << cls;
3770 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3771 }
3772 // Classes must be equal for the checkcast to succeed.
3773 __ j(kNotEqual, slow_path->GetEntryLabel());
3774 __ Bind(slow_path->GetExitLabel());
3775}
3776
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003777void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3778 LocationSummary* locations =
3779 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3780 InvokeRuntimeCallingConvention calling_convention;
3781 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3782}
3783
3784void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3785 __ gs()->call(Address::Absolute(instruction->IsEnter()
3786 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3787 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3788 true));
3789 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3790}
3791
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003792void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3793void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3794void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3795
3796void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3797 LocationSummary* locations =
3798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3799 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3800 || instruction->GetResultType() == Primitive::kPrimLong);
3801 locations->SetInAt(0, Location::RequiresRegister());
3802 if (instruction->GetType() == Primitive::kPrimInt) {
3803 locations->SetInAt(1, Location::Any());
3804 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003805 // We can handle 32 bit constants.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003806 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003807 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003808 }
3809 locations->SetOut(Location::SameAsFirstInput());
3810}
3811
3812void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3813 HandleBitwiseOperation(instruction);
3814}
3815
3816void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3817 HandleBitwiseOperation(instruction);
3818}
3819
3820void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3821 HandleBitwiseOperation(instruction);
3822}
3823
3824void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3825 LocationSummary* locations = instruction->GetLocations();
3826 Location first = locations->InAt(0);
3827 Location second = locations->InAt(1);
3828 DCHECK(first.Equals(locations->Out()));
3829
3830 if (instruction->GetResultType() == Primitive::kPrimInt) {
3831 if (second.IsRegister()) {
3832 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003833 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003834 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003836 } else {
3837 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003838 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003839 }
3840 } else if (second.IsConstant()) {
3841 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3842 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003843 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003844 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003845 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003846 } else {
3847 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003848 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003849 }
3850 } else {
3851 Address address(CpuRegister(RSP), second.GetStackIndex());
3852 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003853 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003854 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003856 } else {
3857 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003858 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003859 }
3860 }
3861 } else {
3862 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003863 CpuRegister first_reg = first.AsRegister<CpuRegister>();
3864 bool second_is_constant = false;
3865 int64_t value = 0;
3866 if (second.IsConstant()) {
3867 second_is_constant = true;
3868 value = second.GetConstant()->AsLongConstant()->GetValue();
3869 DCHECK(IsInt<32>(value));
3870 }
3871
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003872 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003873 if (second_is_constant) {
3874 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
3875 } else {
3876 __ andq(first_reg, second.AsRegister<CpuRegister>());
3877 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003878 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003879 if (second_is_constant) {
3880 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
3881 } else {
3882 __ orq(first_reg, second.AsRegister<CpuRegister>());
3883 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003884 } else {
3885 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003886 if (second_is_constant) {
3887 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
3888 } else {
3889 __ xorq(first_reg, second.AsRegister<CpuRegister>());
3890 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003891 }
3892 }
3893}
3894
Calin Juravleb1498f62015-02-16 13:13:29 +00003895void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
3896 // Nothing to do, this should be removed during prepare for register allocator.
3897 UNUSED(instruction);
3898 LOG(FATAL) << "Unreachable";
3899}
3900
3901void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
3902 // Nothing to do, this should be removed during prepare for register allocator.
3903 UNUSED(instruction);
3904 LOG(FATAL) << "Unreachable";
3905}
3906
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003907} // namespace x86_64
3908} // namespace art