blob: a21a09ee8aa6aa0712394260ff254c1f17ed94ae [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());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100391 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000392 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 }
394
Alexandre Rames9931f312015-06-19 14:47:01 +0100395 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
396
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
399};
400
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401class ArraySetSlowPathX86_64 : public SlowPathCode {
402 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 LocationSummary* locations = instruction_->GetLocations();
407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, locations);
409
410 InvokeRuntimeCallingConvention calling_convention;
411 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
412 parallel_move.AddMove(
413 locations->InAt(0),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
415 Primitive::kPrimNot,
416 nullptr);
417 parallel_move.AddMove(
418 locations->InAt(1),
419 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
420 Primitive::kPrimInt,
421 nullptr);
422 parallel_move.AddMove(
423 locations->InAt(2),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
425 Primitive::kPrimNot,
426 nullptr);
427 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
428
Roland Levillain0d5a2812015-11-13 10:07:31 +0000429 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100430 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000431 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 RestoreLiveRegisters(codegen, locations);
433 __ jmp(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
437
438 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
440};
441
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000442// Slow path marking an object during a read barrier.
443class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
444 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000445 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj, bool unpoison)
446 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000447 DCHECK(kEmitCompilerReadBarrier);
448 }
449
450 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
451
452 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
453 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100454 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000455 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100456 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000457 DCHECK(instruction_->IsInstanceFieldGet() ||
458 instruction_->IsStaticFieldGet() ||
459 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100460 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000461 instruction_->IsLoadClass() ||
462 instruction_->IsLoadString() ||
463 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100464 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100465 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
466 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000467 << "Unexpected instruction in read barrier marking slow path: "
468 << instruction_->DebugName();
469
470 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000471 if (unpoison_) {
472 // Object* ref = ref_addr->AsMirrorPtr()
473 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
474 }
Roland Levillain4359e612016-07-20 11:32:19 +0100475 // No need to save live registers; it's taken care of by the
476 // entrypoint. Also, there is no need to update the stack mask,
477 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000478 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100479 DCHECK_NE(reg, RSP);
480 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
481 // "Compact" slow path, saving two moves.
482 //
483 // Instead of using the standard runtime calling convention (input
484 // and output in R0):
485 //
486 // RDI <- obj
487 // RAX <- ReadBarrierMark(RDI)
488 // obj <- RAX
489 //
490 // we just use rX (the register holding `obj`) as input and output
491 // of a dedicated entrypoint:
492 //
493 // rX <- ReadBarrierMarkRegX(rX)
494 //
495 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700496 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100497 // This runtime call does not require a stack map.
498 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000499 __ jmp(GetExitLabel());
500 }
501
502 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000503 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000504 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000505
506 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
507};
508
Roland Levillain0d5a2812015-11-13 10:07:31 +0000509// Slow path generating a read barrier for a heap reference.
510class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
511 public:
512 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
513 Location out,
514 Location ref,
515 Location obj,
516 uint32_t offset,
517 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000518 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000519 out_(out),
520 ref_(ref),
521 obj_(obj),
522 offset_(offset),
523 index_(index) {
524 DCHECK(kEmitCompilerReadBarrier);
525 // If `obj` is equal to `out` or `ref`, it means the initial
526 // object has been overwritten by (or after) the heap object
527 // reference load to be instrumented, e.g.:
528 //
529 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000530 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000531 //
532 // In that case, we have lost the information about the original
533 // object, and the emitted read barrier cannot work properly.
534 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
535 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
536}
537
538 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
539 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
540 LocationSummary* locations = instruction_->GetLocations();
541 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
542 DCHECK(locations->CanCall());
543 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100544 DCHECK(instruction_->IsInstanceFieldGet() ||
545 instruction_->IsStaticFieldGet() ||
546 instruction_->IsArrayGet() ||
547 instruction_->IsInstanceOf() ||
548 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100549 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000550 << "Unexpected instruction in read barrier for heap reference slow path: "
551 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000552
553 __ Bind(GetEntryLabel());
554 SaveLiveRegisters(codegen, locations);
555
556 // We may have to change the index's value, but as `index_` is a
557 // constant member (like other "inputs" of this slow path),
558 // introduce a copy of it, `index`.
559 Location index = index_;
560 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100561 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000562 if (instruction_->IsArrayGet()) {
563 // Compute real offset and store it in index_.
564 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
565 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
566 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
567 // We are about to change the value of `index_reg` (see the
568 // calls to art::x86_64::X86_64Assembler::shll and
569 // art::x86_64::X86_64Assembler::AddImmediate below), but it
570 // has not been saved by the previous call to
571 // art::SlowPathCode::SaveLiveRegisters, as it is a
572 // callee-save register --
573 // art::SlowPathCode::SaveLiveRegisters does not consider
574 // callee-save registers, as it has been designed with the
575 // assumption that callee-save registers are supposed to be
576 // handled by the called function. So, as a callee-save
577 // register, `index_reg` _would_ eventually be saved onto
578 // the stack, but it would be too late: we would have
579 // changed its value earlier. Therefore, we manually save
580 // it here into another freely available register,
581 // `free_reg`, chosen of course among the caller-save
582 // registers (as a callee-save `free_reg` register would
583 // exhibit the same problem).
584 //
585 // Note we could have requested a temporary register from
586 // the register allocator instead; but we prefer not to, as
587 // this is a slow path, and we know we can find a
588 // caller-save register that is available.
589 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
590 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
591 index_reg = free_reg;
592 index = Location::RegisterLocation(index_reg);
593 } else {
594 // The initial register stored in `index_` has already been
595 // saved in the call to art::SlowPathCode::SaveLiveRegisters
596 // (as it is not a callee-save register), so we can freely
597 // use it.
598 }
599 // Shifting the index value contained in `index_reg` by the
600 // scale factor (2) cannot overflow in practice, as the
601 // runtime is unable to allocate object arrays with a size
602 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
603 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
604 static_assert(
605 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
606 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
607 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
608 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100609 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
610 // intrinsics, `index_` is not shifted by a scale factor of 2
611 // (as in the case of ArrayGet), as it is actually an offset
612 // to an object field within an object.
613 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000614 DCHECK(instruction_->GetLocations()->Intrinsified());
615 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
616 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
617 << instruction_->AsInvoke()->GetIntrinsic();
618 DCHECK_EQ(offset_, 0U);
619 DCHECK(index_.IsRegister());
620 }
621 }
622
623 // We're moving two or three locations to locations that could
624 // overlap, so we need a parallel move resolver.
625 InvokeRuntimeCallingConvention calling_convention;
626 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
627 parallel_move.AddMove(ref_,
628 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
629 Primitive::kPrimNot,
630 nullptr);
631 parallel_move.AddMove(obj_,
632 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
633 Primitive::kPrimNot,
634 nullptr);
635 if (index.IsValid()) {
636 parallel_move.AddMove(index,
637 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
638 Primitive::kPrimInt,
639 nullptr);
640 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
641 } else {
642 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
643 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
644 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100645 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000646 instruction_,
647 instruction_->GetDexPc(),
648 this);
649 CheckEntrypointTypes<
650 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
651 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
652
653 RestoreLiveRegisters(codegen, locations);
654 __ jmp(GetExitLabel());
655 }
656
657 const char* GetDescription() const OVERRIDE {
658 return "ReadBarrierForHeapReferenceSlowPathX86_64";
659 }
660
661 private:
662 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
663 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
664 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
665 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
666 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
667 return static_cast<CpuRegister>(i);
668 }
669 }
670 // We shall never fail to find a free caller-save register, as
671 // there are more than two core caller-save registers on x86-64
672 // (meaning it is possible to find one which is different from
673 // `ref` and `obj`).
674 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
675 LOG(FATAL) << "Could not find a free caller-save register";
676 UNREACHABLE();
677 }
678
Roland Levillain0d5a2812015-11-13 10:07:31 +0000679 const Location out_;
680 const Location ref_;
681 const Location obj_;
682 const uint32_t offset_;
683 // An additional location containing an index to an array.
684 // Only used for HArrayGet and the UnsafeGetObject &
685 // UnsafeGetObjectVolatile intrinsics.
686 const Location index_;
687
688 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
689};
690
691// Slow path generating a read barrier for a GC root.
692class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
693 public:
694 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000695 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000696 DCHECK(kEmitCompilerReadBarrier);
697 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698
699 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
700 LocationSummary* locations = instruction_->GetLocations();
701 DCHECK(locations->CanCall());
702 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000703 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
704 << "Unexpected instruction in read barrier for GC root slow path: "
705 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000706
707 __ Bind(GetEntryLabel());
708 SaveLiveRegisters(codegen, locations);
709
710 InvokeRuntimeCallingConvention calling_convention;
711 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
712 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100713 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000714 instruction_,
715 instruction_->GetDexPc(),
716 this);
717 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
718 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
719
720 RestoreLiveRegisters(codegen, locations);
721 __ jmp(GetExitLabel());
722 }
723
724 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
725
726 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000727 const Location out_;
728 const Location root_;
729
730 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
731};
732
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100733#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100734// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
735#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100736
Roland Levillain4fa13f62015-07-06 18:11:54 +0100737inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700738 switch (cond) {
739 case kCondEQ: return kEqual;
740 case kCondNE: return kNotEqual;
741 case kCondLT: return kLess;
742 case kCondLE: return kLessEqual;
743 case kCondGT: return kGreater;
744 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700745 case kCondB: return kBelow;
746 case kCondBE: return kBelowEqual;
747 case kCondA: return kAbove;
748 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700749 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100750 LOG(FATAL) << "Unreachable";
751 UNREACHABLE();
752}
753
Aart Bike9f37602015-10-09 11:15:55 -0700754// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100755inline Condition X86_64FPCondition(IfCondition cond) {
756 switch (cond) {
757 case kCondEQ: return kEqual;
758 case kCondNE: return kNotEqual;
759 case kCondLT: return kBelow;
760 case kCondLE: return kBelowEqual;
761 case kCondGT: return kAbove;
762 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700763 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100764 };
765 LOG(FATAL) << "Unreachable";
766 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700767}
768
Vladimir Markodc151b22015-10-15 18:02:30 +0100769HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
770 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
771 MethodReference target_method ATTRIBUTE_UNUSED) {
772 switch (desired_dispatch_info.code_ptr_location) {
773 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
774 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
775 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
776 return HInvokeStaticOrDirect::DispatchInfo {
777 desired_dispatch_info.method_load_kind,
778 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
779 desired_dispatch_info.method_load_data,
780 0u
781 };
782 default:
783 return desired_dispatch_info;
784 }
785}
786
Serguei Katkov288c7a82016-05-16 11:53:15 +0600787Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
788 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800789 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000790 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
791 switch (invoke->GetMethodLoadKind()) {
792 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
793 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000794 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000795 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000796 break;
797 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000798 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000799 break;
800 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
801 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
802 break;
803 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
804 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
805 method_patches_.emplace_back(invoke->GetTargetMethod());
806 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
807 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000808 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000809 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000810 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000811 // Bind a new fixup label at the end of the "movl" insn.
812 uint32_t offset = invoke->GetDexCacheArrayOffset();
813 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000814 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000815 }
Vladimir Marko58155012015-08-19 12:49:41 +0000816 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000817 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000818 Register method_reg;
819 CpuRegister reg = temp.AsRegister<CpuRegister>();
820 if (current_method.IsRegister()) {
821 method_reg = current_method.AsRegister<Register>();
822 } else {
823 DCHECK(invoke->GetLocations()->Intrinsified());
824 DCHECK(!current_method.IsValid());
825 method_reg = reg.AsRegister();
826 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
827 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100829 __ movq(reg,
830 Address(CpuRegister(method_reg),
831 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100832 // temp = temp[index_in_cache];
833 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
834 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000835 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
836 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100837 }
Vladimir Marko58155012015-08-19 12:49:41 +0000838 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600839 return callee_method;
840}
841
842void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
843 Location temp) {
844 // All registers are assumed to be correctly set up.
845 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000846
847 switch (invoke->GetCodePtrLocation()) {
848 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
849 __ call(&frame_entry_label_);
850 break;
851 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
852 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
853 Label* label = &relative_call_patches_.back().label;
854 __ call(label); // Bind to the patch label, override at link time.
855 __ Bind(label); // Bind the label at the end of the "call" insn.
856 break;
857 }
858 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
859 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100860 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
861 LOG(FATAL) << "Unsupported";
862 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000863 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
864 // (callee_method + offset_of_quick_compiled_code)()
865 __ call(Address(callee_method.AsRegister<CpuRegister>(),
866 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700867 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000868 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000869 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800870
871 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800872}
873
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000874void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
875 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
876 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
877 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000878
879 // Use the calling convention instead of the location of the receiver, as
880 // intrinsics may have put the receiver in a different register. In the intrinsics
881 // slow path, the arguments have been moved to the right place, so here we are
882 // guaranteed that the receiver is the first register of the calling convention.
883 InvokeDexCallingConvention calling_convention;
884 Register receiver = calling_convention.GetRegisterAt(0);
885
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000886 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000888 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000889 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000890 // Instead of simply (possibly) unpoisoning `temp` here, we should
891 // emit a read barrier for the previous class reference load.
892 // However this is not required in practice, as this is an
893 // intermediate/temporary reference and because the current
894 // concurrent copying collector keeps the from-space memory
895 // intact/accessible until the end of the marking phase (the
896 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000897 __ MaybeUnpoisonHeapReference(temp);
898 // temp = temp->GetMethodAt(method_offset);
899 __ movq(temp, Address(temp, method_offset));
900 // call temp->GetEntryPoint();
901 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700902 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000903}
904
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000905void CodeGeneratorX86_64::RecordSimplePatch() {
906 if (GetCompilerOptions().GetIncludePatchInformation()) {
907 simple_patches_.emplace_back();
908 __ Bind(&simple_patches_.back());
909 }
910}
911
912void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
913 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
914 __ Bind(&string_patches_.back().label);
915}
916
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100917void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
918 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
919 __ Bind(&type_patches_.back().label);
920}
921
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000922Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
923 uint32_t element_offset) {
924 // Add a patch entry and return the label.
925 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
926 return &pc_relative_dex_cache_patches_.back().label;
927}
928
Vladimir Marko58155012015-08-19 12:49:41 +0000929void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
930 DCHECK(linker_patches->empty());
931 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000932 method_patches_.size() +
933 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000934 pc_relative_dex_cache_patches_.size() +
935 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100936 string_patches_.size() +
937 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000938 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000939 // The label points to the end of the "movl" insn but the literal offset for method
940 // patch needs to point to the embedded constant which occupies the last 4 bytes.
941 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000942 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000943 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000944 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
945 info.target_method.dex_file,
946 info.target_method.dex_method_index));
947 }
948 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000949 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000950 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
951 info.target_method.dex_file,
952 info.target_method.dex_method_index));
953 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000954 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
955 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000956 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
957 &info.target_dex_file,
958 info.label.Position(),
959 info.element_offset));
960 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000961 for (const Label& label : simple_patches_) {
962 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
963 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
964 }
965 for (const StringPatchInfo<Label>& info : string_patches_) {
966 // These are always PC-relative, see GetSupportedLoadStringKind().
967 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
968 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
969 &info.dex_file,
970 info.label.Position(),
971 info.string_index));
972 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100973 for (const TypePatchInfo<Label>& info : type_patches_) {
974 // These are always PC-relative, see GetSupportedLoadClassKind().
975 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
976 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
977 &info.dex_file,
978 info.label.Position(),
979 info.type_index));
980 }
Vladimir Marko58155012015-08-19 12:49:41 +0000981}
982
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100983void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100984 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100985}
986
987void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100988 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100989}
990
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100991size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
992 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
993 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100994}
995
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100996size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
997 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
998 return kX86_64WordSize;
999}
1000
1001size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1002 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1003 return kX86_64WordSize;
1004}
1005
1006size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1007 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1008 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001009}
1010
Calin Juravle175dc732015-08-25 15:42:32 +01001011void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1012 HInstruction* instruction,
1013 uint32_t dex_pc,
1014 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001015 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001016 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1017 if (EntrypointRequiresStackMap(entrypoint)) {
1018 RecordPcInfo(instruction, dex_pc, slow_path);
1019 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001020}
1021
Roland Levillaindec8f632016-07-22 17:10:06 +01001022void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1023 HInstruction* instruction,
1024 SlowPathCode* slow_path) {
1025 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001026 GenerateInvokeRuntime(entry_point_offset);
1027}
1028
1029void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001030 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1031}
1032
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001033static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001034// Use a fake return address register to mimic Quick.
1035static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001036CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001037 const X86_64InstructionSetFeatures& isa_features,
1038 const CompilerOptions& compiler_options,
1039 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001040 : CodeGenerator(graph,
1041 kNumberOfCpuRegisters,
1042 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001043 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001044 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1045 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001046 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001047 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1048 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001049 compiler_options,
1050 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001051 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001053 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001054 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001055 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001056 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001057 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001058 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1059 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001060 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001061 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1062 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001063 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001064 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001065 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1066}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001067
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001068InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1069 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001070 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001071 assembler_(codegen->GetAssembler()),
1072 codegen_(codegen) {}
1073
David Brazdil58282f42016-01-14 12:45:10 +00001074void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001076 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001077
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001078 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001079 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001080}
1081
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001082static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001083 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001084}
David Srbecky9d8606d2015-04-12 09:35:32 +01001085
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001087 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001088}
1089
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001090void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001091 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001092 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001093 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001094 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001095 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001096
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001097 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001098 __ testq(CpuRegister(RAX), Address(
1099 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001100 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001101 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001102
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001103 if (HasEmptyFrame()) {
1104 return;
1105 }
1106
Nicolas Geoffray98893962015-01-21 12:32:32 +00001107 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001108 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001109 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001110 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001111 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1112 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001113 }
1114 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001115
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001116 int adjust = GetFrameSize() - GetCoreSpillSize();
1117 __ subq(CpuRegister(RSP), Immediate(adjust));
1118 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001119 uint32_t xmm_spill_location = GetFpuSpillStart();
1120 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001121
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001122 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1123 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001124 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1125 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1126 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001127 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001128 }
1129
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001131 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001132}
1133
1134void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001135 __ cfi().RememberState();
1136 if (!HasEmptyFrame()) {
1137 uint32_t xmm_spill_location = GetFpuSpillStart();
1138 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1139 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1140 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1141 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1142 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1143 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1144 }
1145 }
1146
1147 int adjust = GetFrameSize() - GetCoreSpillSize();
1148 __ addq(CpuRegister(RSP), Immediate(adjust));
1149 __ cfi().AdjustCFAOffset(-adjust);
1150
1151 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1152 Register reg = kCoreCalleeSaves[i];
1153 if (allocated_registers_.ContainsCoreRegister(reg)) {
1154 __ popq(CpuRegister(reg));
1155 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1156 __ cfi().Restore(DWARFReg(reg));
1157 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001158 }
1159 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001160 __ ret();
1161 __ cfi().RestoreState();
1162 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001163}
1164
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001165void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1166 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001167}
1168
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001169void CodeGeneratorX86_64::Move(Location destination, Location source) {
1170 if (source.Equals(destination)) {
1171 return;
1172 }
1173 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001174 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001175 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001176 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001177 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001178 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001179 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001180 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1181 } else if (source.IsConstant()) {
1182 HConstant* constant = source.GetConstant();
1183 if (constant->IsLongConstant()) {
1184 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1185 } else {
1186 Load32BitValue(dest, GetInt32ValueOf(constant));
1187 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188 } else {
1189 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001190 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 }
1192 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001193 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001195 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001196 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001197 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1198 } else if (source.IsConstant()) {
1199 HConstant* constant = source.GetConstant();
1200 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1201 if (constant->IsFloatConstant()) {
1202 Load32BitValue(dest, static_cast<int32_t>(value));
1203 } else {
1204 Load64BitValue(dest, value);
1205 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001206 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001207 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001208 } else {
1209 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001210 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001211 }
1212 } else if (destination.IsStackSlot()) {
1213 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001215 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001216 } else if (source.IsFpuRegister()) {
1217 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001218 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001219 } else if (source.IsConstant()) {
1220 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001221 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001222 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001223 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001224 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001225 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1226 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001227 }
1228 } else {
1229 DCHECK(destination.IsDoubleStackSlot());
1230 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else if (source.IsFpuRegister()) {
1234 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001236 } else if (source.IsConstant()) {
1237 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001238 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001239 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001240 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001241 } else {
1242 DCHECK(constant->IsLongConstant());
1243 value = constant->AsLongConstant()->GetValue();
1244 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001245 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001246 } else {
1247 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001248 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1249 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250 }
1251 }
1252}
1253
Calin Juravle175dc732015-08-25 15:42:32 +01001254void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1255 DCHECK(location.IsRegister());
1256 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1257}
1258
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259void CodeGeneratorX86_64::MoveLocation(
1260 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1261 Move(dst, src);
1262}
1263
1264void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1265 if (location.IsRegister()) {
1266 locations->AddTemp(location);
1267 } else {
1268 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1269 }
1270}
1271
David Brazdilfc6a86a2015-06-26 10:33:45 +00001272void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001273 DCHECK(!successor->IsExitBlock());
1274
1275 HBasicBlock* block = got->GetBlock();
1276 HInstruction* previous = got->GetPrevious();
1277
1278 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001279 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001280 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1281 return;
1282 }
1283
1284 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1285 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1286 }
1287 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288 __ jmp(codegen_->GetLabelOf(successor));
1289 }
1290}
1291
David Brazdilfc6a86a2015-06-26 10:33:45 +00001292void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1293 got->SetLocations(nullptr);
1294}
1295
1296void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1297 HandleGoto(got, got->GetSuccessor());
1298}
1299
1300void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1301 try_boundary->SetLocations(nullptr);
1302}
1303
1304void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1305 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1306 if (!successor->IsExitBlock()) {
1307 HandleGoto(try_boundary, successor);
1308 }
1309}
1310
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001311void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1312 exit->SetLocations(nullptr);
1313}
1314
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001315void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316}
1317
Mark Mendell152408f2015-12-31 12:28:50 -05001318template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001319void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001320 LabelType* true_label,
1321 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001322 if (cond->IsFPConditionTrueIfNaN()) {
1323 __ j(kUnordered, true_label);
1324 } else if (cond->IsFPConditionFalseIfNaN()) {
1325 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001326 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001327 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001328}
1329
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001330void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001331 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001332
Mark Mendellc4701932015-04-10 13:18:51 -04001333 Location left = locations->InAt(0);
1334 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001335 Primitive::Type type = condition->InputAt(0)->GetType();
1336 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001337 case Primitive::kPrimBoolean:
1338 case Primitive::kPrimByte:
1339 case Primitive::kPrimChar:
1340 case Primitive::kPrimShort:
1341 case Primitive::kPrimInt:
1342 case Primitive::kPrimNot: {
1343 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1344 if (right.IsConstant()) {
1345 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1346 if (value == 0) {
1347 __ testl(left_reg, left_reg);
1348 } else {
1349 __ cmpl(left_reg, Immediate(value));
1350 }
1351 } else if (right.IsStackSlot()) {
1352 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1353 } else {
1354 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1355 }
1356 break;
1357 }
Mark Mendellc4701932015-04-10 13:18:51 -04001358 case Primitive::kPrimLong: {
1359 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1360 if (right.IsConstant()) {
1361 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001362 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001363 } else if (right.IsDoubleStackSlot()) {
1364 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1365 } else {
1366 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1367 }
Mark Mendellc4701932015-04-10 13:18:51 -04001368 break;
1369 }
1370 case Primitive::kPrimFloat: {
1371 if (right.IsFpuRegister()) {
1372 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1373 } else if (right.IsConstant()) {
1374 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1375 codegen_->LiteralFloatAddress(
1376 right.GetConstant()->AsFloatConstant()->GetValue()));
1377 } else {
1378 DCHECK(right.IsStackSlot());
1379 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1380 Address(CpuRegister(RSP), right.GetStackIndex()));
1381 }
Mark Mendellc4701932015-04-10 13:18:51 -04001382 break;
1383 }
1384 case Primitive::kPrimDouble: {
1385 if (right.IsFpuRegister()) {
1386 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1387 } else if (right.IsConstant()) {
1388 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1389 codegen_->LiteralDoubleAddress(
1390 right.GetConstant()->AsDoubleConstant()->GetValue()));
1391 } else {
1392 DCHECK(right.IsDoubleStackSlot());
1393 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1394 Address(CpuRegister(RSP), right.GetStackIndex()));
1395 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001396 break;
1397 }
1398 default:
1399 LOG(FATAL) << "Unexpected condition type " << type;
1400 }
1401}
1402
1403template<class LabelType>
1404void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1405 LabelType* true_target_in,
1406 LabelType* false_target_in) {
1407 // Generated branching requires both targets to be explicit. If either of the
1408 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1409 LabelType fallthrough_target;
1410 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1411 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1412
1413 // Generate the comparison to set the CC.
1414 GenerateCompareTest(condition);
1415
1416 // Now generate the correct jump(s).
1417 Primitive::Type type = condition->InputAt(0)->GetType();
1418 switch (type) {
1419 case Primitive::kPrimLong: {
1420 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1421 break;
1422 }
1423 case Primitive::kPrimFloat: {
1424 GenerateFPJumps(condition, true_target, false_target);
1425 break;
1426 }
1427 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001428 GenerateFPJumps(condition, true_target, false_target);
1429 break;
1430 }
1431 default:
1432 LOG(FATAL) << "Unexpected condition type " << type;
1433 }
1434
David Brazdil0debae72015-11-12 18:37:00 +00001435 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001436 __ jmp(false_target);
1437 }
David Brazdil0debae72015-11-12 18:37:00 +00001438
1439 if (fallthrough_target.IsLinked()) {
1440 __ Bind(&fallthrough_target);
1441 }
Mark Mendellc4701932015-04-10 13:18:51 -04001442}
1443
David Brazdil0debae72015-11-12 18:37:00 +00001444static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1445 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1446 // are set only strictly before `branch`. We can't use the eflags on long
1447 // conditions if they are materialized due to the complex branching.
1448 return cond->IsCondition() &&
1449 cond->GetNext() == branch &&
1450 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1451}
1452
Mark Mendell152408f2015-12-31 12:28:50 -05001453template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001454void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001455 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001456 LabelType* true_target,
1457 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001458 HInstruction* cond = instruction->InputAt(condition_input_index);
1459
1460 if (true_target == nullptr && false_target == nullptr) {
1461 // Nothing to do. The code always falls through.
1462 return;
1463 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001464 // Constant condition, statically compared against "true" (integer value 1).
1465 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001466 if (true_target != nullptr) {
1467 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001468 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001469 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001470 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001471 if (false_target != nullptr) {
1472 __ jmp(false_target);
1473 }
1474 }
1475 return;
1476 }
1477
1478 // The following code generates these patterns:
1479 // (1) true_target == nullptr && false_target != nullptr
1480 // - opposite condition true => branch to false_target
1481 // (2) true_target != nullptr && false_target == nullptr
1482 // - condition true => branch to true_target
1483 // (3) true_target != nullptr && false_target != nullptr
1484 // - condition true => branch to true_target
1485 // - branch to false_target
1486 if (IsBooleanValueOrMaterializedCondition(cond)) {
1487 if (AreEflagsSetFrom(cond, instruction)) {
1488 if (true_target == nullptr) {
1489 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1490 } else {
1491 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1492 }
1493 } else {
1494 // Materialized condition, compare against 0.
1495 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1496 if (lhs.IsRegister()) {
1497 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1498 } else {
1499 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1500 }
1501 if (true_target == nullptr) {
1502 __ j(kEqual, false_target);
1503 } else {
1504 __ j(kNotEqual, true_target);
1505 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001506 }
1507 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001508 // Condition has not been materialized, use its inputs as the
1509 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001510 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001511
David Brazdil0debae72015-11-12 18:37:00 +00001512 // If this is a long or FP comparison that has been folded into
1513 // the HCondition, generate the comparison directly.
1514 Primitive::Type type = condition->InputAt(0)->GetType();
1515 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1516 GenerateCompareTestAndBranch(condition, true_target, false_target);
1517 return;
1518 }
1519
1520 Location lhs = condition->GetLocations()->InAt(0);
1521 Location rhs = condition->GetLocations()->InAt(1);
1522 if (rhs.IsRegister()) {
1523 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1524 } else if (rhs.IsConstant()) {
1525 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001526 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001527 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001528 __ cmpl(lhs.AsRegister<CpuRegister>(),
1529 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1530 }
1531 if (true_target == nullptr) {
1532 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1533 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001534 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001535 }
Dave Allison20dfc792014-06-16 20:44:29 -07001536 }
David Brazdil0debae72015-11-12 18:37:00 +00001537
1538 // If neither branch falls through (case 3), the conditional branch to `true_target`
1539 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1540 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001541 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001542 }
1543}
1544
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001545void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1547 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001548 locations->SetInAt(0, Location::Any());
1549 }
1550}
1551
1552void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001553 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1554 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1555 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1556 nullptr : codegen_->GetLabelOf(true_successor);
1557 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1558 nullptr : codegen_->GetLabelOf(false_successor);
1559 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001560}
1561
1562void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1563 LocationSummary* locations = new (GetGraph()->GetArena())
1564 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001565 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
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);
Vladimir Marko70e97462016-08-09 11:04:26 +01004117 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4118 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4119 }
Calin Juravle52c48962014-12-16 17:02:57 +00004120 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004121 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4122 locations->SetOut(Location::RequiresFpuRegister());
4123 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004124 // The output overlaps for an object field get when read barriers
4125 // are enabled: we do not want the move to overwrite the object's
4126 // location, as we need it to emit the read barrier.
4127 locations->SetOut(
4128 Location::RequiresRegister(),
4129 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004130 }
Calin Juravle52c48962014-12-16 17:02:57 +00004131}
4132
4133void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4134 const FieldInfo& field_info) {
4135 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4136
4137 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004138 Location base_loc = locations->InAt(0);
4139 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004140 Location out = locations->Out();
4141 bool is_volatile = field_info.IsVolatile();
4142 Primitive::Type field_type = field_info.GetFieldType();
4143 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4144
4145 switch (field_type) {
4146 case Primitive::kPrimBoolean: {
4147 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4148 break;
4149 }
4150
4151 case Primitive::kPrimByte: {
4152 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4153 break;
4154 }
4155
4156 case Primitive::kPrimShort: {
4157 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4158 break;
4159 }
4160
4161 case Primitive::kPrimChar: {
4162 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4163 break;
4164 }
4165
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004166 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004167 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4168 break;
4169 }
4170
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004171 case Primitive::kPrimNot: {
4172 // /* HeapReference<Object> */ out = *(base + offset)
4173 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004174 // Note that a potential implicit null check is handled in this
4175 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4176 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004177 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004178 if (is_volatile) {
4179 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4180 }
4181 } else {
4182 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4183 codegen_->MaybeRecordImplicitNullCheck(instruction);
4184 if (is_volatile) {
4185 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4186 }
4187 // If read barriers are enabled, emit read barriers other than
4188 // Baker's using a slow path (and also unpoison the loaded
4189 // reference, if heap poisoning is enabled).
4190 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4191 }
4192 break;
4193 }
4194
Calin Juravle52c48962014-12-16 17:02:57 +00004195 case Primitive::kPrimLong: {
4196 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4197 break;
4198 }
4199
4200 case Primitive::kPrimFloat: {
4201 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4202 break;
4203 }
4204
4205 case Primitive::kPrimDouble: {
4206 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4207 break;
4208 }
4209
4210 case Primitive::kPrimVoid:
4211 LOG(FATAL) << "Unreachable type " << field_type;
4212 UNREACHABLE();
4213 }
4214
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004215 if (field_type == Primitive::kPrimNot) {
4216 // Potential implicit null checks, in the case of reference
4217 // fields, are handled in the previous switch statement.
4218 } else {
4219 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004220 }
Roland Levillain4d027112015-07-01 15:41:14 +01004221
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004222 if (is_volatile) {
4223 if (field_type == Primitive::kPrimNot) {
4224 // Memory barriers, in the case of references, are also handled
4225 // in the previous switch statement.
4226 } else {
4227 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4228 }
Roland Levillain4d027112015-07-01 15:41:14 +01004229 }
Calin Juravle52c48962014-12-16 17:02:57 +00004230}
4231
4232void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4233 const FieldInfo& field_info) {
4234 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4235
4236 LocationSummary* locations =
4237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004238 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004239 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004240 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004241 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004242
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004243 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004244 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004245 if (is_volatile) {
4246 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4247 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4248 } else {
4249 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4250 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004251 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004252 if (is_volatile) {
4253 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4254 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4255 } else {
4256 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4257 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004258 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004259 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004260 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004261 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004262 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004263 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4264 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004265 locations->AddTemp(Location::RequiresRegister());
4266 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267}
4268
Calin Juravle52c48962014-12-16 17:02:57 +00004269void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004270 const FieldInfo& field_info,
4271 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004272 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4273
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004275 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4276 Location value = locations->InAt(1);
4277 bool is_volatile = field_info.IsVolatile();
4278 Primitive::Type field_type = field_info.GetFieldType();
4279 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4280
4281 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004282 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004283 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284
Mark Mendellea5af682015-10-22 17:35:49 -04004285 bool maybe_record_implicit_null_check_done = false;
4286
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004287 switch (field_type) {
4288 case Primitive::kPrimBoolean:
4289 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004290 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004291 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004292 __ movb(Address(base, offset), Immediate(v));
4293 } else {
4294 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4295 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004296 break;
4297 }
4298
4299 case Primitive::kPrimShort:
4300 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004301 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004302 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004303 __ movw(Address(base, offset), Immediate(v));
4304 } else {
4305 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4306 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004307 break;
4308 }
4309
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004310 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004311 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004312 if (value.IsConstant()) {
4313 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004314 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4315 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4316 // Note: if heap poisoning is enabled, no need to poison
4317 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004318 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004319 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004320 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4321 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4322 __ movl(temp, value.AsRegister<CpuRegister>());
4323 __ PoisonHeapReference(temp);
4324 __ movl(Address(base, offset), temp);
4325 } else {
4326 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4327 }
Mark Mendell40741f32015-04-20 22:10:34 -04004328 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004329 break;
4330 }
4331
4332 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004333 if (value.IsConstant()) {
4334 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004335 codegen_->MoveInt64ToAddress(Address(base, offset),
4336 Address(base, offset + sizeof(int32_t)),
4337 v,
4338 instruction);
4339 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004340 } else {
4341 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4342 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004343 break;
4344 }
4345
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004346 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004347 if (value.IsConstant()) {
4348 int32_t v =
4349 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4350 __ movl(Address(base, offset), Immediate(v));
4351 } else {
4352 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4353 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004354 break;
4355 }
4356
4357 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004358 if (value.IsConstant()) {
4359 int64_t v =
4360 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4361 codegen_->MoveInt64ToAddress(Address(base, offset),
4362 Address(base, offset + sizeof(int32_t)),
4363 v,
4364 instruction);
4365 maybe_record_implicit_null_check_done = true;
4366 } else {
4367 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4368 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004369 break;
4370 }
4371
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372 case Primitive::kPrimVoid:
4373 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004374 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375 }
Calin Juravle52c48962014-12-16 17:02:57 +00004376
Mark Mendellea5af682015-10-22 17:35:49 -04004377 if (!maybe_record_implicit_null_check_done) {
4378 codegen_->MaybeRecordImplicitNullCheck(instruction);
4379 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004380
4381 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4382 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4383 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004384 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004385 }
4386
Calin Juravle52c48962014-12-16 17:02:57 +00004387 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004388 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004389 }
4390}
4391
4392void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4393 HandleFieldSet(instruction, instruction->GetFieldInfo());
4394}
4395
4396void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004397 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004398}
4399
4400void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004401 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004402}
4403
4404void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004405 HandleFieldGet(instruction, instruction->GetFieldInfo());
4406}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407
Calin Juravle52c48962014-12-16 17:02:57 +00004408void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4409 HandleFieldGet(instruction);
4410}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004411
Calin Juravle52c48962014-12-16 17:02:57 +00004412void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4413 HandleFieldGet(instruction, instruction->GetFieldInfo());
4414}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004415
Calin Juravle52c48962014-12-16 17:02:57 +00004416void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4417 HandleFieldSet(instruction, instruction->GetFieldInfo());
4418}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419
Calin Juravle52c48962014-12-16 17:02:57 +00004420void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004421 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004422}
4423
Calin Juravlee460d1d2015-09-29 04:52:17 +01004424void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4425 HUnresolvedInstanceFieldGet* instruction) {
4426 FieldAccessCallingConventionX86_64 calling_convention;
4427 codegen_->CreateUnresolvedFieldLocationSummary(
4428 instruction, instruction->GetFieldType(), calling_convention);
4429}
4430
4431void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4432 HUnresolvedInstanceFieldGet* instruction) {
4433 FieldAccessCallingConventionX86_64 calling_convention;
4434 codegen_->GenerateUnresolvedFieldAccess(instruction,
4435 instruction->GetFieldType(),
4436 instruction->GetFieldIndex(),
4437 instruction->GetDexPc(),
4438 calling_convention);
4439}
4440
4441void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4442 HUnresolvedInstanceFieldSet* instruction) {
4443 FieldAccessCallingConventionX86_64 calling_convention;
4444 codegen_->CreateUnresolvedFieldLocationSummary(
4445 instruction, instruction->GetFieldType(), calling_convention);
4446}
4447
4448void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4449 HUnresolvedInstanceFieldSet* instruction) {
4450 FieldAccessCallingConventionX86_64 calling_convention;
4451 codegen_->GenerateUnresolvedFieldAccess(instruction,
4452 instruction->GetFieldType(),
4453 instruction->GetFieldIndex(),
4454 instruction->GetDexPc(),
4455 calling_convention);
4456}
4457
4458void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4459 HUnresolvedStaticFieldGet* instruction) {
4460 FieldAccessCallingConventionX86_64 calling_convention;
4461 codegen_->CreateUnresolvedFieldLocationSummary(
4462 instruction, instruction->GetFieldType(), calling_convention);
4463}
4464
4465void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4466 HUnresolvedStaticFieldGet* instruction) {
4467 FieldAccessCallingConventionX86_64 calling_convention;
4468 codegen_->GenerateUnresolvedFieldAccess(instruction,
4469 instruction->GetFieldType(),
4470 instruction->GetFieldIndex(),
4471 instruction->GetDexPc(),
4472 calling_convention);
4473}
4474
4475void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4476 HUnresolvedStaticFieldSet* instruction) {
4477 FieldAccessCallingConventionX86_64 calling_convention;
4478 codegen_->CreateUnresolvedFieldLocationSummary(
4479 instruction, instruction->GetFieldType(), calling_convention);
4480}
4481
4482void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4483 HUnresolvedStaticFieldSet* instruction) {
4484 FieldAccessCallingConventionX86_64 calling_convention;
4485 codegen_->GenerateUnresolvedFieldAccess(instruction,
4486 instruction->GetFieldType(),
4487 instruction->GetFieldIndex(),
4488 instruction->GetDexPc(),
4489 calling_convention);
4490}
4491
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004493 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4494 ? LocationSummary::kCallOnSlowPath
4495 : LocationSummary::kNoCall;
4496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4497 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004498 ? Location::RequiresRegister()
4499 : Location::Any();
4500 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004501 if (instruction->HasUses()) {
4502 locations->SetOut(Location::SameAsFirstInput());
4503 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504}
4505
Calin Juravle2ae48182016-03-16 14:05:09 +00004506void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4507 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004508 return;
4509 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004510 LocationSummary* locations = instruction->GetLocations();
4511 Location obj = locations->InAt(0);
4512
4513 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004514 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004515}
4516
Calin Juravle2ae48182016-03-16 14:05:09 +00004517void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004518 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004519 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520
4521 LocationSummary* locations = instruction->GetLocations();
4522 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523
4524 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004525 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004526 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004527 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004528 } else {
4529 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004530 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004531 __ jmp(slow_path->GetEntryLabel());
4532 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533 }
4534 __ j(kEqual, slow_path->GetEntryLabel());
4535}
4536
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004537void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004538 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004539}
4540
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004541void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004542 bool object_array_get_with_read_barrier =
4543 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004544 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004545 new (GetGraph()->GetArena()) LocationSummary(instruction,
4546 object_array_get_with_read_barrier ?
4547 LocationSummary::kCallOnSlowPath :
4548 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004549 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4550 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4551 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004552 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004553 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004554 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4556 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004557 // The output overlaps for an object array get when read barriers
4558 // are enabled: we do not want the move to overwrite the array's
4559 // location, as we need it to emit the read barrier.
4560 locations->SetOut(
4561 Location::RequiresRegister(),
4562 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004563 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004564}
4565
4566void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4567 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004568 Location obj_loc = locations->InAt(0);
4569 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004571 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004572 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004573
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004574 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004575 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004577 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578 if (index.IsConstant()) {
4579 __ movzxb(out, Address(obj,
4580 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4581 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004582 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583 }
4584 break;
4585 }
4586
4587 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004588 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 if (index.IsConstant()) {
4590 __ movsxb(out, Address(obj,
4591 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4592 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004593 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 }
4595 break;
4596 }
4597
4598 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004599 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 if (index.IsConstant()) {
4601 __ movsxw(out, Address(obj,
4602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4603 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004604 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 }
4606 break;
4607 }
4608
4609 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004610 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 if (index.IsConstant()) {
4612 __ movzxw(out, Address(obj,
4613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4614 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004615 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 }
4617 break;
4618 }
4619
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004620 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004621 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622 if (index.IsConstant()) {
4623 __ movl(out, Address(obj,
4624 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4625 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004626 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 }
4628 break;
4629 }
4630
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004631 case Primitive::kPrimNot: {
4632 static_assert(
4633 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4634 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004635 // /* HeapReference<Object> */ out =
4636 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4637 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004638 // Note that a potential implicit null check is handled in this
4639 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4640 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004641 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004642 } else {
4643 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4644 if (index.IsConstant()) {
4645 uint32_t offset =
4646 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4647 __ movl(out, Address(obj, offset));
4648 codegen_->MaybeRecordImplicitNullCheck(instruction);
4649 // If read barriers are enabled, emit read barriers other than
4650 // Baker's using a slow path (and also unpoison the loaded
4651 // reference, if heap poisoning is enabled).
4652 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4653 } else {
4654 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4655 codegen_->MaybeRecordImplicitNullCheck(instruction);
4656 // If read barriers are enabled, emit read barriers other than
4657 // Baker's using a slow path (and also unpoison the loaded
4658 // reference, if heap poisoning is enabled).
4659 codegen_->MaybeGenerateReadBarrierSlow(
4660 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4661 }
4662 }
4663 break;
4664 }
4665
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004667 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004668 if (index.IsConstant()) {
4669 __ movq(out, Address(obj,
4670 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4671 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004672 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 }
4674 break;
4675 }
4676
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004677 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004678 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004679 if (index.IsConstant()) {
4680 __ movss(out, Address(obj,
4681 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4682 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004683 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004684 }
4685 break;
4686 }
4687
4688 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004689 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004690 if (index.IsConstant()) {
4691 __ movsd(out, Address(obj,
4692 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4693 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004694 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004695 }
4696 break;
4697 }
4698
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004699 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004700 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004701 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 }
Roland Levillain4d027112015-07-01 15:41:14 +01004703
4704 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004705 // Potential implicit null checks, in the case of reference
4706 // arrays, are handled in the previous switch statement.
4707 } else {
4708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710}
4711
4712void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004713 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004714
4715 bool needs_write_barrier =
4716 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004717 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004718
Nicolas Geoffray39468442014-09-02 15:17:15 +01004719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004720 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004721 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004722 LocationSummary::kCallOnSlowPath :
4723 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004724
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004725 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004726 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4727 if (Primitive::IsFloatingPointType(value_type)) {
4728 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004729 } else {
4730 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4731 }
4732
4733 if (needs_write_barrier) {
4734 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004735 // These registers may be used for Baker read barriers too.
4736 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004737 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739}
4740
4741void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4742 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004743 Location array_loc = locations->InAt(0);
4744 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004745 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004746 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004747 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004748 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004749 bool needs_write_barrier =
4750 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004751 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4752 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4753 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754
4755 switch (value_type) {
4756 case Primitive::kPrimBoolean:
4757 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004758 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4759 Address address = index.IsConstant()
4760 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4761 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4762 if (value.IsRegister()) {
4763 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004765 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 break;
4769 }
4770
4771 case Primitive::kPrimShort:
4772 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4774 Address address = index.IsConstant()
4775 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4776 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4777 if (value.IsRegister()) {
4778 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004780 DCHECK(value.IsConstant()) << value;
4781 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004783 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784 break;
4785 }
4786
4787 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004788 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4789 Address address = index.IsConstant()
4790 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4791 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004792
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 if (!value.IsRegister()) {
4794 // Just setting null.
4795 DCHECK(instruction->InputAt(2)->IsNullConstant());
4796 DCHECK(value.IsConstant()) << value;
4797 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004800 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004802 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004803
4804 DCHECK(needs_write_barrier);
4805 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004806 // We cannot use a NearLabel for `done`, as its range may be too
4807 // short when Baker read barriers are enabled.
4808 Label done;
4809 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004810 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004811 Location temp_loc = locations->GetTemp(0);
4812 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004813 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004814 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4815 codegen_->AddSlowPath(slow_path);
4816 if (instruction->GetValueCanBeNull()) {
4817 __ testl(register_value, register_value);
4818 __ j(kNotEqual, &not_null);
4819 __ movl(address, Immediate(0));
4820 codegen_->MaybeRecordImplicitNullCheck(instruction);
4821 __ jmp(&done);
4822 __ Bind(&not_null);
4823 }
4824
Roland Levillain0d5a2812015-11-13 10:07:31 +00004825 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01004826 if (!kUseBakerReadBarrier) {
4827 // When (non-Baker) read barriers are enabled, the type
4828 // checking instrumentation requires two read barriers
4829 // generated by CodeGeneratorX86_64::GenerateReadBarrierSlow:
4830 //
4831 // __ movl(temp2, temp);
4832 // // /* HeapReference<Class> */ temp = temp->component_type_
4833 // __ movl(temp, Address(temp, component_offset));
4834 // codegen_->GenerateReadBarrierSlow(
4835 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4836 //
4837 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4838 // __ movl(temp2, Address(register_value, class_offset));
4839 // codegen_->GenerateReadBarrierSlow(
4840 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4841 //
4842 // __ cmpl(temp, temp2);
4843 //
4844 // However, the second read barrier may trash `temp`, as it
4845 // is a temporary register, and as such would not be saved
4846 // along with live registers before calling the runtime (nor
4847 // restored afterwards). So in this case, we bail out and
4848 // delegate the work to the array set slow path.
4849 //
4850 // TODO: Extend the register allocator to support a new
4851 // "(locally) live temp" location so as to avoid always
4852 // going into the slow path when read barriers are enabled?
4853 //
4854 // There is no such problem with Baker read barriers (see below).
4855 __ jmp(slow_path->GetEntryLabel());
4856 } else {
4857 Location temp2_loc = locations->GetTemp(1);
4858 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
4859 // /* HeapReference<Class> */ temp = array->klass_
4860 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4861 instruction, temp_loc, array, class_offset, /* needs_null_check */ true);
4862
4863 // /* HeapReference<Class> */ temp = temp->component_type_
4864 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4865 instruction, temp_loc, temp, component_offset, /* needs_null_check */ false);
4866 // Register `temp` is not trashed by the read barrier
4867 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
4868 // as that method produces a call to a ReadBarrierMarkRegX
4869 // entry point, which saves all potentially live registers,
4870 // including temporaries such a `temp`.
4871 // /* HeapReference<Class> */ temp2 = register_value->klass_
4872 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4873 instruction, temp2_loc, register_value, class_offset, /* needs_null_check */ false);
4874 // If heap poisoning is enabled, `temp` and `temp2` have
4875 // been unpoisoned by the the previous calls to
4876 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4877 __ cmpl(temp, temp2);
4878
4879 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4880 __ j(kEqual, &do_put);
4881 // We do not need to emit a read barrier for the
4882 // following heap reference load, as `temp` is only used
4883 // in a comparison with null below, and this reference
4884 // is not kept afterwards. Also, if heap poisoning is
4885 // enabled, there is no need to unpoison that heap
4886 // reference for the same reason (comparison with null).
4887 __ cmpl(Address(temp, super_offset), Immediate(0));
4888 __ j(kNotEqual, slow_path->GetEntryLabel());
4889 __ Bind(&do_put);
4890 } else {
4891 __ j(kNotEqual, slow_path->GetEntryLabel());
4892 }
4893 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004894 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01004895 // Non read barrier code.
4896
Roland Levillain0d5a2812015-11-13 10:07:31 +00004897 // /* HeapReference<Class> */ temp = array->klass_
4898 __ movl(temp, Address(array, class_offset));
4899 codegen_->MaybeRecordImplicitNullCheck(instruction);
4900 __ MaybeUnpoisonHeapReference(temp);
4901
4902 // /* HeapReference<Class> */ temp = temp->component_type_
4903 __ movl(temp, Address(temp, component_offset));
4904 // If heap poisoning is enabled, no need to unpoison `temp`
4905 // nor the object reference in `register_value->klass`, as
4906 // we are comparing two poisoned references.
4907 __ cmpl(temp, Address(register_value, class_offset));
4908
4909 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4910 __ j(kEqual, &do_put);
4911 // If heap poisoning is enabled, the `temp` reference has
4912 // not been unpoisoned yet; unpoison it now.
4913 __ MaybeUnpoisonHeapReference(temp);
4914
Roland Levillain16d9f942016-08-25 17:27:56 +01004915 // If heap poisoning is enabled, no need to unpoison the
4916 // heap reference loaded below, as it is only used for a
4917 // comparison with null.
4918 __ cmpl(Address(temp, super_offset), Immediate(0));
Roland Levillain0d5a2812015-11-13 10:07:31 +00004919 __ j(kNotEqual, slow_path->GetEntryLabel());
4920 __ Bind(&do_put);
4921 } else {
4922 __ j(kNotEqual, slow_path->GetEntryLabel());
4923 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924 }
4925 }
4926
4927 if (kPoisonHeapReferences) {
4928 __ movl(temp, register_value);
4929 __ PoisonHeapReference(temp);
4930 __ movl(address, temp);
4931 } else {
4932 __ movl(address, register_value);
4933 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004934 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004935 codegen_->MaybeRecordImplicitNullCheck(instruction);
4936 }
4937
4938 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4939 codegen_->MarkGCCard(
4940 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4941 __ Bind(&done);
4942
4943 if (slow_path != nullptr) {
4944 __ Bind(slow_path->GetExitLabel());
4945 }
4946
4947 break;
4948 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004949
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004950 case Primitive::kPrimInt: {
4951 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4952 Address address = index.IsConstant()
4953 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4954 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4955 if (value.IsRegister()) {
4956 __ movl(address, value.AsRegister<CpuRegister>());
4957 } else {
4958 DCHECK(value.IsConstant()) << value;
4959 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4960 __ movl(address, Immediate(v));
4961 }
4962 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004963 break;
4964 }
4965
4966 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004967 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4968 Address address = index.IsConstant()
4969 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4970 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4971 if (value.IsRegister()) {
4972 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004973 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004974 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004975 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004976 Address address_high = index.IsConstant()
4977 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4978 offset + sizeof(int32_t))
4979 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4980 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004981 }
4982 break;
4983 }
4984
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004985 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004986 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4987 Address address = index.IsConstant()
4988 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4989 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004990 if (value.IsFpuRegister()) {
4991 __ movss(address, value.AsFpuRegister<XmmRegister>());
4992 } else {
4993 DCHECK(value.IsConstant());
4994 int32_t v =
4995 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4996 __ movl(address, Immediate(v));
4997 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004998 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004999 break;
5000 }
5001
5002 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005003 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5004 Address address = index.IsConstant()
5005 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5006 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005007 if (value.IsFpuRegister()) {
5008 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5009 codegen_->MaybeRecordImplicitNullCheck(instruction);
5010 } else {
5011 int64_t v =
5012 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5013 Address address_high = index.IsConstant()
5014 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5015 offset + sizeof(int32_t))
5016 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5017 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5018 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005019 break;
5020 }
5021
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005022 case Primitive::kPrimVoid:
5023 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005024 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005025 }
5026}
5027
5028void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005029 LocationSummary* locations =
5030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005031 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005032 if (!instruction->IsEmittedAtUseSite()) {
5033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5034 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035}
5036
5037void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005038 if (instruction->IsEmittedAtUseSite()) {
5039 return;
5040 }
5041
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005043 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005044 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5045 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005047 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048}
5049
5050void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005051 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5052 ? LocationSummary::kCallOnSlowPath
5053 : LocationSummary::kNoCall;
5054 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005055 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005056 HInstruction* length = instruction->InputAt(1);
5057 if (!length->IsEmittedAtUseSite()) {
5058 locations->SetInAt(1, Location::RegisterOrConstant(length));
5059 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005060 if (instruction->HasUses()) {
5061 locations->SetOut(Location::SameAsFirstInput());
5062 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063}
5064
5065void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5066 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005067 Location index_loc = locations->InAt(0);
5068 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005069 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070
Mark Mendell99dbd682015-04-22 16:18:52 -04005071 if (length_loc.IsConstant()) {
5072 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5073 if (index_loc.IsConstant()) {
5074 // BCE will remove the bounds check if we are guarenteed to pass.
5075 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5076 if (index < 0 || index >= length) {
5077 codegen_->AddSlowPath(slow_path);
5078 __ jmp(slow_path->GetEntryLabel());
5079 } else {
5080 // Some optimization after BCE may have generated this, and we should not
5081 // generate a bounds check if it is a valid range.
5082 }
5083 return;
5084 }
5085
5086 // We have to reverse the jump condition because the length is the constant.
5087 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5088 __ cmpl(index_reg, Immediate(length));
5089 codegen_->AddSlowPath(slow_path);
5090 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005091 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005092 HInstruction* array_length = instruction->InputAt(1);
5093 if (array_length->IsEmittedAtUseSite()) {
5094 // Address the length field in the array.
5095 DCHECK(array_length->IsArrayLength());
5096 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5097 Location array_loc = array_length->GetLocations()->InAt(0);
5098 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
5099 if (index_loc.IsConstant()) {
5100 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5101 __ cmpl(array_len, Immediate(value));
5102 } else {
5103 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5104 }
5105 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005106 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005107 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5108 if (index_loc.IsConstant()) {
5109 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5110 __ cmpl(length, Immediate(value));
5111 } else {
5112 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5113 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005114 }
5115 codegen_->AddSlowPath(slow_path);
5116 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005117 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118}
5119
5120void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5121 CpuRegister card,
5122 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005123 CpuRegister value,
5124 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005125 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005126 if (value_can_be_null) {
5127 __ testl(value, value);
5128 __ j(kEqual, &is_null);
5129 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005130 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005131 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 __ movq(temp, object);
5133 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005134 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005135 if (value_can_be_null) {
5136 __ Bind(&is_null);
5137 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138}
5139
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005140void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005141 LOG(FATAL) << "Unimplemented";
5142}
5143
5144void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005145 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5146}
5147
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005148void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005149 LocationSummary* locations =
5150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5151 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005152}
5153
5154void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005155 HBasicBlock* block = instruction->GetBlock();
5156 if (block->GetLoopInformation() != nullptr) {
5157 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5158 // The back edge will generate the suspend check.
5159 return;
5160 }
5161 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5162 // The goto will generate the suspend check.
5163 return;
5164 }
5165 GenerateSuspendCheck(instruction, nullptr);
5166}
5167
5168void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5169 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005170 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005171 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5172 if (slow_path == nullptr) {
5173 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5174 instruction->SetSlowPath(slow_path);
5175 codegen_->AddSlowPath(slow_path);
5176 if (successor != nullptr) {
5177 DCHECK(successor->IsLoopHeader());
5178 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5179 }
5180 } else {
5181 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5182 }
5183
Andreas Gampe542451c2016-07-26 09:02:02 -07005184 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005185 /* no_rip */ true),
5186 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005187 if (successor == nullptr) {
5188 __ j(kNotEqual, slow_path->GetEntryLabel());
5189 __ Bind(slow_path->GetReturnLabel());
5190 } else {
5191 __ j(kEqual, codegen_->GetLabelOf(successor));
5192 __ jmp(slow_path->GetEntryLabel());
5193 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005194}
5195
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005196X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5197 return codegen_->GetAssembler();
5198}
5199
5200void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005201 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005202 Location source = move->GetSource();
5203 Location destination = move->GetDestination();
5204
5205 if (source.IsRegister()) {
5206 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005207 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005208 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005209 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005211 } else {
5212 DCHECK(destination.IsDoubleStackSlot());
5213 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005214 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005215 }
5216 } else if (source.IsStackSlot()) {
5217 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005218 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005219 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005220 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005221 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005222 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005223 } else {
5224 DCHECK(destination.IsStackSlot());
5225 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5226 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5227 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005228 } else if (source.IsDoubleStackSlot()) {
5229 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005230 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005231 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005232 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005233 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5234 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005235 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005236 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005237 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5238 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5239 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005240 } else if (source.IsConstant()) {
5241 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005242 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5243 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005244 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005245 if (value == 0) {
5246 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5247 } else {
5248 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5249 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005250 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005251 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005252 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005253 }
5254 } else if (constant->IsLongConstant()) {
5255 int64_t value = constant->AsLongConstant()->GetValue();
5256 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005257 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005258 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005259 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005260 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005261 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005262 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005263 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005264 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005265 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005266 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005267 } else {
5268 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005269 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005270 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5271 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005272 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005273 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005274 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005275 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005276 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005277 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005278 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005279 } else {
5280 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005281 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005282 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005283 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 } else if (source.IsFpuRegister()) {
5285 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005286 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005287 } else if (destination.IsStackSlot()) {
5288 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005289 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005290 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005291 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005295 }
5296}
5297
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005298void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005299 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005300 __ movl(Address(CpuRegister(RSP), mem), reg);
5301 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005302}
5303
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005304void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005305 ScratchRegisterScope ensure_scratch(
5306 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5307
5308 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5309 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5310 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5311 Address(CpuRegister(RSP), mem2 + stack_offset));
5312 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5313 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5314 CpuRegister(ensure_scratch.GetRegister()));
5315}
5316
Mark Mendell8a1c7282015-06-29 15:41:28 -04005317void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5318 __ movq(CpuRegister(TMP), reg1);
5319 __ movq(reg1, reg2);
5320 __ movq(reg2, CpuRegister(TMP));
5321}
5322
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005323void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5324 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5325 __ movq(Address(CpuRegister(RSP), mem), reg);
5326 __ movq(reg, CpuRegister(TMP));
5327}
5328
5329void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5330 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005331 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005332
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005333 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5334 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5335 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5336 Address(CpuRegister(RSP), mem2 + stack_offset));
5337 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5338 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5339 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005340}
5341
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005342void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5343 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5344 __ movss(Address(CpuRegister(RSP), mem), reg);
5345 __ movd(reg, CpuRegister(TMP));
5346}
5347
5348void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5349 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5350 __ movsd(Address(CpuRegister(RSP), mem), reg);
5351 __ movd(reg, CpuRegister(TMP));
5352}
5353
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005354void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005355 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005356 Location source = move->GetSource();
5357 Location destination = move->GetDestination();
5358
5359 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005360 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005361 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005362 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005363 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005364 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005365 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005366 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5367 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005368 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005369 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005370 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005371 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5372 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005373 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005374 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5375 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5376 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005377 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005378 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005379 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005380 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005381 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005382 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005383 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005384 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005385 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005386 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005387 }
5388}
5389
5390
5391void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5392 __ pushq(CpuRegister(reg));
5393}
5394
5395
5396void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5397 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005398}
5399
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005400void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005401 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005402 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5403 Immediate(mirror::Class::kStatusInitialized));
5404 __ j(kLess, slow_path->GetEntryLabel());
5405 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005406 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005407}
5408
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005409HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5410 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005411 switch (desired_class_load_kind) {
5412 case HLoadClass::LoadKind::kReferrersClass:
5413 break;
5414 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5415 DCHECK(!GetCompilerOptions().GetCompilePic());
5416 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5417 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5418 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5419 DCHECK(GetCompilerOptions().GetCompilePic());
5420 break;
5421 case HLoadClass::LoadKind::kBootImageAddress:
5422 break;
5423 case HLoadClass::LoadKind::kDexCacheAddress:
5424 DCHECK(Runtime::Current()->UseJitCompilation());
5425 break;
5426 case HLoadClass::LoadKind::kDexCachePcRelative:
5427 DCHECK(!Runtime::Current()->UseJitCompilation());
5428 break;
5429 case HLoadClass::LoadKind::kDexCacheViaMethod:
5430 break;
5431 }
5432 return desired_class_load_kind;
5433}
5434
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005435void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005436 if (cls->NeedsAccessCheck()) {
5437 InvokeRuntimeCallingConvention calling_convention;
5438 CodeGenerator::CreateLoadClassLocationSummary(
5439 cls,
5440 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5441 Location::RegisterLocation(RAX),
5442 /* code_generator_supports_read_barrier */ true);
5443 return;
5444 }
5445
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005446 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5447 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005448 ? LocationSummary::kCallOnSlowPath
5449 : LocationSummary::kNoCall;
5450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005451 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005452 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5453 }
5454
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005455 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5456 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5457 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5458 locations->SetInAt(0, Location::RequiresRegister());
5459 }
5460 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005461}
5462
5463void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005464 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005465 if (cls->NeedsAccessCheck()) {
5466 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005467 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005468 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005469 return;
5470 }
5471
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472 Location out_loc = locations->Out();
5473 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005474
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005475 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005476 bool generate_null_check = false;
5477 switch (cls->GetLoadKind()) {
5478 case HLoadClass::LoadKind::kReferrersClass: {
5479 DCHECK(!cls->CanCallRuntime());
5480 DCHECK(!cls->MustGenerateClinitCheck());
5481 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5482 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5483 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005484 cls,
5485 out_loc,
5486 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5487 /*fixup_label*/nullptr,
5488 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005489 break;
5490 }
5491 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005492 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005493 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5494 codegen_->RecordTypePatch(cls);
5495 break;
5496 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005497 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005498 DCHECK_NE(cls->GetAddress(), 0u);
5499 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5500 __ movl(out, Immediate(address)); // Zero-extended.
5501 codegen_->RecordSimplePatch();
5502 break;
5503 }
5504 case HLoadClass::LoadKind::kDexCacheAddress: {
5505 DCHECK_NE(cls->GetAddress(), 0u);
5506 // /* GcRoot<mirror::Class> */ out = *address
5507 if (IsUint<32>(cls->GetAddress())) {
5508 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005509 GenerateGcRootFieldLoad(cls,
5510 out_loc,
5511 address,
5512 /*fixup_label*/nullptr,
5513 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005514 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005515 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5516 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005517 GenerateGcRootFieldLoad(cls,
5518 out_loc,
5519 Address(out, 0),
5520 /*fixup_label*/nullptr,
5521 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005522 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005523 generate_null_check = !cls->IsInDexCache();
5524 break;
5525 }
5526 case HLoadClass::LoadKind::kDexCachePcRelative: {
5527 uint32_t offset = cls->GetDexCacheElementOffset();
5528 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5529 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5530 /* no_rip */ false);
5531 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005532 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005533 generate_null_check = !cls->IsInDexCache();
5534 break;
5535 }
5536 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5537 // /* GcRoot<mirror::Class>[] */ out =
5538 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5539 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5540 __ movq(out,
5541 Address(current_method,
5542 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5543 // /* GcRoot<mirror::Class> */ out = out[type_index]
5544 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005545 cls,
5546 out_loc,
5547 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5548 /*fixup_label*/nullptr,
5549 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005550 generate_null_check = !cls->IsInDexCache();
5551 break;
5552 }
5553 default:
5554 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5555 UNREACHABLE();
5556 }
5557
5558 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5559 DCHECK(cls->CanCallRuntime());
5560 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5561 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5562 codegen_->AddSlowPath(slow_path);
5563 if (generate_null_check) {
5564 __ testl(out, out);
5565 __ j(kEqual, slow_path->GetEntryLabel());
5566 }
5567 if (cls->MustGenerateClinitCheck()) {
5568 GenerateClassInitializationCheck(slow_path, out);
5569 } else {
5570 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005571 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005572 }
5573}
5574
5575void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5576 LocationSummary* locations =
5577 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5578 locations->SetInAt(0, Location::RequiresRegister());
5579 if (check->HasUses()) {
5580 locations->SetOut(Location::SameAsFirstInput());
5581 }
5582}
5583
5584void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005585 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005586 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005587 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005588 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005589 GenerateClassInitializationCheck(slow_path,
5590 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005591}
5592
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005593HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5594 HLoadString::LoadKind desired_string_load_kind) {
5595 if (kEmitCompilerReadBarrier) {
5596 switch (desired_string_load_kind) {
5597 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5598 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5599 case HLoadString::LoadKind::kBootImageAddress:
5600 // TODO: Implement for read barrier.
5601 return HLoadString::LoadKind::kDexCacheViaMethod;
5602 default:
5603 break;
5604 }
5605 }
5606 switch (desired_string_load_kind) {
5607 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5608 DCHECK(!GetCompilerOptions().GetCompilePic());
5609 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5610 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5611 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5612 DCHECK(GetCompilerOptions().GetCompilePic());
5613 break;
5614 case HLoadString::LoadKind::kBootImageAddress:
5615 break;
5616 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005617 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005618 break;
5619 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005620 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005621 break;
5622 case HLoadString::LoadKind::kDexCacheViaMethod:
5623 break;
5624 }
5625 return desired_string_load_kind;
5626}
5627
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005628void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005629 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005630 ? LocationSummary::kCallOnSlowPath
5631 : LocationSummary::kNoCall;
5632 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005633 if (kUseBakerReadBarrier && !load->NeedsEnvironment()) {
5634 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5635 }
5636
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005637 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5638 locations->SetInAt(0, Location::RequiresRegister());
5639 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005640 locations->SetOut(Location::RequiresRegister());
5641}
5642
5643void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005644 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005645 Location out_loc = locations->Out();
5646 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005647
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005648 switch (load->GetLoadKind()) {
5649 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5650 DCHECK(!kEmitCompilerReadBarrier);
5651 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5652 codegen_->RecordStringPatch(load);
5653 return; // No dex cache slow path.
5654 }
5655 case HLoadString::LoadKind::kBootImageAddress: {
5656 DCHECK(!kEmitCompilerReadBarrier);
5657 DCHECK_NE(load->GetAddress(), 0u);
5658 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5659 __ movl(out, Immediate(address)); // Zero-extended.
5660 codegen_->RecordSimplePatch();
5661 return; // No dex cache slow path.
5662 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005663 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005664 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005665 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005666
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005667 // TODO: Re-add the compiler code to do string dex cache lookup again.
5668 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5669 codegen_->AddSlowPath(slow_path);
5670 __ jmp(slow_path->GetEntryLabel());
5671 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005672}
5673
David Brazdilcb1c0552015-08-04 16:22:25 +01005674static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005675 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005676 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005677}
5678
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005679void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5680 LocationSummary* locations =
5681 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5682 locations->SetOut(Location::RequiresRegister());
5683}
5684
5685void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005686 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5687}
5688
5689void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5690 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5691}
5692
5693void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5694 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005695}
5696
5697void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5698 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005700 InvokeRuntimeCallingConvention calling_convention;
5701 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5702}
5703
5704void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005705 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005706 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005707}
5708
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005709static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5710 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005711 !kUseBakerReadBarrier &&
5712 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005713 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5714 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5715}
5716
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005717void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005718 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005719 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005720 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005721 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005722 case TypeCheckKind::kExactCheck:
5723 case TypeCheckKind::kAbstractClassCheck:
5724 case TypeCheckKind::kClassHierarchyCheck:
5725 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005726 call_kind =
5727 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005728 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005729 break;
5730 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005731 case TypeCheckKind::kUnresolvedCheck:
5732 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005733 call_kind = LocationSummary::kCallOnSlowPath;
5734 break;
5735 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005736
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005738 if (baker_read_barrier_slow_path) {
5739 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5740 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005741 locations->SetInAt(0, Location::RequiresRegister());
5742 locations->SetInAt(1, Location::Any());
5743 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5744 locations->SetOut(Location::RequiresRegister());
5745 // When read barriers are enabled, we need a temporary register for
5746 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005747 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005748 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005749 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005750}
5751
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005752void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005753 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005754 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755 Location obj_loc = locations->InAt(0);
5756 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005757 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005758 Location out_loc = locations->Out();
5759 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005760 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005761 locations->GetTemp(0) :
5762 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005763 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5765 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5766 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005767 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005769
5770 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005771 // Avoid null check if we know obj is not null.
5772 if (instruction->MustDoNullCheck()) {
5773 __ testl(obj, obj);
5774 __ j(kEqual, &zero);
5775 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005776
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005778 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005779
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005780 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 case TypeCheckKind::kExactCheck: {
5782 if (cls.IsRegister()) {
5783 __ cmpl(out, cls.AsRegister<CpuRegister>());
5784 } else {
5785 DCHECK(cls.IsStackSlot()) << cls;
5786 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5787 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005788 if (zero.IsLinked()) {
5789 // Classes must be equal for the instanceof to succeed.
5790 __ j(kNotEqual, &zero);
5791 __ movl(out, Immediate(1));
5792 __ jmp(&done);
5793 } else {
5794 __ setcc(kEqual, out);
5795 // setcc only sets the low byte.
5796 __ andl(out, Immediate(1));
5797 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005798 break;
5799 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005800
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005801 case TypeCheckKind::kAbstractClassCheck: {
5802 // If the class is abstract, we eagerly fetch the super class of the
5803 // object to avoid doing a comparison we know will fail.
5804 NearLabel loop, success;
5805 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005807 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005808 __ testl(out, out);
5809 // If `out` is null, we use it for the result, and jump to `done`.
5810 __ j(kEqual, &done);
5811 if (cls.IsRegister()) {
5812 __ cmpl(out, cls.AsRegister<CpuRegister>());
5813 } else {
5814 DCHECK(cls.IsStackSlot()) << cls;
5815 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5816 }
5817 __ j(kNotEqual, &loop);
5818 __ movl(out, Immediate(1));
5819 if (zero.IsLinked()) {
5820 __ jmp(&done);
5821 }
5822 break;
5823 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005824
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005825 case TypeCheckKind::kClassHierarchyCheck: {
5826 // Walk over the class hierarchy to find a match.
5827 NearLabel loop, success;
5828 __ Bind(&loop);
5829 if (cls.IsRegister()) {
5830 __ cmpl(out, cls.AsRegister<CpuRegister>());
5831 } else {
5832 DCHECK(cls.IsStackSlot()) << cls;
5833 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5834 }
5835 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005836 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005837 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 __ testl(out, out);
5839 __ j(kNotEqual, &loop);
5840 // If `out` is null, we use it for the result, and jump to `done`.
5841 __ jmp(&done);
5842 __ Bind(&success);
5843 __ movl(out, Immediate(1));
5844 if (zero.IsLinked()) {
5845 __ jmp(&done);
5846 }
5847 break;
5848 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005849
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005850 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005851 // Do an exact check.
5852 NearLabel exact_check;
5853 if (cls.IsRegister()) {
5854 __ cmpl(out, cls.AsRegister<CpuRegister>());
5855 } else {
5856 DCHECK(cls.IsStackSlot()) << cls;
5857 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5858 }
5859 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005860 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005861 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005862 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863 __ testl(out, out);
5864 // If `out` is null, we use it for the result, and jump to `done`.
5865 __ j(kEqual, &done);
5866 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5867 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005868 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869 __ movl(out, Immediate(1));
5870 __ jmp(&done);
5871 break;
5872 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005873
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874 case TypeCheckKind::kArrayCheck: {
5875 if (cls.IsRegister()) {
5876 __ cmpl(out, cls.AsRegister<CpuRegister>());
5877 } else {
5878 DCHECK(cls.IsStackSlot()) << cls;
5879 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5880 }
5881 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005882 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5883 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 codegen_->AddSlowPath(slow_path);
5885 __ j(kNotEqual, slow_path->GetEntryLabel());
5886 __ movl(out, Immediate(1));
5887 if (zero.IsLinked()) {
5888 __ jmp(&done);
5889 }
5890 break;
5891 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005892
Calin Juravle98893e12015-10-02 21:05:03 +01005893 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005894 case TypeCheckKind::kInterfaceCheck: {
5895 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005896 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 // cases.
5898 //
5899 // We cannot directly call the InstanceofNonTrivial runtime
5900 // entry point without resorting to a type checking slow path
5901 // here (i.e. by calling InvokeRuntime directly), as it would
5902 // require to assign fixed registers for the inputs of this
5903 // HInstanceOf instruction (following the runtime calling
5904 // convention), which might be cluttered by the potential first
5905 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005906 //
5907 // TODO: Introduce a new runtime entry point taking the object
5908 // to test (instead of its class) as argument, and let it deal
5909 // with the read barrier issues. This will let us refactor this
5910 // case of the `switch` code as it was previously (with a direct
5911 // call to the runtime not using a type checking slow path).
5912 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913 DCHECK(locations->OnlyCallsOnSlowPath());
5914 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5915 /* is_fatal */ false);
5916 codegen_->AddSlowPath(slow_path);
5917 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918 if (zero.IsLinked()) {
5919 __ jmp(&done);
5920 }
5921 break;
5922 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005923 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005924
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005926 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 __ xorl(out, out);
5928 }
5929
5930 if (done.IsLinked()) {
5931 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005932 }
5933
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005934 if (slow_path != nullptr) {
5935 __ Bind(slow_path->GetExitLabel());
5936 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005937}
5938
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005939void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5941 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005943 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005944 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945 case TypeCheckKind::kExactCheck:
5946 case TypeCheckKind::kAbstractClassCheck:
5947 case TypeCheckKind::kClassHierarchyCheck:
5948 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005949 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5950 LocationSummary::kCallOnSlowPath :
5951 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01005952 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 break;
5954 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005955 case TypeCheckKind::kUnresolvedCheck:
5956 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 call_kind = LocationSummary::kCallOnSlowPath;
5958 break;
5959 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005960 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005961 if (baker_read_barrier_slow_path) {
5962 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5963 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964 locations->SetInAt(0, Location::RequiresRegister());
5965 locations->SetInAt(1, Location::Any());
5966 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5967 locations->AddTemp(Location::RequiresRegister());
5968 // When read barriers are enabled, we need an additional temporary
5969 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005970 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005971 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005973}
5974
5975void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005976 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005977 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978 Location obj_loc = locations->InAt(0);
5979 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005980 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981 Location temp_loc = locations->GetTemp(0);
5982 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005983 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005984 locations->GetTemp(1) :
5985 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005986 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5987 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5988 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5989 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005990
Roland Levillain0d5a2812015-11-13 10:07:31 +00005991 bool is_type_check_slow_path_fatal =
5992 (type_check_kind == TypeCheckKind::kExactCheck ||
5993 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5994 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5995 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5996 !instruction->CanThrowIntoCatchBlock();
5997 SlowPathCode* type_check_slow_path =
5998 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5999 is_type_check_slow_path_fatal);
6000 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006001
Roland Levillain0d5a2812015-11-13 10:07:31 +00006002 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 case TypeCheckKind::kExactCheck:
6004 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006005 NearLabel done;
6006 // Avoid null check if we know obj is not null.
6007 if (instruction->MustDoNullCheck()) {
6008 __ testl(obj, obj);
6009 __ j(kEqual, &done);
6010 }
6011
6012 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006013 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006014
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006015 if (cls.IsRegister()) {
6016 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6017 } else {
6018 DCHECK(cls.IsStackSlot()) << cls;
6019 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6020 }
6021 // Jump to slow path for throwing the exception or doing a
6022 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006023 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006024 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 break;
6026 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006027
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006029 NearLabel done;
6030 // Avoid null check if we know obj is not null.
6031 if (instruction->MustDoNullCheck()) {
6032 __ testl(obj, obj);
6033 __ j(kEqual, &done);
6034 }
6035
6036 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006037 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006038
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 // If the class is abstract, we eagerly fetch the super class of the
6040 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006041 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006042 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006043 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006044 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045
6046 // If the class reference currently in `temp` is not null, jump
6047 // to the `compare_classes` label to compare it with the checked
6048 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006049 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006050 __ j(kNotEqual, &compare_classes);
6051 // Otherwise, jump to the slow path to throw the exception.
6052 //
6053 // But before, move back the object's class into `temp` before
6054 // going into the slow path, as it has been overwritten in the
6055 // meantime.
6056 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006057 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006058 __ jmp(type_check_slow_path->GetEntryLabel());
6059
6060 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006061 if (cls.IsRegister()) {
6062 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6063 } else {
6064 DCHECK(cls.IsStackSlot()) << cls;
6065 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6066 }
6067 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006068 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006069 break;
6070 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006071
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006072 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006073 NearLabel done;
6074 // Avoid null check if we know obj is not null.
6075 if (instruction->MustDoNullCheck()) {
6076 __ testl(obj, obj);
6077 __ j(kEqual, &done);
6078 }
6079
6080 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006081 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006082
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006083 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006084 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085 __ Bind(&loop);
6086 if (cls.IsRegister()) {
6087 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6088 } else {
6089 DCHECK(cls.IsStackSlot()) << cls;
6090 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6091 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006092 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006093
Roland Levillain0d5a2812015-11-13 10:07:31 +00006094 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006095 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096
6097 // If the class reference currently in `temp` is not null, jump
6098 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006099 __ testl(temp, temp);
6100 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006101 // Otherwise, jump to the slow path to throw the exception.
6102 //
6103 // But before, move back the object's class into `temp` before
6104 // going into the slow path, as it has been overwritten in the
6105 // meantime.
6106 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006107 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006108 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006109 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006110 break;
6111 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006112
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006113 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006114 // We cannot use a NearLabel here, as its range might be too
6115 // short in some cases when read barriers are enabled. This has
6116 // been observed for instance when the code emitted for this
6117 // case uses high x86-64 registers (R8-R15).
6118 Label done;
6119 // Avoid null check if we know obj is not null.
6120 if (instruction->MustDoNullCheck()) {
6121 __ testl(obj, obj);
6122 __ j(kEqual, &done);
6123 }
6124
6125 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006126 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006127
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006128 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006129 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006130 if (cls.IsRegister()) {
6131 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6132 } else {
6133 DCHECK(cls.IsStackSlot()) << cls;
6134 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6135 }
6136 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137
6138 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006140 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141
6142 // If the component type is not null (i.e. the object is indeed
6143 // an array), jump to label `check_non_primitive_component_type`
6144 // to further check that this component type is not a primitive
6145 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006146 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006147 __ j(kNotEqual, &check_non_primitive_component_type);
6148 // Otherwise, jump to the slow path to throw the exception.
6149 //
6150 // But before, move back the object's class into `temp` before
6151 // going into the slow path, as it has been overwritten in the
6152 // meantime.
6153 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006154 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 __ jmp(type_check_slow_path->GetEntryLabel());
6156
6157 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159 __ j(kEqual, &done);
6160 // Same comment as above regarding `temp` and the slow path.
6161 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006162 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006163 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006164 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 break;
6166 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006167
Calin Juravle98893e12015-10-02 21:05:03 +01006168 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006170 NearLabel done;
6171 // Avoid null check if we know obj is not null.
6172 if (instruction->MustDoNullCheck()) {
6173 __ testl(obj, obj);
6174 __ j(kEqual, &done);
6175 }
6176
6177 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006178 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006179
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006180 // We always go into the type check slow path for the unresolved
6181 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182 //
6183 // We cannot directly call the CheckCast runtime entry point
6184 // without resorting to a type checking slow path here (i.e. by
6185 // calling InvokeRuntime directly), as it would require to
6186 // assign fixed registers for the inputs of this HInstanceOf
6187 // instruction (following the runtime calling convention), which
6188 // might be cluttered by the potential first read barrier
6189 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006190 //
6191 // TODO: Introduce a new runtime entry point taking the object
6192 // to test (instead of its class) as argument, and let it deal
6193 // with the read barrier issues. This will let us refactor this
6194 // case of the `switch` code as it was previously (with a direct
6195 // call to the runtime not using a type checking slow path).
6196 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006197 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006198 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199 break;
6200 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006203}
6204
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006205void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6206 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006207 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006208 InvokeRuntimeCallingConvention calling_convention;
6209 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6210}
6211
6212void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006213 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006214 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006215 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006216 if (instruction->IsEnter()) {
6217 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6218 } else {
6219 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6220 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006221}
6222
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006223void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6224void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6225void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6226
6227void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6228 LocationSummary* locations =
6229 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6230 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6231 || instruction->GetResultType() == Primitive::kPrimLong);
6232 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006233 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006234 locations->SetOut(Location::SameAsFirstInput());
6235}
6236
6237void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6238 HandleBitwiseOperation(instruction);
6239}
6240
6241void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6242 HandleBitwiseOperation(instruction);
6243}
6244
6245void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6246 HandleBitwiseOperation(instruction);
6247}
6248
6249void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6250 LocationSummary* locations = instruction->GetLocations();
6251 Location first = locations->InAt(0);
6252 Location second = locations->InAt(1);
6253 DCHECK(first.Equals(locations->Out()));
6254
6255 if (instruction->GetResultType() == Primitive::kPrimInt) {
6256 if (second.IsRegister()) {
6257 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006258 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006259 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006260 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006261 } else {
6262 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006263 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006264 }
6265 } else if (second.IsConstant()) {
6266 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6267 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006268 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006269 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006270 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006271 } else {
6272 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006273 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006274 }
6275 } else {
6276 Address address(CpuRegister(RSP), second.GetStackIndex());
6277 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006278 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006279 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006280 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006281 } else {
6282 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006283 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006284 }
6285 }
6286 } else {
6287 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006288 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6289 bool second_is_constant = false;
6290 int64_t value = 0;
6291 if (second.IsConstant()) {
6292 second_is_constant = true;
6293 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006294 }
Mark Mendell40741f32015-04-20 22:10:34 -04006295 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006296
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006297 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006298 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006299 if (is_int32_value) {
6300 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6301 } else {
6302 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6303 }
6304 } else if (second.IsDoubleStackSlot()) {
6305 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006306 } else {
6307 __ andq(first_reg, second.AsRegister<CpuRegister>());
6308 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006309 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006310 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006311 if (is_int32_value) {
6312 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6313 } else {
6314 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6315 }
6316 } else if (second.IsDoubleStackSlot()) {
6317 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006318 } else {
6319 __ orq(first_reg, second.AsRegister<CpuRegister>());
6320 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006321 } else {
6322 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006323 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006324 if (is_int32_value) {
6325 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6326 } else {
6327 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6328 }
6329 } else if (second.IsDoubleStackSlot()) {
6330 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006331 } else {
6332 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6333 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006334 }
6335 }
6336}
6337
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006338void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6339 Location out,
6340 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006341 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006342 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6343 if (kEmitCompilerReadBarrier) {
6344 if (kUseBakerReadBarrier) {
6345 // Load with fast path based Baker's read barrier.
6346 // /* HeapReference<Object> */ out = *(out + offset)
6347 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006348 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006349 } else {
6350 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006351 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006352 // in the following move operation, as we will need it for the
6353 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006354 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006355 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006356 // /* HeapReference<Object> */ out = *(out + offset)
6357 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006358 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006359 }
6360 } else {
6361 // Plain load with no read barrier.
6362 // /* HeapReference<Object> */ out = *(out + offset)
6363 __ movl(out_reg, Address(out_reg, offset));
6364 __ MaybeUnpoisonHeapReference(out_reg);
6365 }
6366}
6367
6368void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6369 Location out,
6370 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006371 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006372 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6373 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6374 if (kEmitCompilerReadBarrier) {
6375 if (kUseBakerReadBarrier) {
6376 // Load with fast path based Baker's read barrier.
6377 // /* HeapReference<Object> */ out = *(obj + offset)
6378 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006379 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006380 } else {
6381 // Load with slow path based read barrier.
6382 // /* HeapReference<Object> */ out = *(obj + offset)
6383 __ movl(out_reg, Address(obj_reg, offset));
6384 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6385 }
6386 } else {
6387 // Plain load with no read barrier.
6388 // /* HeapReference<Object> */ out = *(obj + offset)
6389 __ movl(out_reg, Address(obj_reg, offset));
6390 __ MaybeUnpoisonHeapReference(out_reg);
6391 }
6392}
6393
6394void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6395 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006396 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006397 Label* fixup_label,
6398 bool requires_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006399 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006400 if (requires_read_barrier) {
6401 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006402 if (kUseBakerReadBarrier) {
6403 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6404 // Baker's read barrier are used:
6405 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006406 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006407 // if (Thread::Current()->GetIsGcMarking()) {
6408 // root = ReadBarrier::Mark(root)
6409 // }
6410
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006411 // /* GcRoot<mirror::Object> */ root = *address
6412 __ movl(root_reg, address);
6413 if (fixup_label != nullptr) {
6414 __ Bind(fixup_label);
6415 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006416 static_assert(
6417 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6418 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6419 "have different sizes.");
6420 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6421 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6422 "have different sizes.");
6423
Vladimir Marko953437b2016-08-24 08:30:46 +00006424 // Slow path marking the GC root `root`.
6425 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6426 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006427 codegen_->AddSlowPath(slow_path);
6428
Andreas Gampe542451c2016-07-26 09:02:02 -07006429 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006430 /* no_rip */ true),
6431 Immediate(0));
6432 __ j(kNotEqual, slow_path->GetEntryLabel());
6433 __ Bind(slow_path->GetExitLabel());
6434 } else {
6435 // GC root loaded through a slow path for read barriers other
6436 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006437 // /* GcRoot<mirror::Object>* */ root = address
6438 __ leaq(root_reg, address);
6439 if (fixup_label != nullptr) {
6440 __ Bind(fixup_label);
6441 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006442 // /* mirror::Object* */ root = root->Read()
6443 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6444 }
6445 } else {
6446 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006447 // /* GcRoot<mirror::Object> */ root = *address
6448 __ movl(root_reg, address);
6449 if (fixup_label != nullptr) {
6450 __ Bind(fixup_label);
6451 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006452 // Note that GC roots are not affected by heap poisoning, thus we
6453 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006454 }
6455}
6456
6457void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6458 Location ref,
6459 CpuRegister obj,
6460 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006461 bool needs_null_check) {
6462 DCHECK(kEmitCompilerReadBarrier);
6463 DCHECK(kUseBakerReadBarrier);
6464
6465 // /* HeapReference<Object> */ ref = *(obj + offset)
6466 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006467 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006468}
6469
6470void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6471 Location ref,
6472 CpuRegister obj,
6473 uint32_t data_offset,
6474 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006475 bool needs_null_check) {
6476 DCHECK(kEmitCompilerReadBarrier);
6477 DCHECK(kUseBakerReadBarrier);
6478
Roland Levillain3d312422016-06-23 13:53:42 +01006479 static_assert(
6480 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6481 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006482 // /* HeapReference<Object> */ ref =
6483 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6484 Address src = index.IsConstant() ?
6485 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6486 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006487 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006488}
6489
6490void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6491 Location ref,
6492 CpuRegister obj,
6493 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006494 bool needs_null_check) {
6495 DCHECK(kEmitCompilerReadBarrier);
6496 DCHECK(kUseBakerReadBarrier);
6497
6498 // In slow path based read barriers, the read barrier call is
6499 // inserted after the original load. However, in fast path based
6500 // Baker's read barriers, we need to perform the load of
6501 // mirror::Object::monitor_ *before* the original reference load.
6502 // This load-load ordering is required by the read barrier.
6503 // The fast path/slow path (for Baker's algorithm) should look like:
6504 //
6505 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6506 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6507 // HeapReference<Object> ref = *src; // Original reference load.
6508 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6509 // if (is_gray) {
6510 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6511 // }
6512 //
6513 // Note: the original implementation in ReadBarrier::Barrier is
6514 // slightly more complex as:
6515 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006516 // the high-bits of rb_state, which are expected to be all zeroes
6517 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6518 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006519 // - it performs additional checks that we do not do here for
6520 // performance reasons.
6521
6522 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006523 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6524
Vladimir Marko953437b2016-08-24 08:30:46 +00006525 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6526 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6527 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6528 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6529 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6530 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6531 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6532
6533 // if (rb_state == ReadBarrier::gray_ptr_)
6534 // ref = ReadBarrier::Mark(ref);
6535 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6536 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006537 if (needs_null_check) {
6538 MaybeRecordImplicitNullCheck(instruction);
6539 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006540
6541 // Load fence to prevent load-load reordering.
6542 // Note that this is a no-op, thanks to the x86-64 memory model.
6543 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6544
6545 // The actual reference load.
6546 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006547 __ movl(ref_reg, src); // Flags are unaffected.
6548
6549 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6550 // Slow path marking the object `ref` when it is gray.
6551 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6552 instruction, ref, /* unpoison */ true);
6553 AddSlowPath(slow_path);
6554
6555 // We have done the "if" of the gray bit check above, now branch based on the flags.
6556 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006557
6558 // Object* ref = ref_addr->AsMirrorPtr()
6559 __ MaybeUnpoisonHeapReference(ref_reg);
6560
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006561 __ Bind(slow_path->GetExitLabel());
6562}
6563
6564void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6565 Location out,
6566 Location ref,
6567 Location obj,
6568 uint32_t offset,
6569 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006570 DCHECK(kEmitCompilerReadBarrier);
6571
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006572 // Insert a slow path based read barrier *after* the reference load.
6573 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006574 // If heap poisoning is enabled, the unpoisoning of the loaded
6575 // reference will be carried out by the runtime within the slow
6576 // path.
6577 //
6578 // Note that `ref` currently does not get unpoisoned (when heap
6579 // poisoning is enabled), which is alright as the `ref` argument is
6580 // not used by the artReadBarrierSlow entry point.
6581 //
6582 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6583 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6584 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
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 Levillain1e7f8db2015-12-15 10:54:19 +00006591void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6592 Location out,
6593 Location ref,
6594 Location obj,
6595 uint32_t offset,
6596 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006597 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006598 // Baker's read barriers shall be handled by the fast path
6599 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6600 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601 // If heap poisoning is enabled, unpoisoning will be taken care of
6602 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006603 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604 } else if (kPoisonHeapReferences) {
6605 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6606 }
6607}
6608
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006609void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6610 Location out,
6611 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006612 DCHECK(kEmitCompilerReadBarrier);
6613
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006614 // Insert a slow path based read barrier *after* the GC root load.
6615 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006616 // Note that GC roots are not affected by heap poisoning, so we do
6617 // not need to do anything special for this here.
6618 SlowPathCode* slow_path =
6619 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6620 AddSlowPath(slow_path);
6621
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 __ jmp(slow_path->GetEntryLabel());
6623 __ Bind(slow_path->GetExitLabel());
6624}
6625
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006626void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006627 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006628 LOG(FATAL) << "Unreachable";
6629}
6630
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006631void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006632 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006633 LOG(FATAL) << "Unreachable";
6634}
6635
Mark Mendellfe57faa2015-09-18 09:26:15 -04006636// Simple implementation of packed switch - generate cascaded compare/jumps.
6637void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6638 LocationSummary* locations =
6639 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6640 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006641 locations->AddTemp(Location::RequiresRegister());
6642 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006643}
6644
6645void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6646 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006647 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006648 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006649 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6650 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6651 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006652 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6653
6654 // Should we generate smaller inline compare/jumps?
6655 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6656 // Figure out the correct compare values and jump conditions.
6657 // Handle the first compare/branch as a special case because it might
6658 // jump to the default case.
6659 DCHECK_GT(num_entries, 2u);
6660 Condition first_condition;
6661 uint32_t index;
6662 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6663 if (lower_bound != 0) {
6664 first_condition = kLess;
6665 __ cmpl(value_reg_in, Immediate(lower_bound));
6666 __ j(first_condition, codegen_->GetLabelOf(default_block));
6667 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6668
6669 index = 1;
6670 } else {
6671 // Handle all the compare/jumps below.
6672 first_condition = kBelow;
6673 index = 0;
6674 }
6675
6676 // Handle the rest of the compare/jumps.
6677 for (; index + 1 < num_entries; index += 2) {
6678 int32_t compare_to_value = lower_bound + index + 1;
6679 __ cmpl(value_reg_in, Immediate(compare_to_value));
6680 // Jump to successors[index] if value < case_value[index].
6681 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6682 // Jump to successors[index + 1] if value == case_value[index + 1].
6683 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6684 }
6685
6686 if (index != num_entries) {
6687 // There are an odd number of entries. Handle the last one.
6688 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006689 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006690 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6691 }
6692
6693 // And the default for any other value.
6694 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6695 __ jmp(codegen_->GetLabelOf(default_block));
6696 }
6697 return;
6698 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006699
6700 // Remove the bias, if needed.
6701 Register value_reg_out = value_reg_in.AsRegister();
6702 if (lower_bound != 0) {
6703 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6704 value_reg_out = temp_reg.AsRegister();
6705 }
6706 CpuRegister value_reg(value_reg_out);
6707
6708 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006709 __ cmpl(value_reg, Immediate(num_entries - 1));
6710 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006711
Mark Mendell9c86b482015-09-18 13:36:07 -04006712 // We are in the range of the table.
6713 // Load the address of the jump table in the constant area.
6714 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006715
Mark Mendell9c86b482015-09-18 13:36:07 -04006716 // Load the (signed) offset from the jump table.
6717 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6718
6719 // Add the offset to the address of the table base.
6720 __ addq(temp_reg, base_reg);
6721
6722 // And jump.
6723 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006724}
6725
Aart Bikc5d47542016-01-27 17:00:35 -08006726void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6727 if (value == 0) {
6728 __ xorl(dest, dest);
6729 } else {
6730 __ movl(dest, Immediate(value));
6731 }
6732}
6733
Mark Mendell92e83bf2015-05-07 11:25:03 -04006734void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6735 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006736 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006737 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006738 } else if (IsUint<32>(value)) {
6739 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006740 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6741 } else {
6742 __ movq(dest, Immediate(value));
6743 }
6744}
6745
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006746void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6747 if (value == 0) {
6748 __ xorps(dest, dest);
6749 } else {
6750 __ movss(dest, LiteralInt32Address(value));
6751 }
6752}
6753
6754void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6755 if (value == 0) {
6756 __ xorpd(dest, dest);
6757 } else {
6758 __ movsd(dest, LiteralInt64Address(value));
6759 }
6760}
6761
6762void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6763 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6764}
6765
6766void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6767 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6768}
6769
Aart Bika19616e2016-02-01 18:57:58 -08006770void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6771 if (value == 0) {
6772 __ testl(dest, dest);
6773 } else {
6774 __ cmpl(dest, Immediate(value));
6775 }
6776}
6777
6778void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6779 if (IsInt<32>(value)) {
6780 if (value == 0) {
6781 __ testq(dest, dest);
6782 } else {
6783 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6784 }
6785 } else {
6786 // Value won't fit in an int.
6787 __ cmpq(dest, LiteralInt64Address(value));
6788 }
6789}
6790
Mark Mendellcfa410b2015-05-25 16:02:44 -04006791void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6792 DCHECK(dest.IsDoubleStackSlot());
6793 if (IsInt<32>(value)) {
6794 // Can move directly as an int32 constant.
6795 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6796 Immediate(static_cast<int32_t>(value)));
6797 } else {
6798 Load64BitValue(CpuRegister(TMP), value);
6799 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6800 }
6801}
6802
Mark Mendell9c86b482015-09-18 13:36:07 -04006803/**
6804 * Class to handle late fixup of offsets into constant area.
6805 */
6806class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6807 public:
6808 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6809 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6810
6811 protected:
6812 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6813
6814 CodeGeneratorX86_64* codegen_;
6815
6816 private:
6817 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6818 // Patch the correct offset for the instruction. We use the address of the
6819 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6820 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6821 int32_t relative_position = constant_offset - pos;
6822
6823 // Patch in the right value.
6824 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6825 }
6826
6827 // Location in constant area that the fixup refers to.
6828 size_t offset_into_constant_area_;
6829};
6830
6831/**
6832 t * Class to handle late fixup of offsets to a jump table that will be created in the
6833 * constant area.
6834 */
6835class JumpTableRIPFixup : public RIPFixup {
6836 public:
6837 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6838 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6839
6840 void CreateJumpTable() {
6841 X86_64Assembler* assembler = codegen_->GetAssembler();
6842
6843 // Ensure that the reference to the jump table has the correct offset.
6844 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6845 SetOffset(offset_in_constant_table);
6846
6847 // Compute the offset from the start of the function to this jump table.
6848 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6849
6850 // Populate the jump table with the correct values for the jump table.
6851 int32_t num_entries = switch_instr_->GetNumEntries();
6852 HBasicBlock* block = switch_instr_->GetBlock();
6853 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6854 // The value that we want is the target offset - the position of the table.
6855 for (int32_t i = 0; i < num_entries; i++) {
6856 HBasicBlock* b = successors[i];
6857 Label* l = codegen_->GetLabelOf(b);
6858 DCHECK(l->IsBound());
6859 int32_t offset_to_block = l->Position() - current_table_offset;
6860 assembler->AppendInt32(offset_to_block);
6861 }
6862 }
6863
6864 private:
6865 const HPackedSwitch* switch_instr_;
6866};
6867
Mark Mendellf55c3e02015-03-26 21:07:46 -04006868void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6869 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006870 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006871 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6872 // 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 -04006873 assembler->Align(4, 0);
6874 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006875
6876 // Populate any jump tables.
6877 for (auto jump_table : fixups_to_jump_tables_) {
6878 jump_table->CreateJumpTable();
6879 }
6880
6881 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006882 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006883 }
6884
6885 // And finish up.
6886 CodeGenerator::Finalize(allocator);
6887}
6888
Mark Mendellf55c3e02015-03-26 21:07:46 -04006889Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6890 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6891 return Address::RIP(fixup);
6892}
6893
6894Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6895 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6896 return Address::RIP(fixup);
6897}
6898
6899Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6900 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6901 return Address::RIP(fixup);
6902}
6903
6904Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6905 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6906 return Address::RIP(fixup);
6907}
6908
Andreas Gampe85b62f22015-09-09 13:15:38 -07006909// TODO: trg as memory.
6910void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6911 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006912 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006913 return;
6914 }
6915
6916 DCHECK_NE(type, Primitive::kPrimVoid);
6917
6918 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6919 if (trg.Equals(return_loc)) {
6920 return;
6921 }
6922
6923 // Let the parallel move resolver take care of all of this.
6924 HParallelMove parallel_move(GetGraph()->GetArena());
6925 parallel_move.AddMove(return_loc, trg, type, nullptr);
6926 GetMoveResolver()->EmitNativeCode(&parallel_move);
6927}
6928
Mark Mendell9c86b482015-09-18 13:36:07 -04006929Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6930 // Create a fixup to be used to create and address the jump table.
6931 JumpTableRIPFixup* table_fixup =
6932 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6933
6934 // We have to populate the jump tables.
6935 fixups_to_jump_tables_.push_back(table_fixup);
6936 return Address::RIP(table_fixup);
6937}
6938
Mark Mendellea5af682015-10-22 17:35:49 -04006939void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6940 const Address& addr_high,
6941 int64_t v,
6942 HInstruction* instruction) {
6943 if (IsInt<32>(v)) {
6944 int32_t v_32 = v;
6945 __ movq(addr_low, Immediate(v_32));
6946 MaybeRecordImplicitNullCheck(instruction);
6947 } else {
6948 // Didn't fit in a register. Do it in pieces.
6949 int32_t low_v = Low32Bits(v);
6950 int32_t high_v = High32Bits(v);
6951 __ movl(addr_low, Immediate(low_v));
6952 MaybeRecordImplicitNullCheck(instruction);
6953 __ movl(addr_high, Immediate(high_v));
6954 }
6955}
6956
Roland Levillain4d027112015-07-01 15:41:14 +01006957#undef __
6958
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006959} // namespace x86_64
6960} // namespace art