blob: aef4cd7ef52c8e98dbb52cb4535db5184c54eb2a [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"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Roland Levillain7cbd27f2016-08-11 23:53:33 +010054// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
55#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Andreas Gampe85b62f22015-09-09 13:15:38 -070058class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000060 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Alexandre Rames2ed20af2015-03-06 13:55:35 +000062 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000063 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000065 if (instruction_->CanThrowIntoCatchBlock()) {
66 // Live registers will be restored in the catch block if caught.
67 SaveLiveRegisters(codegen, instruction_->GetLocations());
68 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010069 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000070 instruction_,
71 instruction_->GetDexPc(),
72 this);
Roland Levillain888d0672015-11-23 18:53:50 +000073 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 }
75
Alexandre Rames8158f282015-08-07 10:26:17 +010076 bool IsFatal() const OVERRIDE { return true; }
77
Alexandre Rames9931f312015-06-19 14:47:01 +010078 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
79
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
82};
83
Andreas Gampe85b62f22015-09-09 13:15:38 -070084class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000085 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000086 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000089 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000091 if (instruction_->CanThrowIntoCatchBlock()) {
92 // Live registers will be restored in the catch block if caught.
93 SaveLiveRegisters(codegen, instruction_->GetLocations());
94 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010095 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
105};
106
Andreas Gampe85b62f22015-09-09 13:15:38 -0700107class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000109 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
110 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000114 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 if (is_div_) {
116 __ negl(cpu_reg_);
117 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400118 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 }
120
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000121 } else {
122 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 if (is_div_) {
124 __ negq(cpu_reg_);
125 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400126 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000128 }
Calin Juravled0d48522014-11-04 16:40:20 +0000129 __ jmp(GetExitLabel());
130 }
131
Alexandre Rames9931f312015-06-19 14:47:01 +0100132 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
133
Calin Juravled0d48522014-11-04 16:40:20 +0000134 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000136 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 const bool is_div_;
138 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000139};
140
Andreas Gampe85b62f22015-09-09 13:15:38 -0700141class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100143 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000147 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100149 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000150 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 if (successor_ == nullptr) {
152 __ jmp(GetReturnLabel());
153 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000154 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156 }
157
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 Label* GetReturnLabel() {
159 DCHECK(successor_ == nullptr);
160 return &return_label_;
161 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100163 HBasicBlock* GetSuccessor() const {
164 return successor_;
165 }
166
Alexandre Rames9931f312015-06-19 14:47:01 +0100167 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
168
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 Label return_label_;
172
173 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
174};
175
Andreas Gampe85b62f22015-09-09 13:15:38 -0700176class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000179 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000181 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100182 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000183 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000185 if (instruction_->CanThrowIntoCatchBlock()) {
186 // Live registers will be restored in the catch block if caught.
187 SaveLiveRegisters(codegen, instruction_->GetLocations());
188 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400189 // Are we using an array length from memory?
190 HInstruction* array_length = instruction_->InputAt(1);
191 Location length_loc = locations->InAt(1);
192 InvokeRuntimeCallingConvention calling_convention;
193 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
194 // Load the array length into our temporary.
195 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
196 Location array_loc = array_length->GetLocations()->InAt(0);
197 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
198 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
199 // Check for conflicts with index.
200 if (length_loc.Equals(locations->InAt(0))) {
201 // We know we aren't using parameter 2.
202 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
203 }
204 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
205 }
206
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000207 // We're moving two locations to locations that could overlap, so we need a parallel
208 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000209 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100210 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000211 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100212 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400213 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100214 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
215 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100216 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
217 ? kQuickThrowStringBounds
218 : kQuickThrowArrayBounds;
219 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100220 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000221 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100222 }
223
Alexandre Rames8158f282015-08-07 10:26:17 +0100224 bool IsFatal() const OVERRIDE { return true; }
225
Alexandre Rames9931f312015-06-19 14:47:01 +0100226 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
227
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100228 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100229 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
230};
231
Andreas Gampe85b62f22015-09-09 13:15:38 -0700232class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 LoadClassSlowPathX86_64(HLoadClass* cls,
235 HInstruction* at,
236 uint32_t dex_pc,
237 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000238 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
240 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100241
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000242 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000244 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100245 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100246
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000247 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100249 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100251 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000252 at_,
253 dex_pc_,
254 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000255 if (do_clinit_) {
256 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
257 } else {
258 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
259 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100260
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000261 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263 if (out.IsValid()) {
264 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000265 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 }
267
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000268 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100269 __ jmp(GetExitLabel());
270 }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
273
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100274 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000275 // The class this slow path will load.
276 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100277
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 // The instruction where this slow path is happening.
279 // (Might be the load class or an initialization check).
280 HInstruction* const at_;
281
282 // The dex PC of `at_`.
283 const uint32_t dex_pc_;
284
285 // Whether to initialize the class.
286 const bool do_clinit_;
287
288 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100289};
290
Andreas Gampe85b62f22015-09-09 13:15:38 -0700291class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000292 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000293 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000294
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000296 LocationSummary* locations = instruction_->GetLocations();
297 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
298
Roland Levillain0d5a2812015-11-13 10:07:31 +0000299 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000300 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000301 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000302
303 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000304 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
305 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100306 x86_64_codegen->InvokeRuntime(kQuickResolveString,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000307 instruction_,
308 instruction_->GetDexPc(),
309 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000310 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000311 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000313 __ jmp(GetExitLabel());
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
317
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000318 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000319 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
320};
321
Andreas Gampe85b62f22015-09-09 13:15:38 -0700322class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000324 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000325 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000327 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100329 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
330 : locations->Out();
331 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 DCHECK(instruction_->IsCheckCast()
333 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
Roland Levillain0d5a2812015-11-13 10:07:31 +0000335 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000338 if (!is_fatal_) {
339 SaveLiveRegisters(codegen, locations);
340 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000341
342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
344 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000347 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100350 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
351 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100354 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000355 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700356 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000357 } else {
358 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100359 x86_64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000360 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000361 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 if (!is_fatal_) {
364 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000365 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000367
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 RestoreLiveRegisters(codegen, locations);
369 __ jmp(GetExitLabel());
370 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000371 }
372
Alexandre Rames9931f312015-06-19 14:47:01 +0100373 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
374
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 bool IsFatal() const OVERRIDE { return is_fatal_; }
376
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000378 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379
380 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
381};
382
Andreas Gampe85b62f22015-09-09 13:15:38 -0700383class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 public:
Aart Bik42249c32016-01-07 15:33:50 -0800385 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000389 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 __ Bind(GetEntryLabel());
391 SaveLiveRegisters(codegen, instruction_->GetLocations());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100392 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000393 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
397
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
400};
401
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402class ArraySetSlowPathX86_64 : public SlowPathCode {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100405
406 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
407 LocationSummary* locations = instruction_->GetLocations();
408 __ Bind(GetEntryLabel());
409 SaveLiveRegisters(codegen, locations);
410
411 InvokeRuntimeCallingConvention calling_convention;
412 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
413 parallel_move.AddMove(
414 locations->InAt(0),
415 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
416 Primitive::kPrimNot,
417 nullptr);
418 parallel_move.AddMove(
419 locations->InAt(1),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
421 Primitive::kPrimInt,
422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(2),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
426 Primitive::kPrimNot,
427 nullptr);
428 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
429
Roland Levillain0d5a2812015-11-13 10:07:31 +0000430 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100431 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 RestoreLiveRegisters(codegen, locations);
434 __ jmp(GetExitLabel());
435 }
436
437 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
438
439 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100440 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
441};
442
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000443// Slow path marking an object during a read barrier.
444class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
445 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000446 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj, bool unpoison)
447 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000448 DCHECK(kEmitCompilerReadBarrier);
449 }
450
451 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
452
453 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
454 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100455 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000456 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100457 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000458 DCHECK(instruction_->IsInstanceFieldGet() ||
459 instruction_->IsStaticFieldGet() ||
460 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100461 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000462 instruction_->IsLoadClass() ||
463 instruction_->IsLoadString() ||
464 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100465 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100466 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
467 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000468 << "Unexpected instruction in read barrier marking slow path: "
469 << instruction_->DebugName();
470
471 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000472 if (unpoison_) {
473 // Object* ref = ref_addr->AsMirrorPtr()
474 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
475 }
Roland Levillain4359e612016-07-20 11:32:19 +0100476 // No need to save live registers; it's taken care of by the
477 // entrypoint. Also, there is no need to update the stack mask,
478 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000479 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100480 DCHECK_NE(reg, RSP);
481 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
482 // "Compact" slow path, saving two moves.
483 //
484 // Instead of using the standard runtime calling convention (input
485 // and output in R0):
486 //
487 // RDI <- obj
488 // RAX <- ReadBarrierMark(RDI)
489 // obj <- RAX
490 //
491 // we just use rX (the register holding `obj`) as input and output
492 // of a dedicated entrypoint:
493 //
494 // rX <- ReadBarrierMarkRegX(rX)
495 //
496 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700497 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100498 // This runtime call does not require a stack map.
499 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000500 __ jmp(GetExitLabel());
501 }
502
503 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000504 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000505 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000506
507 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
508};
509
Roland Levillain0d5a2812015-11-13 10:07:31 +0000510// Slow path generating a read barrier for a heap reference.
511class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
512 public:
513 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
514 Location out,
515 Location ref,
516 Location obj,
517 uint32_t offset,
518 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000519 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000520 out_(out),
521 ref_(ref),
522 obj_(obj),
523 offset_(offset),
524 index_(index) {
525 DCHECK(kEmitCompilerReadBarrier);
526 // If `obj` is equal to `out` or `ref`, it means the initial
527 // object has been overwritten by (or after) the heap object
528 // reference load to be instrumented, e.g.:
529 //
530 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000531 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000532 //
533 // In that case, we have lost the information about the original
534 // object, and the emitted read barrier cannot work properly.
535 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
536 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
537}
538
539 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
540 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
541 LocationSummary* locations = instruction_->GetLocations();
542 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
543 DCHECK(locations->CanCall());
544 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100545 DCHECK(instruction_->IsInstanceFieldGet() ||
546 instruction_->IsStaticFieldGet() ||
547 instruction_->IsArrayGet() ||
548 instruction_->IsInstanceOf() ||
549 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100550 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000551 << "Unexpected instruction in read barrier for heap reference slow path: "
552 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000553
554 __ Bind(GetEntryLabel());
555 SaveLiveRegisters(codegen, locations);
556
557 // We may have to change the index's value, but as `index_` is a
558 // constant member (like other "inputs" of this slow path),
559 // introduce a copy of it, `index`.
560 Location index = index_;
561 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100562 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000563 if (instruction_->IsArrayGet()) {
564 // Compute real offset and store it in index_.
565 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
566 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
567 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
568 // We are about to change the value of `index_reg` (see the
569 // calls to art::x86_64::X86_64Assembler::shll and
570 // art::x86_64::X86_64Assembler::AddImmediate below), but it
571 // has not been saved by the previous call to
572 // art::SlowPathCode::SaveLiveRegisters, as it is a
573 // callee-save register --
574 // art::SlowPathCode::SaveLiveRegisters does not consider
575 // callee-save registers, as it has been designed with the
576 // assumption that callee-save registers are supposed to be
577 // handled by the called function. So, as a callee-save
578 // register, `index_reg` _would_ eventually be saved onto
579 // the stack, but it would be too late: we would have
580 // changed its value earlier. Therefore, we manually save
581 // it here into another freely available register,
582 // `free_reg`, chosen of course among the caller-save
583 // registers (as a callee-save `free_reg` register would
584 // exhibit the same problem).
585 //
586 // Note we could have requested a temporary register from
587 // the register allocator instead; but we prefer not to, as
588 // this is a slow path, and we know we can find a
589 // caller-save register that is available.
590 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
591 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
592 index_reg = free_reg;
593 index = Location::RegisterLocation(index_reg);
594 } else {
595 // The initial register stored in `index_` has already been
596 // saved in the call to art::SlowPathCode::SaveLiveRegisters
597 // (as it is not a callee-save register), so we can freely
598 // use it.
599 }
600 // Shifting the index value contained in `index_reg` by the
601 // scale factor (2) cannot overflow in practice, as the
602 // runtime is unable to allocate object arrays with a size
603 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
604 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
605 static_assert(
606 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
607 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
608 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
609 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100610 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
611 // intrinsics, `index_` is not shifted by a scale factor of 2
612 // (as in the case of ArrayGet), as it is actually an offset
613 // to an object field within an object.
614 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000615 DCHECK(instruction_->GetLocations()->Intrinsified());
616 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
617 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
618 << instruction_->AsInvoke()->GetIntrinsic();
619 DCHECK_EQ(offset_, 0U);
620 DCHECK(index_.IsRegister());
621 }
622 }
623
624 // We're moving two or three locations to locations that could
625 // overlap, so we need a parallel move resolver.
626 InvokeRuntimeCallingConvention calling_convention;
627 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
628 parallel_move.AddMove(ref_,
629 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
630 Primitive::kPrimNot,
631 nullptr);
632 parallel_move.AddMove(obj_,
633 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
634 Primitive::kPrimNot,
635 nullptr);
636 if (index.IsValid()) {
637 parallel_move.AddMove(index,
638 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
639 Primitive::kPrimInt,
640 nullptr);
641 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
642 } else {
643 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
644 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
645 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100646 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000647 instruction_,
648 instruction_->GetDexPc(),
649 this);
650 CheckEntrypointTypes<
651 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
652 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
653
654 RestoreLiveRegisters(codegen, locations);
655 __ jmp(GetExitLabel());
656 }
657
658 const char* GetDescription() const OVERRIDE {
659 return "ReadBarrierForHeapReferenceSlowPathX86_64";
660 }
661
662 private:
663 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
664 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
665 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
666 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
667 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
668 return static_cast<CpuRegister>(i);
669 }
670 }
671 // We shall never fail to find a free caller-save register, as
672 // there are more than two core caller-save registers on x86-64
673 // (meaning it is possible to find one which is different from
674 // `ref` and `obj`).
675 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
676 LOG(FATAL) << "Could not find a free caller-save register";
677 UNREACHABLE();
678 }
679
Roland Levillain0d5a2812015-11-13 10:07:31 +0000680 const Location out_;
681 const Location ref_;
682 const Location obj_;
683 const uint32_t offset_;
684 // An additional location containing an index to an array.
685 // Only used for HArrayGet and the UnsafeGetObject &
686 // UnsafeGetObjectVolatile intrinsics.
687 const Location index_;
688
689 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
690};
691
692// Slow path generating a read barrier for a GC root.
693class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
694 public:
695 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000696 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000697 DCHECK(kEmitCompilerReadBarrier);
698 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000699
700 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
701 LocationSummary* locations = instruction_->GetLocations();
702 DCHECK(locations->CanCall());
703 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000704 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
705 << "Unexpected instruction in read barrier for GC root slow path: "
706 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707
708 __ Bind(GetEntryLabel());
709 SaveLiveRegisters(codegen, locations);
710
711 InvokeRuntimeCallingConvention calling_convention;
712 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
713 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100714 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000715 instruction_,
716 instruction_->GetDexPc(),
717 this);
718 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
719 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
720
721 RestoreLiveRegisters(codegen, locations);
722 __ jmp(GetExitLabel());
723 }
724
725 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
726
727 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000728 const Location out_;
729 const Location root_;
730
731 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
732};
733
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100734#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100735// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
736#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100737
Roland Levillain4fa13f62015-07-06 18:11:54 +0100738inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700739 switch (cond) {
740 case kCondEQ: return kEqual;
741 case kCondNE: return kNotEqual;
742 case kCondLT: return kLess;
743 case kCondLE: return kLessEqual;
744 case kCondGT: return kGreater;
745 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700746 case kCondB: return kBelow;
747 case kCondBE: return kBelowEqual;
748 case kCondA: return kAbove;
749 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700750 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100751 LOG(FATAL) << "Unreachable";
752 UNREACHABLE();
753}
754
Aart Bike9f37602015-10-09 11:15:55 -0700755// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100756inline Condition X86_64FPCondition(IfCondition cond) {
757 switch (cond) {
758 case kCondEQ: return kEqual;
759 case kCondNE: return kNotEqual;
760 case kCondLT: return kBelow;
761 case kCondLE: return kBelowEqual;
762 case kCondGT: return kAbove;
763 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700764 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100765 };
766 LOG(FATAL) << "Unreachable";
767 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700768}
769
Vladimir Markodc151b22015-10-15 18:02:30 +0100770HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
771 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
772 MethodReference target_method ATTRIBUTE_UNUSED) {
773 switch (desired_dispatch_info.code_ptr_location) {
774 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
775 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
776 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
777 return HInvokeStaticOrDirect::DispatchInfo {
778 desired_dispatch_info.method_load_kind,
779 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
780 desired_dispatch_info.method_load_data,
781 0u
782 };
783 default:
784 return desired_dispatch_info;
785 }
786}
787
Serguei Katkov288c7a82016-05-16 11:53:15 +0600788Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
789 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800790 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000791 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
792 switch (invoke->GetMethodLoadKind()) {
793 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
794 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000795 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000796 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000797 break;
798 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000799 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000800 break;
801 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
802 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
803 break;
804 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
805 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
806 method_patches_.emplace_back(invoke->GetTargetMethod());
807 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
808 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000809 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000810 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000811 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000812 // Bind a new fixup label at the end of the "movl" insn.
813 uint32_t offset = invoke->GetDexCacheArrayOffset();
814 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000815 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000816 }
Vladimir Marko58155012015-08-19 12:49:41 +0000817 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000818 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000819 Register method_reg;
820 CpuRegister reg = temp.AsRegister<CpuRegister>();
821 if (current_method.IsRegister()) {
822 method_reg = current_method.AsRegister<Register>();
823 } else {
824 DCHECK(invoke->GetLocations()->Intrinsified());
825 DCHECK(!current_method.IsValid());
826 method_reg = reg.AsRegister();
827 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
828 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100830 __ movq(reg,
831 Address(CpuRegister(method_reg),
832 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100833 // temp = temp[index_in_cache];
834 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
835 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000836 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
837 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100838 }
Vladimir Marko58155012015-08-19 12:49:41 +0000839 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600840 return callee_method;
841}
842
843void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
844 Location temp) {
845 // All registers are assumed to be correctly set up.
846 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000847
848 switch (invoke->GetCodePtrLocation()) {
849 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
850 __ call(&frame_entry_label_);
851 break;
852 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
853 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
854 Label* label = &relative_call_patches_.back().label;
855 __ call(label); // Bind to the patch label, override at link time.
856 __ Bind(label); // Bind the label at the end of the "call" insn.
857 break;
858 }
859 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
860 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100861 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
862 LOG(FATAL) << "Unsupported";
863 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000864 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
865 // (callee_method + offset_of_quick_compiled_code)()
866 __ call(Address(callee_method.AsRegister<CpuRegister>(),
867 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700868 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000869 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000870 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800871
872 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800873}
874
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000875void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
876 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
877 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
878 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000879
880 // Use the calling convention instead of the location of the receiver, as
881 // intrinsics may have put the receiver in a different register. In the intrinsics
882 // slow path, the arguments have been moved to the right place, so here we are
883 // guaranteed that the receiver is the first register of the calling convention.
884 InvokeDexCallingConvention calling_convention;
885 Register receiver = calling_convention.GetRegisterAt(0);
886
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000887 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000888 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000889 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000890 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000891 // Instead of simply (possibly) unpoisoning `temp` here, we should
892 // emit a read barrier for the previous class reference load.
893 // However this is not required in practice, as this is an
894 // intermediate/temporary reference and because the current
895 // concurrent copying collector keeps the from-space memory
896 // intact/accessible until the end of the marking phase (the
897 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000898 __ MaybeUnpoisonHeapReference(temp);
899 // temp = temp->GetMethodAt(method_offset);
900 __ movq(temp, Address(temp, method_offset));
901 // call temp->GetEntryPoint();
902 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700903 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000904}
905
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000906void CodeGeneratorX86_64::RecordSimplePatch() {
907 if (GetCompilerOptions().GetIncludePatchInformation()) {
908 simple_patches_.emplace_back();
909 __ Bind(&simple_patches_.back());
910 }
911}
912
913void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
914 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
915 __ Bind(&string_patches_.back().label);
916}
917
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100918void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
919 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
920 __ Bind(&type_patches_.back().label);
921}
922
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000923Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
924 uint32_t element_offset) {
925 // Add a patch entry and return the label.
926 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
927 return &pc_relative_dex_cache_patches_.back().label;
928}
929
Vladimir Marko58155012015-08-19 12:49:41 +0000930void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
931 DCHECK(linker_patches->empty());
932 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000933 method_patches_.size() +
934 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000935 pc_relative_dex_cache_patches_.size() +
936 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100937 string_patches_.size() +
938 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000939 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000940 // The label points to the end of the "movl" insn but the literal offset for method
941 // patch needs to point to the embedded constant which occupies the last 4 bytes.
942 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000943 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000944 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000945 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
946 info.target_method.dex_file,
947 info.target_method.dex_method_index));
948 }
949 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000950 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000951 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
952 info.target_method.dex_file,
953 info.target_method.dex_method_index));
954 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000955 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
956 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000957 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
958 &info.target_dex_file,
959 info.label.Position(),
960 info.element_offset));
961 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000962 for (const Label& label : simple_patches_) {
963 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
964 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
965 }
966 for (const StringPatchInfo<Label>& info : string_patches_) {
967 // These are always PC-relative, see GetSupportedLoadStringKind().
968 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
969 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
970 &info.dex_file,
971 info.label.Position(),
972 info.string_index));
973 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100974 for (const TypePatchInfo<Label>& info : type_patches_) {
975 // These are always PC-relative, see GetSupportedLoadClassKind().
976 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
977 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
978 &info.dex_file,
979 info.label.Position(),
980 info.type_index));
981 }
Vladimir Marko58155012015-08-19 12:49:41 +0000982}
983
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100984void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100985 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100986}
987
988void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100989 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100990}
991
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100992size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
993 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
994 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100995}
996
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100997size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
998 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
999 return kX86_64WordSize;
1000}
1001
1002size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1003 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1004 return kX86_64WordSize;
1005}
1006
1007size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1008 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1009 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001010}
1011
Calin Juravle175dc732015-08-25 15:42:32 +01001012void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1013 HInstruction* instruction,
1014 uint32_t dex_pc,
1015 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001016 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001017 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1018 if (EntrypointRequiresStackMap(entrypoint)) {
1019 RecordPcInfo(instruction, dex_pc, slow_path);
1020 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001021}
1022
Roland Levillaindec8f632016-07-22 17:10:06 +01001023void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1024 HInstruction* instruction,
1025 SlowPathCode* slow_path) {
1026 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001027 GenerateInvokeRuntime(entry_point_offset);
1028}
1029
1030void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001031 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1032}
1033
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001034static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001035// Use a fake return address register to mimic Quick.
1036static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001037CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001038 const X86_64InstructionSetFeatures& isa_features,
1039 const CompilerOptions& compiler_options,
1040 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001041 : CodeGenerator(graph,
1042 kNumberOfCpuRegisters,
1043 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001044 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001045 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1046 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001047 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001048 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1049 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001050 compiler_options,
1051 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001052 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001053 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001054 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001055 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001056 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001057 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001058 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001059 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1060 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001061 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001062 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1063 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001064 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001065 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001066 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1067}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001069InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1070 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001071 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001072 assembler_(codegen->GetAssembler()),
1073 codegen_(codegen) {}
1074
David Brazdil58282f42016-01-14 12:45:10 +00001075void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001076 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001077 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001078
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001079 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001080 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081}
1082
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001083static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001084 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001085}
David Srbecky9d8606d2015-04-12 09:35:32 +01001086
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001087static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001088 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001089}
1090
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001092 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001093 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001094 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001095 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001096 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001097
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001098 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001099 __ testq(CpuRegister(RAX), Address(
1100 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001101 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001102 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001103
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001104 if (HasEmptyFrame()) {
1105 return;
1106 }
1107
Nicolas Geoffray98893962015-01-21 12:32:32 +00001108 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001109 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001110 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001111 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001112 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1113 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001114 }
1115 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001116
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001117 int adjust = GetFrameSize() - GetCoreSpillSize();
1118 __ subq(CpuRegister(RSP), Immediate(adjust));
1119 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001120 uint32_t xmm_spill_location = GetFpuSpillStart();
1121 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001122
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001123 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1124 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001125 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1126 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1127 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001128 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001129 }
1130
Mathieu Chartiere401d142015-04-22 13:56:20 -07001131 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001132 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001133}
1134
1135void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001136 __ cfi().RememberState();
1137 if (!HasEmptyFrame()) {
1138 uint32_t xmm_spill_location = GetFpuSpillStart();
1139 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1140 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1141 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1142 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1143 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1144 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1145 }
1146 }
1147
1148 int adjust = GetFrameSize() - GetCoreSpillSize();
1149 __ addq(CpuRegister(RSP), Immediate(adjust));
1150 __ cfi().AdjustCFAOffset(-adjust);
1151
1152 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1153 Register reg = kCoreCalleeSaves[i];
1154 if (allocated_registers_.ContainsCoreRegister(reg)) {
1155 __ popq(CpuRegister(reg));
1156 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1157 __ cfi().Restore(DWARFReg(reg));
1158 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001159 }
1160 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001161 __ ret();
1162 __ cfi().RestoreState();
1163 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001164}
1165
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001166void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1167 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001168}
1169
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001170void CodeGeneratorX86_64::Move(Location destination, Location source) {
1171 if (source.Equals(destination)) {
1172 return;
1173 }
1174 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001175 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001176 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001177 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001178 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001179 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001181 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1182 } else if (source.IsConstant()) {
1183 HConstant* constant = source.GetConstant();
1184 if (constant->IsLongConstant()) {
1185 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1186 } else {
1187 Load32BitValue(dest, GetInt32ValueOf(constant));
1188 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001189 } else {
1190 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001191 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 }
1193 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001194 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001196 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001198 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1199 } else if (source.IsConstant()) {
1200 HConstant* constant = source.GetConstant();
1201 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1202 if (constant->IsFloatConstant()) {
1203 Load32BitValue(dest, static_cast<int32_t>(value));
1204 } else {
1205 Load64BitValue(dest, value);
1206 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001208 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 } else {
1210 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001211 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001212 }
1213 } else if (destination.IsStackSlot()) {
1214 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 } else if (source.IsFpuRegister()) {
1218 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001220 } else if (source.IsConstant()) {
1221 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001222 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001223 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001225 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001226 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1227 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 }
1229 } else {
1230 DCHECK(destination.IsDoubleStackSlot());
1231 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else if (source.IsFpuRegister()) {
1235 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001237 } else if (source.IsConstant()) {
1238 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001239 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001240 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001241 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001242 } else {
1243 DCHECK(constant->IsLongConstant());
1244 value = constant->AsLongConstant()->GetValue();
1245 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001246 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247 } else {
1248 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001249 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1250 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251 }
1252 }
1253}
1254
Calin Juravle175dc732015-08-25 15:42:32 +01001255void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1256 DCHECK(location.IsRegister());
1257 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1258}
1259
Calin Juravlee460d1d2015-09-29 04:52:17 +01001260void CodeGeneratorX86_64::MoveLocation(
1261 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1262 Move(dst, src);
1263}
1264
1265void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1266 if (location.IsRegister()) {
1267 locations->AddTemp(location);
1268 } else {
1269 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1270 }
1271}
1272
David Brazdilfc6a86a2015-06-26 10:33:45 +00001273void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001274 DCHECK(!successor->IsExitBlock());
1275
1276 HBasicBlock* block = got->GetBlock();
1277 HInstruction* previous = got->GetPrevious();
1278
1279 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001280 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001281 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1282 return;
1283 }
1284
1285 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1286 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1287 }
1288 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001289 __ jmp(codegen_->GetLabelOf(successor));
1290 }
1291}
1292
David Brazdilfc6a86a2015-06-26 10:33:45 +00001293void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1294 got->SetLocations(nullptr);
1295}
1296
1297void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1298 HandleGoto(got, got->GetSuccessor());
1299}
1300
1301void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1302 try_boundary->SetLocations(nullptr);
1303}
1304
1305void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1306 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1307 if (!successor->IsExitBlock()) {
1308 HandleGoto(try_boundary, successor);
1309 }
1310}
1311
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001312void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1313 exit->SetLocations(nullptr);
1314}
1315
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001316void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001317}
1318
Mark Mendell152408f2015-12-31 12:28:50 -05001319template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001320void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001321 LabelType* true_label,
1322 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001323 if (cond->IsFPConditionTrueIfNaN()) {
1324 __ j(kUnordered, true_label);
1325 } else if (cond->IsFPConditionFalseIfNaN()) {
1326 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001327 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001328 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001329}
1330
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001331void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001332 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001333
Mark Mendellc4701932015-04-10 13:18:51 -04001334 Location left = locations->InAt(0);
1335 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001336 Primitive::Type type = condition->InputAt(0)->GetType();
1337 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001338 case Primitive::kPrimBoolean:
1339 case Primitive::kPrimByte:
1340 case Primitive::kPrimChar:
1341 case Primitive::kPrimShort:
1342 case Primitive::kPrimInt:
1343 case Primitive::kPrimNot: {
1344 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1345 if (right.IsConstant()) {
1346 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1347 if (value == 0) {
1348 __ testl(left_reg, left_reg);
1349 } else {
1350 __ cmpl(left_reg, Immediate(value));
1351 }
1352 } else if (right.IsStackSlot()) {
1353 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1354 } else {
1355 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1356 }
1357 break;
1358 }
Mark Mendellc4701932015-04-10 13:18:51 -04001359 case Primitive::kPrimLong: {
1360 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1361 if (right.IsConstant()) {
1362 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001363 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001364 } else if (right.IsDoubleStackSlot()) {
1365 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1366 } else {
1367 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1368 }
Mark Mendellc4701932015-04-10 13:18:51 -04001369 break;
1370 }
1371 case Primitive::kPrimFloat: {
1372 if (right.IsFpuRegister()) {
1373 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1374 } else if (right.IsConstant()) {
1375 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1376 codegen_->LiteralFloatAddress(
1377 right.GetConstant()->AsFloatConstant()->GetValue()));
1378 } else {
1379 DCHECK(right.IsStackSlot());
1380 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1381 Address(CpuRegister(RSP), right.GetStackIndex()));
1382 }
Mark Mendellc4701932015-04-10 13:18:51 -04001383 break;
1384 }
1385 case Primitive::kPrimDouble: {
1386 if (right.IsFpuRegister()) {
1387 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1388 } else if (right.IsConstant()) {
1389 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1390 codegen_->LiteralDoubleAddress(
1391 right.GetConstant()->AsDoubleConstant()->GetValue()));
1392 } else {
1393 DCHECK(right.IsDoubleStackSlot());
1394 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1395 Address(CpuRegister(RSP), right.GetStackIndex()));
1396 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001397 break;
1398 }
1399 default:
1400 LOG(FATAL) << "Unexpected condition type " << type;
1401 }
1402}
1403
1404template<class LabelType>
1405void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1406 LabelType* true_target_in,
1407 LabelType* false_target_in) {
1408 // Generated branching requires both targets to be explicit. If either of the
1409 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1410 LabelType fallthrough_target;
1411 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1412 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1413
1414 // Generate the comparison to set the CC.
1415 GenerateCompareTest(condition);
1416
1417 // Now generate the correct jump(s).
1418 Primitive::Type type = condition->InputAt(0)->GetType();
1419 switch (type) {
1420 case Primitive::kPrimLong: {
1421 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1422 break;
1423 }
1424 case Primitive::kPrimFloat: {
1425 GenerateFPJumps(condition, true_target, false_target);
1426 break;
1427 }
1428 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001429 GenerateFPJumps(condition, true_target, false_target);
1430 break;
1431 }
1432 default:
1433 LOG(FATAL) << "Unexpected condition type " << type;
1434 }
1435
David Brazdil0debae72015-11-12 18:37:00 +00001436 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001437 __ jmp(false_target);
1438 }
David Brazdil0debae72015-11-12 18:37:00 +00001439
1440 if (fallthrough_target.IsLinked()) {
1441 __ Bind(&fallthrough_target);
1442 }
Mark Mendellc4701932015-04-10 13:18:51 -04001443}
1444
David Brazdil0debae72015-11-12 18:37:00 +00001445static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1446 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1447 // are set only strictly before `branch`. We can't use the eflags on long
1448 // conditions if they are materialized due to the complex branching.
1449 return cond->IsCondition() &&
1450 cond->GetNext() == branch &&
1451 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1452}
1453
Mark Mendell152408f2015-12-31 12:28:50 -05001454template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001455void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001456 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001457 LabelType* true_target,
1458 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001459 HInstruction* cond = instruction->InputAt(condition_input_index);
1460
1461 if (true_target == nullptr && false_target == nullptr) {
1462 // Nothing to do. The code always falls through.
1463 return;
1464 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001465 // Constant condition, statically compared against "true" (integer value 1).
1466 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001467 if (true_target != nullptr) {
1468 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001469 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001470 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001471 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001472 if (false_target != nullptr) {
1473 __ jmp(false_target);
1474 }
1475 }
1476 return;
1477 }
1478
1479 // The following code generates these patterns:
1480 // (1) true_target == nullptr && false_target != nullptr
1481 // - opposite condition true => branch to false_target
1482 // (2) true_target != nullptr && false_target == nullptr
1483 // - condition true => branch to true_target
1484 // (3) true_target != nullptr && false_target != nullptr
1485 // - condition true => branch to true_target
1486 // - branch to false_target
1487 if (IsBooleanValueOrMaterializedCondition(cond)) {
1488 if (AreEflagsSetFrom(cond, instruction)) {
1489 if (true_target == nullptr) {
1490 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1491 } else {
1492 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1493 }
1494 } else {
1495 // Materialized condition, compare against 0.
1496 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1497 if (lhs.IsRegister()) {
1498 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1499 } else {
1500 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1501 }
1502 if (true_target == nullptr) {
1503 __ j(kEqual, false_target);
1504 } else {
1505 __ j(kNotEqual, true_target);
1506 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001507 }
1508 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001509 // Condition has not been materialized, use its inputs as the
1510 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001511 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001512
David Brazdil0debae72015-11-12 18:37:00 +00001513 // If this is a long or FP comparison that has been folded into
1514 // the HCondition, generate the comparison directly.
1515 Primitive::Type type = condition->InputAt(0)->GetType();
1516 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1517 GenerateCompareTestAndBranch(condition, true_target, false_target);
1518 return;
1519 }
1520
1521 Location lhs = condition->GetLocations()->InAt(0);
1522 Location rhs = condition->GetLocations()->InAt(1);
1523 if (rhs.IsRegister()) {
1524 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1525 } else if (rhs.IsConstant()) {
1526 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001527 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001528 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001529 __ cmpl(lhs.AsRegister<CpuRegister>(),
1530 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1531 }
1532 if (true_target == nullptr) {
1533 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1534 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001535 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001536 }
Dave Allison20dfc792014-06-16 20:44:29 -07001537 }
David Brazdil0debae72015-11-12 18:37:00 +00001538
1539 // If neither branch falls through (case 3), the conditional branch to `true_target`
1540 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1541 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001542 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001543 }
1544}
1545
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001546void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1548 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001549 locations->SetInAt(0, Location::Any());
1550 }
1551}
1552
1553void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001554 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1555 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1556 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1557 nullptr : codegen_->GetLabelOf(true_successor);
1558 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1559 nullptr : codegen_->GetLabelOf(false_successor);
1560 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001561}
1562
1563void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1564 LocationSummary* locations = new (GetGraph()->GetArena())
1565 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001566 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001567 locations->SetInAt(0, Location::Any());
1568 }
1569}
1570
1571void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001572 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001573 GenerateTestAndBranch<Label>(deoptimize,
1574 /* condition_input_index */ 0,
1575 slow_path->GetEntryLabel(),
1576 /* false_target */ nullptr);
1577}
1578
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001579static bool SelectCanUseCMOV(HSelect* select) {
1580 // There are no conditional move instructions for XMMs.
1581 if (Primitive::IsFloatingPointType(select->GetType())) {
1582 return false;
1583 }
1584
1585 // A FP condition doesn't generate the single CC that we need.
1586 HInstruction* condition = select->GetCondition();
1587 if (condition->IsCondition() &&
1588 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1589 return false;
1590 }
1591
1592 // We can generate a CMOV for this Select.
1593 return true;
1594}
1595
David Brazdil74eb1b22015-12-14 11:44:01 +00001596void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1598 if (Primitive::IsFloatingPointType(select->GetType())) {
1599 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001600 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001601 } else {
1602 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001603 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001604 if (select->InputAt(1)->IsConstant()) {
1605 locations->SetInAt(1, Location::RequiresRegister());
1606 } else {
1607 locations->SetInAt(1, Location::Any());
1608 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001609 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001610 locations->SetInAt(1, Location::Any());
1611 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001612 }
1613 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1614 locations->SetInAt(2, Location::RequiresRegister());
1615 }
1616 locations->SetOut(Location::SameAsFirstInput());
1617}
1618
1619void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1620 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001621 if (SelectCanUseCMOV(select)) {
1622 // If both the condition and the source types are integer, we can generate
1623 // a CMOV to implement Select.
1624 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001625 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001626 DCHECK(locations->InAt(0).Equals(locations->Out()));
1627
1628 HInstruction* select_condition = select->GetCondition();
1629 Condition cond = kNotEqual;
1630
1631 // Figure out how to test the 'condition'.
1632 if (select_condition->IsCondition()) {
1633 HCondition* condition = select_condition->AsCondition();
1634 if (!condition->IsEmittedAtUseSite()) {
1635 // This was a previously materialized condition.
1636 // Can we use the existing condition code?
1637 if (AreEflagsSetFrom(condition, select)) {
1638 // Materialization was the previous instruction. Condition codes are right.
1639 cond = X86_64IntegerCondition(condition->GetCondition());
1640 } else {
1641 // No, we have to recreate the condition code.
1642 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1643 __ testl(cond_reg, cond_reg);
1644 }
1645 } else {
1646 GenerateCompareTest(condition);
1647 cond = X86_64IntegerCondition(condition->GetCondition());
1648 }
1649 } else {
1650 // Must be a boolean condition, which needs to be compared to 0.
1651 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1652 __ testl(cond_reg, cond_reg);
1653 }
1654
1655 // If the condition is true, overwrite the output, which already contains false.
1656 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001657 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1658 if (value_true_loc.IsRegister()) {
1659 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1660 } else {
1661 __ cmov(cond,
1662 value_false,
1663 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1664 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001665 } else {
1666 NearLabel false_target;
1667 GenerateTestAndBranch<NearLabel>(select,
1668 /* condition_input_index */ 2,
1669 /* true_target */ nullptr,
1670 &false_target);
1671 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1672 __ Bind(&false_target);
1673 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001674}
1675
David Srbecky0cf44932015-12-09 14:09:59 +00001676void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1677 new (GetGraph()->GetArena()) LocationSummary(info);
1678}
1679
David Srbeckyd28f4a02016-03-14 17:14:24 +00001680void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1681 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001682}
1683
1684void CodeGeneratorX86_64::GenerateNop() {
1685 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001686}
1687
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001688void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001689 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001690 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001691 // Handle the long/FP comparisons made in instruction simplification.
1692 switch (cond->InputAt(0)->GetType()) {
1693 case Primitive::kPrimLong:
1694 locations->SetInAt(0, Location::RequiresRegister());
1695 locations->SetInAt(1, Location::Any());
1696 break;
1697 case Primitive::kPrimFloat:
1698 case Primitive::kPrimDouble:
1699 locations->SetInAt(0, Location::RequiresFpuRegister());
1700 locations->SetInAt(1, Location::Any());
1701 break;
1702 default:
1703 locations->SetInAt(0, Location::RequiresRegister());
1704 locations->SetInAt(1, Location::Any());
1705 break;
1706 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001707 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001708 locations->SetOut(Location::RequiresRegister());
1709 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001710}
1711
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001712void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001713 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001714 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001715 }
Mark Mendellc4701932015-04-10 13:18:51 -04001716
1717 LocationSummary* locations = cond->GetLocations();
1718 Location lhs = locations->InAt(0);
1719 Location rhs = locations->InAt(1);
1720 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001721 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001722
1723 switch (cond->InputAt(0)->GetType()) {
1724 default:
1725 // Integer case.
1726
1727 // Clear output register: setcc only sets the low byte.
1728 __ xorl(reg, reg);
1729
1730 if (rhs.IsRegister()) {
1731 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1732 } else if (rhs.IsConstant()) {
1733 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001734 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001735 } else {
1736 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1737 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001738 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 return;
1740 case Primitive::kPrimLong:
1741 // Clear output register: setcc only sets the low byte.
1742 __ xorl(reg, reg);
1743
1744 if (rhs.IsRegister()) {
1745 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1746 } else if (rhs.IsConstant()) {
1747 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001748 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001749 } else {
1750 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1751 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001752 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001753 return;
1754 case Primitive::kPrimFloat: {
1755 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1756 if (rhs.IsConstant()) {
1757 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1758 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1759 } else if (rhs.IsStackSlot()) {
1760 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1761 } else {
1762 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1763 }
1764 GenerateFPJumps(cond, &true_label, &false_label);
1765 break;
1766 }
1767 case Primitive::kPrimDouble: {
1768 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1769 if (rhs.IsConstant()) {
1770 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1771 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1772 } else if (rhs.IsDoubleStackSlot()) {
1773 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1774 } else {
1775 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1776 }
1777 GenerateFPJumps(cond, &true_label, &false_label);
1778 break;
1779 }
1780 }
1781
1782 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001783 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001784
Roland Levillain4fa13f62015-07-06 18:11:54 +01001785 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001786 __ Bind(&false_label);
1787 __ xorl(reg, reg);
1788 __ jmp(&done_label);
1789
Roland Levillain4fa13f62015-07-06 18:11:54 +01001790 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001791 __ Bind(&true_label);
1792 __ movl(reg, Immediate(1));
1793 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001810}
1811
1812void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001814}
1815
1816void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001818}
1819
1820void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001822}
1823
1824void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001826}
1827
1828void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001830}
1831
1832void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001834}
1835
1836void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001838}
1839
1840void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001842}
1843
Aart Bike9f37602015-10-09 11:15:55 -07001844void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001846}
1847
1848void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001849 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001850}
1851
1852void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001853 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001854}
1855
1856void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001857 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001858}
1859
1860void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001861 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001862}
1863
1864void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001865 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001866}
1867
1868void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001869 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001870}
1871
1872void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001873 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001874}
1875
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001876void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001877 LocationSummary* locations =
1878 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001879 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001880 case Primitive::kPrimBoolean:
1881 case Primitive::kPrimByte:
1882 case Primitive::kPrimShort:
1883 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001884 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001885 case Primitive::kPrimLong: {
1886 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001887 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1889 break;
1890 }
1891 case Primitive::kPrimFloat:
1892 case Primitive::kPrimDouble: {
1893 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001894 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001895 locations->SetOut(Location::RequiresRegister());
1896 break;
1897 }
1898 default:
1899 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1900 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001901}
1902
1903void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001904 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001905 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001906 Location left = locations->InAt(0);
1907 Location right = locations->InAt(1);
1908
Mark Mendell0c9497d2015-08-21 09:30:05 -04001909 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001910 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001911 Condition less_cond = kLess;
1912
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001914 case Primitive::kPrimBoolean:
1915 case Primitive::kPrimByte:
1916 case Primitive::kPrimShort:
1917 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001918 case Primitive::kPrimInt: {
1919 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1920 if (right.IsConstant()) {
1921 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1922 codegen_->Compare32BitValue(left_reg, value);
1923 } else if (right.IsStackSlot()) {
1924 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1925 } else {
1926 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1927 }
1928 break;
1929 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001931 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1932 if (right.IsConstant()) {
1933 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001934 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001935 } else if (right.IsDoubleStackSlot()) {
1936 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001937 } else {
1938 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1939 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001940 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001941 }
1942 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001943 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1944 if (right.IsConstant()) {
1945 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1946 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1947 } else if (right.IsStackSlot()) {
1948 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1949 } else {
1950 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1951 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001952 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001953 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001954 break;
1955 }
1956 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001957 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1958 if (right.IsConstant()) {
1959 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1960 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1961 } else if (right.IsDoubleStackSlot()) {
1962 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1963 } else {
1964 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1965 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001966 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001967 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001968 break;
1969 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001970 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001971 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001972 }
Aart Bika19616e2016-02-01 18:57:58 -08001973
Calin Juravleddb7df22014-11-25 20:56:51 +00001974 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001975 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001976 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001977
Calin Juravle91debbc2014-11-26 19:01:09 +00001978 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001979 __ movl(out, Immediate(1));
1980 __ jmp(&done);
1981
1982 __ Bind(&less);
1983 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001984
1985 __ Bind(&done);
1986}
1987
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001988void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001989 LocationSummary* locations =
1990 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001991 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001992}
1993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001994void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001995 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001996}
1997
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001998void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2001 locations->SetOut(Location::ConstantLocation(constant));
2002}
2003
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002004void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002005 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002006}
2007
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002011 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002012}
2013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002014void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002015 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016}
2017
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002018void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2021 locations->SetOut(Location::ConstantLocation(constant));
2022}
2023
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002024void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002025 // Will be generated at use site.
2026}
2027
2028void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2029 LocationSummary* locations =
2030 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2031 locations->SetOut(Location::ConstantLocation(constant));
2032}
2033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2035 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002036 // Will be generated at use site.
2037}
2038
Calin Juravle27df7582015-04-17 19:12:31 +01002039void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2040 memory_barrier->SetLocations(nullptr);
2041}
2042
2043void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002044 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002045}
2046
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002047void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2048 ret->SetLocations(nullptr);
2049}
2050
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002051void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002052 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002053}
2054
2055void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002056 LocationSummary* locations =
2057 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002058 switch (ret->InputAt(0)->GetType()) {
2059 case Primitive::kPrimBoolean:
2060 case Primitive::kPrimByte:
2061 case Primitive::kPrimChar:
2062 case Primitive::kPrimShort:
2063 case Primitive::kPrimInt:
2064 case Primitive::kPrimNot:
2065 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002066 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002067 break;
2068
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002069 case Primitive::kPrimFloat:
2070 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002071 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002072 break;
2073
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002074 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002075 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002076 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002077}
2078
2079void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2080 if (kIsDebugBuild) {
2081 switch (ret->InputAt(0)->GetType()) {
2082 case Primitive::kPrimBoolean:
2083 case Primitive::kPrimByte:
2084 case Primitive::kPrimChar:
2085 case Primitive::kPrimShort:
2086 case Primitive::kPrimInt:
2087 case Primitive::kPrimNot:
2088 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002089 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002090 break;
2091
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002092 case Primitive::kPrimFloat:
2093 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002095 XMM0);
2096 break;
2097
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002098 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002099 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002100 }
2101 }
2102 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103}
2104
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002105Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2106 switch (type) {
2107 case Primitive::kPrimBoolean:
2108 case Primitive::kPrimByte:
2109 case Primitive::kPrimChar:
2110 case Primitive::kPrimShort:
2111 case Primitive::kPrimInt:
2112 case Primitive::kPrimNot:
2113 case Primitive::kPrimLong:
2114 return Location::RegisterLocation(RAX);
2115
2116 case Primitive::kPrimVoid:
2117 return Location::NoLocation();
2118
2119 case Primitive::kPrimDouble:
2120 case Primitive::kPrimFloat:
2121 return Location::FpuRegisterLocation(XMM0);
2122 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002123
2124 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002125}
2126
2127Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2128 return Location::RegisterLocation(kMethodRegisterArgument);
2129}
2130
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002131Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002132 switch (type) {
2133 case Primitive::kPrimBoolean:
2134 case Primitive::kPrimByte:
2135 case Primitive::kPrimChar:
2136 case Primitive::kPrimShort:
2137 case Primitive::kPrimInt:
2138 case Primitive::kPrimNot: {
2139 uint32_t index = gp_index_++;
2140 stack_index_++;
2141 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002142 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002143 } else {
2144 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2145 }
2146 }
2147
2148 case Primitive::kPrimLong: {
2149 uint32_t index = gp_index_;
2150 stack_index_ += 2;
2151 if (index < calling_convention.GetNumberOfRegisters()) {
2152 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002153 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002154 } else {
2155 gp_index_ += 2;
2156 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2157 }
2158 }
2159
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002160 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002161 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002162 stack_index_++;
2163 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002165 } else {
2166 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2167 }
2168 }
2169
2170 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002171 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002172 stack_index_ += 2;
2173 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002174 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002175 } else {
2176 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2177 }
2178 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002179
2180 case Primitive::kPrimVoid:
2181 LOG(FATAL) << "Unexpected parameter type " << type;
2182 break;
2183 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002184 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002185}
2186
Calin Juravle175dc732015-08-25 15:42:32 +01002187void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2188 // The trampoline uses the same calling convention as dex calling conventions,
2189 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2190 // the method_idx.
2191 HandleInvoke(invoke);
2192}
2193
2194void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2195 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2196}
2197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002198void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002199 // Explicit clinit checks triggered by static invokes must have been pruned by
2200 // art::PrepareForRegisterAllocation.
2201 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002202
Mark Mendellfb8d2792015-03-31 22:16:59 -04002203 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002204 if (intrinsic.TryDispatch(invoke)) {
2205 return;
2206 }
2207
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002208 HandleInvoke(invoke);
2209}
2210
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002211static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2212 if (invoke->GetLocations()->Intrinsified()) {
2213 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2214 intrinsic.Dispatch(invoke);
2215 return true;
2216 }
2217 return false;
2218}
2219
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002220void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002221 // Explicit clinit checks triggered by static invokes must have been pruned by
2222 // art::PrepareForRegisterAllocation.
2223 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002224
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002225 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2226 return;
2227 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002228
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002229 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002230 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002231 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002232 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002233}
2234
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002235void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002236 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002237 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002238}
2239
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002241 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002242 if (intrinsic.TryDispatch(invoke)) {
2243 return;
2244 }
2245
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246 HandleInvoke(invoke);
2247}
2248
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002249void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002250 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2251 return;
2252 }
2253
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002254 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002255 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002256 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002257}
2258
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002259void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2260 HandleInvoke(invoke);
2261 // Add the hidden argument.
2262 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2263}
2264
2265void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2266 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002267 LocationSummary* locations = invoke->GetLocations();
2268 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2269 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 Location receiver = locations->InAt(0);
2271 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2272
Roland Levillain0d5a2812015-11-13 10:07:31 +00002273 // Set the hidden argument. This is safe to do this here, as RAX
2274 // won't be modified thereafter, before the `call` instruction.
2275 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002276 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002278 if (receiver.IsStackSlot()) {
2279 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002280 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281 __ movl(temp, Address(temp, class_offset));
2282 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002283 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002284 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002285 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002286 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002287 // Instead of simply (possibly) unpoisoning `temp` here, we should
2288 // emit a read barrier for the previous class reference load.
2289 // However this is not required in practice, as this is an
2290 // intermediate/temporary reference and because the current
2291 // concurrent copying collector keeps the from-space memory
2292 // intact/accessible until the end of the marking phase (the
2293 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002294 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002295 // temp = temp->GetAddressOfIMT()
2296 __ movq(temp,
2297 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2298 // temp = temp->GetImtEntryAt(method_offset);
2299 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002300 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002301 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002302 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002303 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002304 __ call(Address(
2305 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002306
2307 DCHECK(!codegen_->IsLeafMethod());
2308 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2309}
2310
Roland Levillain88cb1752014-10-20 16:36:47 +01002311void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2312 LocationSummary* locations =
2313 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2314 switch (neg->GetResultType()) {
2315 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002316 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002317 locations->SetInAt(0, Location::RequiresRegister());
2318 locations->SetOut(Location::SameAsFirstInput());
2319 break;
2320
Roland Levillain88cb1752014-10-20 16:36:47 +01002321 case Primitive::kPrimFloat:
2322 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002323 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002324 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002325 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002326 break;
2327
2328 default:
2329 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2330 }
2331}
2332
2333void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2334 LocationSummary* locations = neg->GetLocations();
2335 Location out = locations->Out();
2336 Location in = locations->InAt(0);
2337 switch (neg->GetResultType()) {
2338 case Primitive::kPrimInt:
2339 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002340 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002341 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002342 break;
2343
2344 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002345 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002346 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002348 break;
2349
Roland Levillain5368c212014-11-27 15:03:41 +00002350 case Primitive::kPrimFloat: {
2351 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002352 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002353 // Implement float negation with an exclusive or with value
2354 // 0x80000000 (mask for bit 31, representing the sign of a
2355 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002356 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002358 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002359 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002360
Roland Levillain5368c212014-11-27 15:03:41 +00002361 case Primitive::kPrimDouble: {
2362 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002363 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002364 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002365 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002366 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002367 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002369 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002370 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002371
2372 default:
2373 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2374 }
2375}
2376
Roland Levillaindff1f282014-11-05 14:15:05 +00002377void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2378 LocationSummary* locations =
2379 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2380 Primitive::Type result_type = conversion->GetResultType();
2381 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002382 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002383
David Brazdilb2bd1c52015-03-25 11:17:37 +00002384 // The Java language does not allow treating boolean as an integral type but
2385 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002386
Roland Levillaindff1f282014-11-05 14:15:05 +00002387 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002388 case Primitive::kPrimByte:
2389 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002390 case Primitive::kPrimLong:
2391 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002392 case Primitive::kPrimBoolean:
2393 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002394 case Primitive::kPrimShort:
2395 case Primitive::kPrimInt:
2396 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002397 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002398 locations->SetInAt(0, Location::Any());
2399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2400 break;
2401
2402 default:
2403 LOG(FATAL) << "Unexpected type conversion from " << input_type
2404 << " to " << result_type;
2405 }
2406 break;
2407
Roland Levillain01a8d712014-11-14 16:27:39 +00002408 case Primitive::kPrimShort:
2409 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002410 case Primitive::kPrimLong:
2411 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002412 case Primitive::kPrimBoolean:
2413 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002414 case Primitive::kPrimByte:
2415 case Primitive::kPrimInt:
2416 case Primitive::kPrimChar:
2417 // Processing a Dex `int-to-short' instruction.
2418 locations->SetInAt(0, Location::Any());
2419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 }
2426 break;
2427
Roland Levillain946e1432014-11-11 17:35:19 +00002428 case Primitive::kPrimInt:
2429 switch (input_type) {
2430 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002431 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002432 locations->SetInAt(0, Location::Any());
2433 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2434 break;
2435
2436 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002437 // Processing a Dex `float-to-int' instruction.
2438 locations->SetInAt(0, Location::RequiresFpuRegister());
2439 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002440 break;
2441
Roland Levillain946e1432014-11-11 17:35:19 +00002442 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002443 // Processing a Dex `double-to-int' instruction.
2444 locations->SetInAt(0, Location::RequiresFpuRegister());
2445 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002446 break;
2447
2448 default:
2449 LOG(FATAL) << "Unexpected type conversion from " << input_type
2450 << " to " << result_type;
2451 }
2452 break;
2453
Roland Levillaindff1f282014-11-05 14:15:05 +00002454 case Primitive::kPrimLong:
2455 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002456 case Primitive::kPrimBoolean:
2457 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002458 case Primitive::kPrimByte:
2459 case Primitive::kPrimShort:
2460 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002461 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002462 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002463 // TODO: We would benefit from a (to-be-implemented)
2464 // Location::RegisterOrStackSlot requirement for this input.
2465 locations->SetInAt(0, Location::RequiresRegister());
2466 locations->SetOut(Location::RequiresRegister());
2467 break;
2468
2469 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002470 // Processing a Dex `float-to-long' instruction.
2471 locations->SetInAt(0, Location::RequiresFpuRegister());
2472 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002473 break;
2474
Roland Levillaindff1f282014-11-05 14:15:05 +00002475 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002476 // Processing a Dex `double-to-long' instruction.
2477 locations->SetInAt(0, Location::RequiresFpuRegister());
2478 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002479 break;
2480
2481 default:
2482 LOG(FATAL) << "Unexpected type conversion from " << input_type
2483 << " to " << result_type;
2484 }
2485 break;
2486
Roland Levillain981e4542014-11-14 11:47:14 +00002487 case Primitive::kPrimChar:
2488 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002489 case Primitive::kPrimLong:
2490 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002491 case Primitive::kPrimBoolean:
2492 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002493 case Primitive::kPrimByte:
2494 case Primitive::kPrimShort:
2495 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002496 // Processing a Dex `int-to-char' instruction.
2497 locations->SetInAt(0, Location::Any());
2498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2499 break;
2500
2501 default:
2502 LOG(FATAL) << "Unexpected type conversion from " << input_type
2503 << " to " << result_type;
2504 }
2505 break;
2506
Roland Levillaindff1f282014-11-05 14:15:05 +00002507 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002508 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002509 case Primitive::kPrimBoolean:
2510 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002511 case Primitive::kPrimByte:
2512 case Primitive::kPrimShort:
2513 case Primitive::kPrimInt:
2514 case Primitive::kPrimChar:
2515 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002516 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002517 locations->SetOut(Location::RequiresFpuRegister());
2518 break;
2519
2520 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002521 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002522 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002523 locations->SetOut(Location::RequiresFpuRegister());
2524 break;
2525
Roland Levillaincff13742014-11-17 14:32:17 +00002526 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002527 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002528 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002529 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002530 break;
2531
2532 default:
2533 LOG(FATAL) << "Unexpected type conversion from " << input_type
2534 << " to " << result_type;
2535 };
2536 break;
2537
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002539 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002542 case Primitive::kPrimByte:
2543 case Primitive::kPrimShort:
2544 case Primitive::kPrimInt:
2545 case Primitive::kPrimChar:
2546 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002547 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002548 locations->SetOut(Location::RequiresFpuRegister());
2549 break;
2550
2551 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002552 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002553 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002554 locations->SetOut(Location::RequiresFpuRegister());
2555 break;
2556
Roland Levillaincff13742014-11-17 14:32:17 +00002557 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002558 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002559 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002560 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002561 break;
2562
2563 default:
2564 LOG(FATAL) << "Unexpected type conversion from " << input_type
2565 << " to " << result_type;
2566 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002567 break;
2568
2569 default:
2570 LOG(FATAL) << "Unexpected type conversion from " << input_type
2571 << " to " << result_type;
2572 }
2573}
2574
2575void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2576 LocationSummary* locations = conversion->GetLocations();
2577 Location out = locations->Out();
2578 Location in = locations->InAt(0);
2579 Primitive::Type result_type = conversion->GetResultType();
2580 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002581 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002582 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002583 case Primitive::kPrimByte:
2584 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002585 case Primitive::kPrimLong:
2586 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002587 case Primitive::kPrimBoolean:
2588 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002589 case Primitive::kPrimShort:
2590 case Primitive::kPrimInt:
2591 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002592 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002593 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002595 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002596 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002597 Address(CpuRegister(RSP), in.GetStackIndex()));
2598 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002600 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002601 }
2602 break;
2603
2604 default:
2605 LOG(FATAL) << "Unexpected type conversion from " << input_type
2606 << " to " << result_type;
2607 }
2608 break;
2609
Roland Levillain01a8d712014-11-14 16:27:39 +00002610 case Primitive::kPrimShort:
2611 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002612 case Primitive::kPrimLong:
2613 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002614 case Primitive::kPrimBoolean:
2615 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002616 case Primitive::kPrimByte:
2617 case Primitive::kPrimInt:
2618 case Primitive::kPrimChar:
2619 // Processing a Dex `int-to-short' instruction.
2620 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002622 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002623 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002624 Address(CpuRegister(RSP), in.GetStackIndex()));
2625 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002627 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002628 }
2629 break;
2630
2631 default:
2632 LOG(FATAL) << "Unexpected type conversion from " << input_type
2633 << " to " << result_type;
2634 }
2635 break;
2636
Roland Levillain946e1432014-11-11 17:35:19 +00002637 case Primitive::kPrimInt:
2638 switch (input_type) {
2639 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002640 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002641 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002643 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002645 Address(CpuRegister(RSP), in.GetStackIndex()));
2646 } else {
2647 DCHECK(in.IsConstant());
2648 DCHECK(in.GetConstant()->IsLongConstant());
2649 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002650 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002651 }
2652 break;
2653
Roland Levillain3f8f9362014-12-02 17:45:01 +00002654 case Primitive::kPrimFloat: {
2655 // Processing a Dex `float-to-int' instruction.
2656 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2657 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002658 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002659
2660 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002661 // if input >= (float)INT_MAX goto done
2662 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002663 __ j(kAboveEqual, &done);
2664 // if input == NaN goto nan
2665 __ j(kUnordered, &nan);
2666 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002667 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002668 __ jmp(&done);
2669 __ Bind(&nan);
2670 // output = 0
2671 __ xorl(output, output);
2672 __ Bind(&done);
2673 break;
2674 }
2675
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002676 case Primitive::kPrimDouble: {
2677 // Processing a Dex `double-to-int' instruction.
2678 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2679 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002680 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002681
2682 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002683 // if input >= (double)INT_MAX goto done
2684 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002685 __ j(kAboveEqual, &done);
2686 // if input == NaN goto nan
2687 __ j(kUnordered, &nan);
2688 // output = double-to-int-truncate(input)
2689 __ cvttsd2si(output, input);
2690 __ jmp(&done);
2691 __ Bind(&nan);
2692 // output = 0
2693 __ xorl(output, output);
2694 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002695 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002696 }
Roland Levillain946e1432014-11-11 17:35:19 +00002697
2698 default:
2699 LOG(FATAL) << "Unexpected type conversion from " << input_type
2700 << " to " << result_type;
2701 }
2702 break;
2703
Roland Levillaindff1f282014-11-05 14:15:05 +00002704 case Primitive::kPrimLong:
2705 switch (input_type) {
2706 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002707 case Primitive::kPrimBoolean:
2708 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002709 case Primitive::kPrimByte:
2710 case Primitive::kPrimShort:
2711 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002712 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002713 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002714 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002716 break;
2717
Roland Levillain624279f2014-12-04 11:54:28 +00002718 case Primitive::kPrimFloat: {
2719 // Processing a Dex `float-to-long' instruction.
2720 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2721 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002722 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002723
Mark Mendell92e83bf2015-05-07 11:25:03 -04002724 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002725 // if input >= (float)LONG_MAX goto done
2726 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002727 __ j(kAboveEqual, &done);
2728 // if input == NaN goto nan
2729 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002730 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002731 __ cvttss2si(output, input, true);
2732 __ jmp(&done);
2733 __ Bind(&nan);
2734 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002735 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002736 __ Bind(&done);
2737 break;
2738 }
2739
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002740 case Primitive::kPrimDouble: {
2741 // Processing a Dex `double-to-long' instruction.
2742 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2743 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002744 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002745
Mark Mendell92e83bf2015-05-07 11:25:03 -04002746 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002747 // if input >= (double)LONG_MAX goto done
2748 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002749 __ j(kAboveEqual, &done);
2750 // if input == NaN goto nan
2751 __ j(kUnordered, &nan);
2752 // output = double-to-long-truncate(input)
2753 __ cvttsd2si(output, input, true);
2754 __ jmp(&done);
2755 __ Bind(&nan);
2756 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002757 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002758 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002759 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002760 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002761
2762 default:
2763 LOG(FATAL) << "Unexpected type conversion from " << input_type
2764 << " to " << result_type;
2765 }
2766 break;
2767
Roland Levillain981e4542014-11-14 11:47:14 +00002768 case Primitive::kPrimChar:
2769 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002770 case Primitive::kPrimLong:
2771 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002772 case Primitive::kPrimBoolean:
2773 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002774 case Primitive::kPrimByte:
2775 case Primitive::kPrimShort:
2776 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002777 // Processing a Dex `int-to-char' instruction.
2778 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002779 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002780 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002782 Address(CpuRegister(RSP), in.GetStackIndex()));
2783 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002785 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002786 }
2787 break;
2788
2789 default:
2790 LOG(FATAL) << "Unexpected type conversion from " << input_type
2791 << " to " << result_type;
2792 }
2793 break;
2794
Roland Levillaindff1f282014-11-05 14:15:05 +00002795 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002796 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002797 case Primitive::kPrimBoolean:
2798 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002799 case Primitive::kPrimByte:
2800 case Primitive::kPrimShort:
2801 case Primitive::kPrimInt:
2802 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002803 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002804 if (in.IsRegister()) {
2805 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2806 } else if (in.IsConstant()) {
2807 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2808 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002809 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002810 } else {
2811 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2812 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2813 }
Roland Levillaincff13742014-11-17 14:32:17 +00002814 break;
2815
2816 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002817 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002818 if (in.IsRegister()) {
2819 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2820 } else if (in.IsConstant()) {
2821 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2822 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002823 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002824 } else {
2825 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2826 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2827 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002828 break;
2829
Roland Levillaincff13742014-11-17 14:32:17 +00002830 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002831 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002832 if (in.IsFpuRegister()) {
2833 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2834 } else if (in.IsConstant()) {
2835 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2836 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002837 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002838 } else {
2839 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2840 Address(CpuRegister(RSP), in.GetStackIndex()));
2841 }
Roland Levillaincff13742014-11-17 14:32:17 +00002842 break;
2843
2844 default:
2845 LOG(FATAL) << "Unexpected type conversion from " << input_type
2846 << " to " << result_type;
2847 };
2848 break;
2849
Roland Levillaindff1f282014-11-05 14:15:05 +00002850 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002851 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002852 case Primitive::kPrimBoolean:
2853 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002854 case Primitive::kPrimByte:
2855 case Primitive::kPrimShort:
2856 case Primitive::kPrimInt:
2857 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002858 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002859 if (in.IsRegister()) {
2860 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2861 } else if (in.IsConstant()) {
2862 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2863 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002864 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002865 } else {
2866 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2867 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2868 }
Roland Levillaincff13742014-11-17 14:32:17 +00002869 break;
2870
2871 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002872 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002873 if (in.IsRegister()) {
2874 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2875 } else if (in.IsConstant()) {
2876 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2877 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002878 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002879 } else {
2880 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2881 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2882 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002883 break;
2884
Roland Levillaincff13742014-11-17 14:32:17 +00002885 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002886 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002887 if (in.IsFpuRegister()) {
2888 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2889 } else if (in.IsConstant()) {
2890 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2891 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002892 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002893 } else {
2894 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2895 Address(CpuRegister(RSP), in.GetStackIndex()));
2896 }
Roland Levillaincff13742014-11-17 14:32:17 +00002897 break;
2898
2899 default:
2900 LOG(FATAL) << "Unexpected type conversion from " << input_type
2901 << " to " << result_type;
2902 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002903 break;
2904
2905 default:
2906 LOG(FATAL) << "Unexpected type conversion from " << input_type
2907 << " to " << result_type;
2908 }
2909}
2910
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002911void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002912 LocationSummary* locations =
2913 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002914 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002915 case Primitive::kPrimInt: {
2916 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002917 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002919 break;
2920 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002921
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002922 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002923 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002924 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002925 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002927 break;
2928 }
2929
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002930 case Primitive::kPrimDouble:
2931 case Primitive::kPrimFloat: {
2932 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002933 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002935 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002936 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002937
2938 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002939 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002940 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002941}
2942
2943void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2944 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002945 Location first = locations->InAt(0);
2946 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002947 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002948
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002949 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002950 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002952 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2953 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002954 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2955 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002956 } else {
2957 __ leal(out.AsRegister<CpuRegister>(), Address(
2958 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2959 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002960 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2962 __ addl(out.AsRegister<CpuRegister>(),
2963 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2964 } else {
2965 __ leal(out.AsRegister<CpuRegister>(), Address(
2966 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2967 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002968 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002969 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002971 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002972 break;
2973 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002975 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002976 if (second.IsRegister()) {
2977 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2978 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002979 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2980 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002981 } else {
2982 __ leaq(out.AsRegister<CpuRegister>(), Address(
2983 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2984 }
2985 } else {
2986 DCHECK(second.IsConstant());
2987 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2988 int32_t int32_value = Low32Bits(value);
2989 DCHECK_EQ(int32_value, value);
2990 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2991 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2992 } else {
2993 __ leaq(out.AsRegister<CpuRegister>(), Address(
2994 first.AsRegister<CpuRegister>(), int32_value));
2995 }
2996 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002997 break;
2998 }
2999
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003000 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003001 if (second.IsFpuRegister()) {
3002 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3003 } else if (second.IsConstant()) {
3004 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003005 codegen_->LiteralFloatAddress(
3006 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003007 } else {
3008 DCHECK(second.IsStackSlot());
3009 __ addss(first.AsFpuRegister<XmmRegister>(),
3010 Address(CpuRegister(RSP), second.GetStackIndex()));
3011 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003012 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003013 }
3014
3015 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003016 if (second.IsFpuRegister()) {
3017 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3018 } else if (second.IsConstant()) {
3019 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003020 codegen_->LiteralDoubleAddress(
3021 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003022 } else {
3023 DCHECK(second.IsDoubleStackSlot());
3024 __ addsd(first.AsFpuRegister<XmmRegister>(),
3025 Address(CpuRegister(RSP), second.GetStackIndex()));
3026 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003027 break;
3028 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003029
3030 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003031 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003032 }
3033}
3034
3035void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003036 LocationSummary* locations =
3037 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003038 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003039 case Primitive::kPrimInt: {
3040 locations->SetInAt(0, Location::RequiresRegister());
3041 locations->SetInAt(1, Location::Any());
3042 locations->SetOut(Location::SameAsFirstInput());
3043 break;
3044 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003045 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003046 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003047 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003048 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003049 break;
3050 }
Calin Juravle11351682014-10-23 15:38:15 +01003051 case Primitive::kPrimFloat:
3052 case Primitive::kPrimDouble: {
3053 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003054 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003055 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003056 break;
Calin Juravle11351682014-10-23 15:38:15 +01003057 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 default:
Calin Juravle11351682014-10-23 15:38:15 +01003059 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003060 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003061}
3062
3063void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3064 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003065 Location first = locations->InAt(0);
3066 Location second = locations->InAt(1);
3067 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003068 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003069 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003070 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003071 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003072 } else if (second.IsConstant()) {
3073 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003075 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003076 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003077 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003078 break;
3079 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003080 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003081 if (second.IsConstant()) {
3082 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3083 DCHECK(IsInt<32>(value));
3084 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3085 } else {
3086 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3087 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003088 break;
3089 }
3090
Calin Juravle11351682014-10-23 15:38:15 +01003091 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003092 if (second.IsFpuRegister()) {
3093 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3094 } else if (second.IsConstant()) {
3095 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003096 codegen_->LiteralFloatAddress(
3097 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003098 } else {
3099 DCHECK(second.IsStackSlot());
3100 __ subss(first.AsFpuRegister<XmmRegister>(),
3101 Address(CpuRegister(RSP), second.GetStackIndex()));
3102 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003103 break;
Calin Juravle11351682014-10-23 15:38:15 +01003104 }
3105
3106 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003107 if (second.IsFpuRegister()) {
3108 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3109 } else if (second.IsConstant()) {
3110 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003111 codegen_->LiteralDoubleAddress(
3112 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003113 } else {
3114 DCHECK(second.IsDoubleStackSlot());
3115 __ subsd(first.AsFpuRegister<XmmRegister>(),
3116 Address(CpuRegister(RSP), second.GetStackIndex()));
3117 }
Calin Juravle11351682014-10-23 15:38:15 +01003118 break;
3119 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003120
3121 default:
Calin Juravle11351682014-10-23 15:38:15 +01003122 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003123 }
3124}
3125
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3127 LocationSummary* locations =
3128 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3129 switch (mul->GetResultType()) {
3130 case Primitive::kPrimInt: {
3131 locations->SetInAt(0, Location::RequiresRegister());
3132 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003133 if (mul->InputAt(1)->IsIntConstant()) {
3134 // Can use 3 operand multiply.
3135 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3136 } else {
3137 locations->SetOut(Location::SameAsFirstInput());
3138 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139 break;
3140 }
3141 case Primitive::kPrimLong: {
3142 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003143 locations->SetInAt(1, Location::Any());
3144 if (mul->InputAt(1)->IsLongConstant() &&
3145 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003146 // Can use 3 operand multiply.
3147 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3148 } else {
3149 locations->SetOut(Location::SameAsFirstInput());
3150 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151 break;
3152 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003153 case Primitive::kPrimFloat:
3154 case Primitive::kPrimDouble: {
3155 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003156 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003157 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003158 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003159 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160
3161 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003162 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003163 }
3164}
3165
3166void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3167 LocationSummary* locations = mul->GetLocations();
3168 Location first = locations->InAt(0);
3169 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003170 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003172 case Primitive::kPrimInt:
3173 // The constant may have ended up in a register, so test explicitly to avoid
3174 // problems where the output may not be the same as the first operand.
3175 if (mul->InputAt(1)->IsIntConstant()) {
3176 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3177 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3178 } else if (second.IsRegister()) {
3179 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003180 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003182 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003184 __ imull(first.AsRegister<CpuRegister>(),
3185 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003186 }
3187 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003188 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003189 // The constant may have ended up in a register, so test explicitly to avoid
3190 // problems where the output may not be the same as the first operand.
3191 if (mul->InputAt(1)->IsLongConstant()) {
3192 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3193 if (IsInt<32>(value)) {
3194 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3195 Immediate(static_cast<int32_t>(value)));
3196 } else {
3197 // Have to use the constant area.
3198 DCHECK(first.Equals(out));
3199 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3200 }
3201 } else if (second.IsRegister()) {
3202 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003203 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003204 } else {
3205 DCHECK(second.IsDoubleStackSlot());
3206 DCHECK(first.Equals(out));
3207 __ imulq(first.AsRegister<CpuRegister>(),
3208 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003209 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003210 break;
3211 }
3212
Calin Juravleb5bfa962014-10-21 18:02:24 +01003213 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003214 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003215 if (second.IsFpuRegister()) {
3216 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3217 } else if (second.IsConstant()) {
3218 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003219 codegen_->LiteralFloatAddress(
3220 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003221 } else {
3222 DCHECK(second.IsStackSlot());
3223 __ mulss(first.AsFpuRegister<XmmRegister>(),
3224 Address(CpuRegister(RSP), second.GetStackIndex()));
3225 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003227 }
3228
3229 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003230 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003231 if (second.IsFpuRegister()) {
3232 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3233 } else if (second.IsConstant()) {
3234 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003235 codegen_->LiteralDoubleAddress(
3236 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003237 } else {
3238 DCHECK(second.IsDoubleStackSlot());
3239 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3240 Address(CpuRegister(RSP), second.GetStackIndex()));
3241 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003242 break;
3243 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003244
3245 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003246 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003247 }
3248}
3249
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003250void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3251 uint32_t stack_adjustment, bool is_float) {
3252 if (source.IsStackSlot()) {
3253 DCHECK(is_float);
3254 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3255 } else if (source.IsDoubleStackSlot()) {
3256 DCHECK(!is_float);
3257 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3258 } else {
3259 // Write the value to the temporary location on the stack and load to FP stack.
3260 if (is_float) {
3261 Location stack_temp = Location::StackSlot(temp_offset);
3262 codegen_->Move(stack_temp, source);
3263 __ flds(Address(CpuRegister(RSP), temp_offset));
3264 } else {
3265 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3266 codegen_->Move(stack_temp, source);
3267 __ fldl(Address(CpuRegister(RSP), temp_offset));
3268 }
3269 }
3270}
3271
3272void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3273 Primitive::Type type = rem->GetResultType();
3274 bool is_float = type == Primitive::kPrimFloat;
3275 size_t elem_size = Primitive::ComponentSize(type);
3276 LocationSummary* locations = rem->GetLocations();
3277 Location first = locations->InAt(0);
3278 Location second = locations->InAt(1);
3279 Location out = locations->Out();
3280
3281 // Create stack space for 2 elements.
3282 // TODO: enhance register allocator to ask for stack temporaries.
3283 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3284
3285 // Load the values to the FP stack in reverse order, using temporaries if needed.
3286 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3287 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3288
3289 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003290 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003291 __ Bind(&retry);
3292 __ fprem();
3293
3294 // Move FP status to AX.
3295 __ fstsw();
3296
3297 // And see if the argument reduction is complete. This is signaled by the
3298 // C2 FPU flag bit set to 0.
3299 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3300 __ j(kNotEqual, &retry);
3301
3302 // We have settled on the final value. Retrieve it into an XMM register.
3303 // Store FP top of stack to real stack.
3304 if (is_float) {
3305 __ fsts(Address(CpuRegister(RSP), 0));
3306 } else {
3307 __ fstl(Address(CpuRegister(RSP), 0));
3308 }
3309
3310 // Pop the 2 items from the FP stack.
3311 __ fucompp();
3312
3313 // Load the value from the stack into an XMM register.
3314 DCHECK(out.IsFpuRegister()) << out;
3315 if (is_float) {
3316 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3317 } else {
3318 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3319 }
3320
3321 // And remove the temporary stack space we allocated.
3322 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3323}
3324
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003325void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3326 DCHECK(instruction->IsDiv() || instruction->IsRem());
3327
3328 LocationSummary* locations = instruction->GetLocations();
3329 Location second = locations->InAt(1);
3330 DCHECK(second.IsConstant());
3331
3332 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3333 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003334 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003335
3336 DCHECK(imm == 1 || imm == -1);
3337
3338 switch (instruction->GetResultType()) {
3339 case Primitive::kPrimInt: {
3340 if (instruction->IsRem()) {
3341 __ xorl(output_register, output_register);
3342 } else {
3343 __ movl(output_register, input_register);
3344 if (imm == -1) {
3345 __ negl(output_register);
3346 }
3347 }
3348 break;
3349 }
3350
3351 case Primitive::kPrimLong: {
3352 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003353 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003354 } else {
3355 __ movq(output_register, input_register);
3356 if (imm == -1) {
3357 __ negq(output_register);
3358 }
3359 }
3360 break;
3361 }
3362
3363 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003364 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003365 }
3366}
3367
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003368void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003369 LocationSummary* locations = instruction->GetLocations();
3370 Location second = locations->InAt(1);
3371
3372 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3373 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3374
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003375 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003376 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3377 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003378
3379 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3380
3381 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003382 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003383 __ testl(numerator, numerator);
3384 __ cmov(kGreaterEqual, tmp, numerator);
3385 int shift = CTZ(imm);
3386 __ sarl(tmp, Immediate(shift));
3387
3388 if (imm < 0) {
3389 __ negl(tmp);
3390 }
3391
3392 __ movl(output_register, tmp);
3393 } else {
3394 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3395 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3396
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003397 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003398 __ addq(rdx, numerator);
3399 __ testq(numerator, numerator);
3400 __ cmov(kGreaterEqual, rdx, numerator);
3401 int shift = CTZ(imm);
3402 __ sarq(rdx, Immediate(shift));
3403
3404 if (imm < 0) {
3405 __ negq(rdx);
3406 }
3407
3408 __ movq(output_register, rdx);
3409 }
3410}
3411
3412void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3413 DCHECK(instruction->IsDiv() || instruction->IsRem());
3414
3415 LocationSummary* locations = instruction->GetLocations();
3416 Location second = locations->InAt(1);
3417
3418 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3419 : locations->GetTemp(0).AsRegister<CpuRegister>();
3420 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3421 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3422 : locations->Out().AsRegister<CpuRegister>();
3423 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3424
3425 DCHECK_EQ(RAX, eax.AsRegister());
3426 DCHECK_EQ(RDX, edx.AsRegister());
3427 if (instruction->IsDiv()) {
3428 DCHECK_EQ(RAX, out.AsRegister());
3429 } else {
3430 DCHECK_EQ(RDX, out.AsRegister());
3431 }
3432
3433 int64_t magic;
3434 int shift;
3435
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003436 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003437 if (instruction->GetResultType() == Primitive::kPrimInt) {
3438 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3439
3440 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3441
3442 __ movl(numerator, eax);
3443
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003444 __ movl(eax, Immediate(magic));
3445 __ imull(numerator);
3446
3447 if (imm > 0 && magic < 0) {
3448 __ addl(edx, numerator);
3449 } else if (imm < 0 && magic > 0) {
3450 __ subl(edx, numerator);
3451 }
3452
3453 if (shift != 0) {
3454 __ sarl(edx, Immediate(shift));
3455 }
3456
3457 __ movl(eax, edx);
3458 __ shrl(edx, Immediate(31));
3459 __ addl(edx, eax);
3460
3461 if (instruction->IsRem()) {
3462 __ movl(eax, numerator);
3463 __ imull(edx, Immediate(imm));
3464 __ subl(eax, edx);
3465 __ movl(edx, eax);
3466 } else {
3467 __ movl(eax, edx);
3468 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 } else {
3470 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3471
3472 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3473
3474 CpuRegister rax = eax;
3475 CpuRegister rdx = edx;
3476
3477 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3478
3479 // Save the numerator.
3480 __ movq(numerator, rax);
3481
3482 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003483 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003484
3485 // RDX:RAX = magic * numerator
3486 __ imulq(numerator);
3487
3488 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003489 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 __ addq(rdx, numerator);
3491 } else if (imm < 0 && magic > 0) {
3492 // RDX -= numerator
3493 __ subq(rdx, numerator);
3494 }
3495
3496 // Shift if needed.
3497 if (shift != 0) {
3498 __ sarq(rdx, Immediate(shift));
3499 }
3500
3501 // RDX += 1 if RDX < 0
3502 __ movq(rax, rdx);
3503 __ shrq(rdx, Immediate(63));
3504 __ addq(rdx, rax);
3505
3506 if (instruction->IsRem()) {
3507 __ movq(rax, numerator);
3508
3509 if (IsInt<32>(imm)) {
3510 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3511 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003512 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513 }
3514
3515 __ subq(rax, rdx);
3516 __ movq(rdx, rax);
3517 } else {
3518 __ movq(rax, rdx);
3519 }
3520 }
3521}
3522
Calin Juravlebacfec32014-11-14 15:54:36 +00003523void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3524 DCHECK(instruction->IsDiv() || instruction->IsRem());
3525 Primitive::Type type = instruction->GetResultType();
3526 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3527
3528 bool is_div = instruction->IsDiv();
3529 LocationSummary* locations = instruction->GetLocations();
3530
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003531 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3532 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003533
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003535 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003536
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003537 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003538 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003539
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 if (imm == 0) {
3541 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3542 } else if (imm == 1 || imm == -1) {
3543 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003544 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003545 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 } else {
3547 DCHECK(imm <= -2 || imm >= 2);
3548 GenerateDivRemWithAnyConstant(instruction);
3549 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003550 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003551 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003552 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003553 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3557 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3558 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3559 // so it's safe to just use negl instead of more complex comparisons.
3560 if (type == Primitive::kPrimInt) {
3561 __ cmpl(second_reg, Immediate(-1));
3562 __ j(kEqual, slow_path->GetEntryLabel());
3563 // edx:eax <- sign-extended of eax
3564 __ cdq();
3565 // eax = quotient, edx = remainder
3566 __ idivl(second_reg);
3567 } else {
3568 __ cmpq(second_reg, Immediate(-1));
3569 __ j(kEqual, slow_path->GetEntryLabel());
3570 // rdx:rax <- sign-extended of rax
3571 __ cqo();
3572 // rax = quotient, rdx = remainder
3573 __ idivq(second_reg);
3574 }
3575 __ Bind(slow_path->GetExitLabel());
3576 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003577}
3578
Calin Juravle7c4954d2014-10-28 16:57:40 +00003579void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3580 LocationSummary* locations =
3581 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3582 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003583 case Primitive::kPrimInt:
3584 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003585 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003586 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003587 locations->SetOut(Location::SameAsFirstInput());
3588 // Intel uses edx:eax as the dividend.
3589 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3591 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3592 // output and request another temp.
3593 if (div->InputAt(1)->IsConstant()) {
3594 locations->AddTemp(Location::RequiresRegister());
3595 }
Calin Juravled0d48522014-11-04 16:40:20 +00003596 break;
3597 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003598
Calin Juravle7c4954d2014-10-28 16:57:40 +00003599 case Primitive::kPrimFloat:
3600 case Primitive::kPrimDouble: {
3601 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003602 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003603 locations->SetOut(Location::SameAsFirstInput());
3604 break;
3605 }
3606
3607 default:
3608 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3609 }
3610}
3611
3612void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3613 LocationSummary* locations = div->GetLocations();
3614 Location first = locations->InAt(0);
3615 Location second = locations->InAt(1);
3616 DCHECK(first.Equals(locations->Out()));
3617
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003618 Primitive::Type type = div->GetResultType();
3619 switch (type) {
3620 case Primitive::kPrimInt:
3621 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003623 break;
3624 }
3625
Calin Juravle7c4954d2014-10-28 16:57:40 +00003626 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003627 if (second.IsFpuRegister()) {
3628 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3629 } else if (second.IsConstant()) {
3630 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003631 codegen_->LiteralFloatAddress(
3632 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003633 } else {
3634 DCHECK(second.IsStackSlot());
3635 __ divss(first.AsFpuRegister<XmmRegister>(),
3636 Address(CpuRegister(RSP), second.GetStackIndex()));
3637 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003638 break;
3639 }
3640
3641 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003642 if (second.IsFpuRegister()) {
3643 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3644 } else if (second.IsConstant()) {
3645 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003646 codegen_->LiteralDoubleAddress(
3647 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003648 } else {
3649 DCHECK(second.IsDoubleStackSlot());
3650 __ divsd(first.AsFpuRegister<XmmRegister>(),
3651 Address(CpuRegister(RSP), second.GetStackIndex()));
3652 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003653 break;
3654 }
3655
3656 default:
3657 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3658 }
3659}
3660
Calin Juravlebacfec32014-11-14 15:54:36 +00003661void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003662 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003663 LocationSummary* locations =
3664 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003665
3666 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003667 case Primitive::kPrimInt:
3668 case Primitive::kPrimLong: {
3669 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003670 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003671 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3672 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003673 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3674 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3675 // output and request another temp.
3676 if (rem->InputAt(1)->IsConstant()) {
3677 locations->AddTemp(Location::RequiresRegister());
3678 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003679 break;
3680 }
3681
3682 case Primitive::kPrimFloat:
3683 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003684 locations->SetInAt(0, Location::Any());
3685 locations->SetInAt(1, Location::Any());
3686 locations->SetOut(Location::RequiresFpuRegister());
3687 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003688 break;
3689 }
3690
3691 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003692 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003693 }
3694}
3695
3696void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3697 Primitive::Type type = rem->GetResultType();
3698 switch (type) {
3699 case Primitive::kPrimInt:
3700 case Primitive::kPrimLong: {
3701 GenerateDivRemIntegral(rem);
3702 break;
3703 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003704 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003705 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003706 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003707 break;
3708 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003709 default:
3710 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3711 }
3712}
3713
Calin Juravled0d48522014-11-04 16:40:20 +00003714void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003715 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3716 ? LocationSummary::kCallOnSlowPath
3717 : LocationSummary::kNoCall;
3718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003719 locations->SetInAt(0, Location::Any());
3720 if (instruction->HasUses()) {
3721 locations->SetOut(Location::SameAsFirstInput());
3722 }
3723}
3724
3725void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003726 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003727 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3728 codegen_->AddSlowPath(slow_path);
3729
3730 LocationSummary* locations = instruction->GetLocations();
3731 Location value = locations->InAt(0);
3732
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003733 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003734 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003735 case Primitive::kPrimByte:
3736 case Primitive::kPrimChar:
3737 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003738 case Primitive::kPrimInt: {
3739 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003740 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003741 __ j(kEqual, slow_path->GetEntryLabel());
3742 } else if (value.IsStackSlot()) {
3743 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3744 __ j(kEqual, slow_path->GetEntryLabel());
3745 } else {
3746 DCHECK(value.IsConstant()) << value;
3747 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3748 __ jmp(slow_path->GetEntryLabel());
3749 }
3750 }
3751 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003752 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003753 case Primitive::kPrimLong: {
3754 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003755 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003756 __ j(kEqual, slow_path->GetEntryLabel());
3757 } else if (value.IsDoubleStackSlot()) {
3758 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3759 __ j(kEqual, slow_path->GetEntryLabel());
3760 } else {
3761 DCHECK(value.IsConstant()) << value;
3762 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3763 __ jmp(slow_path->GetEntryLabel());
3764 }
3765 }
3766 break;
3767 }
3768 default:
3769 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003770 }
Calin Juravled0d48522014-11-04 16:40:20 +00003771}
3772
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3774 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3775
3776 LocationSummary* locations =
3777 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3778
3779 switch (op->GetResultType()) {
3780 case Primitive::kPrimInt:
3781 case Primitive::kPrimLong: {
3782 locations->SetInAt(0, Location::RequiresRegister());
3783 // The shift count needs to be in CL.
3784 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3785 locations->SetOut(Location::SameAsFirstInput());
3786 break;
3787 }
3788 default:
3789 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3790 }
3791}
3792
3793void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3794 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3795
3796 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003797 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003798 Location second = locations->InAt(1);
3799
3800 switch (op->GetResultType()) {
3801 case Primitive::kPrimInt: {
3802 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003803 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804 if (op->IsShl()) {
3805 __ shll(first_reg, second_reg);
3806 } else if (op->IsShr()) {
3807 __ sarl(first_reg, second_reg);
3808 } else {
3809 __ shrl(first_reg, second_reg);
3810 }
3811 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003812 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003813 if (op->IsShl()) {
3814 __ shll(first_reg, imm);
3815 } else if (op->IsShr()) {
3816 __ sarl(first_reg, imm);
3817 } else {
3818 __ shrl(first_reg, imm);
3819 }
3820 }
3821 break;
3822 }
3823 case Primitive::kPrimLong: {
3824 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003825 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003826 if (op->IsShl()) {
3827 __ shlq(first_reg, second_reg);
3828 } else if (op->IsShr()) {
3829 __ sarq(first_reg, second_reg);
3830 } else {
3831 __ shrq(first_reg, second_reg);
3832 }
3833 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003834 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003835 if (op->IsShl()) {
3836 __ shlq(first_reg, imm);
3837 } else if (op->IsShr()) {
3838 __ sarq(first_reg, imm);
3839 } else {
3840 __ shrq(first_reg, imm);
3841 }
3842 }
3843 break;
3844 }
3845 default:
3846 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003847 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848 }
3849}
3850
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003851void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3852 LocationSummary* locations =
3853 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3854
3855 switch (ror->GetResultType()) {
3856 case Primitive::kPrimInt:
3857 case Primitive::kPrimLong: {
3858 locations->SetInAt(0, Location::RequiresRegister());
3859 // The shift count needs to be in CL (unless it is a constant).
3860 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3861 locations->SetOut(Location::SameAsFirstInput());
3862 break;
3863 }
3864 default:
3865 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3866 UNREACHABLE();
3867 }
3868}
3869
3870void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3871 LocationSummary* locations = ror->GetLocations();
3872 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3873 Location second = locations->InAt(1);
3874
3875 switch (ror->GetResultType()) {
3876 case Primitive::kPrimInt:
3877 if (second.IsRegister()) {
3878 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3879 __ rorl(first_reg, second_reg);
3880 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003881 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003882 __ rorl(first_reg, imm);
3883 }
3884 break;
3885 case Primitive::kPrimLong:
3886 if (second.IsRegister()) {
3887 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3888 __ rorq(first_reg, second_reg);
3889 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003890 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003891 __ rorq(first_reg, imm);
3892 }
3893 break;
3894 default:
3895 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3896 UNREACHABLE();
3897 }
3898}
3899
Calin Juravle9aec02f2014-11-18 23:06:35 +00003900void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3901 HandleShift(shl);
3902}
3903
3904void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3905 HandleShift(shl);
3906}
3907
3908void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3909 HandleShift(shr);
3910}
3911
3912void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3913 HandleShift(shr);
3914}
3915
3916void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3917 HandleShift(ushr);
3918}
3919
3920void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3921 HandleShift(ushr);
3922}
3923
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003924void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003925 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003927 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003928 if (instruction->IsStringAlloc()) {
3929 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3930 } else {
3931 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3932 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3933 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003934 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003935}
3936
3937void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003938 // Note: if heap poisoning is enabled, the entry point takes cares
3939 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003940 if (instruction->IsStringAlloc()) {
3941 // String is allocated through StringFactory. Call NewEmptyString entry point.
3942 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003943 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003944 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3945 __ call(Address(temp, code_offset.SizeValue()));
3946 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3947 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003948 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003949 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3950 DCHECK(!codegen_->IsLeafMethod());
3951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003952}
3953
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003954void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3955 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003957 InvokeRuntimeCallingConvention calling_convention;
3958 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003959 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003960 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003961 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003962}
3963
3964void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3965 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003966 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3967 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003968 // Note: if heap poisoning is enabled, the entry point takes cares
3969 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003970 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003971 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003972
3973 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003974}
3975
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003976void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003977 LocationSummary* locations =
3978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003979 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3980 if (location.IsStackSlot()) {
3981 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3982 } else if (location.IsDoubleStackSlot()) {
3983 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3984 }
3985 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003986}
3987
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003988void InstructionCodeGeneratorX86_64::VisitParameterValue(
3989 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003990 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003991}
3992
3993void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3994 LocationSummary* locations =
3995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3996 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3997}
3998
3999void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4000 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4001 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004002}
4003
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004004void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4005 LocationSummary* locations =
4006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4007 locations->SetInAt(0, Location::RequiresRegister());
4008 locations->SetOut(Location::RequiresRegister());
4009}
4010
4011void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4012 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004013 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004014 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004015 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004016 __ movq(locations->Out().AsRegister<CpuRegister>(),
4017 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004018 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004019 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004020 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004021 __ movq(locations->Out().AsRegister<CpuRegister>(),
4022 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4023 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004024 __ movq(locations->Out().AsRegister<CpuRegister>(),
4025 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004026 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004027}
4028
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004029void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004030 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004031 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004032 locations->SetInAt(0, Location::RequiresRegister());
4033 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004034}
4035
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004036void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4037 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004038 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4039 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004040 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004041 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004042 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004044 break;
4045
4046 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004047 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004048 break;
4049
4050 default:
4051 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4052 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004053}
4054
David Brazdil66d126e2015-04-03 16:02:44 +01004055void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4056 LocationSummary* locations =
4057 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4058 locations->SetInAt(0, Location::RequiresRegister());
4059 locations->SetOut(Location::SameAsFirstInput());
4060}
4061
4062void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004063 LocationSummary* locations = bool_not->GetLocations();
4064 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4065 locations->Out().AsRegister<CpuRegister>().AsRegister());
4066 Location out = locations->Out();
4067 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4068}
4069
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004070void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004071 LocationSummary* locations =
4072 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004073 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004074 locations->SetInAt(i, Location::Any());
4075 }
4076 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004077}
4078
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004079void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004080 LOG(FATAL) << "Unimplemented";
4081}
4082
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004083void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004084 /*
4085 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004086 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004087 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4088 */
4089 switch (kind) {
4090 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004091 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004092 break;
4093 }
4094 case MemBarrierKind::kAnyStore:
4095 case MemBarrierKind::kLoadAny:
4096 case MemBarrierKind::kStoreStore: {
4097 // nop
4098 break;
4099 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004100 case MemBarrierKind::kNTStoreStore:
4101 // Non-Temporal Store/Store needs an explicit fence.
4102 MemoryFence(/* non-temporal */ true);
4103 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004104 }
4105}
4106
4107void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4108 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4109
Roland Levillain0d5a2812015-11-13 10:07:31 +00004110 bool object_field_get_with_read_barrier =
4111 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004112 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004113 new (GetGraph()->GetArena()) LocationSummary(instruction,
4114 object_field_get_with_read_barrier ?
4115 LocationSummary::kCallOnSlowPath :
4116 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004117 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004118 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4119 locations->SetOut(Location::RequiresFpuRegister());
4120 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004121 // The output overlaps for an object field get when read barriers
4122 // are enabled: we do not want the move to overwrite the object's
4123 // location, as we need it to emit the read barrier.
4124 locations->SetOut(
4125 Location::RequiresRegister(),
4126 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004127 }
Calin Juravle52c48962014-12-16 17:02:57 +00004128}
4129
4130void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4131 const FieldInfo& field_info) {
4132 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4133
4134 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004135 Location base_loc = locations->InAt(0);
4136 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004137 Location out = locations->Out();
4138 bool is_volatile = field_info.IsVolatile();
4139 Primitive::Type field_type = field_info.GetFieldType();
4140 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4141
4142 switch (field_type) {
4143 case Primitive::kPrimBoolean: {
4144 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4145 break;
4146 }
4147
4148 case Primitive::kPrimByte: {
4149 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4150 break;
4151 }
4152
4153 case Primitive::kPrimShort: {
4154 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4155 break;
4156 }
4157
4158 case Primitive::kPrimChar: {
4159 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4160 break;
4161 }
4162
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004163 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004164 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4165 break;
4166 }
4167
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004168 case Primitive::kPrimNot: {
4169 // /* HeapReference<Object> */ out = *(base + offset)
4170 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004171 // Note that a potential implicit null check is handled in this
4172 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4173 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004174 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004175 if (is_volatile) {
4176 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4177 }
4178 } else {
4179 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4180 codegen_->MaybeRecordImplicitNullCheck(instruction);
4181 if (is_volatile) {
4182 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4183 }
4184 // If read barriers are enabled, emit read barriers other than
4185 // Baker's using a slow path (and also unpoison the loaded
4186 // reference, if heap poisoning is enabled).
4187 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4188 }
4189 break;
4190 }
4191
Calin Juravle52c48962014-12-16 17:02:57 +00004192 case Primitive::kPrimLong: {
4193 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4194 break;
4195 }
4196
4197 case Primitive::kPrimFloat: {
4198 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4199 break;
4200 }
4201
4202 case Primitive::kPrimDouble: {
4203 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4204 break;
4205 }
4206
4207 case Primitive::kPrimVoid:
4208 LOG(FATAL) << "Unreachable type " << field_type;
4209 UNREACHABLE();
4210 }
4211
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004212 if (field_type == Primitive::kPrimNot) {
4213 // Potential implicit null checks, in the case of reference
4214 // fields, are handled in the previous switch statement.
4215 } else {
4216 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004217 }
Roland Levillain4d027112015-07-01 15:41:14 +01004218
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004219 if (is_volatile) {
4220 if (field_type == Primitive::kPrimNot) {
4221 // Memory barriers, in the case of references, are also handled
4222 // in the previous switch statement.
4223 } else {
4224 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4225 }
Roland Levillain4d027112015-07-01 15:41:14 +01004226 }
Calin Juravle52c48962014-12-16 17:02:57 +00004227}
4228
4229void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4230 const FieldInfo& field_info) {
4231 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4232
4233 LocationSummary* locations =
4234 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004235 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004236 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004237 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004238 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004239
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004240 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004241 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004242 if (is_volatile) {
4243 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4244 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4245 } else {
4246 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4247 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004248 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004249 if (is_volatile) {
4250 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4251 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4252 } else {
4253 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4254 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004255 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004256 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004257 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004258 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004259 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004260 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4261 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004262 locations->AddTemp(Location::RequiresRegister());
4263 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004264}
4265
Calin Juravle52c48962014-12-16 17:02:57 +00004266void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004267 const FieldInfo& field_info,
4268 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004269 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4270
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004271 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004272 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4273 Location value = locations->InAt(1);
4274 bool is_volatile = field_info.IsVolatile();
4275 Primitive::Type field_type = field_info.GetFieldType();
4276 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4277
4278 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004279 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004280 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004281
Mark Mendellea5af682015-10-22 17:35:49 -04004282 bool maybe_record_implicit_null_check_done = false;
4283
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284 switch (field_type) {
4285 case Primitive::kPrimBoolean:
4286 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004287 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004288 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004289 __ movb(Address(base, offset), Immediate(v));
4290 } else {
4291 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4292 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004293 break;
4294 }
4295
4296 case Primitive::kPrimShort:
4297 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004298 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004299 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004300 __ movw(Address(base, offset), Immediate(v));
4301 } else {
4302 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4303 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304 break;
4305 }
4306
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004307 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004309 if (value.IsConstant()) {
4310 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004311 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4312 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4313 // Note: if heap poisoning is enabled, no need to poison
4314 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004315 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004316 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004317 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4318 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4319 __ movl(temp, value.AsRegister<CpuRegister>());
4320 __ PoisonHeapReference(temp);
4321 __ movl(Address(base, offset), temp);
4322 } else {
4323 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4324 }
Mark Mendell40741f32015-04-20 22:10:34 -04004325 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004326 break;
4327 }
4328
4329 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004330 if (value.IsConstant()) {
4331 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004332 codegen_->MoveInt64ToAddress(Address(base, offset),
4333 Address(base, offset + sizeof(int32_t)),
4334 v,
4335 instruction);
4336 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004337 } else {
4338 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4339 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004340 break;
4341 }
4342
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004343 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004344 if (value.IsConstant()) {
4345 int32_t v =
4346 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4347 __ movl(Address(base, offset), Immediate(v));
4348 } else {
4349 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4350 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004351 break;
4352 }
4353
4354 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004355 if (value.IsConstant()) {
4356 int64_t v =
4357 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4358 codegen_->MoveInt64ToAddress(Address(base, offset),
4359 Address(base, offset + sizeof(int32_t)),
4360 v,
4361 instruction);
4362 maybe_record_implicit_null_check_done = true;
4363 } else {
4364 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4365 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004366 break;
4367 }
4368
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004369 case Primitive::kPrimVoid:
4370 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004371 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372 }
Calin Juravle52c48962014-12-16 17:02:57 +00004373
Mark Mendellea5af682015-10-22 17:35:49 -04004374 if (!maybe_record_implicit_null_check_done) {
4375 codegen_->MaybeRecordImplicitNullCheck(instruction);
4376 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004377
4378 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4379 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4380 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004381 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004382 }
4383
Calin Juravle52c48962014-12-16 17:02:57 +00004384 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004385 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004386 }
4387}
4388
4389void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4390 HandleFieldSet(instruction, instruction->GetFieldInfo());
4391}
4392
4393void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004394 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004395}
4396
4397void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004398 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399}
4400
4401void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004402 HandleFieldGet(instruction, instruction->GetFieldInfo());
4403}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404
Calin Juravle52c48962014-12-16 17:02:57 +00004405void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4406 HandleFieldGet(instruction);
4407}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408
Calin Juravle52c48962014-12-16 17:02:57 +00004409void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4410 HandleFieldGet(instruction, instruction->GetFieldInfo());
4411}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004412
Calin Juravle52c48962014-12-16 17:02:57 +00004413void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4414 HandleFieldSet(instruction, instruction->GetFieldInfo());
4415}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416
Calin Juravle52c48962014-12-16 17:02:57 +00004417void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004418 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419}
4420
Calin Juravlee460d1d2015-09-29 04:52:17 +01004421void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4422 HUnresolvedInstanceFieldGet* instruction) {
4423 FieldAccessCallingConventionX86_64 calling_convention;
4424 codegen_->CreateUnresolvedFieldLocationSummary(
4425 instruction, instruction->GetFieldType(), calling_convention);
4426}
4427
4428void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4429 HUnresolvedInstanceFieldGet* instruction) {
4430 FieldAccessCallingConventionX86_64 calling_convention;
4431 codegen_->GenerateUnresolvedFieldAccess(instruction,
4432 instruction->GetFieldType(),
4433 instruction->GetFieldIndex(),
4434 instruction->GetDexPc(),
4435 calling_convention);
4436}
4437
4438void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4439 HUnresolvedInstanceFieldSet* instruction) {
4440 FieldAccessCallingConventionX86_64 calling_convention;
4441 codegen_->CreateUnresolvedFieldLocationSummary(
4442 instruction, instruction->GetFieldType(), calling_convention);
4443}
4444
4445void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4446 HUnresolvedInstanceFieldSet* instruction) {
4447 FieldAccessCallingConventionX86_64 calling_convention;
4448 codegen_->GenerateUnresolvedFieldAccess(instruction,
4449 instruction->GetFieldType(),
4450 instruction->GetFieldIndex(),
4451 instruction->GetDexPc(),
4452 calling_convention);
4453}
4454
4455void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4456 HUnresolvedStaticFieldGet* instruction) {
4457 FieldAccessCallingConventionX86_64 calling_convention;
4458 codegen_->CreateUnresolvedFieldLocationSummary(
4459 instruction, instruction->GetFieldType(), calling_convention);
4460}
4461
4462void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4463 HUnresolvedStaticFieldGet* instruction) {
4464 FieldAccessCallingConventionX86_64 calling_convention;
4465 codegen_->GenerateUnresolvedFieldAccess(instruction,
4466 instruction->GetFieldType(),
4467 instruction->GetFieldIndex(),
4468 instruction->GetDexPc(),
4469 calling_convention);
4470}
4471
4472void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4473 HUnresolvedStaticFieldSet* instruction) {
4474 FieldAccessCallingConventionX86_64 calling_convention;
4475 codegen_->CreateUnresolvedFieldLocationSummary(
4476 instruction, instruction->GetFieldType(), calling_convention);
4477}
4478
4479void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4480 HUnresolvedStaticFieldSet* instruction) {
4481 FieldAccessCallingConventionX86_64 calling_convention;
4482 codegen_->GenerateUnresolvedFieldAccess(instruction,
4483 instruction->GetFieldType(),
4484 instruction->GetFieldIndex(),
4485 instruction->GetDexPc(),
4486 calling_convention);
4487}
4488
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004490 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4491 ? LocationSummary::kCallOnSlowPath
4492 : LocationSummary::kNoCall;
4493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4494 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004495 ? Location::RequiresRegister()
4496 : Location::Any();
4497 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004498 if (instruction->HasUses()) {
4499 locations->SetOut(Location::SameAsFirstInput());
4500 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004501}
4502
Calin Juravle2ae48182016-03-16 14:05:09 +00004503void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4504 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004505 return;
4506 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004507 LocationSummary* locations = instruction->GetLocations();
4508 Location obj = locations->InAt(0);
4509
4510 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004511 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004512}
4513
Calin Juravle2ae48182016-03-16 14:05:09 +00004514void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004515 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004516 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004517
4518 LocationSummary* locations = instruction->GetLocations();
4519 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520
4521 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004522 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004523 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004524 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004525 } else {
4526 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004527 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004528 __ jmp(slow_path->GetEntryLabel());
4529 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004530 }
4531 __ j(kEqual, slow_path->GetEntryLabel());
4532}
4533
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004534void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004535 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004536}
4537
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004539 bool object_array_get_with_read_barrier =
4540 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004541 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004542 new (GetGraph()->GetArena()) LocationSummary(instruction,
4543 object_array_get_with_read_barrier ?
4544 LocationSummary::kCallOnSlowPath :
4545 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004546 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004547 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004548 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4550 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004551 // The output overlaps for an object array get when read barriers
4552 // are enabled: we do not want the move to overwrite the array's
4553 // location, as we need it to emit the read barrier.
4554 locations->SetOut(
4555 Location::RequiresRegister(),
4556 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004557 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004558}
4559
4560void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4561 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004562 Location obj_loc = locations->InAt(0);
4563 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004564 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004565 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004566 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004567
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004568 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004569 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004571 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004572 if (index.IsConstant()) {
4573 __ movzxb(out, Address(obj,
4574 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4575 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004576 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 }
4578 break;
4579 }
4580
4581 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004582 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583 if (index.IsConstant()) {
4584 __ movsxb(out, Address(obj,
4585 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4586 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004587 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588 }
4589 break;
4590 }
4591
4592 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004593 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 if (index.IsConstant()) {
4595 __ movsxw(out, Address(obj,
4596 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4597 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004598 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004599 }
4600 break;
4601 }
4602
4603 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004604 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 if (index.IsConstant()) {
4606 __ movzxw(out, Address(obj,
4607 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4608 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004609 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 }
4611 break;
4612 }
4613
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004614 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004615 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 if (index.IsConstant()) {
4617 __ movl(out, Address(obj,
4618 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4619 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004620 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621 }
4622 break;
4623 }
4624
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004625 case Primitive::kPrimNot: {
4626 static_assert(
4627 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4628 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004629 // /* HeapReference<Object> */ out =
4630 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4631 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004632 // Note that a potential implicit null check is handled in this
4633 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4634 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004635 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004636 } else {
4637 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4638 if (index.IsConstant()) {
4639 uint32_t offset =
4640 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4641 __ movl(out, Address(obj, offset));
4642 codegen_->MaybeRecordImplicitNullCheck(instruction);
4643 // If read barriers are enabled, emit read barriers other than
4644 // Baker's using a slow path (and also unpoison the loaded
4645 // reference, if heap poisoning is enabled).
4646 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4647 } else {
4648 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4649 codegen_->MaybeRecordImplicitNullCheck(instruction);
4650 // If read barriers are enabled, emit read barriers other than
4651 // Baker's using a slow path (and also unpoison the loaded
4652 // reference, if heap poisoning is enabled).
4653 codegen_->MaybeGenerateReadBarrierSlow(
4654 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4655 }
4656 }
4657 break;
4658 }
4659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004661 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004662 if (index.IsConstant()) {
4663 __ movq(out, Address(obj,
4664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4665 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 }
4668 break;
4669 }
4670
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004671 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004672 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004673 if (index.IsConstant()) {
4674 __ movss(out, Address(obj,
4675 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4676 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004677 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004678 }
4679 break;
4680 }
4681
4682 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004683 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004684 if (index.IsConstant()) {
4685 __ movsd(out, Address(obj,
4686 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4687 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004688 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004689 }
4690 break;
4691 }
4692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004693 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004694 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004695 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004696 }
Roland Levillain4d027112015-07-01 15:41:14 +01004697
4698 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004699 // Potential implicit null checks, in the case of reference
4700 // arrays, are handled in the previous switch statement.
4701 } else {
4702 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004703 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704}
4705
4706void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004707 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004708
4709 bool needs_write_barrier =
4710 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004711 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004712
Nicolas Geoffray39468442014-09-02 15:17:15 +01004713 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004714 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004715 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004716 LocationSummary::kCallOnSlowPath :
4717 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004718
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004719 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004720 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4721 if (Primitive::IsFloatingPointType(value_type)) {
4722 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004723 } else {
4724 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4725 }
4726
4727 if (needs_write_barrier) {
4728 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004729 // These registers may be used for Baker read barriers too.
4730 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004731 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004733}
4734
4735void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4736 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004737 Location array_loc = locations->InAt(0);
4738 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004740 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004741 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004742 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004743 bool needs_write_barrier =
4744 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004745 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4746 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4747 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004748
4749 switch (value_type) {
4750 case Primitive::kPrimBoolean:
4751 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004752 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4753 Address address = index.IsConstant()
4754 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4755 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4756 if (value.IsRegister()) {
4757 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004758 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004761 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762 break;
4763 }
4764
4765 case Primitive::kPrimShort:
4766 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004767 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4768 Address address = index.IsConstant()
4769 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4770 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4771 if (value.IsRegister()) {
4772 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004774 DCHECK(value.IsConstant()) << value;
4775 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004776 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004777 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004778 break;
4779 }
4780
4781 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004782 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4783 Address address = index.IsConstant()
4784 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4785 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004786
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004787 if (!value.IsRegister()) {
4788 // Just setting null.
4789 DCHECK(instruction->InputAt(2)->IsNullConstant());
4790 DCHECK(value.IsConstant()) << value;
4791 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004792 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004794 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004795 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004796 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004797
4798 DCHECK(needs_write_barrier);
4799 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004800 // We cannot use a NearLabel for `done`, as its range may be too
4801 // short when Baker read barriers are enabled.
4802 Label done;
4803 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004805 Location temp_loc = locations->GetTemp(0);
4806 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004807 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4809 codegen_->AddSlowPath(slow_path);
4810 if (instruction->GetValueCanBeNull()) {
4811 __ testl(register_value, register_value);
4812 __ j(kNotEqual, &not_null);
4813 __ movl(address, Immediate(0));
4814 codegen_->MaybeRecordImplicitNullCheck(instruction);
4815 __ jmp(&done);
4816 __ Bind(&not_null);
4817 }
4818
Roland Levillain0d5a2812015-11-13 10:07:31 +00004819 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01004820 if (!kUseBakerReadBarrier) {
4821 // When (non-Baker) read barriers are enabled, the type
4822 // checking instrumentation requires two read barriers
4823 // generated by CodeGeneratorX86_64::GenerateReadBarrierSlow:
4824 //
4825 // __ movl(temp2, temp);
4826 // // /* HeapReference<Class> */ temp = temp->component_type_
4827 // __ movl(temp, Address(temp, component_offset));
4828 // codegen_->GenerateReadBarrierSlow(
4829 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4830 //
4831 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4832 // __ movl(temp2, Address(register_value, class_offset));
4833 // codegen_->GenerateReadBarrierSlow(
4834 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4835 //
4836 // __ cmpl(temp, temp2);
4837 //
4838 // However, the second read barrier may trash `temp`, as it
4839 // is a temporary register, and as such would not be saved
4840 // along with live registers before calling the runtime (nor
4841 // restored afterwards). So in this case, we bail out and
4842 // delegate the work to the array set slow path.
4843 //
4844 // TODO: Extend the register allocator to support a new
4845 // "(locally) live temp" location so as to avoid always
4846 // going into the slow path when read barriers are enabled?
4847 //
4848 // There is no such problem with Baker read barriers (see below).
4849 __ jmp(slow_path->GetEntryLabel());
4850 } else {
4851 Location temp2_loc = locations->GetTemp(1);
4852 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
4853 // /* HeapReference<Class> */ temp = array->klass_
4854 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4855 instruction, temp_loc, array, class_offset, /* needs_null_check */ true);
4856
4857 // /* HeapReference<Class> */ temp = temp->component_type_
4858 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4859 instruction, temp_loc, temp, component_offset, /* needs_null_check */ false);
4860 // Register `temp` is not trashed by the read barrier
4861 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
4862 // as that method produces a call to a ReadBarrierMarkRegX
4863 // entry point, which saves all potentially live registers,
4864 // including temporaries such a `temp`.
4865 // /* HeapReference<Class> */ temp2 = register_value->klass_
4866 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4867 instruction, temp2_loc, register_value, class_offset, /* needs_null_check */ false);
4868 // If heap poisoning is enabled, `temp` and `temp2` have
4869 // been unpoisoned by the the previous calls to
4870 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4871 __ cmpl(temp, temp2);
4872
4873 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4874 __ j(kEqual, &do_put);
4875 // We do not need to emit a read barrier for the
4876 // following heap reference load, as `temp` is only used
4877 // in a comparison with null below, and this reference
4878 // is not kept afterwards. Also, if heap poisoning is
4879 // enabled, there is no need to unpoison that heap
4880 // reference for the same reason (comparison with null).
4881 __ cmpl(Address(temp, super_offset), Immediate(0));
4882 __ j(kNotEqual, slow_path->GetEntryLabel());
4883 __ Bind(&do_put);
4884 } else {
4885 __ j(kNotEqual, slow_path->GetEntryLabel());
4886 }
4887 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004888 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01004889 // Non read barrier code.
4890
Roland Levillain0d5a2812015-11-13 10:07:31 +00004891 // /* HeapReference<Class> */ temp = array->klass_
4892 __ movl(temp, Address(array, class_offset));
4893 codegen_->MaybeRecordImplicitNullCheck(instruction);
4894 __ MaybeUnpoisonHeapReference(temp);
4895
4896 // /* HeapReference<Class> */ temp = temp->component_type_
4897 __ movl(temp, Address(temp, component_offset));
4898 // If heap poisoning is enabled, no need to unpoison `temp`
4899 // nor the object reference in `register_value->klass`, as
4900 // we are comparing two poisoned references.
4901 __ cmpl(temp, Address(register_value, class_offset));
4902
4903 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4904 __ j(kEqual, &do_put);
4905 // If heap poisoning is enabled, the `temp` reference has
4906 // not been unpoisoned yet; unpoison it now.
4907 __ MaybeUnpoisonHeapReference(temp);
4908
Roland Levillain16d9f942016-08-25 17:27:56 +01004909 // If heap poisoning is enabled, no need to unpoison the
4910 // heap reference loaded below, as it is only used for a
4911 // comparison with null.
4912 __ cmpl(Address(temp, super_offset), Immediate(0));
Roland Levillain0d5a2812015-11-13 10:07:31 +00004913 __ j(kNotEqual, slow_path->GetEntryLabel());
4914 __ Bind(&do_put);
4915 } else {
4916 __ j(kNotEqual, slow_path->GetEntryLabel());
4917 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004918 }
4919 }
4920
4921 if (kPoisonHeapReferences) {
4922 __ movl(temp, register_value);
4923 __ PoisonHeapReference(temp);
4924 __ movl(address, temp);
4925 } else {
4926 __ movl(address, register_value);
4927 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004928 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004929 codegen_->MaybeRecordImplicitNullCheck(instruction);
4930 }
4931
4932 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4933 codegen_->MarkGCCard(
4934 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4935 __ Bind(&done);
4936
4937 if (slow_path != nullptr) {
4938 __ Bind(slow_path->GetExitLabel());
4939 }
4940
4941 break;
4942 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004943
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004944 case Primitive::kPrimInt: {
4945 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4946 Address address = index.IsConstant()
4947 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4948 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4949 if (value.IsRegister()) {
4950 __ movl(address, value.AsRegister<CpuRegister>());
4951 } else {
4952 DCHECK(value.IsConstant()) << value;
4953 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4954 __ movl(address, Immediate(v));
4955 }
4956 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004957 break;
4958 }
4959
4960 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004961 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4962 Address address = index.IsConstant()
4963 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4964 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4965 if (value.IsRegister()) {
4966 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004967 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004969 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004970 Address address_high = index.IsConstant()
4971 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4972 offset + sizeof(int32_t))
4973 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4974 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004975 }
4976 break;
4977 }
4978
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004979 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004980 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4981 Address address = index.IsConstant()
4982 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4983 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004984 if (value.IsFpuRegister()) {
4985 __ movss(address, value.AsFpuRegister<XmmRegister>());
4986 } else {
4987 DCHECK(value.IsConstant());
4988 int32_t v =
4989 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4990 __ movl(address, Immediate(v));
4991 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004992 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004993 break;
4994 }
4995
4996 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004997 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4998 Address address = index.IsConstant()
4999 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5000 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005001 if (value.IsFpuRegister()) {
5002 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5003 codegen_->MaybeRecordImplicitNullCheck(instruction);
5004 } else {
5005 int64_t v =
5006 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5007 Address address_high = index.IsConstant()
5008 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5009 offset + sizeof(int32_t))
5010 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5011 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5012 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005013 break;
5014 }
5015
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005016 case Primitive::kPrimVoid:
5017 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005018 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005019 }
5020}
5021
5022void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005023 LocationSummary* locations =
5024 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005025 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005026 if (!instruction->IsEmittedAtUseSite()) {
5027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5028 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029}
5030
5031void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005032 if (instruction->IsEmittedAtUseSite()) {
5033 return;
5034 }
5035
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005037 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005038 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5039 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005041 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042}
5043
5044void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005045 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5046 ? LocationSummary::kCallOnSlowPath
5047 : LocationSummary::kNoCall;
5048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005049 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005050 HInstruction* length = instruction->InputAt(1);
5051 if (!length->IsEmittedAtUseSite()) {
5052 locations->SetInAt(1, Location::RegisterOrConstant(length));
5053 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005054 if (instruction->HasUses()) {
5055 locations->SetOut(Location::SameAsFirstInput());
5056 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057}
5058
5059void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5060 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005061 Location index_loc = locations->InAt(0);
5062 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005063 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064
Mark Mendell99dbd682015-04-22 16:18:52 -04005065 if (length_loc.IsConstant()) {
5066 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5067 if (index_loc.IsConstant()) {
5068 // BCE will remove the bounds check if we are guarenteed to pass.
5069 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5070 if (index < 0 || index >= length) {
5071 codegen_->AddSlowPath(slow_path);
5072 __ jmp(slow_path->GetEntryLabel());
5073 } else {
5074 // Some optimization after BCE may have generated this, and we should not
5075 // generate a bounds check if it is a valid range.
5076 }
5077 return;
5078 }
5079
5080 // We have to reverse the jump condition because the length is the constant.
5081 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5082 __ cmpl(index_reg, Immediate(length));
5083 codegen_->AddSlowPath(slow_path);
5084 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005085 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005086 HInstruction* array_length = instruction->InputAt(1);
5087 if (array_length->IsEmittedAtUseSite()) {
5088 // Address the length field in the array.
5089 DCHECK(array_length->IsArrayLength());
5090 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5091 Location array_loc = array_length->GetLocations()->InAt(0);
5092 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
5093 if (index_loc.IsConstant()) {
5094 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5095 __ cmpl(array_len, Immediate(value));
5096 } else {
5097 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5098 }
5099 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005100 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005101 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5102 if (index_loc.IsConstant()) {
5103 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5104 __ cmpl(length, Immediate(value));
5105 } else {
5106 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5107 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005108 }
5109 codegen_->AddSlowPath(slow_path);
5110 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005111 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112}
5113
5114void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5115 CpuRegister card,
5116 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005117 CpuRegister value,
5118 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005119 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005120 if (value_can_be_null) {
5121 __ testl(value, value);
5122 __ j(kEqual, &is_null);
5123 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005124 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005125 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 __ movq(temp, object);
5127 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005128 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005129 if (value_can_be_null) {
5130 __ Bind(&is_null);
5131 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132}
5133
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005134void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005135 LOG(FATAL) << "Unimplemented";
5136}
5137
5138void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005139 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5140}
5141
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005142void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5143 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5144}
5145
5146void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005147 HBasicBlock* block = instruction->GetBlock();
5148 if (block->GetLoopInformation() != nullptr) {
5149 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5150 // The back edge will generate the suspend check.
5151 return;
5152 }
5153 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5154 // The goto will generate the suspend check.
5155 return;
5156 }
5157 GenerateSuspendCheck(instruction, nullptr);
5158}
5159
5160void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5161 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005162 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005163 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5164 if (slow_path == nullptr) {
5165 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5166 instruction->SetSlowPath(slow_path);
5167 codegen_->AddSlowPath(slow_path);
5168 if (successor != nullptr) {
5169 DCHECK(successor->IsLoopHeader());
5170 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5171 }
5172 } else {
5173 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5174 }
5175
Andreas Gampe542451c2016-07-26 09:02:02 -07005176 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005177 /* no_rip */ true),
5178 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005179 if (successor == nullptr) {
5180 __ j(kNotEqual, slow_path->GetEntryLabel());
5181 __ Bind(slow_path->GetReturnLabel());
5182 } else {
5183 __ j(kEqual, codegen_->GetLabelOf(successor));
5184 __ jmp(slow_path->GetEntryLabel());
5185 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005186}
5187
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005188X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5189 return codegen_->GetAssembler();
5190}
5191
5192void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005193 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005194 Location source = move->GetSource();
5195 Location destination = move->GetDestination();
5196
5197 if (source.IsRegister()) {
5198 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005199 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005200 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005201 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005202 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005203 } else {
5204 DCHECK(destination.IsDoubleStackSlot());
5205 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005206 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005207 }
5208 } else if (source.IsStackSlot()) {
5209 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005211 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005212 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005213 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005215 } else {
5216 DCHECK(destination.IsStackSlot());
5217 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5218 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5219 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005220 } else if (source.IsDoubleStackSlot()) {
5221 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005222 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005223 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005224 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005225 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5226 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005227 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005228 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005229 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5230 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5231 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005232 } else if (source.IsConstant()) {
5233 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005234 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5235 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005236 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005237 if (value == 0) {
5238 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5239 } else {
5240 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5241 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005242 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005243 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005244 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005245 }
5246 } else if (constant->IsLongConstant()) {
5247 int64_t value = constant->AsLongConstant()->GetValue();
5248 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005249 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005250 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005251 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005252 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005253 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005254 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005255 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005256 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005257 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005258 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005259 } else {
5260 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005261 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005262 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5263 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005264 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005265 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005266 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005267 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005268 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005269 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005270 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005271 } else {
5272 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005273 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005274 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005275 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005276 } else if (source.IsFpuRegister()) {
5277 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005278 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005279 } else if (destination.IsStackSlot()) {
5280 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005281 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005282 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005283 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005285 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005286 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005287 }
5288}
5289
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005290void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005291 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005292 __ movl(Address(CpuRegister(RSP), mem), reg);
5293 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005294}
5295
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005296void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005297 ScratchRegisterScope ensure_scratch(
5298 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5299
5300 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5301 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5302 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5303 Address(CpuRegister(RSP), mem2 + stack_offset));
5304 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5305 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5306 CpuRegister(ensure_scratch.GetRegister()));
5307}
5308
Mark Mendell8a1c7282015-06-29 15:41:28 -04005309void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5310 __ movq(CpuRegister(TMP), reg1);
5311 __ movq(reg1, reg2);
5312 __ movq(reg2, CpuRegister(TMP));
5313}
5314
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005315void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5316 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5317 __ movq(Address(CpuRegister(RSP), mem), reg);
5318 __ movq(reg, CpuRegister(TMP));
5319}
5320
5321void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5322 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005323 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005324
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005325 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5326 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5327 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5328 Address(CpuRegister(RSP), mem2 + stack_offset));
5329 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5330 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5331 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005332}
5333
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005334void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5335 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5336 __ movss(Address(CpuRegister(RSP), mem), reg);
5337 __ movd(reg, CpuRegister(TMP));
5338}
5339
5340void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5341 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5342 __ movsd(Address(CpuRegister(RSP), mem), reg);
5343 __ movd(reg, CpuRegister(TMP));
5344}
5345
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005346void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005347 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005348 Location source = move->GetSource();
5349 Location destination = move->GetDestination();
5350
5351 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005352 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005353 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005354 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005355 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005356 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005357 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005358 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5359 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005360 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005361 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005362 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005363 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5364 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005365 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005366 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5367 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5368 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005369 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005370 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005371 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005372 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005373 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005374 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005375 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005376 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005377 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005378 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005379 }
5380}
5381
5382
5383void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5384 __ pushq(CpuRegister(reg));
5385}
5386
5387
5388void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5389 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005390}
5391
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005392void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005393 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005394 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5395 Immediate(mirror::Class::kStatusInitialized));
5396 __ j(kLess, slow_path->GetEntryLabel());
5397 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005398 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005399}
5400
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005401HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5402 HLoadClass::LoadKind desired_class_load_kind) {
5403 if (kEmitCompilerReadBarrier) {
5404 switch (desired_class_load_kind) {
5405 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5406 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5407 case HLoadClass::LoadKind::kBootImageAddress:
5408 // TODO: Implement for read barrier.
5409 return HLoadClass::LoadKind::kDexCacheViaMethod;
5410 default:
5411 break;
5412 }
5413 }
5414 switch (desired_class_load_kind) {
5415 case HLoadClass::LoadKind::kReferrersClass:
5416 break;
5417 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5418 DCHECK(!GetCompilerOptions().GetCompilePic());
5419 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5420 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5421 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5422 DCHECK(GetCompilerOptions().GetCompilePic());
5423 break;
5424 case HLoadClass::LoadKind::kBootImageAddress:
5425 break;
5426 case HLoadClass::LoadKind::kDexCacheAddress:
5427 DCHECK(Runtime::Current()->UseJitCompilation());
5428 break;
5429 case HLoadClass::LoadKind::kDexCachePcRelative:
5430 DCHECK(!Runtime::Current()->UseJitCompilation());
5431 break;
5432 case HLoadClass::LoadKind::kDexCacheViaMethod:
5433 break;
5434 }
5435 return desired_class_load_kind;
5436}
5437
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005438void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005439 if (cls->NeedsAccessCheck()) {
5440 InvokeRuntimeCallingConvention calling_convention;
5441 CodeGenerator::CreateLoadClassLocationSummary(
5442 cls,
5443 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5444 Location::RegisterLocation(RAX),
5445 /* code_generator_supports_read_barrier */ true);
5446 return;
5447 }
5448
5449 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5450 ? LocationSummary::kCallOnSlowPath
5451 : LocationSummary::kNoCall;
5452 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5453 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5454 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5455 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5456 locations->SetInAt(0, Location::RequiresRegister());
5457 }
5458 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005459}
5460
5461void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005462 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005463 if (cls->NeedsAccessCheck()) {
5464 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005465 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005466 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005467 return;
5468 }
5469
Roland Levillain0d5a2812015-11-13 10:07:31 +00005470 Location out_loc = locations->Out();
5471 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005473 bool generate_null_check = false;
5474 switch (cls->GetLoadKind()) {
5475 case HLoadClass::LoadKind::kReferrersClass: {
5476 DCHECK(!cls->CanCallRuntime());
5477 DCHECK(!cls->MustGenerateClinitCheck());
5478 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5479 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5480 GenerateGcRootFieldLoad(
5481 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5482 break;
5483 }
5484 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5485 DCHECK(!kEmitCompilerReadBarrier);
5486 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5487 codegen_->RecordTypePatch(cls);
5488 break;
5489 case HLoadClass::LoadKind::kBootImageAddress: {
5490 DCHECK(!kEmitCompilerReadBarrier);
5491 DCHECK_NE(cls->GetAddress(), 0u);
5492 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5493 __ movl(out, Immediate(address)); // Zero-extended.
5494 codegen_->RecordSimplePatch();
5495 break;
5496 }
5497 case HLoadClass::LoadKind::kDexCacheAddress: {
5498 DCHECK_NE(cls->GetAddress(), 0u);
5499 // /* GcRoot<mirror::Class> */ out = *address
5500 if (IsUint<32>(cls->GetAddress())) {
5501 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5502 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005503 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005504 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5505 __ movq(out, Immediate(cls->GetAddress()));
5506 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005507 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005508 generate_null_check = !cls->IsInDexCache();
5509 break;
5510 }
5511 case HLoadClass::LoadKind::kDexCachePcRelative: {
5512 uint32_t offset = cls->GetDexCacheElementOffset();
5513 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5514 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5515 /* no_rip */ false);
5516 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5517 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5518 generate_null_check = !cls->IsInDexCache();
5519 break;
5520 }
5521 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5522 // /* GcRoot<mirror::Class>[] */ out =
5523 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5524 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5525 __ movq(out,
5526 Address(current_method,
5527 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5528 // /* GcRoot<mirror::Class> */ out = out[type_index]
5529 GenerateGcRootFieldLoad(
5530 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5531 generate_null_check = !cls->IsInDexCache();
5532 break;
5533 }
5534 default:
5535 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5536 UNREACHABLE();
5537 }
5538
5539 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5540 DCHECK(cls->CanCallRuntime());
5541 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5542 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5543 codegen_->AddSlowPath(slow_path);
5544 if (generate_null_check) {
5545 __ testl(out, out);
5546 __ j(kEqual, slow_path->GetEntryLabel());
5547 }
5548 if (cls->MustGenerateClinitCheck()) {
5549 GenerateClassInitializationCheck(slow_path, out);
5550 } else {
5551 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005552 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005553 }
5554}
5555
5556void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5557 LocationSummary* locations =
5558 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5559 locations->SetInAt(0, Location::RequiresRegister());
5560 if (check->HasUses()) {
5561 locations->SetOut(Location::SameAsFirstInput());
5562 }
5563}
5564
5565void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005566 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005567 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005568 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005569 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005570 GenerateClassInitializationCheck(slow_path,
5571 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005572}
5573
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005574HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5575 HLoadString::LoadKind desired_string_load_kind) {
5576 if (kEmitCompilerReadBarrier) {
5577 switch (desired_string_load_kind) {
5578 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5579 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5580 case HLoadString::LoadKind::kBootImageAddress:
5581 // TODO: Implement for read barrier.
5582 return HLoadString::LoadKind::kDexCacheViaMethod;
5583 default:
5584 break;
5585 }
5586 }
5587 switch (desired_string_load_kind) {
5588 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5589 DCHECK(!GetCompilerOptions().GetCompilePic());
5590 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5591 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5592 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5593 DCHECK(GetCompilerOptions().GetCompilePic());
5594 break;
5595 case HLoadString::LoadKind::kBootImageAddress:
5596 break;
5597 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005598 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005599 break;
5600 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005601 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005602 break;
5603 case HLoadString::LoadKind::kDexCacheViaMethod:
5604 break;
5605 }
5606 return desired_string_load_kind;
5607}
5608
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005609void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005610 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005611 ? LocationSummary::kCallOnSlowPath
5612 : LocationSummary::kNoCall;
5613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005614 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5615 locations->SetInAt(0, Location::RequiresRegister());
5616 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005617 locations->SetOut(Location::RequiresRegister());
5618}
5619
5620void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005621 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005622 Location out_loc = locations->Out();
5623 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005624
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005625 switch (load->GetLoadKind()) {
5626 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5627 DCHECK(!kEmitCompilerReadBarrier);
5628 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5629 codegen_->RecordStringPatch(load);
5630 return; // No dex cache slow path.
5631 }
5632 case HLoadString::LoadKind::kBootImageAddress: {
5633 DCHECK(!kEmitCompilerReadBarrier);
5634 DCHECK_NE(load->GetAddress(), 0u);
5635 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5636 __ movl(out, Immediate(address)); // Zero-extended.
5637 codegen_->RecordSimplePatch();
5638 return; // No dex cache slow path.
5639 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005640 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005641 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005642 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005643
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005644 // TODO: Re-add the compiler code to do string dex cache lookup again.
5645 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5646 codegen_->AddSlowPath(slow_path);
5647 __ jmp(slow_path->GetEntryLabel());
5648 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005649}
5650
David Brazdilcb1c0552015-08-04 16:22:25 +01005651static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005652 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005653 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005654}
5655
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005656void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5657 LocationSummary* locations =
5658 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5659 locations->SetOut(Location::RequiresRegister());
5660}
5661
5662void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005663 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5664}
5665
5666void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5667 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5668}
5669
5670void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5671 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005672}
5673
5674void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5675 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005677 InvokeRuntimeCallingConvention calling_convention;
5678 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5679}
5680
5681void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005682 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005683 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005684}
5685
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005686static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5687 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005688 !kUseBakerReadBarrier &&
5689 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005690 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5691 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5692}
5693
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005694void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005695 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005696 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5697 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005698 case TypeCheckKind::kExactCheck:
5699 case TypeCheckKind::kAbstractClassCheck:
5700 case TypeCheckKind::kClassHierarchyCheck:
5701 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005702 call_kind =
5703 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005704 break;
5705 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706 case TypeCheckKind::kUnresolvedCheck:
5707 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 call_kind = LocationSummary::kCallOnSlowPath;
5709 break;
5710 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005711
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005713 locations->SetInAt(0, Location::RequiresRegister());
5714 locations->SetInAt(1, Location::Any());
5715 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5716 locations->SetOut(Location::RequiresRegister());
5717 // When read barriers are enabled, we need a temporary register for
5718 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005719 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005720 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005721 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005722}
5723
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005724void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005725 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005726 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005727 Location obj_loc = locations->InAt(0);
5728 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005729 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005730 Location out_loc = locations->Out();
5731 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005732 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005733 locations->GetTemp(0) :
5734 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005735 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005736 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5737 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5738 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005739 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005741
5742 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005743 // Avoid null check if we know obj is not null.
5744 if (instruction->MustDoNullCheck()) {
5745 __ testl(obj, obj);
5746 __ j(kEqual, &zero);
5747 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005748
Roland Levillain0d5a2812015-11-13 10:07:31 +00005749 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005750 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005752 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005753 case TypeCheckKind::kExactCheck: {
5754 if (cls.IsRegister()) {
5755 __ cmpl(out, cls.AsRegister<CpuRegister>());
5756 } else {
5757 DCHECK(cls.IsStackSlot()) << cls;
5758 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5759 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005760 if (zero.IsLinked()) {
5761 // Classes must be equal for the instanceof to succeed.
5762 __ j(kNotEqual, &zero);
5763 __ movl(out, Immediate(1));
5764 __ jmp(&done);
5765 } else {
5766 __ setcc(kEqual, out);
5767 // setcc only sets the low byte.
5768 __ andl(out, Immediate(1));
5769 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005770 break;
5771 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 case TypeCheckKind::kAbstractClassCheck: {
5774 // If the class is abstract, we eagerly fetch the super class of the
5775 // object to avoid doing a comparison we know will fail.
5776 NearLabel loop, success;
5777 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005779 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005780 __ testl(out, out);
5781 // If `out` is null, we use it for the result, and jump to `done`.
5782 __ j(kEqual, &done);
5783 if (cls.IsRegister()) {
5784 __ cmpl(out, cls.AsRegister<CpuRegister>());
5785 } else {
5786 DCHECK(cls.IsStackSlot()) << cls;
5787 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5788 }
5789 __ j(kNotEqual, &loop);
5790 __ movl(out, Immediate(1));
5791 if (zero.IsLinked()) {
5792 __ jmp(&done);
5793 }
5794 break;
5795 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 case TypeCheckKind::kClassHierarchyCheck: {
5798 // Walk over the class hierarchy to find a match.
5799 NearLabel loop, success;
5800 __ Bind(&loop);
5801 if (cls.IsRegister()) {
5802 __ cmpl(out, cls.AsRegister<CpuRegister>());
5803 } else {
5804 DCHECK(cls.IsStackSlot()) << cls;
5805 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5806 }
5807 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005808 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005809 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005810 __ testl(out, out);
5811 __ j(kNotEqual, &loop);
5812 // If `out` is null, we use it for the result, and jump to `done`.
5813 __ jmp(&done);
5814 __ Bind(&success);
5815 __ movl(out, Immediate(1));
5816 if (zero.IsLinked()) {
5817 __ jmp(&done);
5818 }
5819 break;
5820 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005822 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005823 // Do an exact check.
5824 NearLabel exact_check;
5825 if (cls.IsRegister()) {
5826 __ cmpl(out, cls.AsRegister<CpuRegister>());
5827 } else {
5828 DCHECK(cls.IsStackSlot()) << cls;
5829 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5830 }
5831 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005832 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005833 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005834 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005835 __ testl(out, out);
5836 // If `out` is null, we use it for the result, and jump to `done`.
5837 __ j(kEqual, &done);
5838 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5839 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005840 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 __ movl(out, Immediate(1));
5842 __ jmp(&done);
5843 break;
5844 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005845
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005846 case TypeCheckKind::kArrayCheck: {
5847 if (cls.IsRegister()) {
5848 __ cmpl(out, cls.AsRegister<CpuRegister>());
5849 } else {
5850 DCHECK(cls.IsStackSlot()) << cls;
5851 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5852 }
5853 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005854 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5855 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005856 codegen_->AddSlowPath(slow_path);
5857 __ j(kNotEqual, slow_path->GetEntryLabel());
5858 __ movl(out, Immediate(1));
5859 if (zero.IsLinked()) {
5860 __ jmp(&done);
5861 }
5862 break;
5863 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864
Calin Juravle98893e12015-10-02 21:05:03 +01005865 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005866 case TypeCheckKind::kInterfaceCheck: {
5867 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005868 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005869 // cases.
5870 //
5871 // We cannot directly call the InstanceofNonTrivial runtime
5872 // entry point without resorting to a type checking slow path
5873 // here (i.e. by calling InvokeRuntime directly), as it would
5874 // require to assign fixed registers for the inputs of this
5875 // HInstanceOf instruction (following the runtime calling
5876 // convention), which might be cluttered by the potential first
5877 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005878 //
5879 // TODO: Introduce a new runtime entry point taking the object
5880 // to test (instead of its class) as argument, and let it deal
5881 // with the read barrier issues. This will let us refactor this
5882 // case of the `switch` code as it was previously (with a direct
5883 // call to the runtime not using a type checking slow path).
5884 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005885 DCHECK(locations->OnlyCallsOnSlowPath());
5886 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5887 /* is_fatal */ false);
5888 codegen_->AddSlowPath(slow_path);
5889 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 if (zero.IsLinked()) {
5891 __ jmp(&done);
5892 }
5893 break;
5894 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005895 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005896
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005897 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005898 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899 __ xorl(out, out);
5900 }
5901
5902 if (done.IsLinked()) {
5903 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005904 }
5905
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005906 if (slow_path != nullptr) {
5907 __ Bind(slow_path->GetExitLabel());
5908 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005909}
5910
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005911void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5913 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005914 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5915 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 case TypeCheckKind::kExactCheck:
5917 case TypeCheckKind::kAbstractClassCheck:
5918 case TypeCheckKind::kClassHierarchyCheck:
5919 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005920 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5921 LocationSummary::kCallOnSlowPath :
5922 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005923 break;
5924 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925 case TypeCheckKind::kUnresolvedCheck:
5926 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 call_kind = LocationSummary::kCallOnSlowPath;
5928 break;
5929 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5931 locations->SetInAt(0, Location::RequiresRegister());
5932 locations->SetInAt(1, Location::Any());
5933 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5934 locations->AddTemp(Location::RequiresRegister());
5935 // When read barriers are enabled, we need an additional temporary
5936 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005937 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005939 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005940}
5941
5942void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005943 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005944 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005945 Location obj_loc = locations->InAt(0);
5946 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005947 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005948 Location temp_loc = locations->GetTemp(0);
5949 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005950 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005951 locations->GetTemp(1) :
5952 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5954 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5955 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5956 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957
Roland Levillain0d5a2812015-11-13 10:07:31 +00005958 bool is_type_check_slow_path_fatal =
5959 (type_check_kind == TypeCheckKind::kExactCheck ||
5960 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5961 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5962 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5963 !instruction->CanThrowIntoCatchBlock();
5964 SlowPathCode* type_check_slow_path =
5965 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5966 is_type_check_slow_path_fatal);
5967 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968
Roland Levillain0d5a2812015-11-13 10:07:31 +00005969 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 case TypeCheckKind::kExactCheck:
5971 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005972 NearLabel done;
5973 // Avoid null check if we know obj is not null.
5974 if (instruction->MustDoNullCheck()) {
5975 __ testl(obj, obj);
5976 __ j(kEqual, &done);
5977 }
5978
5979 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005980 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005981
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 if (cls.IsRegister()) {
5983 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5984 } else {
5985 DCHECK(cls.IsStackSlot()) << cls;
5986 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5987 }
5988 // Jump to slow path for throwing the exception or doing a
5989 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005991 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 break;
5993 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005995 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005996 NearLabel done;
5997 // Avoid null check if we know obj is not null.
5998 if (instruction->MustDoNullCheck()) {
5999 __ testl(obj, obj);
6000 __ j(kEqual, &done);
6001 }
6002
6003 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006004 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006005
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 // If the class is abstract, we eagerly fetch the super class of the
6007 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006009 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006011 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012
6013 // If the class reference currently in `temp` is not null, jump
6014 // to the `compare_classes` label to compare it with the checked
6015 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006016 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006017 __ j(kNotEqual, &compare_classes);
6018 // Otherwise, jump to the slow path to throw the exception.
6019 //
6020 // But before, move back the object's class into `temp` before
6021 // going into the slow path, as it has been overwritten in the
6022 // meantime.
6023 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006024 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006025 __ jmp(type_check_slow_path->GetEntryLabel());
6026
6027 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028 if (cls.IsRegister()) {
6029 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6030 } else {
6031 DCHECK(cls.IsStackSlot()) << cls;
6032 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6033 }
6034 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006035 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 break;
6037 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006038
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006040 NearLabel done;
6041 // Avoid null check if we know obj is not null.
6042 if (instruction->MustDoNullCheck()) {
6043 __ testl(obj, obj);
6044 __ j(kEqual, &done);
6045 }
6046
6047 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006048 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006049
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006051 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 __ Bind(&loop);
6053 if (cls.IsRegister()) {
6054 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6055 } else {
6056 DCHECK(cls.IsStackSlot()) << cls;
6057 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6058 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006059 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006060
Roland Levillain0d5a2812015-11-13 10:07:31 +00006061 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006062 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006063
6064 // If the class reference currently in `temp` is not null, jump
6065 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006066 __ testl(temp, temp);
6067 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068 // Otherwise, jump to the slow path to throw the exception.
6069 //
6070 // But before, move back the object's class into `temp` before
6071 // going into the slow path, as it has been overwritten in the
6072 // meantime.
6073 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006074 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006075 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006076 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006077 break;
6078 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006081 // We cannot use a NearLabel here, as its range might be too
6082 // short in some cases when read barriers are enabled. This has
6083 // been observed for instance when the code emitted for this
6084 // case uses high x86-64 registers (R8-R15).
6085 Label done;
6086 // Avoid null check if we know obj is not null.
6087 if (instruction->MustDoNullCheck()) {
6088 __ testl(obj, obj);
6089 __ j(kEqual, &done);
6090 }
6091
6092 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006093 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006094
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006095 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006097 if (cls.IsRegister()) {
6098 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6099 } else {
6100 DCHECK(cls.IsStackSlot()) << cls;
6101 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6102 }
6103 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104
6105 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006107 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006108
6109 // If the component type is not null (i.e. the object is indeed
6110 // an array), jump to label `check_non_primitive_component_type`
6111 // to further check that this component type is not a primitive
6112 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006113 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006114 __ j(kNotEqual, &check_non_primitive_component_type);
6115 // Otherwise, jump to the slow path to throw the exception.
6116 //
6117 // But before, move back the object's class into `temp` before
6118 // going into the slow path, as it has been overwritten in the
6119 // meantime.
6120 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006121 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 __ jmp(type_check_slow_path->GetEntryLabel());
6123
6124 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006125 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006126 __ j(kEqual, &done);
6127 // Same comment as above regarding `temp` and the slow path.
6128 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006129 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006130 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006131 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006132 break;
6133 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134
Calin Juravle98893e12015-10-02 21:05:03 +01006135 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006136 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006137 NearLabel done;
6138 // Avoid null check if we know obj is not null.
6139 if (instruction->MustDoNullCheck()) {
6140 __ testl(obj, obj);
6141 __ j(kEqual, &done);
6142 }
6143
6144 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006145 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006146
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006147 // We always go into the type check slow path for the unresolved
6148 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149 //
6150 // We cannot directly call the CheckCast runtime entry point
6151 // without resorting to a type checking slow path here (i.e. by
6152 // calling InvokeRuntime directly), as it would require to
6153 // assign fixed registers for the inputs of this HInstanceOf
6154 // instruction (following the runtime calling convention), which
6155 // might be cluttered by the potential first read barrier
6156 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006157 //
6158 // TODO: Introduce a new runtime entry point taking the object
6159 // to test (instead of its class) as argument, and let it deal
6160 // with the read barrier issues. This will let us refactor this
6161 // case of the `switch` code as it was previously (with a direct
6162 // call to the runtime not using a type checking slow path).
6163 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006164 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006165 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 break;
6167 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168
Roland Levillain0d5a2812015-11-13 10:07:31 +00006169 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006170}
6171
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006172void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6173 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006175 InvokeRuntimeCallingConvention calling_convention;
6176 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6177}
6178
6179void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006180 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006181 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006182 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006183 if (instruction->IsEnter()) {
6184 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6185 } else {
6186 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6187 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006188}
6189
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006190void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6191void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6192void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6193
6194void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6195 LocationSummary* locations =
6196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6197 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6198 || instruction->GetResultType() == Primitive::kPrimLong);
6199 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006200 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006201 locations->SetOut(Location::SameAsFirstInput());
6202}
6203
6204void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6205 HandleBitwiseOperation(instruction);
6206}
6207
6208void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6209 HandleBitwiseOperation(instruction);
6210}
6211
6212void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6213 HandleBitwiseOperation(instruction);
6214}
6215
6216void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6217 LocationSummary* locations = instruction->GetLocations();
6218 Location first = locations->InAt(0);
6219 Location second = locations->InAt(1);
6220 DCHECK(first.Equals(locations->Out()));
6221
6222 if (instruction->GetResultType() == Primitive::kPrimInt) {
6223 if (second.IsRegister()) {
6224 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006225 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006226 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006227 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006228 } else {
6229 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006230 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006231 }
6232 } else if (second.IsConstant()) {
6233 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6234 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006235 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006236 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006237 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006238 } else {
6239 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006240 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006241 }
6242 } else {
6243 Address address(CpuRegister(RSP), second.GetStackIndex());
6244 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006245 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006246 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006247 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006248 } else {
6249 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006250 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006251 }
6252 }
6253 } else {
6254 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006255 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6256 bool second_is_constant = false;
6257 int64_t value = 0;
6258 if (second.IsConstant()) {
6259 second_is_constant = true;
6260 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006261 }
Mark Mendell40741f32015-04-20 22:10:34 -04006262 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006263
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006264 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006265 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006266 if (is_int32_value) {
6267 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6268 } else {
6269 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6270 }
6271 } else if (second.IsDoubleStackSlot()) {
6272 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006273 } else {
6274 __ andq(first_reg, second.AsRegister<CpuRegister>());
6275 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006276 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006277 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006278 if (is_int32_value) {
6279 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6280 } else {
6281 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6282 }
6283 } else if (second.IsDoubleStackSlot()) {
6284 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006285 } else {
6286 __ orq(first_reg, second.AsRegister<CpuRegister>());
6287 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006288 } else {
6289 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006290 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006291 if (is_int32_value) {
6292 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6293 } else {
6294 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6295 }
6296 } else if (second.IsDoubleStackSlot()) {
6297 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006298 } else {
6299 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6300 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006301 }
6302 }
6303}
6304
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006305void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6306 Location out,
6307 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006308 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006309 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6310 if (kEmitCompilerReadBarrier) {
6311 if (kUseBakerReadBarrier) {
6312 // Load with fast path based Baker's read barrier.
6313 // /* HeapReference<Object> */ out = *(out + offset)
6314 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006315 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006316 } else {
6317 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006318 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006319 // in the following move operation, as we will need it for the
6320 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006321 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006322 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006323 // /* HeapReference<Object> */ out = *(out + offset)
6324 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006325 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006326 }
6327 } else {
6328 // Plain load with no read barrier.
6329 // /* HeapReference<Object> */ out = *(out + offset)
6330 __ movl(out_reg, Address(out_reg, offset));
6331 __ MaybeUnpoisonHeapReference(out_reg);
6332 }
6333}
6334
6335void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6336 Location out,
6337 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006338 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006339 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6340 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6341 if (kEmitCompilerReadBarrier) {
6342 if (kUseBakerReadBarrier) {
6343 // Load with fast path based Baker's read barrier.
6344 // /* HeapReference<Object> */ out = *(obj + offset)
6345 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006346 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006347 } else {
6348 // Load with slow path based read barrier.
6349 // /* HeapReference<Object> */ out = *(obj + offset)
6350 __ movl(out_reg, Address(obj_reg, offset));
6351 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6352 }
6353 } else {
6354 // Plain load with no read barrier.
6355 // /* HeapReference<Object> */ out = *(obj + offset)
6356 __ movl(out_reg, Address(obj_reg, offset));
6357 __ MaybeUnpoisonHeapReference(out_reg);
6358 }
6359}
6360
6361void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6362 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006363 const Address& address,
6364 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006365 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6366 if (kEmitCompilerReadBarrier) {
6367 if (kUseBakerReadBarrier) {
6368 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6369 // Baker's read barrier are used:
6370 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006371 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006372 // if (Thread::Current()->GetIsGcMarking()) {
6373 // root = ReadBarrier::Mark(root)
6374 // }
6375
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006376 // /* GcRoot<mirror::Object> */ root = *address
6377 __ movl(root_reg, address);
6378 if (fixup_label != nullptr) {
6379 __ Bind(fixup_label);
6380 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006381 static_assert(
6382 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6383 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6384 "have different sizes.");
6385 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6386 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6387 "have different sizes.");
6388
Vladimir Marko953437b2016-08-24 08:30:46 +00006389 // Slow path marking the GC root `root`.
6390 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6391 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006392 codegen_->AddSlowPath(slow_path);
6393
Andreas Gampe542451c2016-07-26 09:02:02 -07006394 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006395 /* no_rip */ true),
6396 Immediate(0));
6397 __ j(kNotEqual, slow_path->GetEntryLabel());
6398 __ Bind(slow_path->GetExitLabel());
6399 } else {
6400 // GC root loaded through a slow path for read barriers other
6401 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006402 // /* GcRoot<mirror::Object>* */ root = address
6403 __ leaq(root_reg, address);
6404 if (fixup_label != nullptr) {
6405 __ Bind(fixup_label);
6406 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006407 // /* mirror::Object* */ root = root->Read()
6408 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6409 }
6410 } else {
6411 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006412 // /* GcRoot<mirror::Object> */ root = *address
6413 __ movl(root_reg, address);
6414 if (fixup_label != nullptr) {
6415 __ Bind(fixup_label);
6416 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006417 // Note that GC roots are not affected by heap poisoning, thus we
6418 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006419 }
6420}
6421
6422void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6423 Location ref,
6424 CpuRegister obj,
6425 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006426 bool needs_null_check) {
6427 DCHECK(kEmitCompilerReadBarrier);
6428 DCHECK(kUseBakerReadBarrier);
6429
6430 // /* HeapReference<Object> */ ref = *(obj + offset)
6431 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006432 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006433}
6434
6435void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6436 Location ref,
6437 CpuRegister obj,
6438 uint32_t data_offset,
6439 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006440 bool needs_null_check) {
6441 DCHECK(kEmitCompilerReadBarrier);
6442 DCHECK(kUseBakerReadBarrier);
6443
Roland Levillain3d312422016-06-23 13:53:42 +01006444 static_assert(
6445 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6446 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006447 // /* HeapReference<Object> */ ref =
6448 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6449 Address src = index.IsConstant() ?
6450 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6451 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006452 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006453}
6454
6455void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6456 Location ref,
6457 CpuRegister obj,
6458 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006459 bool needs_null_check) {
6460 DCHECK(kEmitCompilerReadBarrier);
6461 DCHECK(kUseBakerReadBarrier);
6462
6463 // In slow path based read barriers, the read barrier call is
6464 // inserted after the original load. However, in fast path based
6465 // Baker's read barriers, we need to perform the load of
6466 // mirror::Object::monitor_ *before* the original reference load.
6467 // This load-load ordering is required by the read barrier.
6468 // The fast path/slow path (for Baker's algorithm) should look like:
6469 //
6470 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6471 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6472 // HeapReference<Object> ref = *src; // Original reference load.
6473 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6474 // if (is_gray) {
6475 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6476 // }
6477 //
6478 // Note: the original implementation in ReadBarrier::Barrier is
6479 // slightly more complex as:
6480 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006481 // the high-bits of rb_state, which are expected to be all zeroes
6482 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6483 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006484 // - it performs additional checks that we do not do here for
6485 // performance reasons.
6486
6487 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006488 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6489
Vladimir Marko953437b2016-08-24 08:30:46 +00006490 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6491 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6492 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6493 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6494 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6495 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6496 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6497
6498 // if (rb_state == ReadBarrier::gray_ptr_)
6499 // ref = ReadBarrier::Mark(ref);
6500 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6501 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006502 if (needs_null_check) {
6503 MaybeRecordImplicitNullCheck(instruction);
6504 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006505
6506 // Load fence to prevent load-load reordering.
6507 // Note that this is a no-op, thanks to the x86-64 memory model.
6508 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6509
6510 // The actual reference load.
6511 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006512 __ movl(ref_reg, src); // Flags are unaffected.
6513
6514 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6515 // Slow path marking the object `ref` when it is gray.
6516 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6517 instruction, ref, /* unpoison */ true);
6518 AddSlowPath(slow_path);
6519
6520 // We have done the "if" of the gray bit check above, now branch based on the flags.
6521 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006522
6523 // Object* ref = ref_addr->AsMirrorPtr()
6524 __ MaybeUnpoisonHeapReference(ref_reg);
6525
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006526 __ Bind(slow_path->GetExitLabel());
6527}
6528
6529void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6530 Location out,
6531 Location ref,
6532 Location obj,
6533 uint32_t offset,
6534 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006535 DCHECK(kEmitCompilerReadBarrier);
6536
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006537 // Insert a slow path based read barrier *after* the reference load.
6538 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 // If heap poisoning is enabled, the unpoisoning of the loaded
6540 // reference will be carried out by the runtime within the slow
6541 // path.
6542 //
6543 // Note that `ref` currently does not get unpoisoned (when heap
6544 // poisoning is enabled), which is alright as the `ref` argument is
6545 // not used by the artReadBarrierSlow entry point.
6546 //
6547 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6548 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6549 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6550 AddSlowPath(slow_path);
6551
Roland Levillain0d5a2812015-11-13 10:07:31 +00006552 __ jmp(slow_path->GetEntryLabel());
6553 __ Bind(slow_path->GetExitLabel());
6554}
6555
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006556void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6557 Location out,
6558 Location ref,
6559 Location obj,
6560 uint32_t offset,
6561 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006563 // Baker's read barriers shall be handled by the fast path
6564 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6565 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566 // If heap poisoning is enabled, unpoisoning will be taken care of
6567 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006568 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006569 } else if (kPoisonHeapReferences) {
6570 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6571 }
6572}
6573
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006574void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6575 Location out,
6576 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577 DCHECK(kEmitCompilerReadBarrier);
6578
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006579 // Insert a slow path based read barrier *after* the GC root load.
6580 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006581 // Note that GC roots are not affected by heap poisoning, so we do
6582 // not need to do anything special for this here.
6583 SlowPathCode* slow_path =
6584 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6585 AddSlowPath(slow_path);
6586
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 __ jmp(slow_path->GetEntryLabel());
6588 __ Bind(slow_path->GetExitLabel());
6589}
6590
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006591void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006592 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006593 LOG(FATAL) << "Unreachable";
6594}
6595
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006596void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006597 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006598 LOG(FATAL) << "Unreachable";
6599}
6600
Mark Mendellfe57faa2015-09-18 09:26:15 -04006601// Simple implementation of packed switch - generate cascaded compare/jumps.
6602void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6603 LocationSummary* locations =
6604 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6605 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006606 locations->AddTemp(Location::RequiresRegister());
6607 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006608}
6609
6610void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6611 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006612 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006613 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006614 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6615 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6616 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006617 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6618
6619 // Should we generate smaller inline compare/jumps?
6620 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6621 // Figure out the correct compare values and jump conditions.
6622 // Handle the first compare/branch as a special case because it might
6623 // jump to the default case.
6624 DCHECK_GT(num_entries, 2u);
6625 Condition first_condition;
6626 uint32_t index;
6627 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6628 if (lower_bound != 0) {
6629 first_condition = kLess;
6630 __ cmpl(value_reg_in, Immediate(lower_bound));
6631 __ j(first_condition, codegen_->GetLabelOf(default_block));
6632 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6633
6634 index = 1;
6635 } else {
6636 // Handle all the compare/jumps below.
6637 first_condition = kBelow;
6638 index = 0;
6639 }
6640
6641 // Handle the rest of the compare/jumps.
6642 for (; index + 1 < num_entries; index += 2) {
6643 int32_t compare_to_value = lower_bound + index + 1;
6644 __ cmpl(value_reg_in, Immediate(compare_to_value));
6645 // Jump to successors[index] if value < case_value[index].
6646 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6647 // Jump to successors[index + 1] if value == case_value[index + 1].
6648 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6649 }
6650
6651 if (index != num_entries) {
6652 // There are an odd number of entries. Handle the last one.
6653 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006654 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006655 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6656 }
6657
6658 // And the default for any other value.
6659 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6660 __ jmp(codegen_->GetLabelOf(default_block));
6661 }
6662 return;
6663 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006664
6665 // Remove the bias, if needed.
6666 Register value_reg_out = value_reg_in.AsRegister();
6667 if (lower_bound != 0) {
6668 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6669 value_reg_out = temp_reg.AsRegister();
6670 }
6671 CpuRegister value_reg(value_reg_out);
6672
6673 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006674 __ cmpl(value_reg, Immediate(num_entries - 1));
6675 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006676
Mark Mendell9c86b482015-09-18 13:36:07 -04006677 // We are in the range of the table.
6678 // Load the address of the jump table in the constant area.
6679 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006680
Mark Mendell9c86b482015-09-18 13:36:07 -04006681 // Load the (signed) offset from the jump table.
6682 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6683
6684 // Add the offset to the address of the table base.
6685 __ addq(temp_reg, base_reg);
6686
6687 // And jump.
6688 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006689}
6690
Aart Bikc5d47542016-01-27 17:00:35 -08006691void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6692 if (value == 0) {
6693 __ xorl(dest, dest);
6694 } else {
6695 __ movl(dest, Immediate(value));
6696 }
6697}
6698
Mark Mendell92e83bf2015-05-07 11:25:03 -04006699void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6700 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006701 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006702 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006703 } else if (IsUint<32>(value)) {
6704 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006705 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6706 } else {
6707 __ movq(dest, Immediate(value));
6708 }
6709}
6710
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006711void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6712 if (value == 0) {
6713 __ xorps(dest, dest);
6714 } else {
6715 __ movss(dest, LiteralInt32Address(value));
6716 }
6717}
6718
6719void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6720 if (value == 0) {
6721 __ xorpd(dest, dest);
6722 } else {
6723 __ movsd(dest, LiteralInt64Address(value));
6724 }
6725}
6726
6727void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6728 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6729}
6730
6731void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6732 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6733}
6734
Aart Bika19616e2016-02-01 18:57:58 -08006735void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6736 if (value == 0) {
6737 __ testl(dest, dest);
6738 } else {
6739 __ cmpl(dest, Immediate(value));
6740 }
6741}
6742
6743void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6744 if (IsInt<32>(value)) {
6745 if (value == 0) {
6746 __ testq(dest, dest);
6747 } else {
6748 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6749 }
6750 } else {
6751 // Value won't fit in an int.
6752 __ cmpq(dest, LiteralInt64Address(value));
6753 }
6754}
6755
Mark Mendellcfa410b2015-05-25 16:02:44 -04006756void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6757 DCHECK(dest.IsDoubleStackSlot());
6758 if (IsInt<32>(value)) {
6759 // Can move directly as an int32 constant.
6760 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6761 Immediate(static_cast<int32_t>(value)));
6762 } else {
6763 Load64BitValue(CpuRegister(TMP), value);
6764 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6765 }
6766}
6767
Mark Mendell9c86b482015-09-18 13:36:07 -04006768/**
6769 * Class to handle late fixup of offsets into constant area.
6770 */
6771class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6772 public:
6773 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6774 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6775
6776 protected:
6777 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6778
6779 CodeGeneratorX86_64* codegen_;
6780
6781 private:
6782 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6783 // Patch the correct offset for the instruction. We use the address of the
6784 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6785 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6786 int32_t relative_position = constant_offset - pos;
6787
6788 // Patch in the right value.
6789 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6790 }
6791
6792 // Location in constant area that the fixup refers to.
6793 size_t offset_into_constant_area_;
6794};
6795
6796/**
6797 t * Class to handle late fixup of offsets to a jump table that will be created in the
6798 * constant area.
6799 */
6800class JumpTableRIPFixup : public RIPFixup {
6801 public:
6802 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6803 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6804
6805 void CreateJumpTable() {
6806 X86_64Assembler* assembler = codegen_->GetAssembler();
6807
6808 // Ensure that the reference to the jump table has the correct offset.
6809 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6810 SetOffset(offset_in_constant_table);
6811
6812 // Compute the offset from the start of the function to this jump table.
6813 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6814
6815 // Populate the jump table with the correct values for the jump table.
6816 int32_t num_entries = switch_instr_->GetNumEntries();
6817 HBasicBlock* block = switch_instr_->GetBlock();
6818 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6819 // The value that we want is the target offset - the position of the table.
6820 for (int32_t i = 0; i < num_entries; i++) {
6821 HBasicBlock* b = successors[i];
6822 Label* l = codegen_->GetLabelOf(b);
6823 DCHECK(l->IsBound());
6824 int32_t offset_to_block = l->Position() - current_table_offset;
6825 assembler->AppendInt32(offset_to_block);
6826 }
6827 }
6828
6829 private:
6830 const HPackedSwitch* switch_instr_;
6831};
6832
Mark Mendellf55c3e02015-03-26 21:07:46 -04006833void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6834 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006835 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006836 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6837 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8 byte values.
Mark Mendell39dcf552015-04-09 20:42:42 -04006838 assembler->Align(4, 0);
6839 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006840
6841 // Populate any jump tables.
6842 for (auto jump_table : fixups_to_jump_tables_) {
6843 jump_table->CreateJumpTable();
6844 }
6845
6846 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006847 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006848 }
6849
6850 // And finish up.
6851 CodeGenerator::Finalize(allocator);
6852}
6853
Mark Mendellf55c3e02015-03-26 21:07:46 -04006854Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6855 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6856 return Address::RIP(fixup);
6857}
6858
6859Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6860 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6861 return Address::RIP(fixup);
6862}
6863
6864Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6865 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6866 return Address::RIP(fixup);
6867}
6868
6869Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6870 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6871 return Address::RIP(fixup);
6872}
6873
Andreas Gampe85b62f22015-09-09 13:15:38 -07006874// TODO: trg as memory.
6875void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6876 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006877 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006878 return;
6879 }
6880
6881 DCHECK_NE(type, Primitive::kPrimVoid);
6882
6883 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6884 if (trg.Equals(return_loc)) {
6885 return;
6886 }
6887
6888 // Let the parallel move resolver take care of all of this.
6889 HParallelMove parallel_move(GetGraph()->GetArena());
6890 parallel_move.AddMove(return_loc, trg, type, nullptr);
6891 GetMoveResolver()->EmitNativeCode(&parallel_move);
6892}
6893
Mark Mendell9c86b482015-09-18 13:36:07 -04006894Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6895 // Create a fixup to be used to create and address the jump table.
6896 JumpTableRIPFixup* table_fixup =
6897 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6898
6899 // We have to populate the jump tables.
6900 fixups_to_jump_tables_.push_back(table_fixup);
6901 return Address::RIP(table_fixup);
6902}
6903
Mark Mendellea5af682015-10-22 17:35:49 -04006904void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6905 const Address& addr_high,
6906 int64_t v,
6907 HInstruction* instruction) {
6908 if (IsInt<32>(v)) {
6909 int32_t v_32 = v;
6910 __ movq(addr_low, Immediate(v_32));
6911 MaybeRecordImplicitNullCheck(instruction);
6912 } else {
6913 // Didn't fit in a register. Do it in pieces.
6914 int32_t low_v = Low32Bits(v);
6915 int32_t high_v = High32Bits(v);
6916 __ movl(addr_low, Immediate(low_v));
6917 MaybeRecordImplicitNullCheck(instruction);
6918 __ movl(addr_high, Immediate(high_v));
6919 }
6920}
6921
Roland Levillain4d027112015-07-01 15:41:14 +01006922#undef __
6923
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006924} // namespace x86_64
6925} // namespace art