blob: ddaa60db5ea2c964850b04a6febad52188d6c1db [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080023#include "intrinsics.h"
24#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010027#include "mirror/object_reference.h"
28#include "thread.h"
29#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/managed_register_x86_64.h"
33
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010034namespace art {
35
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010036namespace x86_64 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038// Some x86_64 instructions require a register to be available as temp.
39static constexpr Register TMP = R11;
40
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010042static constexpr Register kMethodRegisterArgument = RDI;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000044static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
Roland Levillain62a46b22015-06-01 18:24:13 +010049#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010051class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Alexandre Rames2ed20af2015-03-06 13:55:35 +000055 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010057 __ gs()->call(
58 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000059 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 }
61
Alexandre Rames9931f312015-06-19 14:47:01 +010062 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
63
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010065 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
67};
68
Calin Juravled0d48522014-11-04 16:40:20 +000069class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
70 public:
71 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
72
Alexandre Rames2ed20af2015-03-06 13:55:35 +000073 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000074 __ Bind(GetEntryLabel());
75 __ gs()->call(
76 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000077 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000078 }
79
Alexandre Rames9931f312015-06-19 14:47:01 +010080 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
81
Calin Juravled0d48522014-11-04 16:40:20 +000082 private:
83 HDivZeroCheck* const instruction_;
84 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
85};
86
Calin Juravlebacfec32014-11-14 15:54:36 +000087class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000088 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000089 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
90 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000091
Alexandre Rames2ed20af2015-03-06 13:55:35 +000092 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000093 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +000094 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +000095 if (is_div_) {
96 __ negl(cpu_reg_);
97 } else {
98 __ movl(cpu_reg_, Immediate(0));
99 }
100
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000101 } else {
102 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000103 if (is_div_) {
104 __ negq(cpu_reg_);
105 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400106 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000107 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000108 }
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ jmp(GetExitLabel());
110 }
111
Alexandre Rames9931f312015-06-19 14:47:01 +0100112 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
113
Calin Juravled0d48522014-11-04 16:40:20 +0000114 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000116 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 const bool is_div_;
118 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000119};
120
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100123 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
124 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100127 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000128 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000129 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000130 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000131 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
132 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 if (successor_ == nullptr) {
134 __ jmp(GetReturnLabel());
135 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 }
139
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100140 Label* GetReturnLabel() {
141 DCHECK(successor_ == nullptr);
142 return &return_label_;
143 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100145 HBasicBlock* GetSuccessor() const {
146 return successor_;
147 }
148
Alexandre Rames9931f312015-06-19 14:47:01 +0100149 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
150
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 private:
152 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154 Label return_label_;
155
156 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
157};
158
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100159class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100161 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
162 Location index_location,
163 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 : instruction_(instruction),
165 index_location_(index_location),
166 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000168 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100169 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 // We're moving two locations to locations that could overlap, so we need a parallel
171 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000173 codegen->EmitParallelMoves(
174 index_location_,
175 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100176 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100178 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
179 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 __ gs()->call(Address::Absolute(
181 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 }
184
Alexandre Rames9931f312015-06-19 14:47:01 +0100185 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
186
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100188 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 const Location index_location_;
190 const Location length_location_;
191
192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
193};
194
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 LoadClassSlowPathX86_64(HLoadClass* cls,
198 HInstruction* at,
199 uint32_t dex_pc,
200 bool do_clinit)
201 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
202 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000205 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
208 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 __ gs()->call(Address::Absolute((do_clinit_
215 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
Roland Levillain4d027112015-07-01 15:41:14 +0100216 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000217 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000219 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221 if (out.IsValid()) {
222 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
223 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 }
225
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 __ jmp(GetExitLabel());
228 }
229
Alexandre Rames9931f312015-06-19 14:47:01 +0100230 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
231
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 // The class this slow path will load.
234 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 // The instruction where this slow path is happening.
237 // (Might be the load class or an initialization check).
238 HInstruction* const at_;
239
240 // The dex PC of `at_`.
241 const uint32_t dex_pc_;
242
243 // Whether to initialize the class.
244 const bool do_clinit_;
245
246 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247};
248
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000249class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
250 public:
251 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
252
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 LocationSummary* locations = instruction_->GetLocations();
255 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
256
257 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
258 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000259 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000260
261 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263 Immediate(instruction_->GetStringIndex()));
264 __ gs()->call(Address::Absolute(
265 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000267 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000268 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000269 __ jmp(GetExitLabel());
270 }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
273
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 private:
275 HLoadString* const instruction_;
276
277 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
278};
279
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
281 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 TypeCheckSlowPathX86_64(HInstruction* instruction,
283 Location class_to_check,
284 Location object_class,
285 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 class_to_check_(class_to_check),
288 object_class_(object_class),
289 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 DCHECK(instruction_->IsCheckCast()
294 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295
296 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
297 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000298 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 // We're moving two locations to locations that could overlap, so we need a parallel
301 // move resolver.
302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 codegen->EmitParallelMoves(
304 class_to_check_,
305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 if (instruction_->IsInstanceOf()) {
312 __ gs()->call(
313 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
314 } else {
315 DCHECK(instruction_->IsCheckCast());
316 __ gs()->call(
317 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
318 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000319 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320
321 if (instruction_->IsInstanceOf()) {
322 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000325 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326 __ jmp(GetExitLabel());
327 }
328
Alexandre Rames9931f312015-06-19 14:47:01 +0100329 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
330
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 HInstruction* const instruction_;
333 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000335 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336
337 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
338};
339
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700340class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
341 public:
342 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
343 : instruction_(instruction) {}
344
345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
346 __ Bind(GetEntryLabel());
347 SaveLiveRegisters(codegen, instruction_->GetLocations());
348 __ gs()->call(
349 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeoptimize), true));
350 DCHECK(instruction_->IsDeoptimize());
351 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
352 uint32_t dex_pc = deoptimize->GetDexPc();
353 codegen->RecordPcInfo(instruction_, dex_pc, this);
354 }
355
Alexandre Rames9931f312015-06-19 14:47:01 +0100356 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
357
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 private:
359 HInstruction* const instruction_;
360 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
361};
362
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100363#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100364#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100365
Dave Allison20dfc792014-06-16 20:44:29 -0700366inline Condition X86_64Condition(IfCondition cond) {
367 switch (cond) {
368 case kCondEQ: return kEqual;
369 case kCondNE: return kNotEqual;
370 case kCondLT: return kLess;
371 case kCondLE: return kLessEqual;
372 case kCondGT: return kGreater;
373 case kCondGE: return kGreaterEqual;
374 default:
375 LOG(FATAL) << "Unknown if condition";
376 }
377 return kEqual;
378}
379
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800380void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100381 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800382 // All registers are assumed to be correctly set up.
383
384 // TODO: Implement all kinds of calls:
385 // 1) boot -> boot
386 // 2) app -> boot
387 // 3) app -> app
388 //
389 // Currently we implement the app -> app logic, which looks up in the resolve cache.
390
Jeff Hao848f70a2014-01-15 13:49:50 -0800391 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100392 CpuRegister reg = temp.AsRegister<CpuRegister>();
Jeff Hao848f70a2014-01-15 13:49:50 -0800393 // temp = thread->string_init_entrypoint
Jeff Haocad65422015-06-18 21:16:08 -0700394 __ gs()->movq(reg, Address::Absolute(invoke->GetStringInitOffset(), true));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000395 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100396 __ call(Address(reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000397 kX86_64WordSize).SizeValue()));
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100398 } else if (invoke->IsRecursive()) {
399 __ call(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000400 } else {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100401 CpuRegister reg = temp.AsRegister<CpuRegister>();
Nicolas Geoffrayae71a052015-06-09 14:12:28 +0100402 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
403 Register method_reg;
404 if (current_method.IsRegister()) {
405 method_reg = current_method.AsRegister<Register>();
406 } else {
407 DCHECK(invoke->GetLocations()->Intrinsified());
408 DCHECK(!current_method.IsValid());
409 method_reg = reg.AsRegister();
410 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
411 }
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100412 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +0100413 __ movl(reg, Address(CpuRegister(method_reg),
414 ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100415 // temp = temp[index_in_cache]
416 __ movq(reg, Address(
417 reg, CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
418 // (temp + offset_of_quick_compiled_code)()
419 __ call(Address(reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
420 kX86_64WordSize).SizeValue()));
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000421 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800422
423 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800424}
425
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100426void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100427 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100428}
429
430void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100431 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100432}
433
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100434size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
435 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
436 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100437}
438
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100439size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
440 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
441 return kX86_64WordSize;
442}
443
444size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
445 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
446 return kX86_64WordSize;
447}
448
449size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
450 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
451 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100452}
453
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000454static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000455// Use a fake return address register to mimic Quick.
456static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400457CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
458 const X86_64InstructionSetFeatures& isa_features,
459 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000460 : CodeGenerator(graph,
461 kNumberOfCpuRegisters,
462 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000463 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000464 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
465 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000466 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000467 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
468 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000469 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100470 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100471 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000472 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400473 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400474 isa_features_(isa_features),
475 constant_area_start_(0) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000476 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
477}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100479InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
480 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100481 : HGraphVisitor(graph),
482 assembler_(codegen->GetAssembler()),
483 codegen_(codegen) {}
484
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100485Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100486 switch (type) {
487 case Primitive::kPrimLong:
488 case Primitive::kPrimByte:
489 case Primitive::kPrimBoolean:
490 case Primitive::kPrimChar:
491 case Primitive::kPrimShort:
492 case Primitive::kPrimInt:
493 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100495 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100496 }
497
498 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100499 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100500 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100501 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100502 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100503
504 case Primitive::kPrimVoid:
505 LOG(FATAL) << "Unreachable type " << type;
506 }
507
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100508 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509}
510
Nicolas Geoffray98893962015-01-21 12:32:32 +0000511void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100513 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100514
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000515 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100516 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000517
Nicolas Geoffray98893962015-01-21 12:32:32 +0000518 if (is_baseline) {
519 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
520 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
521 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000522 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
523 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
524 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000525 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100526}
527
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100528static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100529 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100530}
David Srbecky9d8606d2015-04-12 09:35:32 +0100531
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100532static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100533 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100534}
535
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100537 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000538 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100539 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700540 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000541 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100542
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000543 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100544 __ testq(CpuRegister(RAX), Address(
545 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100546 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100547 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000548
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000549 if (HasEmptyFrame()) {
550 return;
551 }
552
Nicolas Geoffray98893962015-01-21 12:32:32 +0000553 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000554 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000555 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000556 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100557 __ cfi().AdjustCFAOffset(kX86_64WordSize);
558 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000559 }
560 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100561
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100562 int adjust = GetFrameSize() - GetCoreSpillSize();
563 __ subq(CpuRegister(RSP), Immediate(adjust));
564 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000565 uint32_t xmm_spill_location = GetFpuSpillStart();
566 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100567
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000568 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
569 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100570 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
571 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
572 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000573 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100574 }
575
Mathieu Chartiere401d142015-04-22 13:56:20 -0700576 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100577 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100578}
579
580void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100581 __ cfi().RememberState();
582 if (!HasEmptyFrame()) {
583 uint32_t xmm_spill_location = GetFpuSpillStart();
584 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
585 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
586 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
587 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
588 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
589 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
590 }
591 }
592
593 int adjust = GetFrameSize() - GetCoreSpillSize();
594 __ addq(CpuRegister(RSP), Immediate(adjust));
595 __ cfi().AdjustCFAOffset(-adjust);
596
597 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
598 Register reg = kCoreCalleeSaves[i];
599 if (allocated_registers_.ContainsCoreRegister(reg)) {
600 __ popq(CpuRegister(reg));
601 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
602 __ cfi().Restore(DWARFReg(reg));
603 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000604 }
605 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100606 __ ret();
607 __ cfi().RestoreState();
608 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609}
610
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100611void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
612 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100613}
614
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
616 switch (load->GetType()) {
617 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100619 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100620
621 case Primitive::kPrimInt:
622 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100623 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100625
626 case Primitive::kPrimBoolean:
627 case Primitive::kPrimByte:
628 case Primitive::kPrimChar:
629 case Primitive::kPrimShort:
630 case Primitive::kPrimVoid:
631 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700632 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100633 }
634
635 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700636 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100637}
638
639void CodeGeneratorX86_64::Move(Location destination, Location source) {
640 if (source.Equals(destination)) {
641 return;
642 }
643 if (destination.IsRegister()) {
644 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000645 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000647 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000649 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100651 } else {
652 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100654 Address(CpuRegister(RSP), source.GetStackIndex()));
655 }
656 } else if (destination.IsFpuRegister()) {
657 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000658 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100659 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000660 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100661 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000662 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100663 Address(CpuRegister(RSP), source.GetStackIndex()));
664 } else {
665 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000666 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100667 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100668 }
669 } else if (destination.IsStackSlot()) {
670 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100671 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000672 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100673 } else if (source.IsFpuRegister()) {
674 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000675 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500676 } else if (source.IsConstant()) {
677 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000678 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500679 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100680 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500681 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000682 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
683 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100684 }
685 } else {
686 DCHECK(destination.IsDoubleStackSlot());
687 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000689 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100690 } else if (source.IsFpuRegister()) {
691 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000692 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500693 } else if (source.IsConstant()) {
694 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800695 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500696 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000697 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500698 } else {
699 DCHECK(constant->IsLongConstant());
700 value = constant->AsLongConstant()->GetValue();
701 }
Mark Mendell92e83bf2015-05-07 11:25:03 -0400702 Load64BitValue(CpuRegister(TMP), value);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500703 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704 } else {
705 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000706 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
707 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708 }
709 }
710}
711
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100712void CodeGeneratorX86_64::Move(HInstruction* instruction,
713 Location location,
714 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000715 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100716 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700717 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100718 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000719 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100720 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000721 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000722 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
723 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000724 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 } else if (location.IsStackSlot()) {
727 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
728 } else {
729 DCHECK(location.IsConstant());
730 DCHECK_EQ(location.GetConstant(), const_to_move);
731 }
732 } else if (const_to_move->IsLongConstant()) {
733 int64_t value = const_to_move->AsLongConstant()->GetValue();
734 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400735 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000736 } else if (location.IsDoubleStackSlot()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400737 Load64BitValue(CpuRegister(TMP), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000738 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
739 } else {
740 DCHECK(location.IsConstant());
741 DCHECK_EQ(location.GetConstant(), const_to_move);
742 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743 }
Roland Levillain476df552014-10-09 17:51:36 +0100744 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100745 switch (instruction->GetType()) {
746 case Primitive::kPrimBoolean:
747 case Primitive::kPrimByte:
748 case Primitive::kPrimChar:
749 case Primitive::kPrimShort:
750 case Primitive::kPrimInt:
751 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100753 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
754 break;
755
756 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000758 Move(location,
759 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100760 break;
761
762 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100764 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000765 } else if (instruction->IsTemporary()) {
766 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
767 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100768 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100769 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100770 switch (instruction->GetType()) {
771 case Primitive::kPrimBoolean:
772 case Primitive::kPrimByte:
773 case Primitive::kPrimChar:
774 case Primitive::kPrimShort:
775 case Primitive::kPrimInt:
776 case Primitive::kPrimNot:
777 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100778 case Primitive::kPrimFloat:
779 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000780 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100781 break;
782
783 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100784 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100785 }
786 }
787}
788
David Brazdilfc6a86a2015-06-26 10:33:45 +0000789void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100790 DCHECK(!successor->IsExitBlock());
791
792 HBasicBlock* block = got->GetBlock();
793 HInstruction* previous = got->GetPrevious();
794
795 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000796 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100797 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
798 return;
799 }
800
801 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
802 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
803 }
804 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100805 __ jmp(codegen_->GetLabelOf(successor));
806 }
807}
808
David Brazdilfc6a86a2015-06-26 10:33:45 +0000809void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
810 got->SetLocations(nullptr);
811}
812
813void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
814 HandleGoto(got, got->GetSuccessor());
815}
816
817void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
818 try_boundary->SetLocations(nullptr);
819}
820
821void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
822 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
823 if (!successor->IsExitBlock()) {
824 HandleGoto(try_boundary, successor);
825 }
826}
827
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100828void LocationsBuilderX86_64::VisitExit(HExit* exit) {
829 exit->SetLocations(nullptr);
830}
831
832void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700833 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100834}
835
Mark Mendellc4701932015-04-10 13:18:51 -0400836void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
837 Label* true_label,
838 Label* false_label) {
839 bool gt_bias = cond->IsGtBias();
840 IfCondition if_cond = cond->GetCondition();
841 Condition ccode = X86_64Condition(if_cond);
842 switch (if_cond) {
843 case kCondEQ:
844 if (!gt_bias) {
845 __ j(kParityEven, false_label);
846 }
847 break;
848 case kCondNE:
849 if (!gt_bias) {
850 __ j(kParityEven, true_label);
851 }
852 break;
853 case kCondLT:
854 if (gt_bias) {
855 __ j(kParityEven, false_label);
856 }
857 ccode = kBelow;
858 break;
859 case kCondLE:
860 if (gt_bias) {
861 __ j(kParityEven, false_label);
862 }
863 ccode = kBelowEqual;
864 break;
865 case kCondGT:
866 if (gt_bias) {
867 __ j(kParityEven, true_label);
868 }
869 ccode = kAbove;
870 break;
871 case kCondGE:
872 if (gt_bias) {
873 __ j(kParityEven, true_label);
874 }
875 ccode = kAboveEqual;
876 break;
877 }
878 __ j(ccode, true_label);
879}
880
881void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
882 HCondition* condition,
883 Label* true_target,
884 Label* false_target,
885 Label* always_true_target) {
886 LocationSummary* locations = condition->GetLocations();
887 Location left = locations->InAt(0);
888 Location right = locations->InAt(1);
889
890 // We don't want true_target as a nullptr.
891 if (true_target == nullptr) {
892 true_target = always_true_target;
893 }
894 bool falls_through = (false_target == nullptr);
895
896 // FP compares don't like null false_targets.
897 if (false_target == nullptr) {
898 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
899 }
900
901 Primitive::Type type = condition->InputAt(0)->GetType();
902 switch (type) {
903 case Primitive::kPrimLong: {
904 CpuRegister left_reg = left.AsRegister<CpuRegister>();
905 if (right.IsConstant()) {
906 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
907 if (IsInt<32>(value)) {
908 if (value == 0) {
909 __ testq(left_reg, left_reg);
910 } else {
911 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
912 }
913 } else {
914 // Value won't fit in an 32-bit integer.
915 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
916 }
917 } else if (right.IsDoubleStackSlot()) {
918 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
919 } else {
920 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
921 }
922 __ j(X86_64Condition(condition->GetCondition()), true_target);
923 break;
924 }
925 case Primitive::kPrimFloat: {
926 if (right.IsFpuRegister()) {
927 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
928 } else if (right.IsConstant()) {
929 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
930 codegen_->LiteralFloatAddress(
931 right.GetConstant()->AsFloatConstant()->GetValue()));
932 } else {
933 DCHECK(right.IsStackSlot());
934 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
935 Address(CpuRegister(RSP), right.GetStackIndex()));
936 }
937 GenerateFPJumps(condition, true_target, false_target);
938 break;
939 }
940 case Primitive::kPrimDouble: {
941 if (right.IsFpuRegister()) {
942 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
943 } else if (right.IsConstant()) {
944 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
945 codegen_->LiteralDoubleAddress(
946 right.GetConstant()->AsDoubleConstant()->GetValue()));
947 } else {
948 DCHECK(right.IsDoubleStackSlot());
949 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
950 Address(CpuRegister(RSP), right.GetStackIndex()));
951 }
952 GenerateFPJumps(condition, true_target, false_target);
953 break;
954 }
955 default:
956 LOG(FATAL) << "Unexpected condition type " << type;
957 }
958
959 if (!falls_through) {
960 __ jmp(false_target);
961 }
962}
963
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700964void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
965 Label* true_target,
966 Label* false_target,
967 Label* always_true_target) {
968 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100969 if (cond->IsIntConstant()) {
970 // Constant condition, statically compared against 1.
971 int32_t cond_value = cond->AsIntConstant()->GetValue();
972 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700973 if (always_true_target != nullptr) {
974 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100975 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100976 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100977 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100978 DCHECK_EQ(cond_value, 0);
979 }
980 } else {
981 bool materialized =
982 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
983 // Moves do not affect the eflags register, so if the condition is
984 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -0400985 // again. We can't use the eflags on FP conditions if they are
986 // materialized due to the complex branching.
987 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100988 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -0400989 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
990 && !Primitive::IsFloatingPointType(type);
991
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100992 if (materialized) {
993 if (!eflags_set) {
994 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700995 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100996 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +0000997 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100998 } else {
999 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1000 Immediate(0));
1001 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001002 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001003 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001004 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001005 }
1006 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001007 // Is this a long or FP comparison that has been folded into the HCondition?
1008 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1009 // Generate the comparison directly
1010 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1011 true_target, false_target, always_true_target);
1012 return;
1013 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001014 Location lhs = cond->GetLocations()->InAt(0);
1015 Location rhs = cond->GetLocations()->InAt(1);
1016 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001017 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001018 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001019 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001020 if (constant == 0) {
1021 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1022 } else {
1023 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1024 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001025 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001027 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1028 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001029 __ j(X86_64Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001030 }
Dave Allison20dfc792014-06-16 20:44:29 -07001031 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001032 if (false_target != nullptr) {
1033 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001034 }
1035}
1036
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001037void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1038 LocationSummary* locations =
1039 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1040 HInstruction* cond = if_instr->InputAt(0);
1041 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1042 locations->SetInAt(0, Location::Any());
1043 }
1044}
1045
1046void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1047 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1048 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1049 Label* always_true_target = true_target;
1050 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1051 if_instr->IfTrueSuccessor())) {
1052 always_true_target = nullptr;
1053 }
1054 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1055 if_instr->IfFalseSuccessor())) {
1056 false_target = nullptr;
1057 }
1058 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1059}
1060
1061void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1062 LocationSummary* locations = new (GetGraph()->GetArena())
1063 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1064 HInstruction* cond = deoptimize->InputAt(0);
1065 DCHECK(cond->IsCondition());
1066 if (cond->AsCondition()->NeedsMaterialization()) {
1067 locations->SetInAt(0, Location::Any());
1068 }
1069}
1070
1071void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1072 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1073 DeoptimizationSlowPathX86_64(deoptimize);
1074 codegen_->AddSlowPath(slow_path);
1075 Label* slow_path_entry = slow_path->GetEntryLabel();
1076 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1077}
1078
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001079void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1080 local->SetLocations(nullptr);
1081}
1082
1083void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1084 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1085}
1086
1087void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1088 local->SetLocations(nullptr);
1089}
1090
1091void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1092 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001093 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094}
1095
1096void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001097 LocationSummary* locations =
1098 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001099 switch (store->InputAt(1)->GetType()) {
1100 case Primitive::kPrimBoolean:
1101 case Primitive::kPrimByte:
1102 case Primitive::kPrimChar:
1103 case Primitive::kPrimShort:
1104 case Primitive::kPrimInt:
1105 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001107 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1108 break;
1109
1110 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001111 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001112 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1113 break;
1114
1115 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118}
1119
1120void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001121 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001122}
1123
Roland Levillain0d37cd02015-05-27 16:39:19 +01001124void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001125 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001126 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001127 // Handle the long/FP comparisons made in instruction simplification.
1128 switch (cond->InputAt(0)->GetType()) {
1129 case Primitive::kPrimLong:
1130 locations->SetInAt(0, Location::RequiresRegister());
1131 locations->SetInAt(1, Location::Any());
1132 break;
1133 case Primitive::kPrimFloat:
1134 case Primitive::kPrimDouble:
1135 locations->SetInAt(0, Location::RequiresFpuRegister());
1136 locations->SetInAt(1, Location::Any());
1137 break;
1138 default:
1139 locations->SetInAt(0, Location::RequiresRegister());
1140 locations->SetInAt(1, Location::Any());
1141 break;
1142 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001143 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001144 locations->SetOut(Location::RequiresRegister());
1145 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001146}
1147
Roland Levillain0d37cd02015-05-27 16:39:19 +01001148void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001149 if (!cond->NeedsMaterialization()) {
1150 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001151 }
Mark Mendellc4701932015-04-10 13:18:51 -04001152
1153 LocationSummary* locations = cond->GetLocations();
1154 Location lhs = locations->InAt(0);
1155 Location rhs = locations->InAt(1);
1156 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1157 Label true_label, false_label;
1158
1159 switch (cond->InputAt(0)->GetType()) {
1160 default:
1161 // Integer case.
1162
1163 // Clear output register: setcc only sets the low byte.
1164 __ xorl(reg, reg);
1165
1166 if (rhs.IsRegister()) {
1167 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1168 } else if (rhs.IsConstant()) {
1169 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1170 if (constant == 0) {
1171 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1172 } else {
1173 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1174 }
1175 } else {
1176 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1177 }
1178 __ setcc(X86_64Condition(cond->GetCondition()), reg);
1179 return;
1180 case Primitive::kPrimLong:
1181 // Clear output register: setcc only sets the low byte.
1182 __ xorl(reg, reg);
1183
1184 if (rhs.IsRegister()) {
1185 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1186 } else if (rhs.IsConstant()) {
1187 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1188 if (IsInt<32>(value)) {
1189 if (value == 0) {
1190 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1191 } else {
1192 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1193 }
1194 } else {
1195 // Value won't fit in an int.
1196 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1197 }
1198 } else {
1199 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1200 }
1201 __ setcc(X86_64Condition(cond->GetCondition()), reg);
1202 return;
1203 case Primitive::kPrimFloat: {
1204 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1205 if (rhs.IsConstant()) {
1206 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1207 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1208 } else if (rhs.IsStackSlot()) {
1209 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1210 } else {
1211 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1212 }
1213 GenerateFPJumps(cond, &true_label, &false_label);
1214 break;
1215 }
1216 case Primitive::kPrimDouble: {
1217 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1218 if (rhs.IsConstant()) {
1219 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1220 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1221 } else if (rhs.IsDoubleStackSlot()) {
1222 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1223 } else {
1224 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1225 }
1226 GenerateFPJumps(cond, &true_label, &false_label);
1227 break;
1228 }
1229 }
1230
1231 // Convert the jumps into the result.
1232 Label done_label;
1233
1234 // false case: result = 0;
1235 __ Bind(&false_label);
1236 __ xorl(reg, reg);
1237 __ jmp(&done_label);
1238
1239 // True case: result = 1
1240 __ Bind(&true_label);
1241 __ movl(reg, Immediate(1));
1242 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001243}
1244
1245void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1246 VisitCondition(comp);
1247}
1248
1249void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1250 VisitCondition(comp);
1251}
1252
1253void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1254 VisitCondition(comp);
1255}
1256
1257void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1258 VisitCondition(comp);
1259}
1260
1261void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1262 VisitCondition(comp);
1263}
1264
1265void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1266 VisitCondition(comp);
1267}
1268
1269void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1270 VisitCondition(comp);
1271}
1272
1273void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1274 VisitCondition(comp);
1275}
1276
1277void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1278 VisitCondition(comp);
1279}
1280
1281void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1282 VisitCondition(comp);
1283}
1284
1285void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1286 VisitCondition(comp);
1287}
1288
1289void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1290 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001291}
1292
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001293void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001294 LocationSummary* locations =
1295 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001296 switch (compare->InputAt(0)->GetType()) {
1297 case Primitive::kPrimLong: {
1298 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001299 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001300 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1301 break;
1302 }
1303 case Primitive::kPrimFloat:
1304 case Primitive::kPrimDouble: {
1305 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001306 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001307 locations->SetOut(Location::RequiresRegister());
1308 break;
1309 }
1310 default:
1311 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1312 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001313}
1314
1315void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001316 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001317 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001318 Location left = locations->InAt(0);
1319 Location right = locations->InAt(1);
1320
1321 Label less, greater, done;
1322 Primitive::Type type = compare->InputAt(0)->GetType();
1323 switch (type) {
1324 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001325 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1326 if (right.IsConstant()) {
1327 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001328 if (IsInt<32>(value)) {
1329 if (value == 0) {
1330 __ testq(left_reg, left_reg);
1331 } else {
1332 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1333 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001334 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001335 // Value won't fit in an int.
1336 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001337 }
Mark Mendell40741f32015-04-20 22:10:34 -04001338 } else if (right.IsDoubleStackSlot()) {
1339 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001340 } else {
1341 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1342 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001343 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001344 }
1345 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001346 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1347 if (right.IsConstant()) {
1348 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1349 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1350 } else if (right.IsStackSlot()) {
1351 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1352 } else {
1353 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1354 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001355 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1356 break;
1357 }
1358 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001359 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1360 if (right.IsConstant()) {
1361 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1362 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1363 } else if (right.IsDoubleStackSlot()) {
1364 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1365 } else {
1366 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1367 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001368 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1369 break;
1370 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001371 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001372 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001373 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001374 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001375 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001376 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001377
Calin Juravle91debbc2014-11-26 19:01:09 +00001378 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001379 __ movl(out, Immediate(1));
1380 __ jmp(&done);
1381
1382 __ Bind(&less);
1383 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001384
1385 __ Bind(&done);
1386}
1387
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001388void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001389 LocationSummary* locations =
1390 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001391 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001392}
1393
1394void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001395 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001396 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001397}
1398
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001399void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1400 LocationSummary* locations =
1401 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1402 locations->SetOut(Location::ConstantLocation(constant));
1403}
1404
1405void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1406 // Will be generated at use site.
1407 UNUSED(constant);
1408}
1409
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001410void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001411 LocationSummary* locations =
1412 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001413 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001414}
1415
1416void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001417 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001418 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001419}
1420
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001421void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1422 LocationSummary* locations =
1423 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1424 locations->SetOut(Location::ConstantLocation(constant));
1425}
1426
1427void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1428 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001429 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001430}
1431
1432void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1433 LocationSummary* locations =
1434 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1435 locations->SetOut(Location::ConstantLocation(constant));
1436}
1437
1438void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1439 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001440 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001441}
1442
Calin Juravle27df7582015-04-17 19:12:31 +01001443void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1444 memory_barrier->SetLocations(nullptr);
1445}
1446
1447void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1448 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1449}
1450
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001451void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1452 ret->SetLocations(nullptr);
1453}
1454
1455void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001456 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001457 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001458}
1459
1460void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001461 LocationSummary* locations =
1462 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001463 switch (ret->InputAt(0)->GetType()) {
1464 case Primitive::kPrimBoolean:
1465 case Primitive::kPrimByte:
1466 case Primitive::kPrimChar:
1467 case Primitive::kPrimShort:
1468 case Primitive::kPrimInt:
1469 case Primitive::kPrimNot:
1470 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001471 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001472 break;
1473
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001474 case Primitive::kPrimFloat:
1475 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001476 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001477 break;
1478
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001479 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001480 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001481 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001482}
1483
1484void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1485 if (kIsDebugBuild) {
1486 switch (ret->InputAt(0)->GetType()) {
1487 case Primitive::kPrimBoolean:
1488 case Primitive::kPrimByte:
1489 case Primitive::kPrimChar:
1490 case Primitive::kPrimShort:
1491 case Primitive::kPrimInt:
1492 case Primitive::kPrimNot:
1493 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001495 break;
1496
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001497 case Primitive::kPrimFloat:
1498 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001499 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001500 XMM0);
1501 break;
1502
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001503 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001504 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001505 }
1506 }
1507 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001508}
1509
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001510Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1511 switch (type) {
1512 case Primitive::kPrimBoolean:
1513 case Primitive::kPrimByte:
1514 case Primitive::kPrimChar:
1515 case Primitive::kPrimShort:
1516 case Primitive::kPrimInt:
1517 case Primitive::kPrimNot:
1518 case Primitive::kPrimLong:
1519 return Location::RegisterLocation(RAX);
1520
1521 case Primitive::kPrimVoid:
1522 return Location::NoLocation();
1523
1524 case Primitive::kPrimDouble:
1525 case Primitive::kPrimFloat:
1526 return Location::FpuRegisterLocation(XMM0);
1527 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001528
1529 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001530}
1531
1532Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1533 return Location::RegisterLocation(kMethodRegisterArgument);
1534}
1535
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001536Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001537 switch (type) {
1538 case Primitive::kPrimBoolean:
1539 case Primitive::kPrimByte:
1540 case Primitive::kPrimChar:
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
1543 case Primitive::kPrimNot: {
1544 uint32_t index = gp_index_++;
1545 stack_index_++;
1546 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001547 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001548 } else {
1549 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1550 }
1551 }
1552
1553 case Primitive::kPrimLong: {
1554 uint32_t index = gp_index_;
1555 stack_index_ += 2;
1556 if (index < calling_convention.GetNumberOfRegisters()) {
1557 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001558 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001559 } else {
1560 gp_index_ += 2;
1561 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1562 }
1563 }
1564
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001565 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001566 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 stack_index_++;
1568 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001569 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001570 } else {
1571 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1572 }
1573 }
1574
1575 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001576 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001577 stack_index_ += 2;
1578 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001579 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001580 } else {
1581 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1582 }
1583 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584
1585 case Primitive::kPrimVoid:
1586 LOG(FATAL) << "Unexpected parameter type " << type;
1587 break;
1588 }
1589 return Location();
1590}
1591
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001592void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001593 // When we do not run baseline, explicit clinit checks triggered by static
1594 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1595 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001596
Mark Mendellfb8d2792015-03-31 22:16:59 -04001597 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001598 if (intrinsic.TryDispatch(invoke)) {
1599 return;
1600 }
1601
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001602 HandleInvoke(invoke);
1603}
1604
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001605static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1606 if (invoke->GetLocations()->Intrinsified()) {
1607 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1608 intrinsic.Dispatch(invoke);
1609 return true;
1610 }
1611 return false;
1612}
1613
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001614void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001615 // When we do not run baseline, explicit clinit checks triggered by static
1616 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1617 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001618
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001619 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1620 return;
1621 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001622
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001623 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001624 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001625 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001626 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001627}
1628
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001629void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001630 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001631 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632}
1633
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001634void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001635 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001636 if (intrinsic.TryDispatch(invoke)) {
1637 return;
1638 }
1639
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001640 HandleInvoke(invoke);
1641}
1642
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001643void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001644 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1645 return;
1646 }
1647
Roland Levillain271ab9c2014-11-27 15:23:57 +00001648 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001649 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1650 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001651 LocationSummary* locations = invoke->GetLocations();
1652 Location receiver = locations->InAt(0);
1653 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1654 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001655 DCHECK(receiver.IsRegister());
1656 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001657 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001658 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001659 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001660 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001661 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001662 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001663 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001664
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001665 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001666 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667}
1668
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001669void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1670 HandleInvoke(invoke);
1671 // Add the hidden argument.
1672 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1673}
1674
1675void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1676 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001678 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1679 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001680 LocationSummary* locations = invoke->GetLocations();
1681 Location receiver = locations->InAt(0);
1682 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1683
1684 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001685 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1686 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001687
1688 // temp = object->GetClass();
1689 if (receiver.IsStackSlot()) {
1690 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1691 __ movl(temp, Address(temp, class_offset));
1692 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001693 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001694 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001695 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001696 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001697 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001698 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001699 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001700 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001701 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001702
1703 DCHECK(!codegen_->IsLeafMethod());
1704 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1705}
1706
Roland Levillain88cb1752014-10-20 16:36:47 +01001707void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1708 LocationSummary* locations =
1709 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1710 switch (neg->GetResultType()) {
1711 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001712 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001713 locations->SetInAt(0, Location::RequiresRegister());
1714 locations->SetOut(Location::SameAsFirstInput());
1715 break;
1716
Roland Levillain88cb1752014-10-20 16:36:47 +01001717 case Primitive::kPrimFloat:
1718 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001719 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001720 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001721 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001722 break;
1723
1724 default:
1725 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1726 }
1727}
1728
1729void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1730 LocationSummary* locations = neg->GetLocations();
1731 Location out = locations->Out();
1732 Location in = locations->InAt(0);
1733 switch (neg->GetResultType()) {
1734 case Primitive::kPrimInt:
1735 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001736 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001737 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001738 break;
1739
1740 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001741 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001742 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001743 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001744 break;
1745
Roland Levillain5368c212014-11-27 15:03:41 +00001746 case Primitive::kPrimFloat: {
1747 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001748 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001749 // Implement float negation with an exclusive or with value
1750 // 0x80000000 (mask for bit 31, representing the sign of a
1751 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001752 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001753 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001754 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001755 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001756
Roland Levillain5368c212014-11-27 15:03:41 +00001757 case Primitive::kPrimDouble: {
1758 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001759 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001760 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001761 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001762 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001763 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001765 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001766 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001767
1768 default:
1769 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1770 }
1771}
1772
Roland Levillaindff1f282014-11-05 14:15:05 +00001773void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1774 LocationSummary* locations =
1775 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1776 Primitive::Type result_type = conversion->GetResultType();
1777 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001778 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001779
David Brazdilb2bd1c52015-03-25 11:17:37 +00001780 // The Java language does not allow treating boolean as an integral type but
1781 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001782
Roland Levillaindff1f282014-11-05 14:15:05 +00001783 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001784 case Primitive::kPrimByte:
1785 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001786 case Primitive::kPrimBoolean:
1787 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001788 case Primitive::kPrimShort:
1789 case Primitive::kPrimInt:
1790 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001791 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001792 locations->SetInAt(0, Location::Any());
1793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1794 break;
1795
1796 default:
1797 LOG(FATAL) << "Unexpected type conversion from " << input_type
1798 << " to " << result_type;
1799 }
1800 break;
1801
Roland Levillain01a8d712014-11-14 16:27:39 +00001802 case Primitive::kPrimShort:
1803 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001804 case Primitive::kPrimBoolean:
1805 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001806 case Primitive::kPrimByte:
1807 case Primitive::kPrimInt:
1808 case Primitive::kPrimChar:
1809 // Processing a Dex `int-to-short' instruction.
1810 locations->SetInAt(0, Location::Any());
1811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1812 break;
1813
1814 default:
1815 LOG(FATAL) << "Unexpected type conversion from " << input_type
1816 << " to " << result_type;
1817 }
1818 break;
1819
Roland Levillain946e1432014-11-11 17:35:19 +00001820 case Primitive::kPrimInt:
1821 switch (input_type) {
1822 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001823 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001824 locations->SetInAt(0, Location::Any());
1825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1826 break;
1827
1828 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001829 // Processing a Dex `float-to-int' instruction.
1830 locations->SetInAt(0, Location::RequiresFpuRegister());
1831 locations->SetOut(Location::RequiresRegister());
1832 locations->AddTemp(Location::RequiresFpuRegister());
1833 break;
1834
Roland Levillain946e1432014-11-11 17:35:19 +00001835 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001836 // Processing a Dex `double-to-int' instruction.
1837 locations->SetInAt(0, Location::RequiresFpuRegister());
1838 locations->SetOut(Location::RequiresRegister());
1839 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001840 break;
1841
1842 default:
1843 LOG(FATAL) << "Unexpected type conversion from " << input_type
1844 << " to " << result_type;
1845 }
1846 break;
1847
Roland Levillaindff1f282014-11-05 14:15:05 +00001848 case Primitive::kPrimLong:
1849 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001850 case Primitive::kPrimBoolean:
1851 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001852 case Primitive::kPrimByte:
1853 case Primitive::kPrimShort:
1854 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001855 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001856 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001857 // TODO: We would benefit from a (to-be-implemented)
1858 // Location::RegisterOrStackSlot requirement for this input.
1859 locations->SetInAt(0, Location::RequiresRegister());
1860 locations->SetOut(Location::RequiresRegister());
1861 break;
1862
1863 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001864 // Processing a Dex `float-to-long' instruction.
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
1866 locations->SetOut(Location::RequiresRegister());
1867 locations->AddTemp(Location::RequiresFpuRegister());
1868 break;
1869
Roland Levillaindff1f282014-11-05 14:15:05 +00001870 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001871 // Processing a Dex `double-to-long' instruction.
1872 locations->SetInAt(0, Location::RequiresFpuRegister());
1873 locations->SetOut(Location::RequiresRegister());
1874 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001875 break;
1876
1877 default:
1878 LOG(FATAL) << "Unexpected type conversion from " << input_type
1879 << " to " << result_type;
1880 }
1881 break;
1882
Roland Levillain981e4542014-11-14 11:47:14 +00001883 case Primitive::kPrimChar:
1884 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001885 case Primitive::kPrimBoolean:
1886 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001887 case Primitive::kPrimByte:
1888 case Primitive::kPrimShort:
1889 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001890 // Processing a Dex `int-to-char' instruction.
1891 locations->SetInAt(0, Location::Any());
1892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1893 break;
1894
1895 default:
1896 LOG(FATAL) << "Unexpected type conversion from " << input_type
1897 << " to " << result_type;
1898 }
1899 break;
1900
Roland Levillaindff1f282014-11-05 14:15:05 +00001901 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001902 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001903 case Primitive::kPrimBoolean:
1904 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001905 case Primitive::kPrimByte:
1906 case Primitive::kPrimShort:
1907 case Primitive::kPrimInt:
1908 case Primitive::kPrimChar:
1909 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001910 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001911 locations->SetOut(Location::RequiresFpuRegister());
1912 break;
1913
1914 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001915 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001916 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001917 locations->SetOut(Location::RequiresFpuRegister());
1918 break;
1919
Roland Levillaincff13742014-11-17 14:32:17 +00001920 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001921 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001922 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001923 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001924 break;
1925
1926 default:
1927 LOG(FATAL) << "Unexpected type conversion from " << input_type
1928 << " to " << result_type;
1929 };
1930 break;
1931
Roland Levillaindff1f282014-11-05 14:15:05 +00001932 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001933 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001934 case Primitive::kPrimBoolean:
1935 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001936 case Primitive::kPrimByte:
1937 case Primitive::kPrimShort:
1938 case Primitive::kPrimInt:
1939 case Primitive::kPrimChar:
1940 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001941 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001942 locations->SetOut(Location::RequiresFpuRegister());
1943 break;
1944
1945 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001946 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001947 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001948 locations->SetOut(Location::RequiresFpuRegister());
1949 break;
1950
Roland Levillaincff13742014-11-17 14:32:17 +00001951 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001952 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001953 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001954 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001955 break;
1956
1957 default:
1958 LOG(FATAL) << "Unexpected type conversion from " << input_type
1959 << " to " << result_type;
1960 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001961 break;
1962
1963 default:
1964 LOG(FATAL) << "Unexpected type conversion from " << input_type
1965 << " to " << result_type;
1966 }
1967}
1968
1969void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1970 LocationSummary* locations = conversion->GetLocations();
1971 Location out = locations->Out();
1972 Location in = locations->InAt(0);
1973 Primitive::Type result_type = conversion->GetResultType();
1974 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001975 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001976 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001977 case Primitive::kPrimByte:
1978 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001979 case Primitive::kPrimBoolean:
1980 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001981 case Primitive::kPrimShort:
1982 case Primitive::kPrimInt:
1983 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001984 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001985 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001986 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001987 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001988 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001989 Address(CpuRegister(RSP), in.GetStackIndex()));
1990 } else {
1991 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001992 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001993 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1994 }
1995 break;
1996
1997 default:
1998 LOG(FATAL) << "Unexpected type conversion from " << input_type
1999 << " to " << result_type;
2000 }
2001 break;
2002
Roland Levillain01a8d712014-11-14 16:27:39 +00002003 case Primitive::kPrimShort:
2004 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002005 case Primitive::kPrimBoolean:
2006 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002007 case Primitive::kPrimByte:
2008 case Primitive::kPrimInt:
2009 case Primitive::kPrimChar:
2010 // Processing a Dex `int-to-short' instruction.
2011 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002013 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002014 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002015 Address(CpuRegister(RSP), in.GetStackIndex()));
2016 } else {
2017 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002018 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002019 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2020 }
2021 break;
2022
2023 default:
2024 LOG(FATAL) << "Unexpected type conversion from " << input_type
2025 << " to " << result_type;
2026 }
2027 break;
2028
Roland Levillain946e1432014-11-11 17:35:19 +00002029 case Primitive::kPrimInt:
2030 switch (input_type) {
2031 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002032 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002033 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002034 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002035 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002036 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002037 Address(CpuRegister(RSP), in.GetStackIndex()));
2038 } else {
2039 DCHECK(in.IsConstant());
2040 DCHECK(in.GetConstant()->IsLongConstant());
2041 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002042 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002043 }
2044 break;
2045
Roland Levillain3f8f9362014-12-02 17:45:01 +00002046 case Primitive::kPrimFloat: {
2047 // Processing a Dex `float-to-int' instruction.
2048 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2049 CpuRegister output = out.AsRegister<CpuRegister>();
2050 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2051 Label done, nan;
2052
2053 __ movl(output, Immediate(kPrimIntMax));
2054 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00002055 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002056 // if input >= temp goto done
2057 __ comiss(input, temp);
2058 __ j(kAboveEqual, &done);
2059 // if input == NaN goto nan
2060 __ j(kUnordered, &nan);
2061 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002062 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002063 __ jmp(&done);
2064 __ Bind(&nan);
2065 // output = 0
2066 __ xorl(output, output);
2067 __ Bind(&done);
2068 break;
2069 }
2070
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002071 case Primitive::kPrimDouble: {
2072 // Processing a Dex `double-to-int' instruction.
2073 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2074 CpuRegister output = out.AsRegister<CpuRegister>();
2075 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2076 Label done, nan;
2077
2078 __ movl(output, Immediate(kPrimIntMax));
2079 // temp = int-to-double(output)
2080 __ cvtsi2sd(temp, output);
2081 // if input >= temp goto done
2082 __ comisd(input, temp);
2083 __ j(kAboveEqual, &done);
2084 // if input == NaN goto nan
2085 __ j(kUnordered, &nan);
2086 // output = double-to-int-truncate(input)
2087 __ cvttsd2si(output, input);
2088 __ jmp(&done);
2089 __ Bind(&nan);
2090 // output = 0
2091 __ xorl(output, output);
2092 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002093 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002094 }
Roland Levillain946e1432014-11-11 17:35:19 +00002095
2096 default:
2097 LOG(FATAL) << "Unexpected type conversion from " << input_type
2098 << " to " << result_type;
2099 }
2100 break;
2101
Roland Levillaindff1f282014-11-05 14:15:05 +00002102 case Primitive::kPrimLong:
2103 switch (input_type) {
2104 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002105 case Primitive::kPrimBoolean:
2106 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002107 case Primitive::kPrimByte:
2108 case Primitive::kPrimShort:
2109 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002110 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002111 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002112 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002114 break;
2115
Roland Levillain624279f2014-12-04 11:54:28 +00002116 case Primitive::kPrimFloat: {
2117 // Processing a Dex `float-to-long' instruction.
2118 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2119 CpuRegister output = out.AsRegister<CpuRegister>();
2120 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2121 Label done, nan;
2122
Mark Mendell92e83bf2015-05-07 11:25:03 -04002123 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002124 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00002125 __ cvtsi2ss(temp, output, true);
2126 // if input >= temp goto done
2127 __ comiss(input, temp);
2128 __ j(kAboveEqual, &done);
2129 // if input == NaN goto nan
2130 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002131 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002132 __ cvttss2si(output, input, true);
2133 __ jmp(&done);
2134 __ Bind(&nan);
2135 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002136 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002137 __ Bind(&done);
2138 break;
2139 }
2140
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002141 case Primitive::kPrimDouble: {
2142 // Processing a Dex `double-to-long' instruction.
2143 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2144 CpuRegister output = out.AsRegister<CpuRegister>();
2145 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2146 Label done, nan;
2147
Mark Mendell92e83bf2015-05-07 11:25:03 -04002148 codegen_->Load64BitValue(output, kPrimLongMax);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002149 // temp = long-to-double(output)
2150 __ cvtsi2sd(temp, output, true);
2151 // if input >= temp goto done
2152 __ comisd(input, temp);
2153 __ j(kAboveEqual, &done);
2154 // if input == NaN goto nan
2155 __ j(kUnordered, &nan);
2156 // output = double-to-long-truncate(input)
2157 __ cvttsd2si(output, input, true);
2158 __ jmp(&done);
2159 __ Bind(&nan);
2160 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002161 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002162 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002163 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002164 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002165
2166 default:
2167 LOG(FATAL) << "Unexpected type conversion from " << input_type
2168 << " to " << result_type;
2169 }
2170 break;
2171
Roland Levillain981e4542014-11-14 11:47:14 +00002172 case Primitive::kPrimChar:
2173 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002174 case Primitive::kPrimBoolean:
2175 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002176 case Primitive::kPrimByte:
2177 case Primitive::kPrimShort:
2178 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002179 // Processing a Dex `int-to-char' instruction.
2180 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002181 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002182 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002183 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002184 Address(CpuRegister(RSP), in.GetStackIndex()));
2185 } else {
2186 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002187 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002188 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2189 }
2190 break;
2191
2192 default:
2193 LOG(FATAL) << "Unexpected type conversion from " << input_type
2194 << " to " << result_type;
2195 }
2196 break;
2197
Roland Levillaindff1f282014-11-05 14:15:05 +00002198 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002199 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002200 case Primitive::kPrimBoolean:
2201 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002202 case Primitive::kPrimByte:
2203 case Primitive::kPrimShort:
2204 case Primitive::kPrimInt:
2205 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002206 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002207 if (in.IsRegister()) {
2208 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2209 } else if (in.IsConstant()) {
2210 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2211 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2212 if (v == 0) {
2213 __ xorps(dest, dest);
2214 } else {
2215 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2216 }
2217 } else {
2218 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2219 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2220 }
Roland Levillaincff13742014-11-17 14:32:17 +00002221 break;
2222
2223 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002224 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002225 if (in.IsRegister()) {
2226 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2227 } else if (in.IsConstant()) {
2228 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2229 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2230 if (v == 0) {
2231 __ xorps(dest, dest);
2232 } else {
2233 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2234 }
2235 } else {
2236 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2237 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2238 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002239 break;
2240
Roland Levillaincff13742014-11-17 14:32:17 +00002241 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002242 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002243 if (in.IsFpuRegister()) {
2244 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2245 } else if (in.IsConstant()) {
2246 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2247 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2248 if (bit_cast<int64_t, double>(v) == 0) {
2249 __ xorps(dest, dest);
2250 } else {
2251 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2252 }
2253 } else {
2254 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2255 Address(CpuRegister(RSP), in.GetStackIndex()));
2256 }
Roland Levillaincff13742014-11-17 14:32:17 +00002257 break;
2258
2259 default:
2260 LOG(FATAL) << "Unexpected type conversion from " << input_type
2261 << " to " << result_type;
2262 };
2263 break;
2264
Roland Levillaindff1f282014-11-05 14:15:05 +00002265 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002266 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002267 case Primitive::kPrimBoolean:
2268 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002269 case Primitive::kPrimByte:
2270 case Primitive::kPrimShort:
2271 case Primitive::kPrimInt:
2272 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002273 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002274 if (in.IsRegister()) {
2275 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2276 } else if (in.IsConstant()) {
2277 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2278 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2279 if (v == 0) {
2280 __ xorpd(dest, dest);
2281 } else {
2282 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2283 }
2284 } else {
2285 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2286 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2287 }
Roland Levillaincff13742014-11-17 14:32:17 +00002288 break;
2289
2290 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002291 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002292 if (in.IsRegister()) {
2293 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2294 } else if (in.IsConstant()) {
2295 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2296 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2297 if (v == 0) {
2298 __ xorpd(dest, dest);
2299 } else {
2300 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2301 }
2302 } else {
2303 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2304 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2305 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002306 break;
2307
Roland Levillaincff13742014-11-17 14:32:17 +00002308 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002309 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002310 if (in.IsFpuRegister()) {
2311 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2312 } else if (in.IsConstant()) {
2313 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2314 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2315 if (bit_cast<int32_t, float>(v) == 0) {
2316 __ xorpd(dest, dest);
2317 } else {
2318 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2319 }
2320 } else {
2321 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2322 Address(CpuRegister(RSP), in.GetStackIndex()));
2323 }
Roland Levillaincff13742014-11-17 14:32:17 +00002324 break;
2325
2326 default:
2327 LOG(FATAL) << "Unexpected type conversion from " << input_type
2328 << " to " << result_type;
2329 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002330 break;
2331
2332 default:
2333 LOG(FATAL) << "Unexpected type conversion from " << input_type
2334 << " to " << result_type;
2335 }
2336}
2337
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002338void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002339 LocationSummary* locations =
2340 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002341 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002342 case Primitive::kPrimInt: {
2343 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002344 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002346 break;
2347 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002348
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002349 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002350 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002351 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002352 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002354 break;
2355 }
2356
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002357 case Primitive::kPrimDouble:
2358 case Primitive::kPrimFloat: {
2359 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002360 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002361 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002362 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002363 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002364
2365 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002366 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002367 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002368}
2369
2370void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2371 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002372 Location first = locations->InAt(0);
2373 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002374 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002375
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002376 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002377 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002378 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002379 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2380 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002381 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2382 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002383 } else {
2384 __ leal(out.AsRegister<CpuRegister>(), Address(
2385 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2386 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002387 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002388 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2389 __ addl(out.AsRegister<CpuRegister>(),
2390 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2391 } else {
2392 __ leal(out.AsRegister<CpuRegister>(), Address(
2393 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2394 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002395 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002396 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002397 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002398 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002399 break;
2400 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002401
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002402 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002403 if (second.IsRegister()) {
2404 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2405 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002406 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2407 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002408 } else {
2409 __ leaq(out.AsRegister<CpuRegister>(), Address(
2410 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2411 }
2412 } else {
2413 DCHECK(second.IsConstant());
2414 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2415 int32_t int32_value = Low32Bits(value);
2416 DCHECK_EQ(int32_value, value);
2417 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2418 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2419 } else {
2420 __ leaq(out.AsRegister<CpuRegister>(), Address(
2421 first.AsRegister<CpuRegister>(), int32_value));
2422 }
2423 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002424 break;
2425 }
2426
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002427 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002428 if (second.IsFpuRegister()) {
2429 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2430 } else if (second.IsConstant()) {
2431 __ addss(first.AsFpuRegister<XmmRegister>(),
2432 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2433 } else {
2434 DCHECK(second.IsStackSlot());
2435 __ addss(first.AsFpuRegister<XmmRegister>(),
2436 Address(CpuRegister(RSP), second.GetStackIndex()));
2437 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002438 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002439 }
2440
2441 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002442 if (second.IsFpuRegister()) {
2443 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2444 } else if (second.IsConstant()) {
2445 __ addsd(first.AsFpuRegister<XmmRegister>(),
2446 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2447 } else {
2448 DCHECK(second.IsDoubleStackSlot());
2449 __ addsd(first.AsFpuRegister<XmmRegister>(),
2450 Address(CpuRegister(RSP), second.GetStackIndex()));
2451 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002452 break;
2453 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002454
2455 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002456 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002457 }
2458}
2459
2460void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002461 LocationSummary* locations =
2462 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002463 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002464 case Primitive::kPrimInt: {
2465 locations->SetInAt(0, Location::RequiresRegister());
2466 locations->SetInAt(1, Location::Any());
2467 locations->SetOut(Location::SameAsFirstInput());
2468 break;
2469 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002470 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002471 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002472 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002473 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002474 break;
2475 }
Calin Juravle11351682014-10-23 15:38:15 +01002476 case Primitive::kPrimFloat:
2477 case Primitive::kPrimDouble: {
2478 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002479 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002480 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002481 break;
Calin Juravle11351682014-10-23 15:38:15 +01002482 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002483 default:
Calin Juravle11351682014-10-23 15:38:15 +01002484 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002485 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002486}
2487
2488void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2489 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002490 Location first = locations->InAt(0);
2491 Location second = locations->InAt(1);
2492 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002493 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002494 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002495 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002497 } else if (second.IsConstant()) {
2498 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002500 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002502 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002503 break;
2504 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002505 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002506 if (second.IsConstant()) {
2507 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2508 DCHECK(IsInt<32>(value));
2509 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2510 } else {
2511 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2512 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002513 break;
2514 }
2515
Calin Juravle11351682014-10-23 15:38:15 +01002516 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002517 if (second.IsFpuRegister()) {
2518 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2519 } else if (second.IsConstant()) {
2520 __ subss(first.AsFpuRegister<XmmRegister>(),
2521 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2522 } else {
2523 DCHECK(second.IsStackSlot());
2524 __ subss(first.AsFpuRegister<XmmRegister>(),
2525 Address(CpuRegister(RSP), second.GetStackIndex()));
2526 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002527 break;
Calin Juravle11351682014-10-23 15:38:15 +01002528 }
2529
2530 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002531 if (second.IsFpuRegister()) {
2532 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2533 } else if (second.IsConstant()) {
2534 __ subsd(first.AsFpuRegister<XmmRegister>(),
2535 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2536 } else {
2537 DCHECK(second.IsDoubleStackSlot());
2538 __ subsd(first.AsFpuRegister<XmmRegister>(),
2539 Address(CpuRegister(RSP), second.GetStackIndex()));
2540 }
Calin Juravle11351682014-10-23 15:38:15 +01002541 break;
2542 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002543
2544 default:
Calin Juravle11351682014-10-23 15:38:15 +01002545 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002546 }
2547}
2548
Calin Juravle34bacdf2014-10-07 20:23:36 +01002549void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2550 LocationSummary* locations =
2551 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2552 switch (mul->GetResultType()) {
2553 case Primitive::kPrimInt: {
2554 locations->SetInAt(0, Location::RequiresRegister());
2555 locations->SetInAt(1, Location::Any());
2556 locations->SetOut(Location::SameAsFirstInput());
2557 break;
2558 }
2559 case Primitive::kPrimLong: {
2560 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002561 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(mul->InputAt(1)));
2562 if (locations->InAt(1).IsConstant()) {
2563 // Can use 3 operand multiply.
2564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2565 } else {
2566 locations->SetOut(Location::SameAsFirstInput());
2567 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002568 break;
2569 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002570 case Primitive::kPrimFloat:
2571 case Primitive::kPrimDouble: {
2572 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002573 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002574 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002575 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002576 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002577
2578 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002579 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002580 }
2581}
2582
2583void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2584 LocationSummary* locations = mul->GetLocations();
2585 Location first = locations->InAt(0);
2586 Location second = locations->InAt(1);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002587 switch (mul->GetResultType()) {
2588 case Primitive::kPrimInt: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002589 DCHECK(first.Equals(locations->Out()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002590 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002591 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002592 } else if (second.IsConstant()) {
2593 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002595 } else {
2596 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002597 __ imull(first.AsRegister<CpuRegister>(),
2598 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002599 }
2600 break;
2601 }
2602 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002603 if (second.IsConstant()) {
2604 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2605 DCHECK(IsInt<32>(value));
2606 __ imulq(locations->Out().AsRegister<CpuRegister>(),
2607 first.AsRegister<CpuRegister>(),
2608 Immediate(static_cast<int32_t>(value)));
2609 } else {
2610 DCHECK(first.Equals(locations->Out()));
2611 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2612 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002613 break;
2614 }
2615
Calin Juravleb5bfa962014-10-21 18:02:24 +01002616 case Primitive::kPrimFloat: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002617 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002618 if (second.IsFpuRegister()) {
2619 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2620 } else if (second.IsConstant()) {
2621 __ mulss(first.AsFpuRegister<XmmRegister>(),
2622 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2623 } else {
2624 DCHECK(second.IsStackSlot());
2625 __ mulss(first.AsFpuRegister<XmmRegister>(),
2626 Address(CpuRegister(RSP), second.GetStackIndex()));
2627 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002628 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002629 }
2630
2631 case Primitive::kPrimDouble: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002632 DCHECK(first.Equals(locations->Out()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002633 if (second.IsFpuRegister()) {
2634 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2635 } else if (second.IsConstant()) {
2636 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2637 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2638 } else {
2639 DCHECK(second.IsDoubleStackSlot());
2640 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2641 Address(CpuRegister(RSP), second.GetStackIndex()));
2642 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002643 break;
2644 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002645
2646 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002647 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002648 }
2649}
2650
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002651void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2652 uint32_t stack_adjustment, bool is_float) {
2653 if (source.IsStackSlot()) {
2654 DCHECK(is_float);
2655 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2656 } else if (source.IsDoubleStackSlot()) {
2657 DCHECK(!is_float);
2658 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2659 } else {
2660 // Write the value to the temporary location on the stack and load to FP stack.
2661 if (is_float) {
2662 Location stack_temp = Location::StackSlot(temp_offset);
2663 codegen_->Move(stack_temp, source);
2664 __ flds(Address(CpuRegister(RSP), temp_offset));
2665 } else {
2666 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2667 codegen_->Move(stack_temp, source);
2668 __ fldl(Address(CpuRegister(RSP), temp_offset));
2669 }
2670 }
2671}
2672
2673void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2674 Primitive::Type type = rem->GetResultType();
2675 bool is_float = type == Primitive::kPrimFloat;
2676 size_t elem_size = Primitive::ComponentSize(type);
2677 LocationSummary* locations = rem->GetLocations();
2678 Location first = locations->InAt(0);
2679 Location second = locations->InAt(1);
2680 Location out = locations->Out();
2681
2682 // Create stack space for 2 elements.
2683 // TODO: enhance register allocator to ask for stack temporaries.
2684 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2685
2686 // Load the values to the FP stack in reverse order, using temporaries if needed.
2687 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2688 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2689
2690 // Loop doing FPREM until we stabilize.
2691 Label retry;
2692 __ Bind(&retry);
2693 __ fprem();
2694
2695 // Move FP status to AX.
2696 __ fstsw();
2697
2698 // And see if the argument reduction is complete. This is signaled by the
2699 // C2 FPU flag bit set to 0.
2700 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2701 __ j(kNotEqual, &retry);
2702
2703 // We have settled on the final value. Retrieve it into an XMM register.
2704 // Store FP top of stack to real stack.
2705 if (is_float) {
2706 __ fsts(Address(CpuRegister(RSP), 0));
2707 } else {
2708 __ fstl(Address(CpuRegister(RSP), 0));
2709 }
2710
2711 // Pop the 2 items from the FP stack.
2712 __ fucompp();
2713
2714 // Load the value from the stack into an XMM register.
2715 DCHECK(out.IsFpuRegister()) << out;
2716 if (is_float) {
2717 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2718 } else {
2719 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2720 }
2721
2722 // And remove the temporary stack space we allocated.
2723 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2724}
2725
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002726void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2727 DCHECK(instruction->IsDiv() || instruction->IsRem());
2728
2729 LocationSummary* locations = instruction->GetLocations();
2730 Location second = locations->InAt(1);
2731 DCHECK(second.IsConstant());
2732
2733 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2734 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002735 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002736
2737 DCHECK(imm == 1 || imm == -1);
2738
2739 switch (instruction->GetResultType()) {
2740 case Primitive::kPrimInt: {
2741 if (instruction->IsRem()) {
2742 __ xorl(output_register, output_register);
2743 } else {
2744 __ movl(output_register, input_register);
2745 if (imm == -1) {
2746 __ negl(output_register);
2747 }
2748 }
2749 break;
2750 }
2751
2752 case Primitive::kPrimLong: {
2753 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002754 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002755 } else {
2756 __ movq(output_register, input_register);
2757 if (imm == -1) {
2758 __ negq(output_register);
2759 }
2760 }
2761 break;
2762 }
2763
2764 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002765 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002766 }
2767}
2768
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002769void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002770 LocationSummary* locations = instruction->GetLocations();
2771 Location second = locations->InAt(1);
2772
2773 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2774 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2775
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002776 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002777
2778 DCHECK(IsPowerOfTwo(std::abs(imm)));
2779
2780 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2781
2782 if (instruction->GetResultType() == Primitive::kPrimInt) {
2783 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2784 __ testl(numerator, numerator);
2785 __ cmov(kGreaterEqual, tmp, numerator);
2786 int shift = CTZ(imm);
2787 __ sarl(tmp, Immediate(shift));
2788
2789 if (imm < 0) {
2790 __ negl(tmp);
2791 }
2792
2793 __ movl(output_register, tmp);
2794 } else {
2795 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2796 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2797
Mark Mendell92e83bf2015-05-07 11:25:03 -04002798 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002799 __ addq(rdx, numerator);
2800 __ testq(numerator, numerator);
2801 __ cmov(kGreaterEqual, rdx, numerator);
2802 int shift = CTZ(imm);
2803 __ sarq(rdx, Immediate(shift));
2804
2805 if (imm < 0) {
2806 __ negq(rdx);
2807 }
2808
2809 __ movq(output_register, rdx);
2810 }
2811}
2812
2813void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2814 DCHECK(instruction->IsDiv() || instruction->IsRem());
2815
2816 LocationSummary* locations = instruction->GetLocations();
2817 Location second = locations->InAt(1);
2818
2819 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2820 : locations->GetTemp(0).AsRegister<CpuRegister>();
2821 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2822 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2823 : locations->Out().AsRegister<CpuRegister>();
2824 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2825
2826 DCHECK_EQ(RAX, eax.AsRegister());
2827 DCHECK_EQ(RDX, edx.AsRegister());
2828 if (instruction->IsDiv()) {
2829 DCHECK_EQ(RAX, out.AsRegister());
2830 } else {
2831 DCHECK_EQ(RDX, out.AsRegister());
2832 }
2833
2834 int64_t magic;
2835 int shift;
2836
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002837 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002838 if (instruction->GetResultType() == Primitive::kPrimInt) {
2839 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2840
2841 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2842
2843 __ movl(numerator, eax);
2844
2845 Label no_div;
2846 Label end;
2847 __ testl(eax, eax);
2848 __ j(kNotEqual, &no_div);
2849
2850 __ xorl(out, out);
2851 __ jmp(&end);
2852
2853 __ Bind(&no_div);
2854
2855 __ movl(eax, Immediate(magic));
2856 __ imull(numerator);
2857
2858 if (imm > 0 && magic < 0) {
2859 __ addl(edx, numerator);
2860 } else if (imm < 0 && magic > 0) {
2861 __ subl(edx, numerator);
2862 }
2863
2864 if (shift != 0) {
2865 __ sarl(edx, Immediate(shift));
2866 }
2867
2868 __ movl(eax, edx);
2869 __ shrl(edx, Immediate(31));
2870 __ addl(edx, eax);
2871
2872 if (instruction->IsRem()) {
2873 __ movl(eax, numerator);
2874 __ imull(edx, Immediate(imm));
2875 __ subl(eax, edx);
2876 __ movl(edx, eax);
2877 } else {
2878 __ movl(eax, edx);
2879 }
2880 __ Bind(&end);
2881 } else {
2882 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2883
2884 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2885
2886 CpuRegister rax = eax;
2887 CpuRegister rdx = edx;
2888
2889 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2890
2891 // Save the numerator.
2892 __ movq(numerator, rax);
2893
2894 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002895 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002896
2897 // RDX:RAX = magic * numerator
2898 __ imulq(numerator);
2899
2900 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002901 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002902 __ addq(rdx, numerator);
2903 } else if (imm < 0 && magic > 0) {
2904 // RDX -= numerator
2905 __ subq(rdx, numerator);
2906 }
2907
2908 // Shift if needed.
2909 if (shift != 0) {
2910 __ sarq(rdx, Immediate(shift));
2911 }
2912
2913 // RDX += 1 if RDX < 0
2914 __ movq(rax, rdx);
2915 __ shrq(rdx, Immediate(63));
2916 __ addq(rdx, rax);
2917
2918 if (instruction->IsRem()) {
2919 __ movq(rax, numerator);
2920
2921 if (IsInt<32>(imm)) {
2922 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2923 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002924 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002925 }
2926
2927 __ subq(rax, rdx);
2928 __ movq(rdx, rax);
2929 } else {
2930 __ movq(rax, rdx);
2931 }
2932 }
2933}
2934
Calin Juravlebacfec32014-11-14 15:54:36 +00002935void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2936 DCHECK(instruction->IsDiv() || instruction->IsRem());
2937 Primitive::Type type = instruction->GetResultType();
2938 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2939
2940 bool is_div = instruction->IsDiv();
2941 LocationSummary* locations = instruction->GetLocations();
2942
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002943 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2944 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00002945
Roland Levillain271ab9c2014-11-27 15:23:57 +00002946 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002947 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002948
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002949 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002950 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00002951
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002952 if (imm == 0) {
2953 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2954 } else if (imm == 1 || imm == -1) {
2955 DivRemOneOrMinusOne(instruction);
2956 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002957 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002958 } else {
2959 DCHECK(imm <= -2 || imm >= 2);
2960 GenerateDivRemWithAnyConstant(instruction);
2961 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002962 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002963 SlowPathCodeX86_64* slow_path =
2964 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2965 out.AsRegister(), type, is_div);
2966 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002967
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002968 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2969 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2970 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2971 // so it's safe to just use negl instead of more complex comparisons.
2972 if (type == Primitive::kPrimInt) {
2973 __ cmpl(second_reg, Immediate(-1));
2974 __ j(kEqual, slow_path->GetEntryLabel());
2975 // edx:eax <- sign-extended of eax
2976 __ cdq();
2977 // eax = quotient, edx = remainder
2978 __ idivl(second_reg);
2979 } else {
2980 __ cmpq(second_reg, Immediate(-1));
2981 __ j(kEqual, slow_path->GetEntryLabel());
2982 // rdx:rax <- sign-extended of rax
2983 __ cqo();
2984 // rax = quotient, rdx = remainder
2985 __ idivq(second_reg);
2986 }
2987 __ Bind(slow_path->GetExitLabel());
2988 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002989}
2990
Calin Juravle7c4954d2014-10-28 16:57:40 +00002991void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2992 LocationSummary* locations =
2993 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2994 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002995 case Primitive::kPrimInt:
2996 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002997 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002998 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002999 locations->SetOut(Location::SameAsFirstInput());
3000 // Intel uses edx:eax as the dividend.
3001 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003002 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3003 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3004 // output and request another temp.
3005 if (div->InputAt(1)->IsConstant()) {
3006 locations->AddTemp(Location::RequiresRegister());
3007 }
Calin Juravled0d48522014-11-04 16:40:20 +00003008 break;
3009 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003010
Calin Juravle7c4954d2014-10-28 16:57:40 +00003011 case Primitive::kPrimFloat:
3012 case Primitive::kPrimDouble: {
3013 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003014 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003015 locations->SetOut(Location::SameAsFirstInput());
3016 break;
3017 }
3018
3019 default:
3020 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3021 }
3022}
3023
3024void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3025 LocationSummary* locations = div->GetLocations();
3026 Location first = locations->InAt(0);
3027 Location second = locations->InAt(1);
3028 DCHECK(first.Equals(locations->Out()));
3029
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003030 Primitive::Type type = div->GetResultType();
3031 switch (type) {
3032 case Primitive::kPrimInt:
3033 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003034 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003035 break;
3036 }
3037
Calin Juravle7c4954d2014-10-28 16:57:40 +00003038 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003039 if (second.IsFpuRegister()) {
3040 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3041 } else if (second.IsConstant()) {
3042 __ divss(first.AsFpuRegister<XmmRegister>(),
3043 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3044 } else {
3045 DCHECK(second.IsStackSlot());
3046 __ divss(first.AsFpuRegister<XmmRegister>(),
3047 Address(CpuRegister(RSP), second.GetStackIndex()));
3048 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003049 break;
3050 }
3051
3052 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003053 if (second.IsFpuRegister()) {
3054 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3055 } else if (second.IsConstant()) {
3056 __ divsd(first.AsFpuRegister<XmmRegister>(),
3057 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3058 } else {
3059 DCHECK(second.IsDoubleStackSlot());
3060 __ divsd(first.AsFpuRegister<XmmRegister>(),
3061 Address(CpuRegister(RSP), second.GetStackIndex()));
3062 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003063 break;
3064 }
3065
3066 default:
3067 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3068 }
3069}
3070
Calin Juravlebacfec32014-11-14 15:54:36 +00003071void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003072 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003073 LocationSummary* locations =
3074 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003075
3076 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003077 case Primitive::kPrimInt:
3078 case Primitive::kPrimLong: {
3079 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003080 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003081 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3082 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003083 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3084 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3085 // output and request another temp.
3086 if (rem->InputAt(1)->IsConstant()) {
3087 locations->AddTemp(Location::RequiresRegister());
3088 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003089 break;
3090 }
3091
3092 case Primitive::kPrimFloat:
3093 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003094 locations->SetInAt(0, Location::Any());
3095 locations->SetInAt(1, Location::Any());
3096 locations->SetOut(Location::RequiresFpuRegister());
3097 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003098 break;
3099 }
3100
3101 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003102 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003103 }
3104}
3105
3106void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3107 Primitive::Type type = rem->GetResultType();
3108 switch (type) {
3109 case Primitive::kPrimInt:
3110 case Primitive::kPrimLong: {
3111 GenerateDivRemIntegral(rem);
3112 break;
3113 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003114 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003115 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003116 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003117 break;
3118 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003119 default:
3120 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3121 }
3122}
3123
Calin Juravled0d48522014-11-04 16:40:20 +00003124void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3125 LocationSummary* locations =
3126 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3127 locations->SetInAt(0, Location::Any());
3128 if (instruction->HasUses()) {
3129 locations->SetOut(Location::SameAsFirstInput());
3130 }
3131}
3132
3133void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3134 SlowPathCodeX86_64* slow_path =
3135 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3136 codegen_->AddSlowPath(slow_path);
3137
3138 LocationSummary* locations = instruction->GetLocations();
3139 Location value = locations->InAt(0);
3140
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003141 switch (instruction->GetType()) {
3142 case Primitive::kPrimInt: {
3143 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003144 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003145 __ j(kEqual, slow_path->GetEntryLabel());
3146 } else if (value.IsStackSlot()) {
3147 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3148 __ j(kEqual, slow_path->GetEntryLabel());
3149 } else {
3150 DCHECK(value.IsConstant()) << value;
3151 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3152 __ jmp(slow_path->GetEntryLabel());
3153 }
3154 }
3155 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003156 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003157 case Primitive::kPrimLong: {
3158 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003159 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003160 __ j(kEqual, slow_path->GetEntryLabel());
3161 } else if (value.IsDoubleStackSlot()) {
3162 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3163 __ j(kEqual, slow_path->GetEntryLabel());
3164 } else {
3165 DCHECK(value.IsConstant()) << value;
3166 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3167 __ jmp(slow_path->GetEntryLabel());
3168 }
3169 }
3170 break;
3171 }
3172 default:
3173 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003174 }
Calin Juravled0d48522014-11-04 16:40:20 +00003175}
3176
Calin Juravle9aec02f2014-11-18 23:06:35 +00003177void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3178 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3179
3180 LocationSummary* locations =
3181 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3182
3183 switch (op->GetResultType()) {
3184 case Primitive::kPrimInt:
3185 case Primitive::kPrimLong: {
3186 locations->SetInAt(0, Location::RequiresRegister());
3187 // The shift count needs to be in CL.
3188 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3189 locations->SetOut(Location::SameAsFirstInput());
3190 break;
3191 }
3192 default:
3193 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3194 }
3195}
3196
3197void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3198 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3199
3200 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003201 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003202 Location second = locations->InAt(1);
3203
3204 switch (op->GetResultType()) {
3205 case Primitive::kPrimInt: {
3206 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003208 if (op->IsShl()) {
3209 __ shll(first_reg, second_reg);
3210 } else if (op->IsShr()) {
3211 __ sarl(first_reg, second_reg);
3212 } else {
3213 __ shrl(first_reg, second_reg);
3214 }
3215 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003216 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003217 if (op->IsShl()) {
3218 __ shll(first_reg, imm);
3219 } else if (op->IsShr()) {
3220 __ sarl(first_reg, imm);
3221 } else {
3222 __ shrl(first_reg, imm);
3223 }
3224 }
3225 break;
3226 }
3227 case Primitive::kPrimLong: {
3228 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003230 if (op->IsShl()) {
3231 __ shlq(first_reg, second_reg);
3232 } else if (op->IsShr()) {
3233 __ sarq(first_reg, second_reg);
3234 } else {
3235 __ shrq(first_reg, second_reg);
3236 }
3237 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003238 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003239 if (op->IsShl()) {
3240 __ shlq(first_reg, imm);
3241 } else if (op->IsShr()) {
3242 __ sarq(first_reg, imm);
3243 } else {
3244 __ shrq(first_reg, imm);
3245 }
3246 }
3247 break;
3248 }
3249 default:
3250 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3251 }
3252}
3253
3254void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3255 HandleShift(shl);
3256}
3257
3258void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3259 HandleShift(shl);
3260}
3261
3262void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3263 HandleShift(shr);
3264}
3265
3266void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3267 HandleShift(shr);
3268}
3269
3270void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3271 HandleShift(ushr);
3272}
3273
3274void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3275 HandleShift(ushr);
3276}
3277
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003278void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003279 LocationSummary* locations =
3280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003281 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003282 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003283 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003284 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003285}
3286
3287void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3288 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003289 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3290 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003291 // Note: if heap poisoning is enabled, the entry point takes cares
3292 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003293 __ gs()->call(
3294 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003295
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003296 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003297 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003298}
3299
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003300void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3301 LocationSummary* locations =
3302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3303 InvokeRuntimeCallingConvention calling_convention;
3304 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003305 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003306 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003307 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003308}
3309
3310void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3311 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003312 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3313 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003314
Roland Levillain4d027112015-07-01 15:41:14 +01003315 // Note: if heap poisoning is enabled, the entry point takes cares
3316 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003317 __ gs()->call(
3318 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003319
3320 DCHECK(!codegen_->IsLeafMethod());
3321 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3322}
3323
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003324void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003325 LocationSummary* locations =
3326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003327 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3328 if (location.IsStackSlot()) {
3329 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3330 } else if (location.IsDoubleStackSlot()) {
3331 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3332 }
3333 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003334}
3335
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003336void InstructionCodeGeneratorX86_64::VisitParameterValue(
3337 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003338 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003339}
3340
3341void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3342 LocationSummary* locations =
3343 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3344 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3345}
3346
3347void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3348 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3349 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003350}
3351
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003352void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003353 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003354 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003355 locations->SetInAt(0, Location::RequiresRegister());
3356 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003357}
3358
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003359void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3360 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003361 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3362 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003363 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003364 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003365 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003366 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003367 break;
3368
3369 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003370 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003371 break;
3372
3373 default:
3374 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3375 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003376}
3377
David Brazdil66d126e2015-04-03 16:02:44 +01003378void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3379 LocationSummary* locations =
3380 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3381 locations->SetInAt(0, Location::RequiresRegister());
3382 locations->SetOut(Location::SameAsFirstInput());
3383}
3384
3385void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003386 LocationSummary* locations = bool_not->GetLocations();
3387 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3388 locations->Out().AsRegister<CpuRegister>().AsRegister());
3389 Location out = locations->Out();
3390 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3391}
3392
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003393void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003394 LocationSummary* locations =
3395 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003396 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3397 locations->SetInAt(i, Location::Any());
3398 }
3399 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003400}
3401
3402void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003403 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003404 LOG(FATAL) << "Unimplemented";
3405}
3406
Calin Juravle52c48962014-12-16 17:02:57 +00003407void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3408 /*
3409 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3410 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3411 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3412 */
3413 switch (kind) {
3414 case MemBarrierKind::kAnyAny: {
3415 __ mfence();
3416 break;
3417 }
3418 case MemBarrierKind::kAnyStore:
3419 case MemBarrierKind::kLoadAny:
3420 case MemBarrierKind::kStoreStore: {
3421 // nop
3422 break;
3423 }
3424 default:
3425 LOG(FATAL) << "Unexpected memory barier " << kind;
3426 }
3427}
3428
3429void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3430 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3431
Nicolas Geoffray39468442014-09-02 15:17:15 +01003432 LocationSummary* locations =
3433 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003434 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003435 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3436 locations->SetOut(Location::RequiresFpuRegister());
3437 } else {
3438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3439 }
Calin Juravle52c48962014-12-16 17:02:57 +00003440}
3441
3442void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3443 const FieldInfo& field_info) {
3444 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3445
3446 LocationSummary* locations = instruction->GetLocations();
3447 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3448 Location out = locations->Out();
3449 bool is_volatile = field_info.IsVolatile();
3450 Primitive::Type field_type = field_info.GetFieldType();
3451 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3452
3453 switch (field_type) {
3454 case Primitive::kPrimBoolean: {
3455 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3456 break;
3457 }
3458
3459 case Primitive::kPrimByte: {
3460 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3461 break;
3462 }
3463
3464 case Primitive::kPrimShort: {
3465 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3466 break;
3467 }
3468
3469 case Primitive::kPrimChar: {
3470 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3471 break;
3472 }
3473
3474 case Primitive::kPrimInt:
3475 case Primitive::kPrimNot: {
3476 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3477 break;
3478 }
3479
3480 case Primitive::kPrimLong: {
3481 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3482 break;
3483 }
3484
3485 case Primitive::kPrimFloat: {
3486 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3487 break;
3488 }
3489
3490 case Primitive::kPrimDouble: {
3491 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3492 break;
3493 }
3494
3495 case Primitive::kPrimVoid:
3496 LOG(FATAL) << "Unreachable type " << field_type;
3497 UNREACHABLE();
3498 }
3499
Calin Juravle77520bc2015-01-12 18:45:46 +00003500 codegen_->MaybeRecordImplicitNullCheck(instruction);
3501
Calin Juravle52c48962014-12-16 17:02:57 +00003502 if (is_volatile) {
3503 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3504 }
Roland Levillain4d027112015-07-01 15:41:14 +01003505
3506 if (field_type == Primitive::kPrimNot) {
3507 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3508 }
Calin Juravle52c48962014-12-16 17:02:57 +00003509}
3510
3511void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3512 const FieldInfo& field_info) {
3513 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3514
3515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003517 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003518 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003519 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003520
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003521 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003522 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3523 locations->SetInAt(1, Location::RequiresFpuRegister());
3524 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003525 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003526 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003527 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003528 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003529 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003530 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003531 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3532 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003533 locations->AddTemp(Location::RequiresRegister());
3534 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003535}
3536
Calin Juravle52c48962014-12-16 17:02:57 +00003537void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003538 const FieldInfo& field_info,
3539 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003540 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3541
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003542 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003543 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3544 Location value = locations->InAt(1);
3545 bool is_volatile = field_info.IsVolatile();
3546 Primitive::Type field_type = field_info.GetFieldType();
3547 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3548
3549 if (is_volatile) {
3550 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3551 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003552
3553 switch (field_type) {
3554 case Primitive::kPrimBoolean:
3555 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003556 if (value.IsConstant()) {
3557 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3558 __ movb(Address(base, offset), Immediate(v));
3559 } else {
3560 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3561 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003562 break;
3563 }
3564
3565 case Primitive::kPrimShort:
3566 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003567 if (value.IsConstant()) {
3568 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3569 __ movw(Address(base, offset), Immediate(v));
3570 } else {
3571 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3572 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003573 break;
3574 }
3575
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003576 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003577 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003578 if (value.IsConstant()) {
3579 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003580 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3581 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3582 // Note: if heap poisoning is enabled, no need to poison
3583 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003584 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003585 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003586 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3587 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3588 __ movl(temp, value.AsRegister<CpuRegister>());
3589 __ PoisonHeapReference(temp);
3590 __ movl(Address(base, offset), temp);
3591 } else {
3592 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3593 }
Mark Mendell40741f32015-04-20 22:10:34 -04003594 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003595 break;
3596 }
3597
3598 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003599 if (value.IsConstant()) {
3600 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3601 DCHECK(IsInt<32>(v));
3602 int32_t v_32 = v;
3603 __ movq(Address(base, offset), Immediate(v_32));
3604 } else {
3605 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3606 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003607 break;
3608 }
3609
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003610 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003611 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003612 break;
3613 }
3614
3615 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003616 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003617 break;
3618 }
3619
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003620 case Primitive::kPrimVoid:
3621 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003622 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003623 }
Calin Juravle52c48962014-12-16 17:02:57 +00003624
Calin Juravle77520bc2015-01-12 18:45:46 +00003625 codegen_->MaybeRecordImplicitNullCheck(instruction);
3626
3627 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3628 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3629 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003630 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003631 }
3632
Calin Juravle52c48962014-12-16 17:02:57 +00003633 if (is_volatile) {
3634 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3635 }
3636}
3637
3638void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3639 HandleFieldSet(instruction, instruction->GetFieldInfo());
3640}
3641
3642void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003643 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003644}
3645
3646void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003647 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003648}
3649
3650void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003651 HandleFieldGet(instruction, instruction->GetFieldInfo());
3652}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003653
Calin Juravle52c48962014-12-16 17:02:57 +00003654void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3655 HandleFieldGet(instruction);
3656}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003657
Calin Juravle52c48962014-12-16 17:02:57 +00003658void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3659 HandleFieldGet(instruction, instruction->GetFieldInfo());
3660}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003661
Calin Juravle52c48962014-12-16 17:02:57 +00003662void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3663 HandleFieldSet(instruction, instruction->GetFieldInfo());
3664}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003665
Calin Juravle52c48962014-12-16 17:02:57 +00003666void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003667 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003668}
3669
3670void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003671 LocationSummary* locations =
3672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003673 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3674 ? Location::RequiresRegister()
3675 : Location::Any();
3676 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003677 if (instruction->HasUses()) {
3678 locations->SetOut(Location::SameAsFirstInput());
3679 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003680}
3681
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003682void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003683 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3684 return;
3685 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003686 LocationSummary* locations = instruction->GetLocations();
3687 Location obj = locations->InAt(0);
3688
3689 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3690 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3691}
3692
3693void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003694 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695 codegen_->AddSlowPath(slow_path);
3696
3697 LocationSummary* locations = instruction->GetLocations();
3698 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003699
3700 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003701 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003702 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003703 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003704 } else {
3705 DCHECK(obj.IsConstant()) << obj;
3706 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3707 __ jmp(slow_path->GetEntryLabel());
3708 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003709 }
3710 __ j(kEqual, slow_path->GetEntryLabel());
3711}
3712
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003713void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3714 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3715 GenerateImplicitNullCheck(instruction);
3716 } else {
3717 GenerateExplicitNullCheck(instruction);
3718 }
3719}
3720
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003722 LocationSummary* locations =
3723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003724 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003725 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003726 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3727 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3728 } else {
3729 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3730 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003731}
3732
3733void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3734 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003735 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003736 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003737 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738
Roland Levillain4d027112015-07-01 15:41:14 +01003739 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740 case Primitive::kPrimBoolean: {
3741 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003743 if (index.IsConstant()) {
3744 __ movzxb(out, Address(obj,
3745 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3746 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003747 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003748 }
3749 break;
3750 }
3751
3752 case Primitive::kPrimByte: {
3753 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003754 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003755 if (index.IsConstant()) {
3756 __ movsxb(out, Address(obj,
3757 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3758 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003759 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003760 }
3761 break;
3762 }
3763
3764 case Primitive::kPrimShort: {
3765 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767 if (index.IsConstant()) {
3768 __ movsxw(out, Address(obj,
3769 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3770 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003771 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003772 }
3773 break;
3774 }
3775
3776 case Primitive::kPrimChar: {
3777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003778 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003779 if (index.IsConstant()) {
3780 __ movzxw(out, Address(obj,
3781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3782 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003784 }
3785 break;
3786 }
3787
3788 case Primitive::kPrimInt:
3789 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003790 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3791 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003793 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 if (index.IsConstant()) {
3795 __ movl(out, Address(obj,
3796 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3797 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 }
3800 break;
3801 }
3802
3803 case Primitive::kPrimLong: {
3804 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003805 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 if (index.IsConstant()) {
3807 __ movq(out, Address(obj,
3808 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3809 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003810 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003811 }
3812 break;
3813 }
3814
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003815 case Primitive::kPrimFloat: {
3816 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003818 if (index.IsConstant()) {
3819 __ movss(out, Address(obj,
3820 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3821 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003823 }
3824 break;
3825 }
3826
3827 case Primitive::kPrimDouble: {
3828 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003829 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003830 if (index.IsConstant()) {
3831 __ movsd(out, Address(obj,
3832 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3833 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003834 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003835 }
3836 break;
3837 }
3838
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003839 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003840 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003841 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003843 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003844
3845 if (type == Primitive::kPrimNot) {
3846 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3847 __ MaybeUnpoisonHeapReference(out);
3848 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003849}
3850
3851void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003852 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003853
3854 bool needs_write_barrier =
3855 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3856 bool needs_runtime_call = instruction->NeedsTypeCheck();
3857
Nicolas Geoffray39468442014-09-02 15:17:15 +01003858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003859 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3860 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003861 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003862 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3863 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3864 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003866 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003867 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003868 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3869 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003870 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003871 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003872 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3873 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003874 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003875 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003876 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003877
3878 if (needs_write_barrier) {
3879 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003880 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003881 locations->AddTemp(Location::RequiresRegister());
3882 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003883 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003884}
3885
3886void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3887 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003888 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003889 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003890 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003891 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003892 bool needs_runtime_call = locations->WillCall();
3893 bool needs_write_barrier =
3894 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003895
3896 switch (value_type) {
3897 case Primitive::kPrimBoolean:
3898 case Primitive::kPrimByte: {
3899 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003900 if (index.IsConstant()) {
3901 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003902 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003903 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003904 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003905 __ movb(Address(obj, offset),
3906 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003907 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003908 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003909 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003910 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3911 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003912 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003914 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3915 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003916 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003917 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 break;
3919 }
3920
3921 case Primitive::kPrimShort:
3922 case Primitive::kPrimChar: {
3923 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003924 if (index.IsConstant()) {
3925 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003926 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003927 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003928 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003929 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00003930 __ movw(Address(obj, offset),
3931 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003932 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003933 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003934 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003935 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003936 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
3937 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003938 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003939 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003940 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003941 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3942 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003943 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003944 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945 break;
3946 }
3947
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003948 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003950 if (!needs_runtime_call) {
3951 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3952 if (index.IsConstant()) {
3953 size_t offset =
3954 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3955 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003956 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3957 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3958 __ movl(temp, value.AsRegister<CpuRegister>());
3959 __ PoisonHeapReference(temp);
3960 __ movl(Address(obj, offset), temp);
3961 } else {
3962 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
3963 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003964 } else {
3965 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003966 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003967 // `value_type == Primitive::kPrimNot` implies `v == 0`.
3968 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
3969 // Note: if heap poisoning is enabled, no need to poison
3970 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04003971 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003972 }
3973 } else {
3974 DCHECK(index.IsRegister()) << index;
3975 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003976 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3977 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3978 __ movl(temp, value.AsRegister<CpuRegister>());
3979 __ PoisonHeapReference(temp);
3980 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
3981 } else {
3982 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3983 value.AsRegister<CpuRegister>());
3984 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003985 } else {
3986 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04003987 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003988 // `value_type == Primitive::kPrimNot` implies `v == 0`.
3989 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
3990 // Note: if heap poisoning is enabled, no need to poison
3991 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003992 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04003993 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003994 }
3995 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003996 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003997 if (needs_write_barrier) {
3998 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4000 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004001 codegen_->MarkGCCard(
4002 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004003 }
4004 } else {
4005 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004006 // Note: if heap poisoning is enabled, pAputObject takes cares
4007 // of poisoning the reference.
Roland Levillain199f3362014-11-27 17:15:16 +00004008 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
4009 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004010 DCHECK(!codegen_->IsLeafMethod());
4011 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4012 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004013 break;
4014 }
4015
4016 case Primitive::kPrimLong: {
4017 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 if (index.IsConstant()) {
4019 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004020 if (value.IsRegister()) {
4021 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4022 } else {
4023 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4024 DCHECK(IsInt<32>(v));
4025 int32_t v_32 = v;
4026 __ movq(Address(obj, offset), Immediate(v_32));
4027 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004028 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004029 if (value.IsRegister()) {
4030 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4031 value.AsRegister<CpuRegister>());
4032 } else {
4033 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4034 DCHECK(IsInt<32>(v));
4035 int32_t v_32 = v;
4036 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4037 Immediate(v_32));
4038 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004039 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004040 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004041 break;
4042 }
4043
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004044 case Primitive::kPrimFloat: {
4045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4046 if (index.IsConstant()) {
4047 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4048 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004049 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004050 } else {
4051 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004052 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4053 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004054 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004055 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004056 break;
4057 }
4058
4059 case Primitive::kPrimDouble: {
4060 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4061 if (index.IsConstant()) {
4062 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4063 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004064 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004065 } else {
4066 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004067 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4068 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004069 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004070 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004071 break;
4072 }
4073
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004074 case Primitive::kPrimVoid:
4075 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004076 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004077 }
4078}
4079
4080void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004081 LocationSummary* locations =
4082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004083 locations->SetInAt(0, Location::RequiresRegister());
4084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004085}
4086
4087void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4088 LocationSummary* locations = instruction->GetLocations();
4089 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004090 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4091 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004092 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004093 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004094}
4095
4096void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004097 LocationSummary* locations =
4098 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004099 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004100 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004101 if (instruction->HasUses()) {
4102 locations->SetOut(Location::SameAsFirstInput());
4103 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104}
4105
4106void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4107 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004108 Location index_loc = locations->InAt(0);
4109 Location length_loc = locations->InAt(1);
4110 SlowPathCodeX86_64* slow_path =
4111 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004112
Mark Mendell99dbd682015-04-22 16:18:52 -04004113 if (length_loc.IsConstant()) {
4114 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4115 if (index_loc.IsConstant()) {
4116 // BCE will remove the bounds check if we are guarenteed to pass.
4117 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4118 if (index < 0 || index >= length) {
4119 codegen_->AddSlowPath(slow_path);
4120 __ jmp(slow_path->GetEntryLabel());
4121 } else {
4122 // Some optimization after BCE may have generated this, and we should not
4123 // generate a bounds check if it is a valid range.
4124 }
4125 return;
4126 }
4127
4128 // We have to reverse the jump condition because the length is the constant.
4129 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4130 __ cmpl(index_reg, Immediate(length));
4131 codegen_->AddSlowPath(slow_path);
4132 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004133 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004134 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4135 if (index_loc.IsConstant()) {
4136 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4137 __ cmpl(length, Immediate(value));
4138 } else {
4139 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4140 }
4141 codegen_->AddSlowPath(slow_path);
4142 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004144}
4145
4146void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4147 CpuRegister card,
4148 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004149 CpuRegister value,
4150 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004152 if (value_can_be_null) {
4153 __ testl(value, value);
4154 __ j(kEqual, &is_null);
4155 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156 __ gs()->movq(card, Address::Absolute(
4157 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4158 __ movq(temp, object);
4159 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004160 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004161 if (value_can_be_null) {
4162 __ Bind(&is_null);
4163 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164}
4165
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004166void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4167 temp->SetLocations(nullptr);
4168}
4169
4170void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4171 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004172 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004173}
4174
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004175void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004176 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004177 LOG(FATAL) << "Unimplemented";
4178}
4179
4180void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004181 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4182}
4183
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004184void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4186}
4187
4188void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004189 HBasicBlock* block = instruction->GetBlock();
4190 if (block->GetLoopInformation() != nullptr) {
4191 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4192 // The back edge will generate the suspend check.
4193 return;
4194 }
4195 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4196 // The goto will generate the suspend check.
4197 return;
4198 }
4199 GenerateSuspendCheck(instruction, nullptr);
4200}
4201
4202void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4203 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004204 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004205 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4206 if (slow_path == nullptr) {
4207 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4208 instruction->SetSlowPath(slow_path);
4209 codegen_->AddSlowPath(slow_path);
4210 if (successor != nullptr) {
4211 DCHECK(successor->IsLoopHeader());
4212 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4213 }
4214 } else {
4215 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4216 }
4217
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004218 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004219 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004220 if (successor == nullptr) {
4221 __ j(kNotEqual, slow_path->GetEntryLabel());
4222 __ Bind(slow_path->GetReturnLabel());
4223 } else {
4224 __ j(kEqual, codegen_->GetLabelOf(successor));
4225 __ jmp(slow_path->GetEntryLabel());
4226 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004227}
4228
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004229X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4230 return codegen_->GetAssembler();
4231}
4232
4233void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4234 MoveOperands* move = moves_.Get(index);
4235 Location source = move->GetSource();
4236 Location destination = move->GetDestination();
4237
4238 if (source.IsRegister()) {
4239 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004240 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004241 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004242 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004243 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004244 } else {
4245 DCHECK(destination.IsDoubleStackSlot());
4246 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004247 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004248 }
4249 } else if (source.IsStackSlot()) {
4250 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004251 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004252 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004253 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004254 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004255 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004256 } else {
4257 DCHECK(destination.IsStackSlot());
4258 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4259 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4260 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004261 } else if (source.IsDoubleStackSlot()) {
4262 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004263 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004264 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004265 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004266 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4267 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004268 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004269 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004270 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4271 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4272 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004273 } else if (source.IsConstant()) {
4274 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004275 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4276 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004277 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004278 if (value == 0) {
4279 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4280 } else {
4281 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4282 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004283 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004284 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004285 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004286 }
4287 } else if (constant->IsLongConstant()) {
4288 int64_t value = constant->AsLongConstant()->GetValue();
4289 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004290 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004291 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004292 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004293 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004294 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4295 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004296 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004297 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004298 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004299 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004300 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4301 if (value == 0) {
4302 // easy FP 0.0.
4303 __ xorps(dest, dest);
4304 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004305 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004306 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004307 } else {
4308 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004309 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004310 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4311 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004312 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004313 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004314 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004315 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004316 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004317 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4318 if (value == 0) {
4319 __ xorpd(dest, dest);
4320 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004321 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004322 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004323 } else {
4324 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004325 codegen_->Load64BitValue(CpuRegister(TMP), value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004326 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4327 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004328 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004329 } else if (source.IsFpuRegister()) {
4330 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004331 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004332 } else if (destination.IsStackSlot()) {
4333 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004334 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004335 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004336 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004337 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004338 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004339 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004340 }
4341}
4342
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004343void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004344 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004345 __ movl(Address(CpuRegister(RSP), mem), reg);
4346 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004347}
4348
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004349void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004350 ScratchRegisterScope ensure_scratch(
4351 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4352
4353 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4354 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4355 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4356 Address(CpuRegister(RSP), mem2 + stack_offset));
4357 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4358 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4359 CpuRegister(ensure_scratch.GetRegister()));
4360}
4361
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004362void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4363 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4364 __ movq(Address(CpuRegister(RSP), mem), reg);
4365 __ movq(reg, CpuRegister(TMP));
4366}
4367
4368void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4369 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004370 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004371
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004372 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4373 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4374 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4375 Address(CpuRegister(RSP), mem2 + stack_offset));
4376 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4377 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4378 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004379}
4380
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004381void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4382 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4383 __ movss(Address(CpuRegister(RSP), mem), reg);
4384 __ movd(reg, CpuRegister(TMP));
4385}
4386
4387void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4388 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4389 __ movsd(Address(CpuRegister(RSP), mem), reg);
4390 __ movd(reg, CpuRegister(TMP));
4391}
4392
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004393void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4394 MoveOperands* move = moves_.Get(index);
4395 Location source = move->GetSource();
4396 Location destination = move->GetDestination();
4397
4398 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004399 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004400 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004401 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004402 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004403 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004404 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004405 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4406 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004408 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004409 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004410 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4411 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004412 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4414 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4415 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004416 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004417 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004418 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004419 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004420 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004421 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004422 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004423 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004424 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004425 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004426 }
4427}
4428
4429
4430void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4431 __ pushq(CpuRegister(reg));
4432}
4433
4434
4435void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4436 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004437}
4438
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004439void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4440 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4441 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4442 Immediate(mirror::Class::kStatusInitialized));
4443 __ j(kLess, slow_path->GetEntryLabel());
4444 __ Bind(slow_path->GetExitLabel());
4445 // No need for memory fence, thanks to the X86_64 memory model.
4446}
4447
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004448void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004449 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4450 ? LocationSummary::kCallOnSlowPath
4451 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004452 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004453 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004454 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004455 locations->SetOut(Location::RequiresRegister());
4456}
4457
4458void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004459 LocationSummary* locations = cls->GetLocations();
4460 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4461 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004462 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004463 DCHECK(!cls->CanCallRuntime());
4464 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004465 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004466 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004467 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004468 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004469 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004470 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004471 __ MaybeUnpoisonHeapReference(out);
4472
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004473 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4474 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4475 codegen_->AddSlowPath(slow_path);
4476 __ testl(out, out);
4477 __ j(kEqual, slow_path->GetEntryLabel());
4478 if (cls->MustGenerateClinitCheck()) {
4479 GenerateClassInitializationCheck(slow_path, out);
4480 } else {
4481 __ Bind(slow_path->GetExitLabel());
4482 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004483 }
4484}
4485
4486void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4487 LocationSummary* locations =
4488 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4489 locations->SetInAt(0, Location::RequiresRegister());
4490 if (check->HasUses()) {
4491 locations->SetOut(Location::SameAsFirstInput());
4492 }
4493}
4494
4495void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004496 // We assume the class to not be null.
4497 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4498 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004499 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004500 GenerateClassInitializationCheck(slow_path,
4501 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004502}
4503
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004504void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4505 LocationSummary* locations =
4506 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004507 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004508 locations->SetOut(Location::RequiresRegister());
4509}
4510
4511void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4512 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4513 codegen_->AddSlowPath(slow_path);
4514
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004515 LocationSummary* locations = load->GetLocations();
4516 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4517 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004518 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004519 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004520 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004521 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004522 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004523 __ testl(out, out);
4524 __ j(kEqual, slow_path->GetEntryLabel());
4525 __ Bind(slow_path->GetExitLabel());
4526}
4527
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004528void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4529 LocationSummary* locations =
4530 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4531 locations->SetOut(Location::RequiresRegister());
4532}
4533
4534void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
4535 Address address = Address::Absolute(
4536 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004537 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004538 __ gs()->movl(address, Immediate(0));
4539}
4540
4541void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4542 LocationSummary* locations =
4543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4544 InvokeRuntimeCallingConvention calling_convention;
4545 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4546}
4547
4548void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
4549 __ gs()->call(
4550 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
4551 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4552}
4553
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004554void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004555 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4556 ? LocationSummary::kNoCall
4557 : LocationSummary::kCallOnSlowPath;
4558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4559 locations->SetInAt(0, Location::RequiresRegister());
4560 locations->SetInAt(1, Location::Any());
4561 locations->SetOut(Location::RequiresRegister());
4562}
4563
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004564void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004565 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004566 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004567 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004568 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004569 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4570 Label done, zero;
4571 SlowPathCodeX86_64* slow_path = nullptr;
4572
4573 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004574 // Avoid null check if we know obj is not null.
4575 if (instruction->MustDoNullCheck()) {
4576 __ testl(obj, obj);
4577 __ j(kEqual, &zero);
4578 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004579 // Compare the class of `obj` with `cls`.
4580 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004581 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004582 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004583 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004584 } else {
4585 DCHECK(cls.IsStackSlot()) << cls;
4586 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4587 }
4588 if (instruction->IsClassFinal()) {
4589 // Classes must be equal for the instanceof to succeed.
4590 __ j(kNotEqual, &zero);
4591 __ movl(out, Immediate(1));
4592 __ jmp(&done);
4593 } else {
4594 // If the classes are not equal, we go into a slow path.
4595 DCHECK(locations->OnlyCallsOnSlowPath());
4596 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004597 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004598 codegen_->AddSlowPath(slow_path);
4599 __ j(kNotEqual, slow_path->GetEntryLabel());
4600 __ movl(out, Immediate(1));
4601 __ jmp(&done);
4602 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004603
4604 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4605 __ Bind(&zero);
4606 __ movl(out, Immediate(0));
4607 }
4608
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004609 if (slow_path != nullptr) {
4610 __ Bind(slow_path->GetExitLabel());
4611 }
4612 __ Bind(&done);
4613}
4614
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004615void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4616 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4617 instruction, LocationSummary::kCallOnSlowPath);
4618 locations->SetInAt(0, Location::RequiresRegister());
4619 locations->SetInAt(1, Location::Any());
4620 locations->AddTemp(Location::RequiresRegister());
4621}
4622
4623void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4624 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004625 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004626 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004627 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004628 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4629 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4630 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4631 codegen_->AddSlowPath(slow_path);
4632
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004633 // Avoid null check if we know obj is not null.
4634 if (instruction->MustDoNullCheck()) {
4635 __ testl(obj, obj);
4636 __ j(kEqual, slow_path->GetExitLabel());
4637 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004638 // Compare the class of `obj` with `cls`.
4639 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004640 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004641 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004642 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004643 } else {
4644 DCHECK(cls.IsStackSlot()) << cls;
4645 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4646 }
Roland Levillain4d027112015-07-01 15:41:14 +01004647 // The checkcast succeeds if the classes are equal (fast path).
4648 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004649 __ j(kNotEqual, slow_path->GetEntryLabel());
4650 __ Bind(slow_path->GetExitLabel());
4651}
4652
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004653void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4654 LocationSummary* locations =
4655 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4656 InvokeRuntimeCallingConvention calling_convention;
4657 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4658}
4659
4660void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4661 __ gs()->call(Address::Absolute(instruction->IsEnter()
4662 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
4663 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
4664 true));
4665 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4666}
4667
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4669void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4670void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4671
4672void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4673 LocationSummary* locations =
4674 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4675 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4676 || instruction->GetResultType() == Primitive::kPrimLong);
4677 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004678 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004679 locations->SetOut(Location::SameAsFirstInput());
4680}
4681
4682void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4683 HandleBitwiseOperation(instruction);
4684}
4685
4686void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4687 HandleBitwiseOperation(instruction);
4688}
4689
4690void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4691 HandleBitwiseOperation(instruction);
4692}
4693
4694void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4695 LocationSummary* locations = instruction->GetLocations();
4696 Location first = locations->InAt(0);
4697 Location second = locations->InAt(1);
4698 DCHECK(first.Equals(locations->Out()));
4699
4700 if (instruction->GetResultType() == Primitive::kPrimInt) {
4701 if (second.IsRegister()) {
4702 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004703 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004705 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004706 } else {
4707 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004708 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004709 }
4710 } else if (second.IsConstant()) {
4711 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4712 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004713 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004714 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004715 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004716 } else {
4717 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004718 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004719 }
4720 } else {
4721 Address address(CpuRegister(RSP), second.GetStackIndex());
4722 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004723 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004724 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004725 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004726 } else {
4727 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004728 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004729 }
4730 }
4731 } else {
4732 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004733 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4734 bool second_is_constant = false;
4735 int64_t value = 0;
4736 if (second.IsConstant()) {
4737 second_is_constant = true;
4738 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004739 }
Mark Mendell40741f32015-04-20 22:10:34 -04004740 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004741
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004742 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004743 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004744 if (is_int32_value) {
4745 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4746 } else {
4747 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4748 }
4749 } else if (second.IsDoubleStackSlot()) {
4750 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004751 } else {
4752 __ andq(first_reg, second.AsRegister<CpuRegister>());
4753 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004754 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004755 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004756 if (is_int32_value) {
4757 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4758 } else {
4759 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4760 }
4761 } else if (second.IsDoubleStackSlot()) {
4762 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004763 } else {
4764 __ orq(first_reg, second.AsRegister<CpuRegister>());
4765 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004766 } else {
4767 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004768 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004769 if (is_int32_value) {
4770 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4771 } else {
4772 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4773 }
4774 } else if (second.IsDoubleStackSlot()) {
4775 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004776 } else {
4777 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4778 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004779 }
4780 }
4781}
4782
Calin Juravleb1498f62015-02-16 13:13:29 +00004783void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4784 // Nothing to do, this should be removed during prepare for register allocator.
4785 UNUSED(instruction);
4786 LOG(FATAL) << "Unreachable";
4787}
4788
4789void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4790 // Nothing to do, this should be removed during prepare for register allocator.
4791 UNUSED(instruction);
4792 LOG(FATAL) << "Unreachable";
4793}
4794
Mark Mendell92e83bf2015-05-07 11:25:03 -04004795void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4796 if (value == 0) {
4797 __ xorl(dest, dest);
4798 } else if (value > 0 && IsInt<32>(value)) {
4799 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4800 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4801 } else {
4802 __ movq(dest, Immediate(value));
4803 }
4804}
4805
Mark Mendellf55c3e02015-03-26 21:07:46 -04004806void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4807 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004808 X86_64Assembler* assembler = GetAssembler();
4809 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004810 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4811 // byte values. If used for vectors at a later time, this will need to be
4812 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004813 assembler->Align(4, 0);
4814 constant_area_start_ = assembler->CodeSize();
4815 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004816 }
4817
4818 // And finish up.
4819 CodeGenerator::Finalize(allocator);
4820}
4821
4822/**
4823 * Class to handle late fixup of offsets into constant area.
4824 */
4825class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4826 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004827 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004828 : codegen_(codegen), offset_into_constant_area_(offset) {}
4829
4830 private:
4831 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4832 // Patch the correct offset for the instruction. We use the address of the
4833 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4834 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4835 int relative_position = constant_offset - pos;
4836
4837 // Patch in the right value.
4838 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4839 }
4840
Mark Mendell39dcf552015-04-09 20:42:42 -04004841 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004842
4843 // Location in constant area that the fixup refers to.
4844 int offset_into_constant_area_;
4845};
4846
4847Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4848 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4849 return Address::RIP(fixup);
4850}
4851
4852Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4853 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4854 return Address::RIP(fixup);
4855}
4856
4857Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4858 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4859 return Address::RIP(fixup);
4860}
4861
4862Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4863 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4864 return Address::RIP(fixup);
4865}
4866
Roland Levillain4d027112015-07-01 15:41:14 +01004867#undef __
4868
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004869} // namespace x86_64
4870} // namespace art