blob: 46f90603b5fc6eb695cb7b801fa65d0276ef49ff [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 TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000293 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000294 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000296 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100298 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
299 : locations->Out();
300 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 DCHECK(instruction_->IsCheckCast()
302 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Roland Levillain0d5a2812015-11-13 10:07:31 +0000304 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000306
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000307 if (!is_fatal_) {
308 SaveLiveRegisters(codegen, locations);
309 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
311 // We're moving two locations to locations that could overlap, so we need a parallel
312 // move resolver.
313 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000314 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100315 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000316 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100317 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100318 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100319 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
320 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100323 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000324 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700325 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 } else {
327 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100328 x86_64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000329 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000331
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 if (!is_fatal_) {
333 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000334 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000336
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 RestoreLiveRegisters(codegen, locations);
338 __ jmp(GetExitLabel());
339 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 }
341
Alexandre Rames9931f312015-06-19 14:47:01 +0100342 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
343
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 bool IsFatal() const OVERRIDE { return is_fatal_; }
345
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000347 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348
349 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
350};
351
Andreas Gampe85b62f22015-09-09 13:15:38 -0700352class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700353 public:
Aart Bik42249c32016-01-07 15:33:50 -0800354 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000355 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700356
357 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000358 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700359 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100360 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000361 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700362 }
363
Alexandre Rames9931f312015-06-19 14:47:01 +0100364 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
365
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
368};
369
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100370class ArraySetSlowPathX86_64 : public SlowPathCode {
371 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000372 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100373
374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 LocationSummary* locations = instruction_->GetLocations();
376 __ Bind(GetEntryLabel());
377 SaveLiveRegisters(codegen, locations);
378
379 InvokeRuntimeCallingConvention calling_convention;
380 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
381 parallel_move.AddMove(
382 locations->InAt(0),
383 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
384 Primitive::kPrimNot,
385 nullptr);
386 parallel_move.AddMove(
387 locations->InAt(1),
388 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
389 Primitive::kPrimInt,
390 nullptr);
391 parallel_move.AddMove(
392 locations->InAt(2),
393 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
394 Primitive::kPrimNot,
395 nullptr);
396 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
397
Roland Levillain0d5a2812015-11-13 10:07:31 +0000398 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100399 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000400 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401 RestoreLiveRegisters(codegen, locations);
402 __ jmp(GetExitLabel());
403 }
404
405 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
406
407 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100408 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
409};
410
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000411// Slow path marking an object during a read barrier.
412class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
413 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000414 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj, bool unpoison)
415 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000416 DCHECK(kEmitCompilerReadBarrier);
417 }
418
419 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
420
421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
422 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100423 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000424 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100425 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000426 DCHECK(instruction_->IsInstanceFieldGet() ||
427 instruction_->IsStaticFieldGet() ||
428 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100429 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000430 instruction_->IsLoadClass() ||
431 instruction_->IsLoadString() ||
432 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100433 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100434 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
435 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000436 << "Unexpected instruction in read barrier marking slow path: "
437 << instruction_->DebugName();
438
439 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000440 if (unpoison_) {
441 // Object* ref = ref_addr->AsMirrorPtr()
442 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
443 }
Roland Levillain4359e612016-07-20 11:32:19 +0100444 // No need to save live registers; it's taken care of by the
445 // entrypoint. Also, there is no need to update the stack mask,
446 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000447 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100448 DCHECK_NE(reg, RSP);
449 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
450 // "Compact" slow path, saving two moves.
451 //
452 // Instead of using the standard runtime calling convention (input
453 // and output in R0):
454 //
455 // RDI <- obj
456 // RAX <- ReadBarrierMark(RDI)
457 // obj <- RAX
458 //
459 // we just use rX (the register holding `obj`) as input and output
460 // of a dedicated entrypoint:
461 //
462 // rX <- ReadBarrierMarkRegX(rX)
463 //
464 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700465 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100466 // This runtime call does not require a stack map.
467 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000468 __ jmp(GetExitLabel());
469 }
470
471 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000472 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000473 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000474
475 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
476};
477
Roland Levillain0d5a2812015-11-13 10:07:31 +0000478// Slow path generating a read barrier for a heap reference.
479class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
480 public:
481 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
482 Location out,
483 Location ref,
484 Location obj,
485 uint32_t offset,
486 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000487 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000488 out_(out),
489 ref_(ref),
490 obj_(obj),
491 offset_(offset),
492 index_(index) {
493 DCHECK(kEmitCompilerReadBarrier);
494 // If `obj` is equal to `out` or `ref`, it means the initial
495 // object has been overwritten by (or after) the heap object
496 // reference load to be instrumented, e.g.:
497 //
498 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000499 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000500 //
501 // In that case, we have lost the information about the original
502 // object, and the emitted read barrier cannot work properly.
503 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
504 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
505}
506
507 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
508 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
509 LocationSummary* locations = instruction_->GetLocations();
510 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
511 DCHECK(locations->CanCall());
512 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100513 DCHECK(instruction_->IsInstanceFieldGet() ||
514 instruction_->IsStaticFieldGet() ||
515 instruction_->IsArrayGet() ||
516 instruction_->IsInstanceOf() ||
517 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100518 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000519 << "Unexpected instruction in read barrier for heap reference slow path: "
520 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000521
522 __ Bind(GetEntryLabel());
523 SaveLiveRegisters(codegen, locations);
524
525 // We may have to change the index's value, but as `index_` is a
526 // constant member (like other "inputs" of this slow path),
527 // introduce a copy of it, `index`.
528 Location index = index_;
529 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100530 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000531 if (instruction_->IsArrayGet()) {
532 // Compute real offset and store it in index_.
533 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
534 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
535 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
536 // We are about to change the value of `index_reg` (see the
537 // calls to art::x86_64::X86_64Assembler::shll and
538 // art::x86_64::X86_64Assembler::AddImmediate below), but it
539 // has not been saved by the previous call to
540 // art::SlowPathCode::SaveLiveRegisters, as it is a
541 // callee-save register --
542 // art::SlowPathCode::SaveLiveRegisters does not consider
543 // callee-save registers, as it has been designed with the
544 // assumption that callee-save registers are supposed to be
545 // handled by the called function. So, as a callee-save
546 // register, `index_reg` _would_ eventually be saved onto
547 // the stack, but it would be too late: we would have
548 // changed its value earlier. Therefore, we manually save
549 // it here into another freely available register,
550 // `free_reg`, chosen of course among the caller-save
551 // registers (as a callee-save `free_reg` register would
552 // exhibit the same problem).
553 //
554 // Note we could have requested a temporary register from
555 // the register allocator instead; but we prefer not to, as
556 // this is a slow path, and we know we can find a
557 // caller-save register that is available.
558 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
559 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
560 index_reg = free_reg;
561 index = Location::RegisterLocation(index_reg);
562 } else {
563 // The initial register stored in `index_` has already been
564 // saved in the call to art::SlowPathCode::SaveLiveRegisters
565 // (as it is not a callee-save register), so we can freely
566 // use it.
567 }
568 // Shifting the index value contained in `index_reg` by the
569 // scale factor (2) cannot overflow in practice, as the
570 // runtime is unable to allocate object arrays with a size
571 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
572 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
573 static_assert(
574 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
575 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
576 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
577 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100578 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
579 // intrinsics, `index_` is not shifted by a scale factor of 2
580 // (as in the case of ArrayGet), as it is actually an offset
581 // to an object field within an object.
582 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000583 DCHECK(instruction_->GetLocations()->Intrinsified());
584 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
585 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
586 << instruction_->AsInvoke()->GetIntrinsic();
587 DCHECK_EQ(offset_, 0U);
588 DCHECK(index_.IsRegister());
589 }
590 }
591
592 // We're moving two or three locations to locations that could
593 // overlap, so we need a parallel move resolver.
594 InvokeRuntimeCallingConvention calling_convention;
595 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
596 parallel_move.AddMove(ref_,
597 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
598 Primitive::kPrimNot,
599 nullptr);
600 parallel_move.AddMove(obj_,
601 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
602 Primitive::kPrimNot,
603 nullptr);
604 if (index.IsValid()) {
605 parallel_move.AddMove(index,
606 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
607 Primitive::kPrimInt,
608 nullptr);
609 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
610 } else {
611 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
612 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
613 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100614 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000615 instruction_,
616 instruction_->GetDexPc(),
617 this);
618 CheckEntrypointTypes<
619 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
620 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
621
622 RestoreLiveRegisters(codegen, locations);
623 __ jmp(GetExitLabel());
624 }
625
626 const char* GetDescription() const OVERRIDE {
627 return "ReadBarrierForHeapReferenceSlowPathX86_64";
628 }
629
630 private:
631 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
632 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
633 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
634 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
635 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
636 return static_cast<CpuRegister>(i);
637 }
638 }
639 // We shall never fail to find a free caller-save register, as
640 // there are more than two core caller-save registers on x86-64
641 // (meaning it is possible to find one which is different from
642 // `ref` and `obj`).
643 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
644 LOG(FATAL) << "Could not find a free caller-save register";
645 UNREACHABLE();
646 }
647
Roland Levillain0d5a2812015-11-13 10:07:31 +0000648 const Location out_;
649 const Location ref_;
650 const Location obj_;
651 const uint32_t offset_;
652 // An additional location containing an index to an array.
653 // Only used for HArrayGet and the UnsafeGetObject &
654 // UnsafeGetObjectVolatile intrinsics.
655 const Location index_;
656
657 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
658};
659
660// Slow path generating a read barrier for a GC root.
661class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
662 public:
663 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000664 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000665 DCHECK(kEmitCompilerReadBarrier);
666 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000667
668 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
669 LocationSummary* locations = instruction_->GetLocations();
670 DCHECK(locations->CanCall());
671 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000672 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
673 << "Unexpected instruction in read barrier for GC root slow path: "
674 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000675
676 __ Bind(GetEntryLabel());
677 SaveLiveRegisters(codegen, locations);
678
679 InvokeRuntimeCallingConvention calling_convention;
680 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
681 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100682 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000683 instruction_,
684 instruction_->GetDexPc(),
685 this);
686 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
687 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
688
689 RestoreLiveRegisters(codegen, locations);
690 __ jmp(GetExitLabel());
691 }
692
693 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
694
695 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000696 const Location out_;
697 const Location root_;
698
699 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
700};
701
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100702#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100703// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
704#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100705
Roland Levillain4fa13f62015-07-06 18:11:54 +0100706inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700707 switch (cond) {
708 case kCondEQ: return kEqual;
709 case kCondNE: return kNotEqual;
710 case kCondLT: return kLess;
711 case kCondLE: return kLessEqual;
712 case kCondGT: return kGreater;
713 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700714 case kCondB: return kBelow;
715 case kCondBE: return kBelowEqual;
716 case kCondA: return kAbove;
717 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700718 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100719 LOG(FATAL) << "Unreachable";
720 UNREACHABLE();
721}
722
Aart Bike9f37602015-10-09 11:15:55 -0700723// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100724inline Condition X86_64FPCondition(IfCondition cond) {
725 switch (cond) {
726 case kCondEQ: return kEqual;
727 case kCondNE: return kNotEqual;
728 case kCondLT: return kBelow;
729 case kCondLE: return kBelowEqual;
730 case kCondGT: return kAbove;
731 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700732 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100733 };
734 LOG(FATAL) << "Unreachable";
735 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700736}
737
Vladimir Markodc151b22015-10-15 18:02:30 +0100738HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
739 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
740 MethodReference target_method ATTRIBUTE_UNUSED) {
741 switch (desired_dispatch_info.code_ptr_location) {
742 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
743 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
744 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
745 return HInvokeStaticOrDirect::DispatchInfo {
746 desired_dispatch_info.method_load_kind,
747 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
748 desired_dispatch_info.method_load_data,
749 0u
750 };
751 default:
752 return desired_dispatch_info;
753 }
754}
755
Serguei Katkov288c7a82016-05-16 11:53:15 +0600756Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
757 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800758 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000759 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
760 switch (invoke->GetMethodLoadKind()) {
761 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
762 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000763 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000764 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000765 break;
766 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000767 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000768 break;
769 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
770 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
771 break;
772 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
773 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
774 method_patches_.emplace_back(invoke->GetTargetMethod());
775 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
776 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000777 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000778 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000779 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000780 // Bind a new fixup label at the end of the "movl" insn.
781 uint32_t offset = invoke->GetDexCacheArrayOffset();
782 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000783 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000784 }
Vladimir Marko58155012015-08-19 12:49:41 +0000785 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000786 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000787 Register method_reg;
788 CpuRegister reg = temp.AsRegister<CpuRegister>();
789 if (current_method.IsRegister()) {
790 method_reg = current_method.AsRegister<Register>();
791 } else {
792 DCHECK(invoke->GetLocations()->Intrinsified());
793 DCHECK(!current_method.IsValid());
794 method_reg = reg.AsRegister();
795 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
796 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000797 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100798 __ movq(reg,
799 Address(CpuRegister(method_reg),
800 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100801 // temp = temp[index_in_cache];
802 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
803 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000804 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
805 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100806 }
Vladimir Marko58155012015-08-19 12:49:41 +0000807 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600808 return callee_method;
809}
810
811void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
812 Location temp) {
813 // All registers are assumed to be correctly set up.
814 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000815
816 switch (invoke->GetCodePtrLocation()) {
817 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
818 __ call(&frame_entry_label_);
819 break;
820 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
821 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
822 Label* label = &relative_call_patches_.back().label;
823 __ call(label); // Bind to the patch label, override at link time.
824 __ Bind(label); // Bind the label at the end of the "call" insn.
825 break;
826 }
827 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
828 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100829 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
830 LOG(FATAL) << "Unsupported";
831 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000832 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
833 // (callee_method + offset_of_quick_compiled_code)()
834 __ call(Address(callee_method.AsRegister<CpuRegister>(),
835 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700836 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000837 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000838 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800839
840 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800841}
842
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000843void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
844 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
845 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
846 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000847
848 // Use the calling convention instead of the location of the receiver, as
849 // intrinsics may have put the receiver in a different register. In the intrinsics
850 // slow path, the arguments have been moved to the right place, so here we are
851 // guaranteed that the receiver is the first register of the calling convention.
852 InvokeDexCallingConvention calling_convention;
853 Register receiver = calling_convention.GetRegisterAt(0);
854
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000855 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000856 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000857 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000858 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000859 // Instead of simply (possibly) unpoisoning `temp` here, we should
860 // emit a read barrier for the previous class reference load.
861 // However this is not required in practice, as this is an
862 // intermediate/temporary reference and because the current
863 // concurrent copying collector keeps the from-space memory
864 // intact/accessible until the end of the marking phase (the
865 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000866 __ MaybeUnpoisonHeapReference(temp);
867 // temp = temp->GetMethodAt(method_offset);
868 __ movq(temp, Address(temp, method_offset));
869 // call temp->GetEntryPoint();
870 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700871 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000872}
873
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000874void CodeGeneratorX86_64::RecordSimplePatch() {
875 if (GetCompilerOptions().GetIncludePatchInformation()) {
876 simple_patches_.emplace_back();
877 __ Bind(&simple_patches_.back());
878 }
879}
880
881void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
882 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
883 __ Bind(&string_patches_.back().label);
884}
885
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100886void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
887 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
888 __ Bind(&type_patches_.back().label);
889}
890
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000891Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
892 uint32_t element_offset) {
893 // Add a patch entry and return the label.
894 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
895 return &pc_relative_dex_cache_patches_.back().label;
896}
897
Vladimir Marko58155012015-08-19 12:49:41 +0000898void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
899 DCHECK(linker_patches->empty());
900 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000901 method_patches_.size() +
902 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000903 pc_relative_dex_cache_patches_.size() +
904 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100905 string_patches_.size() +
906 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000907 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000908 // The label points to the end of the "movl" insn but the literal offset for method
909 // patch needs to point to the embedded constant which occupies the last 4 bytes.
910 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000911 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000912 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000913 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
914 info.target_method.dex_file,
915 info.target_method.dex_method_index));
916 }
917 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000918 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000919 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
920 info.target_method.dex_file,
921 info.target_method.dex_method_index));
922 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000923 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
924 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000925 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
926 &info.target_dex_file,
927 info.label.Position(),
928 info.element_offset));
929 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000930 for (const Label& label : simple_patches_) {
931 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
932 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
933 }
934 for (const StringPatchInfo<Label>& info : string_patches_) {
935 // These are always PC-relative, see GetSupportedLoadStringKind().
936 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
937 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
938 &info.dex_file,
939 info.label.Position(),
940 info.string_index));
941 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100942 for (const TypePatchInfo<Label>& info : type_patches_) {
943 // These are always PC-relative, see GetSupportedLoadClassKind().
944 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
945 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
946 &info.dex_file,
947 info.label.Position(),
948 info.type_index));
949 }
Vladimir Marko58155012015-08-19 12:49:41 +0000950}
951
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100953 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100954}
955
956void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100957 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100958}
959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
961 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
962 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100963}
964
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
966 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
967 return kX86_64WordSize;
968}
969
970size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
971 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
972 return kX86_64WordSize;
973}
974
975size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
976 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
977 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100978}
979
Calin Juravle175dc732015-08-25 15:42:32 +0100980void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
981 HInstruction* instruction,
982 uint32_t dex_pc,
983 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100984 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100985 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
986 if (EntrypointRequiresStackMap(entrypoint)) {
987 RecordPcInfo(instruction, dex_pc, slow_path);
988 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100989}
990
Roland Levillaindec8f632016-07-22 17:10:06 +0100991void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
992 HInstruction* instruction,
993 SlowPathCode* slow_path) {
994 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100995 GenerateInvokeRuntime(entry_point_offset);
996}
997
998void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100999 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1000}
1001
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001002static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001003// Use a fake return address register to mimic Quick.
1004static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001005CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001006 const X86_64InstructionSetFeatures& isa_features,
1007 const CompilerOptions& compiler_options,
1008 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001009 : CodeGenerator(graph,
1010 kNumberOfCpuRegisters,
1011 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001012 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001013 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1014 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001015 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001016 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1017 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001018 compiler_options,
1019 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001020 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001021 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001022 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001023 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001024 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001025 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001026 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001027 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1028 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001029 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001030 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1031 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001032 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001033 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001034 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1035}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001036
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001037InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1038 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001039 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001040 assembler_(codegen->GetAssembler()),
1041 codegen_(codegen) {}
1042
David Brazdil58282f42016-01-14 12:45:10 +00001043void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001044 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001045 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001046
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001047 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001048 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001049}
1050
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001051static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001052 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001053}
David Srbecky9d8606d2015-04-12 09:35:32 +01001054
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001055static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001056 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001057}
1058
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001059void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001061 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001062 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001063 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001064 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001065
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001066 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001067 __ testq(CpuRegister(RAX), Address(
1068 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001069 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001070 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001071
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001072 if (HasEmptyFrame()) {
1073 return;
1074 }
1075
Nicolas Geoffray98893962015-01-21 12:32:32 +00001076 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001077 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001078 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001079 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001080 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1081 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001082 }
1083 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001084
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001085 int adjust = GetFrameSize() - GetCoreSpillSize();
1086 __ subq(CpuRegister(RSP), Immediate(adjust));
1087 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001088 uint32_t xmm_spill_location = GetFpuSpillStart();
1089 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001090
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001091 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1092 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001093 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1094 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1095 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001096 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001097 }
1098
Mathieu Chartiere401d142015-04-22 13:56:20 -07001099 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001100 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001101}
1102
1103void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001104 __ cfi().RememberState();
1105 if (!HasEmptyFrame()) {
1106 uint32_t xmm_spill_location = GetFpuSpillStart();
1107 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1108 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1109 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1110 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1111 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1112 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1113 }
1114 }
1115
1116 int adjust = GetFrameSize() - GetCoreSpillSize();
1117 __ addq(CpuRegister(RSP), Immediate(adjust));
1118 __ cfi().AdjustCFAOffset(-adjust);
1119
1120 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1121 Register reg = kCoreCalleeSaves[i];
1122 if (allocated_registers_.ContainsCoreRegister(reg)) {
1123 __ popq(CpuRegister(reg));
1124 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1125 __ cfi().Restore(DWARFReg(reg));
1126 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001127 }
1128 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001129 __ ret();
1130 __ cfi().RestoreState();
1131 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001132}
1133
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001134void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1135 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136}
1137
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001138void CodeGeneratorX86_64::Move(Location destination, Location source) {
1139 if (source.Equals(destination)) {
1140 return;
1141 }
1142 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001143 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001144 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001145 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001147 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001149 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1150 } else if (source.IsConstant()) {
1151 HConstant* constant = source.GetConstant();
1152 if (constant->IsLongConstant()) {
1153 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1154 } else {
1155 Load32BitValue(dest, GetInt32ValueOf(constant));
1156 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001157 } else {
1158 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001159 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001160 }
1161 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001162 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001164 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001166 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1167 } else if (source.IsConstant()) {
1168 HConstant* constant = source.GetConstant();
1169 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1170 if (constant->IsFloatConstant()) {
1171 Load32BitValue(dest, static_cast<int32_t>(value));
1172 } else {
1173 Load64BitValue(dest, value);
1174 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001175 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001176 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001177 } else {
1178 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001179 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180 }
1181 } else if (destination.IsStackSlot()) {
1182 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001183 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001184 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001185 } else if (source.IsFpuRegister()) {
1186 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001187 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001188 } else if (source.IsConstant()) {
1189 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001190 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001191 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001192 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001193 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001194 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1195 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196 }
1197 } else {
1198 DCHECK(destination.IsDoubleStackSlot());
1199 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001201 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001202 } else if (source.IsFpuRegister()) {
1203 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001204 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001205 } else if (source.IsConstant()) {
1206 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001207 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001208 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001209 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001210 } else {
1211 DCHECK(constant->IsLongConstant());
1212 value = constant->AsLongConstant()->GetValue();
1213 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001214 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215 } else {
1216 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001217 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1218 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219 }
1220 }
1221}
1222
Calin Juravle175dc732015-08-25 15:42:32 +01001223void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1224 DCHECK(location.IsRegister());
1225 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1226}
1227
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228void CodeGeneratorX86_64::MoveLocation(
1229 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1230 Move(dst, src);
1231}
1232
1233void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1234 if (location.IsRegister()) {
1235 locations->AddTemp(location);
1236 } else {
1237 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1238 }
1239}
1240
David Brazdilfc6a86a2015-06-26 10:33:45 +00001241void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001242 DCHECK(!successor->IsExitBlock());
1243
1244 HBasicBlock* block = got->GetBlock();
1245 HInstruction* previous = got->GetPrevious();
1246
1247 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001248 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001249 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1250 return;
1251 }
1252
1253 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1254 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1255 }
1256 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001257 __ jmp(codegen_->GetLabelOf(successor));
1258 }
1259}
1260
David Brazdilfc6a86a2015-06-26 10:33:45 +00001261void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1262 got->SetLocations(nullptr);
1263}
1264
1265void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1266 HandleGoto(got, got->GetSuccessor());
1267}
1268
1269void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1270 try_boundary->SetLocations(nullptr);
1271}
1272
1273void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1274 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1275 if (!successor->IsExitBlock()) {
1276 HandleGoto(try_boundary, successor);
1277 }
1278}
1279
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001280void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1281 exit->SetLocations(nullptr);
1282}
1283
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001284void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001285}
1286
Mark Mendell152408f2015-12-31 12:28:50 -05001287template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001288void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001289 LabelType* true_label,
1290 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001291 if (cond->IsFPConditionTrueIfNaN()) {
1292 __ j(kUnordered, true_label);
1293 } else if (cond->IsFPConditionFalseIfNaN()) {
1294 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001295 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001296 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001297}
1298
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001299void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001300 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001301
Mark Mendellc4701932015-04-10 13:18:51 -04001302 Location left = locations->InAt(0);
1303 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001304 Primitive::Type type = condition->InputAt(0)->GetType();
1305 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001306 case Primitive::kPrimBoolean:
1307 case Primitive::kPrimByte:
1308 case Primitive::kPrimChar:
1309 case Primitive::kPrimShort:
1310 case Primitive::kPrimInt:
1311 case Primitive::kPrimNot: {
1312 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1313 if (right.IsConstant()) {
1314 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1315 if (value == 0) {
1316 __ testl(left_reg, left_reg);
1317 } else {
1318 __ cmpl(left_reg, Immediate(value));
1319 }
1320 } else if (right.IsStackSlot()) {
1321 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1322 } else {
1323 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1324 }
1325 break;
1326 }
Mark Mendellc4701932015-04-10 13:18:51 -04001327 case Primitive::kPrimLong: {
1328 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1329 if (right.IsConstant()) {
1330 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001331 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001332 } else if (right.IsDoubleStackSlot()) {
1333 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1334 } else {
1335 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1336 }
Mark Mendellc4701932015-04-10 13:18:51 -04001337 break;
1338 }
1339 case Primitive::kPrimFloat: {
1340 if (right.IsFpuRegister()) {
1341 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1342 } else if (right.IsConstant()) {
1343 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1344 codegen_->LiteralFloatAddress(
1345 right.GetConstant()->AsFloatConstant()->GetValue()));
1346 } else {
1347 DCHECK(right.IsStackSlot());
1348 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1349 Address(CpuRegister(RSP), right.GetStackIndex()));
1350 }
Mark Mendellc4701932015-04-10 13:18:51 -04001351 break;
1352 }
1353 case Primitive::kPrimDouble: {
1354 if (right.IsFpuRegister()) {
1355 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1356 } else if (right.IsConstant()) {
1357 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1358 codegen_->LiteralDoubleAddress(
1359 right.GetConstant()->AsDoubleConstant()->GetValue()));
1360 } else {
1361 DCHECK(right.IsDoubleStackSlot());
1362 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1363 Address(CpuRegister(RSP), right.GetStackIndex()));
1364 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001365 break;
1366 }
1367 default:
1368 LOG(FATAL) << "Unexpected condition type " << type;
1369 }
1370}
1371
1372template<class LabelType>
1373void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1374 LabelType* true_target_in,
1375 LabelType* false_target_in) {
1376 // Generated branching requires both targets to be explicit. If either of the
1377 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1378 LabelType fallthrough_target;
1379 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1380 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1381
1382 // Generate the comparison to set the CC.
1383 GenerateCompareTest(condition);
1384
1385 // Now generate the correct jump(s).
1386 Primitive::Type type = condition->InputAt(0)->GetType();
1387 switch (type) {
1388 case Primitive::kPrimLong: {
1389 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1390 break;
1391 }
1392 case Primitive::kPrimFloat: {
1393 GenerateFPJumps(condition, true_target, false_target);
1394 break;
1395 }
1396 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001397 GenerateFPJumps(condition, true_target, false_target);
1398 break;
1399 }
1400 default:
1401 LOG(FATAL) << "Unexpected condition type " << type;
1402 }
1403
David Brazdil0debae72015-11-12 18:37:00 +00001404 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001405 __ jmp(false_target);
1406 }
David Brazdil0debae72015-11-12 18:37:00 +00001407
1408 if (fallthrough_target.IsLinked()) {
1409 __ Bind(&fallthrough_target);
1410 }
Mark Mendellc4701932015-04-10 13:18:51 -04001411}
1412
David Brazdil0debae72015-11-12 18:37:00 +00001413static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1414 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1415 // are set only strictly before `branch`. We can't use the eflags on long
1416 // conditions if they are materialized due to the complex branching.
1417 return cond->IsCondition() &&
1418 cond->GetNext() == branch &&
1419 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1420}
1421
Mark Mendell152408f2015-12-31 12:28:50 -05001422template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001423void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001424 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001425 LabelType* true_target,
1426 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001427 HInstruction* cond = instruction->InputAt(condition_input_index);
1428
1429 if (true_target == nullptr && false_target == nullptr) {
1430 // Nothing to do. The code always falls through.
1431 return;
1432 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001433 // Constant condition, statically compared against "true" (integer value 1).
1434 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001435 if (true_target != nullptr) {
1436 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001437 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001438 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001439 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001440 if (false_target != nullptr) {
1441 __ jmp(false_target);
1442 }
1443 }
1444 return;
1445 }
1446
1447 // The following code generates these patterns:
1448 // (1) true_target == nullptr && false_target != nullptr
1449 // - opposite condition true => branch to false_target
1450 // (2) true_target != nullptr && false_target == nullptr
1451 // - condition true => branch to true_target
1452 // (3) true_target != nullptr && false_target != nullptr
1453 // - condition true => branch to true_target
1454 // - branch to false_target
1455 if (IsBooleanValueOrMaterializedCondition(cond)) {
1456 if (AreEflagsSetFrom(cond, instruction)) {
1457 if (true_target == nullptr) {
1458 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1459 } else {
1460 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1461 }
1462 } else {
1463 // Materialized condition, compare against 0.
1464 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1465 if (lhs.IsRegister()) {
1466 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1467 } else {
1468 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1469 }
1470 if (true_target == nullptr) {
1471 __ j(kEqual, false_target);
1472 } else {
1473 __ j(kNotEqual, true_target);
1474 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001475 }
1476 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001477 // Condition has not been materialized, use its inputs as the
1478 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001479 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001480
David Brazdil0debae72015-11-12 18:37:00 +00001481 // If this is a long or FP comparison that has been folded into
1482 // the HCondition, generate the comparison directly.
1483 Primitive::Type type = condition->InputAt(0)->GetType();
1484 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1485 GenerateCompareTestAndBranch(condition, true_target, false_target);
1486 return;
1487 }
1488
1489 Location lhs = condition->GetLocations()->InAt(0);
1490 Location rhs = condition->GetLocations()->InAt(1);
1491 if (rhs.IsRegister()) {
1492 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1493 } else if (rhs.IsConstant()) {
1494 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001495 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001496 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001497 __ cmpl(lhs.AsRegister<CpuRegister>(),
1498 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1499 }
1500 if (true_target == nullptr) {
1501 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1502 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001503 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001504 }
Dave Allison20dfc792014-06-16 20:44:29 -07001505 }
David Brazdil0debae72015-11-12 18:37:00 +00001506
1507 // If neither branch falls through (case 3), the conditional branch to `true_target`
1508 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1509 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001510 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001511 }
1512}
1513
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001514void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1516 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001517 locations->SetInAt(0, Location::Any());
1518 }
1519}
1520
1521void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001522 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1523 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1524 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1525 nullptr : codegen_->GetLabelOf(true_successor);
1526 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1527 nullptr : codegen_->GetLabelOf(false_successor);
1528 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001529}
1530
1531void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1532 LocationSummary* locations = new (GetGraph()->GetArena())
1533 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001534 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001535 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001536 locations->SetInAt(0, Location::Any());
1537 }
1538}
1539
1540void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001541 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001542 GenerateTestAndBranch<Label>(deoptimize,
1543 /* condition_input_index */ 0,
1544 slow_path->GetEntryLabel(),
1545 /* false_target */ nullptr);
1546}
1547
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001548static bool SelectCanUseCMOV(HSelect* select) {
1549 // There are no conditional move instructions for XMMs.
1550 if (Primitive::IsFloatingPointType(select->GetType())) {
1551 return false;
1552 }
1553
1554 // A FP condition doesn't generate the single CC that we need.
1555 HInstruction* condition = select->GetCondition();
1556 if (condition->IsCondition() &&
1557 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1558 return false;
1559 }
1560
1561 // We can generate a CMOV for this Select.
1562 return true;
1563}
1564
David Brazdil74eb1b22015-12-14 11:44:01 +00001565void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1567 if (Primitive::IsFloatingPointType(select->GetType())) {
1568 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001569 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001570 } else {
1571 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001572 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001573 if (select->InputAt(1)->IsConstant()) {
1574 locations->SetInAt(1, Location::RequiresRegister());
1575 } else {
1576 locations->SetInAt(1, Location::Any());
1577 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001578 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001579 locations->SetInAt(1, Location::Any());
1580 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001581 }
1582 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1583 locations->SetInAt(2, Location::RequiresRegister());
1584 }
1585 locations->SetOut(Location::SameAsFirstInput());
1586}
1587
1588void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1589 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001590 if (SelectCanUseCMOV(select)) {
1591 // If both the condition and the source types are integer, we can generate
1592 // a CMOV to implement Select.
1593 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001594 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001595 DCHECK(locations->InAt(0).Equals(locations->Out()));
1596
1597 HInstruction* select_condition = select->GetCondition();
1598 Condition cond = kNotEqual;
1599
1600 // Figure out how to test the 'condition'.
1601 if (select_condition->IsCondition()) {
1602 HCondition* condition = select_condition->AsCondition();
1603 if (!condition->IsEmittedAtUseSite()) {
1604 // This was a previously materialized condition.
1605 // Can we use the existing condition code?
1606 if (AreEflagsSetFrom(condition, select)) {
1607 // Materialization was the previous instruction. Condition codes are right.
1608 cond = X86_64IntegerCondition(condition->GetCondition());
1609 } else {
1610 // No, we have to recreate the condition code.
1611 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1612 __ testl(cond_reg, cond_reg);
1613 }
1614 } else {
1615 GenerateCompareTest(condition);
1616 cond = X86_64IntegerCondition(condition->GetCondition());
1617 }
1618 } else {
1619 // Must be a boolean condition, which needs to be compared to 0.
1620 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1621 __ testl(cond_reg, cond_reg);
1622 }
1623
1624 // If the condition is true, overwrite the output, which already contains false.
1625 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001626 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1627 if (value_true_loc.IsRegister()) {
1628 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1629 } else {
1630 __ cmov(cond,
1631 value_false,
1632 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1633 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001634 } else {
1635 NearLabel false_target;
1636 GenerateTestAndBranch<NearLabel>(select,
1637 /* condition_input_index */ 2,
1638 /* true_target */ nullptr,
1639 &false_target);
1640 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1641 __ Bind(&false_target);
1642 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001643}
1644
David Srbecky0cf44932015-12-09 14:09:59 +00001645void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1646 new (GetGraph()->GetArena()) LocationSummary(info);
1647}
1648
David Srbeckyd28f4a02016-03-14 17:14:24 +00001649void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1650 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001651}
1652
1653void CodeGeneratorX86_64::GenerateNop() {
1654 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001655}
1656
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001657void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001658 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001659 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001660 // Handle the long/FP comparisons made in instruction simplification.
1661 switch (cond->InputAt(0)->GetType()) {
1662 case Primitive::kPrimLong:
1663 locations->SetInAt(0, Location::RequiresRegister());
1664 locations->SetInAt(1, Location::Any());
1665 break;
1666 case Primitive::kPrimFloat:
1667 case Primitive::kPrimDouble:
1668 locations->SetInAt(0, Location::RequiresFpuRegister());
1669 locations->SetInAt(1, Location::Any());
1670 break;
1671 default:
1672 locations->SetInAt(0, Location::RequiresRegister());
1673 locations->SetInAt(1, Location::Any());
1674 break;
1675 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001676 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001677 locations->SetOut(Location::RequiresRegister());
1678 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001679}
1680
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001682 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001683 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001684 }
Mark Mendellc4701932015-04-10 13:18:51 -04001685
1686 LocationSummary* locations = cond->GetLocations();
1687 Location lhs = locations->InAt(0);
1688 Location rhs = locations->InAt(1);
1689 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001690 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001691
1692 switch (cond->InputAt(0)->GetType()) {
1693 default:
1694 // Integer case.
1695
1696 // Clear output register: setcc only sets the low byte.
1697 __ xorl(reg, reg);
1698
1699 if (rhs.IsRegister()) {
1700 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1701 } else if (rhs.IsConstant()) {
1702 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001703 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001704 } else {
1705 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1706 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001707 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001708 return;
1709 case Primitive::kPrimLong:
1710 // Clear output register: setcc only sets the low byte.
1711 __ xorl(reg, reg);
1712
1713 if (rhs.IsRegister()) {
1714 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1715 } else if (rhs.IsConstant()) {
1716 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001717 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001718 } else {
1719 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1720 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001722 return;
1723 case Primitive::kPrimFloat: {
1724 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1725 if (rhs.IsConstant()) {
1726 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1727 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1728 } else if (rhs.IsStackSlot()) {
1729 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1730 } else {
1731 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1732 }
1733 GenerateFPJumps(cond, &true_label, &false_label);
1734 break;
1735 }
1736 case Primitive::kPrimDouble: {
1737 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1738 if (rhs.IsConstant()) {
1739 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1740 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1741 } else if (rhs.IsDoubleStackSlot()) {
1742 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1743 } else {
1744 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1745 }
1746 GenerateFPJumps(cond, &true_label, &false_label);
1747 break;
1748 }
1749 }
1750
1751 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001752 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001753
Roland Levillain4fa13f62015-07-06 18:11:54 +01001754 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001755 __ Bind(&false_label);
1756 __ xorl(reg, reg);
1757 __ jmp(&done_label);
1758
Roland Levillain4fa13f62015-07-06 18:11:54 +01001759 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001760 __ Bind(&true_label);
1761 __ movl(reg, Immediate(1));
1762 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001795}
1796
1797void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001799}
1800
1801void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001803}
1804
1805void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001807}
1808
1809void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001811}
1812
Aart Bike9f37602015-10-09 11:15:55 -07001813void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001815}
1816
1817void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
1821void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001823}
1824
1825void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001827}
1828
1829void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001830 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001831}
1832
1833void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001835}
1836
1837void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001838 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001839}
1840
1841void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001842 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001843}
1844
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001845void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001848 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001849 case Primitive::kPrimBoolean:
1850 case Primitive::kPrimByte:
1851 case Primitive::kPrimShort:
1852 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001853 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001854 case Primitive::kPrimLong: {
1855 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001856 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001857 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1858 break;
1859 }
1860 case Primitive::kPrimFloat:
1861 case Primitive::kPrimDouble: {
1862 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001863 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001864 locations->SetOut(Location::RequiresRegister());
1865 break;
1866 }
1867 default:
1868 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1869 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001870}
1871
1872void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001873 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001874 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001875 Location left = locations->InAt(0);
1876 Location right = locations->InAt(1);
1877
Mark Mendell0c9497d2015-08-21 09:30:05 -04001878 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001879 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001880 Condition less_cond = kLess;
1881
Calin Juravleddb7df22014-11-25 20:56:51 +00001882 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001883 case Primitive::kPrimBoolean:
1884 case Primitive::kPrimByte:
1885 case Primitive::kPrimShort:
1886 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001887 case Primitive::kPrimInt: {
1888 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1889 if (right.IsConstant()) {
1890 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1891 codegen_->Compare32BitValue(left_reg, value);
1892 } else if (right.IsStackSlot()) {
1893 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1894 } else {
1895 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1896 }
1897 break;
1898 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001899 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001900 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1901 if (right.IsConstant()) {
1902 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001903 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001904 } else if (right.IsDoubleStackSlot()) {
1905 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001906 } else {
1907 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1908 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001909 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001910 }
1911 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001912 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1913 if (right.IsConstant()) {
1914 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1915 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1916 } else if (right.IsStackSlot()) {
1917 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1918 } else {
1919 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1920 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001921 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001922 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001923 break;
1924 }
1925 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001926 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1927 if (right.IsConstant()) {
1928 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1929 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1930 } else if (right.IsDoubleStackSlot()) {
1931 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1932 } else {
1933 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1934 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001935 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001936 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001937 break;
1938 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001939 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001940 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001941 }
Aart Bika19616e2016-02-01 18:57:58 -08001942
Calin Juravleddb7df22014-11-25 20:56:51 +00001943 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001944 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001945 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001946
Calin Juravle91debbc2014-11-26 19:01:09 +00001947 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001948 __ movl(out, Immediate(1));
1949 __ jmp(&done);
1950
1951 __ Bind(&less);
1952 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001953
1954 __ Bind(&done);
1955}
1956
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001957void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001958 LocationSummary* locations =
1959 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001960 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001961}
1962
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001963void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001964 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001965}
1966
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001967void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1968 LocationSummary* locations =
1969 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1970 locations->SetOut(Location::ConstantLocation(constant));
1971}
1972
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001973void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001974 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001975}
1976
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001980 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001981}
1982
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001983void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001984 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001985}
1986
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001987void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1990 locations->SetOut(Location::ConstantLocation(constant));
1991}
1992
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001993void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001994 // Will be generated at use site.
1995}
1996
1997void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1998 LocationSummary* locations =
1999 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2000 locations->SetOut(Location::ConstantLocation(constant));
2001}
2002
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002003void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2004 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002005 // Will be generated at use site.
2006}
2007
Calin Juravle27df7582015-04-17 19:12:31 +01002008void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2009 memory_barrier->SetLocations(nullptr);
2010}
2011
2012void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002013 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002014}
2015
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2017 ret->SetLocations(nullptr);
2018}
2019
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002020void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002021 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002022}
2023
2024void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002025 LocationSummary* locations =
2026 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002027 switch (ret->InputAt(0)->GetType()) {
2028 case Primitive::kPrimBoolean:
2029 case Primitive::kPrimByte:
2030 case Primitive::kPrimChar:
2031 case Primitive::kPrimShort:
2032 case Primitive::kPrimInt:
2033 case Primitive::kPrimNot:
2034 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002035 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036 break;
2037
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002038 case Primitive::kPrimFloat:
2039 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002040 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002041 break;
2042
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002043 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002044 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002045 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002046}
2047
2048void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2049 if (kIsDebugBuild) {
2050 switch (ret->InputAt(0)->GetType()) {
2051 case Primitive::kPrimBoolean:
2052 case Primitive::kPrimByte:
2053 case Primitive::kPrimChar:
2054 case Primitive::kPrimShort:
2055 case Primitive::kPrimInt:
2056 case Primitive::kPrimNot:
2057 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002059 break;
2060
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002061 case Primitive::kPrimFloat:
2062 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002064 XMM0);
2065 break;
2066
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002067 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002068 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002069 }
2070 }
2071 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002072}
2073
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002074Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2075 switch (type) {
2076 case Primitive::kPrimBoolean:
2077 case Primitive::kPrimByte:
2078 case Primitive::kPrimChar:
2079 case Primitive::kPrimShort:
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimNot:
2082 case Primitive::kPrimLong:
2083 return Location::RegisterLocation(RAX);
2084
2085 case Primitive::kPrimVoid:
2086 return Location::NoLocation();
2087
2088 case Primitive::kPrimDouble:
2089 case Primitive::kPrimFloat:
2090 return Location::FpuRegisterLocation(XMM0);
2091 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002092
2093 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002094}
2095
2096Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2097 return Location::RegisterLocation(kMethodRegisterArgument);
2098}
2099
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002100Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002101 switch (type) {
2102 case Primitive::kPrimBoolean:
2103 case Primitive::kPrimByte:
2104 case Primitive::kPrimChar:
2105 case Primitive::kPrimShort:
2106 case Primitive::kPrimInt:
2107 case Primitive::kPrimNot: {
2108 uint32_t index = gp_index_++;
2109 stack_index_++;
2110 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002112 } else {
2113 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2114 }
2115 }
2116
2117 case Primitive::kPrimLong: {
2118 uint32_t index = gp_index_;
2119 stack_index_ += 2;
2120 if (index < calling_convention.GetNumberOfRegisters()) {
2121 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002122 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002123 } else {
2124 gp_index_ += 2;
2125 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2126 }
2127 }
2128
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002129 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002130 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 stack_index_++;
2132 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002133 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002134 } else {
2135 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2136 }
2137 }
2138
2139 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002140 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002141 stack_index_ += 2;
2142 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002144 } else {
2145 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2146 }
2147 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002148
2149 case Primitive::kPrimVoid:
2150 LOG(FATAL) << "Unexpected parameter type " << type;
2151 break;
2152 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002153 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002154}
2155
Calin Juravle175dc732015-08-25 15:42:32 +01002156void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2157 // The trampoline uses the same calling convention as dex calling conventions,
2158 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2159 // the method_idx.
2160 HandleInvoke(invoke);
2161}
2162
2163void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2164 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2165}
2166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002167void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002168 // Explicit clinit checks triggered by static invokes must have been pruned by
2169 // art::PrepareForRegisterAllocation.
2170 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002171
Mark Mendellfb8d2792015-03-31 22:16:59 -04002172 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002173 if (intrinsic.TryDispatch(invoke)) {
2174 return;
2175 }
2176
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002177 HandleInvoke(invoke);
2178}
2179
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002180static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2181 if (invoke->GetLocations()->Intrinsified()) {
2182 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2183 intrinsic.Dispatch(invoke);
2184 return true;
2185 }
2186 return false;
2187}
2188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002189void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002190 // Explicit clinit checks triggered by static invokes must have been pruned by
2191 // art::PrepareForRegisterAllocation.
2192 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002193
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2195 return;
2196 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002197
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002198 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002199 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002200 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002201 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002202}
2203
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002205 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002206 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002207}
2208
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002209void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002210 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002211 if (intrinsic.TryDispatch(invoke)) {
2212 return;
2213 }
2214
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002215 HandleInvoke(invoke);
2216}
2217
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002218void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002219 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2220 return;
2221 }
2222
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002223 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002224 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002225 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002226}
2227
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002228void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2229 HandleInvoke(invoke);
2230 // Add the hidden argument.
2231 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2232}
2233
2234void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2235 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002236 LocationSummary* locations = invoke->GetLocations();
2237 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2238 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239 Location receiver = locations->InAt(0);
2240 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2241
Roland Levillain0d5a2812015-11-13 10:07:31 +00002242 // Set the hidden argument. This is safe to do this here, as RAX
2243 // won't be modified thereafter, before the `call` instruction.
2244 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002245 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002247 if (receiver.IsStackSlot()) {
2248 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002249 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002250 __ movl(temp, Address(temp, class_offset));
2251 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002252 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002253 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002255 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002256 // Instead of simply (possibly) unpoisoning `temp` here, we should
2257 // emit a read barrier for the previous class reference load.
2258 // However this is not required in practice, as this is an
2259 // intermediate/temporary reference and because the current
2260 // concurrent copying collector keeps the from-space memory
2261 // intact/accessible until the end of the marking phase (the
2262 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002263 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002264 // temp = temp->GetAddressOfIMT()
2265 __ movq(temp,
2266 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2267 // temp = temp->GetImtEntryAt(method_offset);
2268 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002269 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002271 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002272 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002273 __ call(Address(
2274 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002275
2276 DCHECK(!codegen_->IsLeafMethod());
2277 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2278}
2279
Roland Levillain88cb1752014-10-20 16:36:47 +01002280void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2281 LocationSummary* locations =
2282 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2283 switch (neg->GetResultType()) {
2284 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002285 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002286 locations->SetInAt(0, Location::RequiresRegister());
2287 locations->SetOut(Location::SameAsFirstInput());
2288 break;
2289
Roland Levillain88cb1752014-10-20 16:36:47 +01002290 case Primitive::kPrimFloat:
2291 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002292 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002293 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002294 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002295 break;
2296
2297 default:
2298 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2299 }
2300}
2301
2302void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2303 LocationSummary* locations = neg->GetLocations();
2304 Location out = locations->Out();
2305 Location in = locations->InAt(0);
2306 switch (neg->GetResultType()) {
2307 case Primitive::kPrimInt:
2308 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002309 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002310 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002311 break;
2312
2313 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002314 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002315 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002316 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002317 break;
2318
Roland Levillain5368c212014-11-27 15:03:41 +00002319 case Primitive::kPrimFloat: {
2320 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002321 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002322 // Implement float negation with an exclusive or with value
2323 // 0x80000000 (mask for bit 31, representing the sign of a
2324 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002325 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002327 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002328 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002329
Roland Levillain5368c212014-11-27 15:03:41 +00002330 case Primitive::kPrimDouble: {
2331 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002332 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002333 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002334 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002335 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002336 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002338 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002339 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002340
2341 default:
2342 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2343 }
2344}
2345
Roland Levillaindff1f282014-11-05 14:15:05 +00002346void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2347 LocationSummary* locations =
2348 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2349 Primitive::Type result_type = conversion->GetResultType();
2350 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002351 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002352
David Brazdilb2bd1c52015-03-25 11:17:37 +00002353 // The Java language does not allow treating boolean as an integral type but
2354 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002357 case Primitive::kPrimByte:
2358 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002359 case Primitive::kPrimLong:
2360 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002361 case Primitive::kPrimBoolean:
2362 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002363 case Primitive::kPrimShort:
2364 case Primitive::kPrimInt:
2365 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002366 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002367 locations->SetInAt(0, Location::Any());
2368 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2369 break;
2370
2371 default:
2372 LOG(FATAL) << "Unexpected type conversion from " << input_type
2373 << " to " << result_type;
2374 }
2375 break;
2376
Roland Levillain01a8d712014-11-14 16:27:39 +00002377 case Primitive::kPrimShort:
2378 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002379 case Primitive::kPrimLong:
2380 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002381 case Primitive::kPrimBoolean:
2382 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002383 case Primitive::kPrimByte:
2384 case Primitive::kPrimInt:
2385 case Primitive::kPrimChar:
2386 // Processing a Dex `int-to-short' instruction.
2387 locations->SetInAt(0, Location::Any());
2388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2389 break;
2390
2391 default:
2392 LOG(FATAL) << "Unexpected type conversion from " << input_type
2393 << " to " << result_type;
2394 }
2395 break;
2396
Roland Levillain946e1432014-11-11 17:35:19 +00002397 case Primitive::kPrimInt:
2398 switch (input_type) {
2399 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002400 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002401 locations->SetInAt(0, Location::Any());
2402 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2403 break;
2404
2405 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002406 // Processing a Dex `float-to-int' instruction.
2407 locations->SetInAt(0, Location::RequiresFpuRegister());
2408 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002409 break;
2410
Roland Levillain946e1432014-11-11 17:35:19 +00002411 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002412 // Processing a Dex `double-to-int' instruction.
2413 locations->SetInAt(0, Location::RequiresFpuRegister());
2414 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002415 break;
2416
2417 default:
2418 LOG(FATAL) << "Unexpected type conversion from " << input_type
2419 << " to " << result_type;
2420 }
2421 break;
2422
Roland Levillaindff1f282014-11-05 14:15:05 +00002423 case Primitive::kPrimLong:
2424 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002425 case Primitive::kPrimBoolean:
2426 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002427 case Primitive::kPrimByte:
2428 case Primitive::kPrimShort:
2429 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002430 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002431 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002432 // TODO: We would benefit from a (to-be-implemented)
2433 // Location::RegisterOrStackSlot requirement for this input.
2434 locations->SetInAt(0, Location::RequiresRegister());
2435 locations->SetOut(Location::RequiresRegister());
2436 break;
2437
2438 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002439 // Processing a Dex `float-to-long' instruction.
2440 locations->SetInAt(0, Location::RequiresFpuRegister());
2441 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002442 break;
2443
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002445 // Processing a Dex `double-to-long' instruction.
2446 locations->SetInAt(0, Location::RequiresFpuRegister());
2447 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002448 break;
2449
2450 default:
2451 LOG(FATAL) << "Unexpected type conversion from " << input_type
2452 << " to " << result_type;
2453 }
2454 break;
2455
Roland Levillain981e4542014-11-14 11:47:14 +00002456 case Primitive::kPrimChar:
2457 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002458 case Primitive::kPrimLong:
2459 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002462 case Primitive::kPrimByte:
2463 case Primitive::kPrimShort:
2464 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002465 // Processing a Dex `int-to-char' instruction.
2466 locations->SetInAt(0, Location::Any());
2467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillaindff1f282014-11-05 14:15:05 +00002476 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002477 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002478 case Primitive::kPrimBoolean:
2479 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002480 case Primitive::kPrimByte:
2481 case Primitive::kPrimShort:
2482 case Primitive::kPrimInt:
2483 case Primitive::kPrimChar:
2484 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002485 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002486 locations->SetOut(Location::RequiresFpuRegister());
2487 break;
2488
2489 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002490 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002491 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002492 locations->SetOut(Location::RequiresFpuRegister());
2493 break;
2494
Roland Levillaincff13742014-11-17 14:32:17 +00002495 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002496 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002497 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002498 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002499 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::kPrimDouble:
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-double' 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 Levillain647b9ed2014-11-27 12:06:00 +00002521 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002522 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002523 locations->SetOut(Location::RequiresFpuRegister());
2524 break;
2525
Roland Levillaincff13742014-11-17 14:32:17 +00002526 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002527 // Processing a Dex `float-to-double' 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 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002536 break;
2537
2538 default:
2539 LOG(FATAL) << "Unexpected type conversion from " << input_type
2540 << " to " << result_type;
2541 }
2542}
2543
2544void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2545 LocationSummary* locations = conversion->GetLocations();
2546 Location out = locations->Out();
2547 Location in = locations->InAt(0);
2548 Primitive::Type result_type = conversion->GetResultType();
2549 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002550 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002551 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002552 case Primitive::kPrimByte:
2553 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002554 case Primitive::kPrimLong:
2555 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002556 case Primitive::kPrimBoolean:
2557 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002558 case Primitive::kPrimShort:
2559 case Primitive::kPrimInt:
2560 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002561 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002562 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002563 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002564 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002565 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002566 Address(CpuRegister(RSP), in.GetStackIndex()));
2567 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002568 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002569 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002570 }
2571 break;
2572
2573 default:
2574 LOG(FATAL) << "Unexpected type conversion from " << input_type
2575 << " to " << result_type;
2576 }
2577 break;
2578
Roland Levillain01a8d712014-11-14 16:27:39 +00002579 case Primitive::kPrimShort:
2580 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002581 case Primitive::kPrimLong:
2582 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002583 case Primitive::kPrimBoolean:
2584 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002585 case Primitive::kPrimByte:
2586 case Primitive::kPrimInt:
2587 case Primitive::kPrimChar:
2588 // Processing a Dex `int-to-short' instruction.
2589 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002591 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002593 Address(CpuRegister(RSP), in.GetStackIndex()));
2594 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002596 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002597 }
2598 break;
2599
2600 default:
2601 LOG(FATAL) << "Unexpected type conversion from " << input_type
2602 << " to " << result_type;
2603 }
2604 break;
2605
Roland Levillain946e1432014-11-11 17:35:19 +00002606 case Primitive::kPrimInt:
2607 switch (input_type) {
2608 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002609 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002610 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002612 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002613 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002614 Address(CpuRegister(RSP), in.GetStackIndex()));
2615 } else {
2616 DCHECK(in.IsConstant());
2617 DCHECK(in.GetConstant()->IsLongConstant());
2618 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002619 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002620 }
2621 break;
2622
Roland Levillain3f8f9362014-12-02 17:45:01 +00002623 case Primitive::kPrimFloat: {
2624 // Processing a Dex `float-to-int' instruction.
2625 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2626 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002627 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002628
2629 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002630 // if input >= (float)INT_MAX goto done
2631 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002632 __ j(kAboveEqual, &done);
2633 // if input == NaN goto nan
2634 __ j(kUnordered, &nan);
2635 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002636 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002637 __ jmp(&done);
2638 __ Bind(&nan);
2639 // output = 0
2640 __ xorl(output, output);
2641 __ Bind(&done);
2642 break;
2643 }
2644
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002645 case Primitive::kPrimDouble: {
2646 // Processing a Dex `double-to-int' instruction.
2647 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2648 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002649 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002650
2651 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002652 // if input >= (double)INT_MAX goto done
2653 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002654 __ j(kAboveEqual, &done);
2655 // if input == NaN goto nan
2656 __ j(kUnordered, &nan);
2657 // output = double-to-int-truncate(input)
2658 __ cvttsd2si(output, input);
2659 __ jmp(&done);
2660 __ Bind(&nan);
2661 // output = 0
2662 __ xorl(output, output);
2663 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002664 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002665 }
Roland Levillain946e1432014-11-11 17:35:19 +00002666
2667 default:
2668 LOG(FATAL) << "Unexpected type conversion from " << input_type
2669 << " to " << result_type;
2670 }
2671 break;
2672
Roland Levillaindff1f282014-11-05 14:15:05 +00002673 case Primitive::kPrimLong:
2674 switch (input_type) {
2675 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002676 case Primitive::kPrimBoolean:
2677 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002678 case Primitive::kPrimByte:
2679 case Primitive::kPrimShort:
2680 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002681 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002682 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002683 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002685 break;
2686
Roland Levillain624279f2014-12-04 11:54:28 +00002687 case Primitive::kPrimFloat: {
2688 // Processing a Dex `float-to-long' instruction.
2689 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2690 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002691 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002692
Mark Mendell92e83bf2015-05-07 11:25:03 -04002693 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002694 // if input >= (float)LONG_MAX goto done
2695 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002696 __ j(kAboveEqual, &done);
2697 // if input == NaN goto nan
2698 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002699 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002700 __ cvttss2si(output, input, true);
2701 __ jmp(&done);
2702 __ Bind(&nan);
2703 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002704 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002705 __ Bind(&done);
2706 break;
2707 }
2708
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002709 case Primitive::kPrimDouble: {
2710 // Processing a Dex `double-to-long' instruction.
2711 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2712 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002713 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002714
Mark Mendell92e83bf2015-05-07 11:25:03 -04002715 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002716 // if input >= (double)LONG_MAX goto done
2717 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002718 __ j(kAboveEqual, &done);
2719 // if input == NaN goto nan
2720 __ j(kUnordered, &nan);
2721 // output = double-to-long-truncate(input)
2722 __ cvttsd2si(output, input, true);
2723 __ jmp(&done);
2724 __ Bind(&nan);
2725 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002726 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002727 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002728 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002729 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002730
2731 default:
2732 LOG(FATAL) << "Unexpected type conversion from " << input_type
2733 << " to " << result_type;
2734 }
2735 break;
2736
Roland Levillain981e4542014-11-14 11:47:14 +00002737 case Primitive::kPrimChar:
2738 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002739 case Primitive::kPrimLong:
2740 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002741 case Primitive::kPrimBoolean:
2742 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002743 case Primitive::kPrimByte:
2744 case Primitive::kPrimShort:
2745 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002746 // Processing a Dex `int-to-char' instruction.
2747 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002749 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002750 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002751 Address(CpuRegister(RSP), in.GetStackIndex()));
2752 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002753 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002754 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002755 }
2756 break;
2757
2758 default:
2759 LOG(FATAL) << "Unexpected type conversion from " << input_type
2760 << " to " << result_type;
2761 }
2762 break;
2763
Roland Levillaindff1f282014-11-05 14:15:05 +00002764 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002765 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002766 case Primitive::kPrimBoolean:
2767 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002768 case Primitive::kPrimByte:
2769 case Primitive::kPrimShort:
2770 case Primitive::kPrimInt:
2771 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002772 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002773 if (in.IsRegister()) {
2774 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2775 } else if (in.IsConstant()) {
2776 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2777 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002778 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002779 } else {
2780 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2781 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2782 }
Roland Levillaincff13742014-11-17 14:32:17 +00002783 break;
2784
2785 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002786 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002787 if (in.IsRegister()) {
2788 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2789 } else if (in.IsConstant()) {
2790 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2791 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002792 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002793 } else {
2794 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2795 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2796 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002797 break;
2798
Roland Levillaincff13742014-11-17 14:32:17 +00002799 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002800 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002801 if (in.IsFpuRegister()) {
2802 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2803 } else if (in.IsConstant()) {
2804 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2805 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002806 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002807 } else {
2808 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2809 Address(CpuRegister(RSP), in.GetStackIndex()));
2810 }
Roland Levillaincff13742014-11-17 14:32:17 +00002811 break;
2812
2813 default:
2814 LOG(FATAL) << "Unexpected type conversion from " << input_type
2815 << " to " << result_type;
2816 };
2817 break;
2818
Roland Levillaindff1f282014-11-05 14:15:05 +00002819 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002820 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002821 case Primitive::kPrimBoolean:
2822 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002823 case Primitive::kPrimByte:
2824 case Primitive::kPrimShort:
2825 case Primitive::kPrimInt:
2826 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002827 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002828 if (in.IsRegister()) {
2829 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2830 } else if (in.IsConstant()) {
2831 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2832 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002833 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002834 } else {
2835 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2836 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2837 }
Roland Levillaincff13742014-11-17 14:32:17 +00002838 break;
2839
2840 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002841 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002842 if (in.IsRegister()) {
2843 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2844 } else if (in.IsConstant()) {
2845 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2846 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002847 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002848 } else {
2849 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2850 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2851 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002852 break;
2853
Roland Levillaincff13742014-11-17 14:32:17 +00002854 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002855 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002856 if (in.IsFpuRegister()) {
2857 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2858 } else if (in.IsConstant()) {
2859 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2860 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002861 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002862 } else {
2863 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2864 Address(CpuRegister(RSP), in.GetStackIndex()));
2865 }
Roland Levillaincff13742014-11-17 14:32:17 +00002866 break;
2867
2868 default:
2869 LOG(FATAL) << "Unexpected type conversion from " << input_type
2870 << " to " << result_type;
2871 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002872 break;
2873
2874 default:
2875 LOG(FATAL) << "Unexpected type conversion from " << input_type
2876 << " to " << result_type;
2877 }
2878}
2879
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002880void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002881 LocationSummary* locations =
2882 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002883 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002884 case Primitive::kPrimInt: {
2885 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002886 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002888 break;
2889 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002890
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002891 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002892 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002893 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002894 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002896 break;
2897 }
2898
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002899 case Primitive::kPrimDouble:
2900 case Primitive::kPrimFloat: {
2901 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002902 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002903 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002905 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002906
2907 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002908 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002909 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002910}
2911
2912void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2913 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002914 Location first = locations->InAt(0);
2915 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002916 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002917
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002918 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002919 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002920 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002921 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2922 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002923 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2924 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002925 } else {
2926 __ leal(out.AsRegister<CpuRegister>(), Address(
2927 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2928 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002930 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2931 __ addl(out.AsRegister<CpuRegister>(),
2932 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2933 } else {
2934 __ leal(out.AsRegister<CpuRegister>(), Address(
2935 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2936 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002937 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002938 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002940 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002941 break;
2942 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002943
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002944 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002945 if (second.IsRegister()) {
2946 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2947 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002948 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2949 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002950 } else {
2951 __ leaq(out.AsRegister<CpuRegister>(), Address(
2952 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2953 }
2954 } else {
2955 DCHECK(second.IsConstant());
2956 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2957 int32_t int32_value = Low32Bits(value);
2958 DCHECK_EQ(int32_value, value);
2959 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2960 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2961 } else {
2962 __ leaq(out.AsRegister<CpuRegister>(), Address(
2963 first.AsRegister<CpuRegister>(), int32_value));
2964 }
2965 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002966 break;
2967 }
2968
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002969 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002970 if (second.IsFpuRegister()) {
2971 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2972 } else if (second.IsConstant()) {
2973 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002974 codegen_->LiteralFloatAddress(
2975 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002976 } else {
2977 DCHECK(second.IsStackSlot());
2978 __ addss(first.AsFpuRegister<XmmRegister>(),
2979 Address(CpuRegister(RSP), second.GetStackIndex()));
2980 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002981 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 }
2983
2984 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002985 if (second.IsFpuRegister()) {
2986 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2987 } else if (second.IsConstant()) {
2988 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002989 codegen_->LiteralDoubleAddress(
2990 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002991 } else {
2992 DCHECK(second.IsDoubleStackSlot());
2993 __ addsd(first.AsFpuRegister<XmmRegister>(),
2994 Address(CpuRegister(RSP), second.GetStackIndex()));
2995 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002996 break;
2997 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002998
2999 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003000 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003001 }
3002}
3003
3004void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003005 LocationSummary* locations =
3006 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003007 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003008 case Primitive::kPrimInt: {
3009 locations->SetInAt(0, Location::RequiresRegister());
3010 locations->SetInAt(1, Location::Any());
3011 locations->SetOut(Location::SameAsFirstInput());
3012 break;
3013 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003014 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003015 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003016 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003017 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003018 break;
3019 }
Calin Juravle11351682014-10-23 15:38:15 +01003020 case Primitive::kPrimFloat:
3021 case Primitive::kPrimDouble: {
3022 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003023 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003024 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003025 break;
Calin Juravle11351682014-10-23 15:38:15 +01003026 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003027 default:
Calin Juravle11351682014-10-23 15:38:15 +01003028 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003029 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003030}
3031
3032void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3033 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003034 Location first = locations->InAt(0);
3035 Location second = locations->InAt(1);
3036 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003037 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003038 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003039 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003040 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003041 } else if (second.IsConstant()) {
3042 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003043 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003044 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003046 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003047 break;
3048 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003049 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003050 if (second.IsConstant()) {
3051 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3052 DCHECK(IsInt<32>(value));
3053 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3054 } else {
3055 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3056 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003057 break;
3058 }
3059
Calin Juravle11351682014-10-23 15:38:15 +01003060 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003061 if (second.IsFpuRegister()) {
3062 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3063 } else if (second.IsConstant()) {
3064 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003065 codegen_->LiteralFloatAddress(
3066 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003067 } else {
3068 DCHECK(second.IsStackSlot());
3069 __ subss(first.AsFpuRegister<XmmRegister>(),
3070 Address(CpuRegister(RSP), second.GetStackIndex()));
3071 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003072 break;
Calin Juravle11351682014-10-23 15:38:15 +01003073 }
3074
3075 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003076 if (second.IsFpuRegister()) {
3077 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3078 } else if (second.IsConstant()) {
3079 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003080 codegen_->LiteralDoubleAddress(
3081 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003082 } else {
3083 DCHECK(second.IsDoubleStackSlot());
3084 __ subsd(first.AsFpuRegister<XmmRegister>(),
3085 Address(CpuRegister(RSP), second.GetStackIndex()));
3086 }
Calin Juravle11351682014-10-23 15:38:15 +01003087 break;
3088 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003089
3090 default:
Calin Juravle11351682014-10-23 15:38:15 +01003091 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003092 }
3093}
3094
Calin Juravle34bacdf2014-10-07 20:23:36 +01003095void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3096 LocationSummary* locations =
3097 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3098 switch (mul->GetResultType()) {
3099 case Primitive::kPrimInt: {
3100 locations->SetInAt(0, Location::RequiresRegister());
3101 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003102 if (mul->InputAt(1)->IsIntConstant()) {
3103 // Can use 3 operand multiply.
3104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3105 } else {
3106 locations->SetOut(Location::SameAsFirstInput());
3107 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003108 break;
3109 }
3110 case Primitive::kPrimLong: {
3111 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003112 locations->SetInAt(1, Location::Any());
3113 if (mul->InputAt(1)->IsLongConstant() &&
3114 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003115 // Can use 3 operand multiply.
3116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3117 } else {
3118 locations->SetOut(Location::SameAsFirstInput());
3119 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003120 break;
3121 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003122 case Primitive::kPrimFloat:
3123 case Primitive::kPrimDouble: {
3124 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003125 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003126 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003127 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003128 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129
3130 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003131 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003132 }
3133}
3134
3135void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3136 LocationSummary* locations = mul->GetLocations();
3137 Location first = locations->InAt(0);
3138 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003139 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003140 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003141 case Primitive::kPrimInt:
3142 // The constant may have ended up in a register, so test explicitly to avoid
3143 // problems where the output may not be the same as the first operand.
3144 if (mul->InputAt(1)->IsIntConstant()) {
3145 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3146 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3147 } else if (second.IsRegister()) {
3148 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003149 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003151 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003152 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003153 __ imull(first.AsRegister<CpuRegister>(),
3154 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003155 }
3156 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003157 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003158 // The constant may have ended up in a register, so test explicitly to avoid
3159 // problems where the output may not be the same as the first operand.
3160 if (mul->InputAt(1)->IsLongConstant()) {
3161 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3162 if (IsInt<32>(value)) {
3163 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3164 Immediate(static_cast<int32_t>(value)));
3165 } else {
3166 // Have to use the constant area.
3167 DCHECK(first.Equals(out));
3168 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3169 }
3170 } else if (second.IsRegister()) {
3171 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003172 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003173 } else {
3174 DCHECK(second.IsDoubleStackSlot());
3175 DCHECK(first.Equals(out));
3176 __ imulq(first.AsRegister<CpuRegister>(),
3177 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003178 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003179 break;
3180 }
3181
Calin Juravleb5bfa962014-10-21 18:02:24 +01003182 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003183 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003184 if (second.IsFpuRegister()) {
3185 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3186 } else if (second.IsConstant()) {
3187 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003188 codegen_->LiteralFloatAddress(
3189 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003190 } else {
3191 DCHECK(second.IsStackSlot());
3192 __ mulss(first.AsFpuRegister<XmmRegister>(),
3193 Address(CpuRegister(RSP), second.GetStackIndex()));
3194 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003196 }
3197
3198 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003199 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003200 if (second.IsFpuRegister()) {
3201 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3202 } else if (second.IsConstant()) {
3203 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003204 codegen_->LiteralDoubleAddress(
3205 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003206 } else {
3207 DCHECK(second.IsDoubleStackSlot());
3208 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3209 Address(CpuRegister(RSP), second.GetStackIndex()));
3210 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003211 break;
3212 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003213
3214 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003215 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003216 }
3217}
3218
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003219void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3220 uint32_t stack_adjustment, bool is_float) {
3221 if (source.IsStackSlot()) {
3222 DCHECK(is_float);
3223 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3224 } else if (source.IsDoubleStackSlot()) {
3225 DCHECK(!is_float);
3226 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3227 } else {
3228 // Write the value to the temporary location on the stack and load to FP stack.
3229 if (is_float) {
3230 Location stack_temp = Location::StackSlot(temp_offset);
3231 codegen_->Move(stack_temp, source);
3232 __ flds(Address(CpuRegister(RSP), temp_offset));
3233 } else {
3234 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3235 codegen_->Move(stack_temp, source);
3236 __ fldl(Address(CpuRegister(RSP), temp_offset));
3237 }
3238 }
3239}
3240
3241void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3242 Primitive::Type type = rem->GetResultType();
3243 bool is_float = type == Primitive::kPrimFloat;
3244 size_t elem_size = Primitive::ComponentSize(type);
3245 LocationSummary* locations = rem->GetLocations();
3246 Location first = locations->InAt(0);
3247 Location second = locations->InAt(1);
3248 Location out = locations->Out();
3249
3250 // Create stack space for 2 elements.
3251 // TODO: enhance register allocator to ask for stack temporaries.
3252 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3253
3254 // Load the values to the FP stack in reverse order, using temporaries if needed.
3255 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3256 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3257
3258 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003259 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003260 __ Bind(&retry);
3261 __ fprem();
3262
3263 // Move FP status to AX.
3264 __ fstsw();
3265
3266 // And see if the argument reduction is complete. This is signaled by the
3267 // C2 FPU flag bit set to 0.
3268 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3269 __ j(kNotEqual, &retry);
3270
3271 // We have settled on the final value. Retrieve it into an XMM register.
3272 // Store FP top of stack to real stack.
3273 if (is_float) {
3274 __ fsts(Address(CpuRegister(RSP), 0));
3275 } else {
3276 __ fstl(Address(CpuRegister(RSP), 0));
3277 }
3278
3279 // Pop the 2 items from the FP stack.
3280 __ fucompp();
3281
3282 // Load the value from the stack into an XMM register.
3283 DCHECK(out.IsFpuRegister()) << out;
3284 if (is_float) {
3285 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3286 } else {
3287 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3288 }
3289
3290 // And remove the temporary stack space we allocated.
3291 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3292}
3293
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003294void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3295 DCHECK(instruction->IsDiv() || instruction->IsRem());
3296
3297 LocationSummary* locations = instruction->GetLocations();
3298 Location second = locations->InAt(1);
3299 DCHECK(second.IsConstant());
3300
3301 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3302 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003303 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003304
3305 DCHECK(imm == 1 || imm == -1);
3306
3307 switch (instruction->GetResultType()) {
3308 case Primitive::kPrimInt: {
3309 if (instruction->IsRem()) {
3310 __ xorl(output_register, output_register);
3311 } else {
3312 __ movl(output_register, input_register);
3313 if (imm == -1) {
3314 __ negl(output_register);
3315 }
3316 }
3317 break;
3318 }
3319
3320 case Primitive::kPrimLong: {
3321 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003322 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003323 } else {
3324 __ movq(output_register, input_register);
3325 if (imm == -1) {
3326 __ negq(output_register);
3327 }
3328 }
3329 break;
3330 }
3331
3332 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003333 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334 }
3335}
3336
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003337void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003338 LocationSummary* locations = instruction->GetLocations();
3339 Location second = locations->InAt(1);
3340
3341 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3342 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3343
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003344 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003345 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3346 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003347
3348 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3349
3350 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003351 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352 __ testl(numerator, numerator);
3353 __ cmov(kGreaterEqual, tmp, numerator);
3354 int shift = CTZ(imm);
3355 __ sarl(tmp, Immediate(shift));
3356
3357 if (imm < 0) {
3358 __ negl(tmp);
3359 }
3360
3361 __ movl(output_register, tmp);
3362 } else {
3363 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3364 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3365
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003366 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003367 __ addq(rdx, numerator);
3368 __ testq(numerator, numerator);
3369 __ cmov(kGreaterEqual, rdx, numerator);
3370 int shift = CTZ(imm);
3371 __ sarq(rdx, Immediate(shift));
3372
3373 if (imm < 0) {
3374 __ negq(rdx);
3375 }
3376
3377 __ movq(output_register, rdx);
3378 }
3379}
3380
3381void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3382 DCHECK(instruction->IsDiv() || instruction->IsRem());
3383
3384 LocationSummary* locations = instruction->GetLocations();
3385 Location second = locations->InAt(1);
3386
3387 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3388 : locations->GetTemp(0).AsRegister<CpuRegister>();
3389 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3390 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3391 : locations->Out().AsRegister<CpuRegister>();
3392 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3393
3394 DCHECK_EQ(RAX, eax.AsRegister());
3395 DCHECK_EQ(RDX, edx.AsRegister());
3396 if (instruction->IsDiv()) {
3397 DCHECK_EQ(RAX, out.AsRegister());
3398 } else {
3399 DCHECK_EQ(RDX, out.AsRegister());
3400 }
3401
3402 int64_t magic;
3403 int shift;
3404
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003405 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003406 if (instruction->GetResultType() == Primitive::kPrimInt) {
3407 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3408
3409 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3410
3411 __ movl(numerator, eax);
3412
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003413 __ movl(eax, Immediate(magic));
3414 __ imull(numerator);
3415
3416 if (imm > 0 && magic < 0) {
3417 __ addl(edx, numerator);
3418 } else if (imm < 0 && magic > 0) {
3419 __ subl(edx, numerator);
3420 }
3421
3422 if (shift != 0) {
3423 __ sarl(edx, Immediate(shift));
3424 }
3425
3426 __ movl(eax, edx);
3427 __ shrl(edx, Immediate(31));
3428 __ addl(edx, eax);
3429
3430 if (instruction->IsRem()) {
3431 __ movl(eax, numerator);
3432 __ imull(edx, Immediate(imm));
3433 __ subl(eax, edx);
3434 __ movl(edx, eax);
3435 } else {
3436 __ movl(eax, edx);
3437 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003438 } else {
3439 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3440
3441 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3442
3443 CpuRegister rax = eax;
3444 CpuRegister rdx = edx;
3445
3446 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3447
3448 // Save the numerator.
3449 __ movq(numerator, rax);
3450
3451 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003452 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453
3454 // RDX:RAX = magic * numerator
3455 __ imulq(numerator);
3456
3457 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003458 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003459 __ addq(rdx, numerator);
3460 } else if (imm < 0 && magic > 0) {
3461 // RDX -= numerator
3462 __ subq(rdx, numerator);
3463 }
3464
3465 // Shift if needed.
3466 if (shift != 0) {
3467 __ sarq(rdx, Immediate(shift));
3468 }
3469
3470 // RDX += 1 if RDX < 0
3471 __ movq(rax, rdx);
3472 __ shrq(rdx, Immediate(63));
3473 __ addq(rdx, rax);
3474
3475 if (instruction->IsRem()) {
3476 __ movq(rax, numerator);
3477
3478 if (IsInt<32>(imm)) {
3479 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3480 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003481 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482 }
3483
3484 __ subq(rax, rdx);
3485 __ movq(rdx, rax);
3486 } else {
3487 __ movq(rax, rdx);
3488 }
3489 }
3490}
3491
Calin Juravlebacfec32014-11-14 15:54:36 +00003492void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3493 DCHECK(instruction->IsDiv() || instruction->IsRem());
3494 Primitive::Type type = instruction->GetResultType();
3495 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3496
3497 bool is_div = instruction->IsDiv();
3498 LocationSummary* locations = instruction->GetLocations();
3499
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3501 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003502
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003505
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003507 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003508
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 if (imm == 0) {
3510 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3511 } else if (imm == 1 || imm == -1) {
3512 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003513 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003514 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003515 } else {
3516 DCHECK(imm <= -2 || imm >= 2);
3517 GenerateDivRemWithAnyConstant(instruction);
3518 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003519 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003520 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003522 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003524
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3526 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3527 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3528 // so it's safe to just use negl instead of more complex comparisons.
3529 if (type == Primitive::kPrimInt) {
3530 __ cmpl(second_reg, Immediate(-1));
3531 __ j(kEqual, slow_path->GetEntryLabel());
3532 // edx:eax <- sign-extended of eax
3533 __ cdq();
3534 // eax = quotient, edx = remainder
3535 __ idivl(second_reg);
3536 } else {
3537 __ cmpq(second_reg, Immediate(-1));
3538 __ j(kEqual, slow_path->GetEntryLabel());
3539 // rdx:rax <- sign-extended of rax
3540 __ cqo();
3541 // rax = quotient, rdx = remainder
3542 __ idivq(second_reg);
3543 }
3544 __ Bind(slow_path->GetExitLabel());
3545 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003546}
3547
Calin Juravle7c4954d2014-10-28 16:57:40 +00003548void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3549 LocationSummary* locations =
3550 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3551 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003552 case Primitive::kPrimInt:
3553 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003554 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003556 locations->SetOut(Location::SameAsFirstInput());
3557 // Intel uses edx:eax as the dividend.
3558 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003559 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3560 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3561 // output and request another temp.
3562 if (div->InputAt(1)->IsConstant()) {
3563 locations->AddTemp(Location::RequiresRegister());
3564 }
Calin Juravled0d48522014-11-04 16:40:20 +00003565 break;
3566 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003567
Calin Juravle7c4954d2014-10-28 16:57:40 +00003568 case Primitive::kPrimFloat:
3569 case Primitive::kPrimDouble: {
3570 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003571 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003572 locations->SetOut(Location::SameAsFirstInput());
3573 break;
3574 }
3575
3576 default:
3577 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3578 }
3579}
3580
3581void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3582 LocationSummary* locations = div->GetLocations();
3583 Location first = locations->InAt(0);
3584 Location second = locations->InAt(1);
3585 DCHECK(first.Equals(locations->Out()));
3586
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003587 Primitive::Type type = div->GetResultType();
3588 switch (type) {
3589 case Primitive::kPrimInt:
3590 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003591 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003592 break;
3593 }
3594
Calin Juravle7c4954d2014-10-28 16:57:40 +00003595 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003596 if (second.IsFpuRegister()) {
3597 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3598 } else if (second.IsConstant()) {
3599 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003600 codegen_->LiteralFloatAddress(
3601 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003602 } else {
3603 DCHECK(second.IsStackSlot());
3604 __ divss(first.AsFpuRegister<XmmRegister>(),
3605 Address(CpuRegister(RSP), second.GetStackIndex()));
3606 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003607 break;
3608 }
3609
3610 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003611 if (second.IsFpuRegister()) {
3612 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3613 } else if (second.IsConstant()) {
3614 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003615 codegen_->LiteralDoubleAddress(
3616 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003617 } else {
3618 DCHECK(second.IsDoubleStackSlot());
3619 __ divsd(first.AsFpuRegister<XmmRegister>(),
3620 Address(CpuRegister(RSP), second.GetStackIndex()));
3621 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003622 break;
3623 }
3624
3625 default:
3626 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3627 }
3628}
3629
Calin Juravlebacfec32014-11-14 15:54:36 +00003630void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003631 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003632 LocationSummary* locations =
3633 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003634
3635 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003636 case Primitive::kPrimInt:
3637 case Primitive::kPrimLong: {
3638 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003639 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003640 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3641 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003642 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3643 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3644 // output and request another temp.
3645 if (rem->InputAt(1)->IsConstant()) {
3646 locations->AddTemp(Location::RequiresRegister());
3647 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 break;
3649 }
3650
3651 case Primitive::kPrimFloat:
3652 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003653 locations->SetInAt(0, Location::Any());
3654 locations->SetInAt(1, Location::Any());
3655 locations->SetOut(Location::RequiresFpuRegister());
3656 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 break;
3658 }
3659
3660 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003661 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003662 }
3663}
3664
3665void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3666 Primitive::Type type = rem->GetResultType();
3667 switch (type) {
3668 case Primitive::kPrimInt:
3669 case Primitive::kPrimLong: {
3670 GenerateDivRemIntegral(rem);
3671 break;
3672 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003673 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003674 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003675 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003676 break;
3677 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003678 default:
3679 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3680 }
3681}
3682
Calin Juravled0d48522014-11-04 16:40:20 +00003683void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003684 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3685 ? LocationSummary::kCallOnSlowPath
3686 : LocationSummary::kNoCall;
3687 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003688 locations->SetInAt(0, Location::Any());
3689 if (instruction->HasUses()) {
3690 locations->SetOut(Location::SameAsFirstInput());
3691 }
3692}
3693
3694void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003695 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003696 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3697 codegen_->AddSlowPath(slow_path);
3698
3699 LocationSummary* locations = instruction->GetLocations();
3700 Location value = locations->InAt(0);
3701
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003702 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003703 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003704 case Primitive::kPrimByte:
3705 case Primitive::kPrimChar:
3706 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003707 case Primitive::kPrimInt: {
3708 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003710 __ j(kEqual, slow_path->GetEntryLabel());
3711 } else if (value.IsStackSlot()) {
3712 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3713 __ j(kEqual, slow_path->GetEntryLabel());
3714 } else {
3715 DCHECK(value.IsConstant()) << value;
3716 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3717 __ jmp(slow_path->GetEntryLabel());
3718 }
3719 }
3720 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003721 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003722 case Primitive::kPrimLong: {
3723 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003725 __ j(kEqual, slow_path->GetEntryLabel());
3726 } else if (value.IsDoubleStackSlot()) {
3727 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3728 __ j(kEqual, slow_path->GetEntryLabel());
3729 } else {
3730 DCHECK(value.IsConstant()) << value;
3731 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3732 __ jmp(slow_path->GetEntryLabel());
3733 }
3734 }
3735 break;
3736 }
3737 default:
3738 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003739 }
Calin Juravled0d48522014-11-04 16:40:20 +00003740}
3741
Calin Juravle9aec02f2014-11-18 23:06:35 +00003742void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3743 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3744
3745 LocationSummary* locations =
3746 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3747
3748 switch (op->GetResultType()) {
3749 case Primitive::kPrimInt:
3750 case Primitive::kPrimLong: {
3751 locations->SetInAt(0, Location::RequiresRegister());
3752 // The shift count needs to be in CL.
3753 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3754 locations->SetOut(Location::SameAsFirstInput());
3755 break;
3756 }
3757 default:
3758 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3759 }
3760}
3761
3762void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3763 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3764
3765 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003767 Location second = locations->InAt(1);
3768
3769 switch (op->GetResultType()) {
3770 case Primitive::kPrimInt: {
3771 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773 if (op->IsShl()) {
3774 __ shll(first_reg, second_reg);
3775 } else if (op->IsShr()) {
3776 __ sarl(first_reg, second_reg);
3777 } else {
3778 __ shrl(first_reg, second_reg);
3779 }
3780 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003781 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003782 if (op->IsShl()) {
3783 __ shll(first_reg, imm);
3784 } else if (op->IsShr()) {
3785 __ sarl(first_reg, imm);
3786 } else {
3787 __ shrl(first_reg, imm);
3788 }
3789 }
3790 break;
3791 }
3792 case Primitive::kPrimLong: {
3793 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003794 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003795 if (op->IsShl()) {
3796 __ shlq(first_reg, second_reg);
3797 } else if (op->IsShr()) {
3798 __ sarq(first_reg, second_reg);
3799 } else {
3800 __ shrq(first_reg, second_reg);
3801 }
3802 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003803 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804 if (op->IsShl()) {
3805 __ shlq(first_reg, imm);
3806 } else if (op->IsShr()) {
3807 __ sarq(first_reg, imm);
3808 } else {
3809 __ shrq(first_reg, imm);
3810 }
3811 }
3812 break;
3813 }
3814 default:
3815 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003816 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003817 }
3818}
3819
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003820void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3821 LocationSummary* locations =
3822 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3823
3824 switch (ror->GetResultType()) {
3825 case Primitive::kPrimInt:
3826 case Primitive::kPrimLong: {
3827 locations->SetInAt(0, Location::RequiresRegister());
3828 // The shift count needs to be in CL (unless it is a constant).
3829 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3830 locations->SetOut(Location::SameAsFirstInput());
3831 break;
3832 }
3833 default:
3834 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3835 UNREACHABLE();
3836 }
3837}
3838
3839void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3840 LocationSummary* locations = ror->GetLocations();
3841 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3842 Location second = locations->InAt(1);
3843
3844 switch (ror->GetResultType()) {
3845 case Primitive::kPrimInt:
3846 if (second.IsRegister()) {
3847 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3848 __ rorl(first_reg, second_reg);
3849 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003850 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003851 __ rorl(first_reg, imm);
3852 }
3853 break;
3854 case Primitive::kPrimLong:
3855 if (second.IsRegister()) {
3856 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3857 __ rorq(first_reg, second_reg);
3858 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003859 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003860 __ rorq(first_reg, imm);
3861 }
3862 break;
3863 default:
3864 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3865 UNREACHABLE();
3866 }
3867}
3868
Calin Juravle9aec02f2014-11-18 23:06:35 +00003869void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3870 HandleShift(shl);
3871}
3872
3873void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3874 HandleShift(shl);
3875}
3876
3877void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3878 HandleShift(shr);
3879}
3880
3881void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3882 HandleShift(shr);
3883}
3884
3885void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3886 HandleShift(ushr);
3887}
3888
3889void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3890 HandleShift(ushr);
3891}
3892
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003893void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003894 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003896 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003897 if (instruction->IsStringAlloc()) {
3898 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3899 } else {
3900 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3901 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3902 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003903 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003904}
3905
3906void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003907 // Note: if heap poisoning is enabled, the entry point takes cares
3908 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003909 if (instruction->IsStringAlloc()) {
3910 // String is allocated through StringFactory. Call NewEmptyString entry point.
3911 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003912 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003913 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3914 __ call(Address(temp, code_offset.SizeValue()));
3915 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3916 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003917 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003918 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3919 DCHECK(!codegen_->IsLeafMethod());
3920 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003921}
3922
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003923void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3924 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003925 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003926 InvokeRuntimeCallingConvention calling_convention;
3927 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003928 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003929 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003930 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003931}
3932
3933void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3934 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003935 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3936 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003937 // Note: if heap poisoning is enabled, the entry point takes cares
3938 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003939 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003940 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003941
3942 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003943}
3944
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003945void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003946 LocationSummary* locations =
3947 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003948 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3949 if (location.IsStackSlot()) {
3950 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3951 } else if (location.IsDoubleStackSlot()) {
3952 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3953 }
3954 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003955}
3956
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003957void InstructionCodeGeneratorX86_64::VisitParameterValue(
3958 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003959 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003960}
3961
3962void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3963 LocationSummary* locations =
3964 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3965 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3966}
3967
3968void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3969 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3970 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003971}
3972
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003973void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3974 LocationSummary* locations =
3975 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3976 locations->SetInAt(0, Location::RequiresRegister());
3977 locations->SetOut(Location::RequiresRegister());
3978}
3979
3980void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3981 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003982 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003983 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003984 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003985 __ movq(locations->Out().AsRegister<CpuRegister>(),
3986 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003987 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003988 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003989 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003990 __ movq(locations->Out().AsRegister<CpuRegister>(),
3991 Address(locations->InAt(0).AsRegister<CpuRegister>(),
3992 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01003993 __ movq(locations->Out().AsRegister<CpuRegister>(),
3994 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003995 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003996}
3997
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003998void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003999 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004000 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004001 locations->SetInAt(0, Location::RequiresRegister());
4002 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004003}
4004
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004005void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4006 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004007 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4008 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004009 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004010 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004011 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004012 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004013 break;
4014
4015 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004016 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004017 break;
4018
4019 default:
4020 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4021 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004022}
4023
David Brazdil66d126e2015-04-03 16:02:44 +01004024void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4025 LocationSummary* locations =
4026 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4027 locations->SetInAt(0, Location::RequiresRegister());
4028 locations->SetOut(Location::SameAsFirstInput());
4029}
4030
4031void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004032 LocationSummary* locations = bool_not->GetLocations();
4033 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4034 locations->Out().AsRegister<CpuRegister>().AsRegister());
4035 Location out = locations->Out();
4036 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4037}
4038
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004039void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004040 LocationSummary* locations =
4041 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004042 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004043 locations->SetInAt(i, Location::Any());
4044 }
4045 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004046}
4047
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004048void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004049 LOG(FATAL) << "Unimplemented";
4050}
4051
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004052void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004053 /*
4054 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004055 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004056 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4057 */
4058 switch (kind) {
4059 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004060 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004061 break;
4062 }
4063 case MemBarrierKind::kAnyStore:
4064 case MemBarrierKind::kLoadAny:
4065 case MemBarrierKind::kStoreStore: {
4066 // nop
4067 break;
4068 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004069 case MemBarrierKind::kNTStoreStore:
4070 // Non-Temporal Store/Store needs an explicit fence.
4071 MemoryFence(/* non-temporal */ true);
4072 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004073 }
4074}
4075
4076void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4077 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4078
Roland Levillain0d5a2812015-11-13 10:07:31 +00004079 bool object_field_get_with_read_barrier =
4080 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004081 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004082 new (GetGraph()->GetArena()) LocationSummary(instruction,
4083 object_field_get_with_read_barrier ?
4084 LocationSummary::kCallOnSlowPath :
4085 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004086 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4087 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4088 }
Calin Juravle52c48962014-12-16 17:02:57 +00004089 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004090 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4091 locations->SetOut(Location::RequiresFpuRegister());
4092 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004093 // The output overlaps for an object field get when read barriers
4094 // are enabled: we do not want the move to overwrite the object's
4095 // location, as we need it to emit the read barrier.
4096 locations->SetOut(
4097 Location::RequiresRegister(),
4098 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004099 }
Calin Juravle52c48962014-12-16 17:02:57 +00004100}
4101
4102void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4103 const FieldInfo& field_info) {
4104 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4105
4106 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004107 Location base_loc = locations->InAt(0);
4108 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004109 Location out = locations->Out();
4110 bool is_volatile = field_info.IsVolatile();
4111 Primitive::Type field_type = field_info.GetFieldType();
4112 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4113
4114 switch (field_type) {
4115 case Primitive::kPrimBoolean: {
4116 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4117 break;
4118 }
4119
4120 case Primitive::kPrimByte: {
4121 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4122 break;
4123 }
4124
4125 case Primitive::kPrimShort: {
4126 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4127 break;
4128 }
4129
4130 case Primitive::kPrimChar: {
4131 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4132 break;
4133 }
4134
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004135 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004136 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4137 break;
4138 }
4139
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004140 case Primitive::kPrimNot: {
4141 // /* HeapReference<Object> */ out = *(base + offset)
4142 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004143 // Note that a potential implicit null check is handled in this
4144 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4145 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004146 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004147 if (is_volatile) {
4148 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4149 }
4150 } else {
4151 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4152 codegen_->MaybeRecordImplicitNullCheck(instruction);
4153 if (is_volatile) {
4154 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4155 }
4156 // If read barriers are enabled, emit read barriers other than
4157 // Baker's using a slow path (and also unpoison the loaded
4158 // reference, if heap poisoning is enabled).
4159 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4160 }
4161 break;
4162 }
4163
Calin Juravle52c48962014-12-16 17:02:57 +00004164 case Primitive::kPrimLong: {
4165 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4166 break;
4167 }
4168
4169 case Primitive::kPrimFloat: {
4170 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4171 break;
4172 }
4173
4174 case Primitive::kPrimDouble: {
4175 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4176 break;
4177 }
4178
4179 case Primitive::kPrimVoid:
4180 LOG(FATAL) << "Unreachable type " << field_type;
4181 UNREACHABLE();
4182 }
4183
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004184 if (field_type == Primitive::kPrimNot) {
4185 // Potential implicit null checks, in the case of reference
4186 // fields, are handled in the previous switch statement.
4187 } else {
4188 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004189 }
Roland Levillain4d027112015-07-01 15:41:14 +01004190
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004191 if (is_volatile) {
4192 if (field_type == Primitive::kPrimNot) {
4193 // Memory barriers, in the case of references, are also handled
4194 // in the previous switch statement.
4195 } else {
4196 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4197 }
Roland Levillain4d027112015-07-01 15:41:14 +01004198 }
Calin Juravle52c48962014-12-16 17:02:57 +00004199}
4200
4201void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4202 const FieldInfo& field_info) {
4203 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4204
4205 LocationSummary* locations =
4206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004207 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004208 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004209 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004210 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004211
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004212 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004213 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004214 if (is_volatile) {
4215 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4216 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4217 } else {
4218 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4219 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004220 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004221 if (is_volatile) {
4222 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4223 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4224 } else {
4225 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4226 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004227 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004228 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004229 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004230 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004231 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004232 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4233 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004234 locations->AddTemp(Location::RequiresRegister());
4235 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236}
4237
Calin Juravle52c48962014-12-16 17:02:57 +00004238void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004239 const FieldInfo& field_info,
4240 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004241 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4242
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004243 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004244 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4245 Location value = locations->InAt(1);
4246 bool is_volatile = field_info.IsVolatile();
4247 Primitive::Type field_type = field_info.GetFieldType();
4248 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4249
4250 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004251 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004252 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253
Mark Mendellea5af682015-10-22 17:35:49 -04004254 bool maybe_record_implicit_null_check_done = false;
4255
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004256 switch (field_type) {
4257 case Primitive::kPrimBoolean:
4258 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004259 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004260 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004261 __ movb(Address(base, offset), Immediate(v));
4262 } else {
4263 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4264 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265 break;
4266 }
4267
4268 case Primitive::kPrimShort:
4269 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004270 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004271 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004272 __ movw(Address(base, offset), Immediate(v));
4273 } else {
4274 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4275 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004276 break;
4277 }
4278
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004279 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004281 if (value.IsConstant()) {
4282 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004283 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4284 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4285 // Note: if heap poisoning is enabled, no need to poison
4286 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004287 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004288 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004289 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4290 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4291 __ movl(temp, value.AsRegister<CpuRegister>());
4292 __ PoisonHeapReference(temp);
4293 __ movl(Address(base, offset), temp);
4294 } else {
4295 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4296 }
Mark Mendell40741f32015-04-20 22:10:34 -04004297 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004298 break;
4299 }
4300
4301 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004302 if (value.IsConstant()) {
4303 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004304 codegen_->MoveInt64ToAddress(Address(base, offset),
4305 Address(base, offset + sizeof(int32_t)),
4306 v,
4307 instruction);
4308 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004309 } else {
4310 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4311 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312 break;
4313 }
4314
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004315 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004316 if (value.IsConstant()) {
4317 int32_t v =
4318 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4319 __ movl(Address(base, offset), Immediate(v));
4320 } else {
4321 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4322 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004323 break;
4324 }
4325
4326 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004327 if (value.IsConstant()) {
4328 int64_t v =
4329 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4330 codegen_->MoveInt64ToAddress(Address(base, offset),
4331 Address(base, offset + sizeof(int32_t)),
4332 v,
4333 instruction);
4334 maybe_record_implicit_null_check_done = true;
4335 } else {
4336 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4337 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004338 break;
4339 }
4340
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341 case Primitive::kPrimVoid:
4342 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004343 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004344 }
Calin Juravle52c48962014-12-16 17:02:57 +00004345
Mark Mendellea5af682015-10-22 17:35:49 -04004346 if (!maybe_record_implicit_null_check_done) {
4347 codegen_->MaybeRecordImplicitNullCheck(instruction);
4348 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004349
4350 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4351 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4352 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004353 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004354 }
4355
Calin Juravle52c48962014-12-16 17:02:57 +00004356 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004357 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004358 }
4359}
4360
4361void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4362 HandleFieldSet(instruction, instruction->GetFieldInfo());
4363}
4364
4365void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004366 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367}
4368
4369void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004370 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371}
4372
4373void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004374 HandleFieldGet(instruction, instruction->GetFieldInfo());
4375}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376
Calin Juravle52c48962014-12-16 17:02:57 +00004377void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4378 HandleFieldGet(instruction);
4379}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380
Calin Juravle52c48962014-12-16 17:02:57 +00004381void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4382 HandleFieldGet(instruction, instruction->GetFieldInfo());
4383}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384
Calin Juravle52c48962014-12-16 17:02:57 +00004385void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4386 HandleFieldSet(instruction, instruction->GetFieldInfo());
4387}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004388
Calin Juravle52c48962014-12-16 17:02:57 +00004389void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004390 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004391}
4392
Calin Juravlee460d1d2015-09-29 04:52:17 +01004393void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4394 HUnresolvedInstanceFieldGet* instruction) {
4395 FieldAccessCallingConventionX86_64 calling_convention;
4396 codegen_->CreateUnresolvedFieldLocationSummary(
4397 instruction, instruction->GetFieldType(), calling_convention);
4398}
4399
4400void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4401 HUnresolvedInstanceFieldGet* instruction) {
4402 FieldAccessCallingConventionX86_64 calling_convention;
4403 codegen_->GenerateUnresolvedFieldAccess(instruction,
4404 instruction->GetFieldType(),
4405 instruction->GetFieldIndex(),
4406 instruction->GetDexPc(),
4407 calling_convention);
4408}
4409
4410void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4411 HUnresolvedInstanceFieldSet* instruction) {
4412 FieldAccessCallingConventionX86_64 calling_convention;
4413 codegen_->CreateUnresolvedFieldLocationSummary(
4414 instruction, instruction->GetFieldType(), calling_convention);
4415}
4416
4417void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4418 HUnresolvedInstanceFieldSet* instruction) {
4419 FieldAccessCallingConventionX86_64 calling_convention;
4420 codegen_->GenerateUnresolvedFieldAccess(instruction,
4421 instruction->GetFieldType(),
4422 instruction->GetFieldIndex(),
4423 instruction->GetDexPc(),
4424 calling_convention);
4425}
4426
4427void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4428 HUnresolvedStaticFieldGet* instruction) {
4429 FieldAccessCallingConventionX86_64 calling_convention;
4430 codegen_->CreateUnresolvedFieldLocationSummary(
4431 instruction, instruction->GetFieldType(), calling_convention);
4432}
4433
4434void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4435 HUnresolvedStaticFieldGet* instruction) {
4436 FieldAccessCallingConventionX86_64 calling_convention;
4437 codegen_->GenerateUnresolvedFieldAccess(instruction,
4438 instruction->GetFieldType(),
4439 instruction->GetFieldIndex(),
4440 instruction->GetDexPc(),
4441 calling_convention);
4442}
4443
4444void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4445 HUnresolvedStaticFieldSet* instruction) {
4446 FieldAccessCallingConventionX86_64 calling_convention;
4447 codegen_->CreateUnresolvedFieldLocationSummary(
4448 instruction, instruction->GetFieldType(), calling_convention);
4449}
4450
4451void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4452 HUnresolvedStaticFieldSet* instruction) {
4453 FieldAccessCallingConventionX86_64 calling_convention;
4454 codegen_->GenerateUnresolvedFieldAccess(instruction,
4455 instruction->GetFieldType(),
4456 instruction->GetFieldIndex(),
4457 instruction->GetDexPc(),
4458 calling_convention);
4459}
4460
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004462 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4463 ? LocationSummary::kCallOnSlowPath
4464 : LocationSummary::kNoCall;
4465 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4466 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004467 ? Location::RequiresRegister()
4468 : Location::Any();
4469 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004470 if (instruction->HasUses()) {
4471 locations->SetOut(Location::SameAsFirstInput());
4472 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004473}
4474
Calin Juravle2ae48182016-03-16 14:05:09 +00004475void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4476 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004477 return;
4478 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004479 LocationSummary* locations = instruction->GetLocations();
4480 Location obj = locations->InAt(0);
4481
4482 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004483 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004484}
4485
Calin Juravle2ae48182016-03-16 14:05:09 +00004486void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004487 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004488 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489
4490 LocationSummary* locations = instruction->GetLocations();
4491 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492
4493 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004494 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004495 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004496 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004497 } else {
4498 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004499 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004500 __ jmp(slow_path->GetEntryLabel());
4501 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502 }
4503 __ j(kEqual, slow_path->GetEntryLabel());
4504}
4505
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004506void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004507 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004508}
4509
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004510void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004511 bool object_array_get_with_read_barrier =
4512 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004513 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004514 new (GetGraph()->GetArena()) LocationSummary(instruction,
4515 object_array_get_with_read_barrier ?
4516 LocationSummary::kCallOnSlowPath :
4517 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004518 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4519 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4520 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004521 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004522 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004523 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4524 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4525 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004526 // The output overlaps for an object array get when read barriers
4527 // are enabled: we do not want the move to overwrite the array's
4528 // location, as we need it to emit the read barrier.
4529 locations->SetOut(
4530 Location::RequiresRegister(),
4531 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004532 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533}
4534
4535void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4536 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004537 Location obj_loc = locations->InAt(0);
4538 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004539 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004540 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004541 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004542
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004543 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004544 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004546 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547 if (index.IsConstant()) {
4548 __ movzxb(out, Address(obj,
4549 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4550 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004551 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 }
4553 break;
4554 }
4555
4556 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004557 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004558 if (index.IsConstant()) {
4559 __ movsxb(out, Address(obj,
4560 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4561 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004562 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004563 }
4564 break;
4565 }
4566
4567 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004568 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569 if (index.IsConstant()) {
4570 __ movsxw(out, Address(obj,
4571 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4572 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004573 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 }
4575 break;
4576 }
4577
4578 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004579 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004580 if (index.IsConstant()) {
4581 __ movzxw(out, Address(obj,
4582 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4583 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004584 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004585 }
4586 break;
4587 }
4588
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004589 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004590 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 if (index.IsConstant()) {
4592 __ movl(out, Address(obj,
4593 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4594 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004595 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596 }
4597 break;
4598 }
4599
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004600 case Primitive::kPrimNot: {
4601 static_assert(
4602 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4603 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004604 // /* HeapReference<Object> */ out =
4605 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4606 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004607 // Note that a potential implicit null check is handled in this
4608 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4609 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004610 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004611 } else {
4612 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4613 if (index.IsConstant()) {
4614 uint32_t offset =
4615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4616 __ movl(out, Address(obj, offset));
4617 codegen_->MaybeRecordImplicitNullCheck(instruction);
4618 // If read barriers are enabled, emit read barriers other than
4619 // Baker's using a slow path (and also unpoison the loaded
4620 // reference, if heap poisoning is enabled).
4621 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4622 } else {
4623 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4624 codegen_->MaybeRecordImplicitNullCheck(instruction);
4625 // If read barriers are enabled, emit read barriers other than
4626 // Baker's using a slow path (and also unpoison the loaded
4627 // reference, if heap poisoning is enabled).
4628 codegen_->MaybeGenerateReadBarrierSlow(
4629 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4630 }
4631 }
4632 break;
4633 }
4634
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004636 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637 if (index.IsConstant()) {
4638 __ movq(out, Address(obj,
4639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4640 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004641 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004642 }
4643 break;
4644 }
4645
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004646 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004647 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004648 if (index.IsConstant()) {
4649 __ movss(out, Address(obj,
4650 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4651 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004652 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004653 }
4654 break;
4655 }
4656
4657 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004658 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004659 if (index.IsConstant()) {
4660 __ movsd(out, Address(obj,
4661 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4662 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004663 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004664 }
4665 break;
4666 }
4667
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004668 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004669 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004670 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004671 }
Roland Levillain4d027112015-07-01 15:41:14 +01004672
4673 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004674 // Potential implicit null checks, in the case of reference
4675 // arrays, are handled in the previous switch statement.
4676 } else {
4677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004678 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004679}
4680
4681void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004682 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004683
4684 bool needs_write_barrier =
4685 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004686 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004687
Nicolas Geoffray39468442014-09-02 15:17:15 +01004688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004689 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004690 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004691 LocationSummary::kCallOnSlowPath :
4692 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004693
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004694 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004695 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4696 if (Primitive::IsFloatingPointType(value_type)) {
4697 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004698 } else {
4699 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4700 }
4701
4702 if (needs_write_barrier) {
4703 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004704 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004705 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707}
4708
4709void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4710 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004711 Location array_loc = locations->InAt(0);
4712 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004713 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004714 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004715 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004716 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004717 bool needs_write_barrier =
4718 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004719 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4720 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4721 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004722
4723 switch (value_type) {
4724 case Primitive::kPrimBoolean:
4725 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004726 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4727 Address address = index.IsConstant()
4728 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4729 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4730 if (value.IsRegister()) {
4731 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004733 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004734 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004735 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004736 break;
4737 }
4738
4739 case Primitive::kPrimShort:
4740 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004741 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4742 Address address = index.IsConstant()
4743 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4744 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4745 if (value.IsRegister()) {
4746 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004747 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004748 DCHECK(value.IsConstant()) << value;
4749 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004751 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 break;
4753 }
4754
4755 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004756 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4757 Address address = index.IsConstant()
4758 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4759 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004760
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004761 if (!value.IsRegister()) {
4762 // Just setting null.
4763 DCHECK(instruction->InputAt(2)->IsNullConstant());
4764 DCHECK(value.IsConstant()) << value;
4765 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004766 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004767 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004768 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004769 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004770 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004771
4772 DCHECK(needs_write_barrier);
4773 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004774 // We cannot use a NearLabel for `done`, as its range may be too
4775 // short when Baker read barriers are enabled.
4776 Label done;
4777 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004778 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004779 Location temp_loc = locations->GetTemp(0);
4780 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004781 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004782 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4783 codegen_->AddSlowPath(slow_path);
4784 if (instruction->GetValueCanBeNull()) {
4785 __ testl(register_value, register_value);
4786 __ j(kNotEqual, &not_null);
4787 __ movl(address, Immediate(0));
4788 codegen_->MaybeRecordImplicitNullCheck(instruction);
4789 __ jmp(&done);
4790 __ Bind(&not_null);
4791 }
4792
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004793 // Note that when Baker read barriers are enabled, the type
4794 // checks are performed without read barriers. This is fine,
4795 // even in the case where a class object is in the from-space
4796 // after the flip, as a comparison involving such a type would
4797 // not produce a false positive; it may of course produce a
4798 // false negative, in which case we would take the ArraySet
4799 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004800
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004801 // /* HeapReference<Class> */ temp = array->klass_
4802 __ movl(temp, Address(array, class_offset));
4803 codegen_->MaybeRecordImplicitNullCheck(instruction);
4804 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004805
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004806 // /* HeapReference<Class> */ temp = temp->component_type_
4807 __ movl(temp, Address(temp, component_offset));
4808 // If heap poisoning is enabled, no need to unpoison `temp`
4809 // nor the object reference in `register_value->klass`, as
4810 // we are comparing two poisoned references.
4811 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004812
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004813 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4814 __ j(kEqual, &do_put);
4815 // If heap poisoning is enabled, the `temp` reference has
4816 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004817 __ MaybeUnpoisonHeapReference(temp);
4818
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004819 // If heap poisoning is enabled, no need to unpoison the
4820 // heap reference loaded below, as it is only used for a
4821 // comparison with null.
4822 __ cmpl(Address(temp, super_offset), Immediate(0));
4823 __ j(kNotEqual, slow_path->GetEntryLabel());
4824 __ Bind(&do_put);
4825 } else {
4826 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 }
4828 }
4829
4830 if (kPoisonHeapReferences) {
4831 __ movl(temp, register_value);
4832 __ PoisonHeapReference(temp);
4833 __ movl(address, temp);
4834 } else {
4835 __ movl(address, register_value);
4836 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004837 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004838 codegen_->MaybeRecordImplicitNullCheck(instruction);
4839 }
4840
4841 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4842 codegen_->MarkGCCard(
4843 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4844 __ Bind(&done);
4845
4846 if (slow_path != nullptr) {
4847 __ Bind(slow_path->GetExitLabel());
4848 }
4849
4850 break;
4851 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004852
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 case Primitive::kPrimInt: {
4854 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4855 Address address = index.IsConstant()
4856 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4857 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4858 if (value.IsRegister()) {
4859 __ movl(address, value.AsRegister<CpuRegister>());
4860 } else {
4861 DCHECK(value.IsConstant()) << value;
4862 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4863 __ movl(address, Immediate(v));
4864 }
4865 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866 break;
4867 }
4868
4869 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004870 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4871 Address address = index.IsConstant()
4872 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4873 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4874 if (value.IsRegister()) {
4875 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004876 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004878 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004879 Address address_high = index.IsConstant()
4880 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4881 offset + sizeof(int32_t))
4882 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4883 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004884 }
4885 break;
4886 }
4887
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004888 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004889 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4890 Address address = index.IsConstant()
4891 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4892 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004893 if (value.IsFpuRegister()) {
4894 __ movss(address, value.AsFpuRegister<XmmRegister>());
4895 } else {
4896 DCHECK(value.IsConstant());
4897 int32_t v =
4898 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4899 __ movl(address, Immediate(v));
4900 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004902 break;
4903 }
4904
4905 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004906 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4907 Address address = index.IsConstant()
4908 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4909 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004910 if (value.IsFpuRegister()) {
4911 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4912 codegen_->MaybeRecordImplicitNullCheck(instruction);
4913 } else {
4914 int64_t v =
4915 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4916 Address address_high = index.IsConstant()
4917 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4918 offset + sizeof(int32_t))
4919 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4920 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4921 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004922 break;
4923 }
4924
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 case Primitive::kPrimVoid:
4926 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004927 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 }
4929}
4930
4931void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004932 LocationSummary* locations =
4933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004934 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04004935 if (!instruction->IsEmittedAtUseSite()) {
4936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4937 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004938}
4939
4940void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04004941 if (instruction->IsEmittedAtUseSite()) {
4942 return;
4943 }
4944
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004945 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004946 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004947 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4948 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004949 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004950 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004951}
4952
4953void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004954 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4955 ? LocationSummary::kCallOnSlowPath
4956 : LocationSummary::kNoCall;
4957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004958 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04004959 HInstruction* length = instruction->InputAt(1);
4960 if (!length->IsEmittedAtUseSite()) {
4961 locations->SetInAt(1, Location::RegisterOrConstant(length));
4962 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004963 if (instruction->HasUses()) {
4964 locations->SetOut(Location::SameAsFirstInput());
4965 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004966}
4967
4968void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4969 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004970 Location index_loc = locations->InAt(0);
4971 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04004972 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004973
Mark Mendell99dbd682015-04-22 16:18:52 -04004974 if (length_loc.IsConstant()) {
4975 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4976 if (index_loc.IsConstant()) {
4977 // BCE will remove the bounds check if we are guarenteed to pass.
4978 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4979 if (index < 0 || index >= length) {
4980 codegen_->AddSlowPath(slow_path);
4981 __ jmp(slow_path->GetEntryLabel());
4982 } else {
4983 // Some optimization after BCE may have generated this, and we should not
4984 // generate a bounds check if it is a valid range.
4985 }
4986 return;
4987 }
4988
4989 // We have to reverse the jump condition because the length is the constant.
4990 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4991 __ cmpl(index_reg, Immediate(length));
4992 codegen_->AddSlowPath(slow_path);
4993 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004994 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04004995 HInstruction* array_length = instruction->InputAt(1);
4996 if (array_length->IsEmittedAtUseSite()) {
4997 // Address the length field in the array.
4998 DCHECK(array_length->IsArrayLength());
4999 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5000 Location array_loc = array_length->GetLocations()->InAt(0);
5001 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
5002 if (index_loc.IsConstant()) {
5003 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5004 __ cmpl(array_len, Immediate(value));
5005 } else {
5006 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5007 }
5008 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005009 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005010 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5011 if (index_loc.IsConstant()) {
5012 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5013 __ cmpl(length, Immediate(value));
5014 } else {
5015 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5016 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005017 }
5018 codegen_->AddSlowPath(slow_path);
5019 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005020 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005021}
5022
5023void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5024 CpuRegister card,
5025 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005026 CpuRegister value,
5027 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005028 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005029 if (value_can_be_null) {
5030 __ testl(value, value);
5031 __ j(kEqual, &is_null);
5032 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005033 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005034 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035 __ movq(temp, object);
5036 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005037 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005038 if (value_can_be_null) {
5039 __ Bind(&is_null);
5040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041}
5042
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005043void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005044 LOG(FATAL) << "Unimplemented";
5045}
5046
5047void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005048 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5049}
5050
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005051void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005052 LocationSummary* locations =
5053 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5054 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005055}
5056
5057void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005058 HBasicBlock* block = instruction->GetBlock();
5059 if (block->GetLoopInformation() != nullptr) {
5060 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5061 // The back edge will generate the suspend check.
5062 return;
5063 }
5064 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5065 // The goto will generate the suspend check.
5066 return;
5067 }
5068 GenerateSuspendCheck(instruction, nullptr);
5069}
5070
5071void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5072 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005073 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005074 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5075 if (slow_path == nullptr) {
5076 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5077 instruction->SetSlowPath(slow_path);
5078 codegen_->AddSlowPath(slow_path);
5079 if (successor != nullptr) {
5080 DCHECK(successor->IsLoopHeader());
5081 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5082 }
5083 } else {
5084 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5085 }
5086
Andreas Gampe542451c2016-07-26 09:02:02 -07005087 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005088 /* no_rip */ true),
5089 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005090 if (successor == nullptr) {
5091 __ j(kNotEqual, slow_path->GetEntryLabel());
5092 __ Bind(slow_path->GetReturnLabel());
5093 } else {
5094 __ j(kEqual, codegen_->GetLabelOf(successor));
5095 __ jmp(slow_path->GetEntryLabel());
5096 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005097}
5098
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005099X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5100 return codegen_->GetAssembler();
5101}
5102
5103void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005104 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005105 Location source = move->GetSource();
5106 Location destination = move->GetDestination();
5107
5108 if (source.IsRegister()) {
5109 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005110 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005111 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005112 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005113 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005114 } else {
5115 DCHECK(destination.IsDoubleStackSlot());
5116 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005117 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005118 }
5119 } else if (source.IsStackSlot()) {
5120 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005121 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005122 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005123 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005124 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005125 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005126 } else {
5127 DCHECK(destination.IsStackSlot());
5128 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5129 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5130 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005131 } else if (source.IsDoubleStackSlot()) {
5132 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005133 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005134 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005135 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005136 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5137 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005138 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005139 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005140 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5141 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5142 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005143 } else if (source.IsConstant()) {
5144 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005145 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5146 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005147 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005148 if (value == 0) {
5149 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5150 } else {
5151 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5152 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005153 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005154 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005155 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005156 }
5157 } else if (constant->IsLongConstant()) {
5158 int64_t value = constant->AsLongConstant()->GetValue();
5159 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005160 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005161 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005162 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005163 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005164 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005165 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005166 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005167 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005168 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005169 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005170 } else {
5171 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005172 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005173 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5174 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005175 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005176 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005177 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005178 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005179 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005180 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005181 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005182 } else {
5183 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005184 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005185 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005186 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005187 } else if (source.IsFpuRegister()) {
5188 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005189 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005190 } else if (destination.IsStackSlot()) {
5191 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005192 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005193 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005194 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005195 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005196 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005197 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005198 }
5199}
5200
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005201void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005202 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005203 __ movl(Address(CpuRegister(RSP), mem), reg);
5204 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005205}
5206
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005207void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005208 ScratchRegisterScope ensure_scratch(
5209 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5210
5211 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5212 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5213 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5214 Address(CpuRegister(RSP), mem2 + stack_offset));
5215 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5216 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5217 CpuRegister(ensure_scratch.GetRegister()));
5218}
5219
Mark Mendell8a1c7282015-06-29 15:41:28 -04005220void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5221 __ movq(CpuRegister(TMP), reg1);
5222 __ movq(reg1, reg2);
5223 __ movq(reg2, CpuRegister(TMP));
5224}
5225
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005226void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5227 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5228 __ movq(Address(CpuRegister(RSP), mem), reg);
5229 __ movq(reg, CpuRegister(TMP));
5230}
5231
5232void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5233 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005234 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005235
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005236 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5237 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5238 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5239 Address(CpuRegister(RSP), mem2 + stack_offset));
5240 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5241 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5242 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005243}
5244
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005245void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5246 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5247 __ movss(Address(CpuRegister(RSP), mem), reg);
5248 __ movd(reg, CpuRegister(TMP));
5249}
5250
5251void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5252 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5253 __ movsd(Address(CpuRegister(RSP), mem), reg);
5254 __ movd(reg, CpuRegister(TMP));
5255}
5256
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005257void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005258 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005259 Location source = move->GetSource();
5260 Location destination = move->GetDestination();
5261
5262 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005263 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005264 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005265 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005266 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005267 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005268 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005269 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5270 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005271 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005272 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005273 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005274 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5275 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005276 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005277 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5278 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5279 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005280 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005281 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005282 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005283 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005285 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005286 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005287 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005288 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005289 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005290 }
5291}
5292
5293
5294void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5295 __ pushq(CpuRegister(reg));
5296}
5297
5298
5299void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5300 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005301}
5302
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005303void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005304 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005305 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5306 Immediate(mirror::Class::kStatusInitialized));
5307 __ j(kLess, slow_path->GetEntryLabel());
5308 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005309 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005310}
5311
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005312HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5313 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005314 switch (desired_class_load_kind) {
5315 case HLoadClass::LoadKind::kReferrersClass:
5316 break;
5317 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5318 DCHECK(!GetCompilerOptions().GetCompilePic());
5319 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5320 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5321 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5322 DCHECK(GetCompilerOptions().GetCompilePic());
5323 break;
5324 case HLoadClass::LoadKind::kBootImageAddress:
5325 break;
5326 case HLoadClass::LoadKind::kDexCacheAddress:
5327 DCHECK(Runtime::Current()->UseJitCompilation());
5328 break;
5329 case HLoadClass::LoadKind::kDexCachePcRelative:
5330 DCHECK(!Runtime::Current()->UseJitCompilation());
5331 break;
5332 case HLoadClass::LoadKind::kDexCacheViaMethod:
5333 break;
5334 }
5335 return desired_class_load_kind;
5336}
5337
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005338void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005339 if (cls->NeedsAccessCheck()) {
5340 InvokeRuntimeCallingConvention calling_convention;
5341 CodeGenerator::CreateLoadClassLocationSummary(
5342 cls,
5343 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5344 Location::RegisterLocation(RAX),
5345 /* code_generator_supports_read_barrier */ true);
5346 return;
5347 }
5348
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005349 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5350 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005351 ? LocationSummary::kCallOnSlowPath
5352 : LocationSummary::kNoCall;
5353 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005354 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005355 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5356 }
5357
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005358 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5359 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5360 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5361 locations->SetInAt(0, Location::RequiresRegister());
5362 }
5363 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005364}
5365
5366void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005367 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005368 if (cls->NeedsAccessCheck()) {
5369 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005370 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005371 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005372 return;
5373 }
5374
Roland Levillain0d5a2812015-11-13 10:07:31 +00005375 Location out_loc = locations->Out();
5376 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005377
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005378 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005379 bool generate_null_check = false;
5380 switch (cls->GetLoadKind()) {
5381 case HLoadClass::LoadKind::kReferrersClass: {
5382 DCHECK(!cls->CanCallRuntime());
5383 DCHECK(!cls->MustGenerateClinitCheck());
5384 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5385 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5386 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005387 cls,
5388 out_loc,
5389 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5390 /*fixup_label*/nullptr,
5391 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005392 break;
5393 }
5394 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005395 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005396 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5397 codegen_->RecordTypePatch(cls);
5398 break;
5399 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005400 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005401 DCHECK_NE(cls->GetAddress(), 0u);
5402 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5403 __ movl(out, Immediate(address)); // Zero-extended.
5404 codegen_->RecordSimplePatch();
5405 break;
5406 }
5407 case HLoadClass::LoadKind::kDexCacheAddress: {
5408 DCHECK_NE(cls->GetAddress(), 0u);
5409 // /* GcRoot<mirror::Class> */ out = *address
5410 if (IsUint<32>(cls->GetAddress())) {
5411 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005412 GenerateGcRootFieldLoad(cls,
5413 out_loc,
5414 address,
5415 /*fixup_label*/nullptr,
5416 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005417 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005418 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5419 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005420 GenerateGcRootFieldLoad(cls,
5421 out_loc,
5422 Address(out, 0),
5423 /*fixup_label*/nullptr,
5424 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005425 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005426 generate_null_check = !cls->IsInDexCache();
5427 break;
5428 }
5429 case HLoadClass::LoadKind::kDexCachePcRelative: {
5430 uint32_t offset = cls->GetDexCacheElementOffset();
5431 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5432 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5433 /* no_rip */ false);
5434 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005435 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005436 generate_null_check = !cls->IsInDexCache();
5437 break;
5438 }
5439 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5440 // /* GcRoot<mirror::Class>[] */ out =
5441 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5442 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5443 __ movq(out,
5444 Address(current_method,
5445 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5446 // /* GcRoot<mirror::Class> */ out = out[type_index]
5447 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005448 cls,
5449 out_loc,
5450 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5451 /*fixup_label*/nullptr,
5452 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005453 generate_null_check = !cls->IsInDexCache();
5454 break;
5455 }
5456 default:
5457 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5458 UNREACHABLE();
5459 }
5460
5461 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5462 DCHECK(cls->CanCallRuntime());
5463 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5464 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5465 codegen_->AddSlowPath(slow_path);
5466 if (generate_null_check) {
5467 __ testl(out, out);
5468 __ j(kEqual, slow_path->GetEntryLabel());
5469 }
5470 if (cls->MustGenerateClinitCheck()) {
5471 GenerateClassInitializationCheck(slow_path, out);
5472 } else {
5473 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005474 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005475 }
5476}
5477
5478void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5479 LocationSummary* locations =
5480 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5481 locations->SetInAt(0, Location::RequiresRegister());
5482 if (check->HasUses()) {
5483 locations->SetOut(Location::SameAsFirstInput());
5484 }
5485}
5486
5487void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005488 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005489 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005490 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005492 GenerateClassInitializationCheck(slow_path,
5493 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005494}
5495
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005496HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5497 HLoadString::LoadKind desired_string_load_kind) {
5498 if (kEmitCompilerReadBarrier) {
5499 switch (desired_string_load_kind) {
5500 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5501 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5502 case HLoadString::LoadKind::kBootImageAddress:
5503 // TODO: Implement for read barrier.
5504 return HLoadString::LoadKind::kDexCacheViaMethod;
5505 default:
5506 break;
5507 }
5508 }
5509 switch (desired_string_load_kind) {
5510 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5511 DCHECK(!GetCompilerOptions().GetCompilePic());
5512 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5513 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5514 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5515 DCHECK(GetCompilerOptions().GetCompilePic());
5516 break;
5517 case HLoadString::LoadKind::kBootImageAddress:
5518 break;
5519 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005520 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005521 break;
5522 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005523 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005524 break;
5525 case HLoadString::LoadKind::kDexCacheViaMethod:
5526 break;
5527 }
5528 return desired_string_load_kind;
5529}
5530
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005531void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005532 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5533 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005534 : LocationSummary::kNoCall;
5535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005536 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5537 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005538 locations->SetOut(Location::RegisterLocation(RAX));
5539 } else {
5540 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005541 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005542}
5543
5544void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005545 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005546 Location out_loc = locations->Out();
5547 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005548
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005549 switch (load->GetLoadKind()) {
5550 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5551 DCHECK(!kEmitCompilerReadBarrier);
5552 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5553 codegen_->RecordStringPatch(load);
5554 return; // No dex cache slow path.
5555 }
5556 case HLoadString::LoadKind::kBootImageAddress: {
5557 DCHECK(!kEmitCompilerReadBarrier);
5558 DCHECK_NE(load->GetAddress(), 0u);
5559 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5560 __ movl(out, Immediate(address)); // Zero-extended.
5561 codegen_->RecordSimplePatch();
5562 return; // No dex cache slow path.
5563 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005564 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005565 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005566 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005567
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005568 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005569 InvokeRuntimeCallingConvention calling_convention;
5570 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
5571 Immediate(load->GetStringIndex()));
5572 codegen_->InvokeRuntime(kQuickResolveString,
5573 load,
5574 load->GetDexPc());
5575 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005576}
5577
David Brazdilcb1c0552015-08-04 16:22:25 +01005578static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005579 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005580 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005581}
5582
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005583void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5584 LocationSummary* locations =
5585 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5586 locations->SetOut(Location::RequiresRegister());
5587}
5588
5589void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005590 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5591}
5592
5593void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5594 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5595}
5596
5597void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5598 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005599}
5600
5601void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5602 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005604 InvokeRuntimeCallingConvention calling_convention;
5605 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5606}
5607
5608void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005609 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005610 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005611}
5612
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005613static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5614 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005615 !kUseBakerReadBarrier &&
5616 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005617 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5618 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5619}
5620
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005621void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005623 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005624 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005625 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005626 case TypeCheckKind::kExactCheck:
5627 case TypeCheckKind::kAbstractClassCheck:
5628 case TypeCheckKind::kClassHierarchyCheck:
5629 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005630 call_kind =
5631 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005632 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005633 break;
5634 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005635 case TypeCheckKind::kUnresolvedCheck:
5636 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005637 call_kind = LocationSummary::kCallOnSlowPath;
5638 break;
5639 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005640
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005642 if (baker_read_barrier_slow_path) {
5643 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5644 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005645 locations->SetInAt(0, Location::RequiresRegister());
5646 locations->SetInAt(1, Location::Any());
5647 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5648 locations->SetOut(Location::RequiresRegister());
5649 // When read barriers are enabled, we need a temporary register for
5650 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005651 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005652 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005653 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005654}
5655
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005656void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005657 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005658 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005659 Location obj_loc = locations->InAt(0);
5660 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005661 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005662 Location out_loc = locations->Out();
5663 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005664 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005665 locations->GetTemp(0) :
5666 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005667 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005668 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5669 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5670 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005671 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005672 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005673
5674 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005675 // Avoid null check if we know obj is not null.
5676 if (instruction->MustDoNullCheck()) {
5677 __ testl(obj, obj);
5678 __ j(kEqual, &zero);
5679 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005680
Roland Levillain0d5a2812015-11-13 10:07:31 +00005681 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005682 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005684 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005685 case TypeCheckKind::kExactCheck: {
5686 if (cls.IsRegister()) {
5687 __ cmpl(out, cls.AsRegister<CpuRegister>());
5688 } else {
5689 DCHECK(cls.IsStackSlot()) << cls;
5690 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5691 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005692 if (zero.IsLinked()) {
5693 // Classes must be equal for the instanceof to succeed.
5694 __ j(kNotEqual, &zero);
5695 __ movl(out, Immediate(1));
5696 __ jmp(&done);
5697 } else {
5698 __ setcc(kEqual, out);
5699 // setcc only sets the low byte.
5700 __ andl(out, Immediate(1));
5701 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005702 break;
5703 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005704
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705 case TypeCheckKind::kAbstractClassCheck: {
5706 // If the class is abstract, we eagerly fetch the super class of the
5707 // object to avoid doing a comparison we know will fail.
5708 NearLabel loop, success;
5709 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005710 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005711 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005712 __ testl(out, out);
5713 // If `out` is null, we use it for the result, and jump to `done`.
5714 __ j(kEqual, &done);
5715 if (cls.IsRegister()) {
5716 __ cmpl(out, cls.AsRegister<CpuRegister>());
5717 } else {
5718 DCHECK(cls.IsStackSlot()) << cls;
5719 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5720 }
5721 __ j(kNotEqual, &loop);
5722 __ movl(out, Immediate(1));
5723 if (zero.IsLinked()) {
5724 __ jmp(&done);
5725 }
5726 break;
5727 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005729 case TypeCheckKind::kClassHierarchyCheck: {
5730 // Walk over the class hierarchy to find a match.
5731 NearLabel loop, success;
5732 __ Bind(&loop);
5733 if (cls.IsRegister()) {
5734 __ cmpl(out, cls.AsRegister<CpuRegister>());
5735 } else {
5736 DCHECK(cls.IsStackSlot()) << cls;
5737 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5738 }
5739 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005740 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005741 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 __ testl(out, out);
5743 __ j(kNotEqual, &loop);
5744 // If `out` is null, we use it for the result, and jump to `done`.
5745 __ jmp(&done);
5746 __ Bind(&success);
5747 __ movl(out, Immediate(1));
5748 if (zero.IsLinked()) {
5749 __ jmp(&done);
5750 }
5751 break;
5752 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005753
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005755 // Do an exact check.
5756 NearLabel exact_check;
5757 if (cls.IsRegister()) {
5758 __ cmpl(out, cls.AsRegister<CpuRegister>());
5759 } else {
5760 DCHECK(cls.IsStackSlot()) << cls;
5761 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5762 }
5763 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005764 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005765 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005766 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005767 __ testl(out, out);
5768 // If `out` is null, we use it for the result, and jump to `done`.
5769 __ j(kEqual, &done);
5770 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5771 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005772 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 __ movl(out, Immediate(1));
5774 __ jmp(&done);
5775 break;
5776 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005778 case TypeCheckKind::kArrayCheck: {
5779 if (cls.IsRegister()) {
5780 __ cmpl(out, cls.AsRegister<CpuRegister>());
5781 } else {
5782 DCHECK(cls.IsStackSlot()) << cls;
5783 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5784 }
5785 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5787 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005788 codegen_->AddSlowPath(slow_path);
5789 __ j(kNotEqual, slow_path->GetEntryLabel());
5790 __ movl(out, Immediate(1));
5791 if (zero.IsLinked()) {
5792 __ jmp(&done);
5793 }
5794 break;
5795 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796
Calin Juravle98893e12015-10-02 21:05:03 +01005797 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005798 case TypeCheckKind::kInterfaceCheck: {
5799 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005800 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801 // cases.
5802 //
5803 // We cannot directly call the InstanceofNonTrivial runtime
5804 // entry point without resorting to a type checking slow path
5805 // here (i.e. by calling InvokeRuntime directly), as it would
5806 // require to assign fixed registers for the inputs of this
5807 // HInstanceOf instruction (following the runtime calling
5808 // convention), which might be cluttered by the potential first
5809 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005810 //
5811 // TODO: Introduce a new runtime entry point taking the object
5812 // to test (instead of its class) as argument, and let it deal
5813 // with the read barrier issues. This will let us refactor this
5814 // case of the `switch` code as it was previously (with a direct
5815 // call to the runtime not using a type checking slow path).
5816 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 DCHECK(locations->OnlyCallsOnSlowPath());
5818 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5819 /* is_fatal */ false);
5820 codegen_->AddSlowPath(slow_path);
5821 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005822 if (zero.IsLinked()) {
5823 __ jmp(&done);
5824 }
5825 break;
5826 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005827 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005828
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005830 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 __ xorl(out, out);
5832 }
5833
5834 if (done.IsLinked()) {
5835 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005836 }
5837
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005838 if (slow_path != nullptr) {
5839 __ Bind(slow_path->GetExitLabel());
5840 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005841}
5842
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005843void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5845 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005846 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005847 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005848 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005849 case TypeCheckKind::kExactCheck:
5850 case TypeCheckKind::kAbstractClassCheck:
5851 case TypeCheckKind::kClassHierarchyCheck:
5852 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005853 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5854 LocationSummary::kCallOnSlowPath :
5855 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01005856 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005857 break;
5858 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859 case TypeCheckKind::kUnresolvedCheck:
5860 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005861 call_kind = LocationSummary::kCallOnSlowPath;
5862 break;
5863 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005865 if (baker_read_barrier_slow_path) {
5866 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5867 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005868 locations->SetInAt(0, Location::RequiresRegister());
5869 locations->SetInAt(1, Location::Any());
5870 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5871 locations->AddTemp(Location::RequiresRegister());
5872 // When read barriers are enabled, we need an additional temporary
5873 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005874 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005877}
5878
5879void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005880 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005881 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005882 Location obj_loc = locations->InAt(0);
5883 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005884 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005885 Location temp_loc = locations->GetTemp(0);
5886 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005887 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005888 locations->GetTemp(1) :
5889 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5891 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5892 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5893 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005894
Roland Levillain0d5a2812015-11-13 10:07:31 +00005895 bool is_type_check_slow_path_fatal =
5896 (type_check_kind == TypeCheckKind::kExactCheck ||
5897 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5898 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5899 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5900 !instruction->CanThrowIntoCatchBlock();
5901 SlowPathCode* type_check_slow_path =
5902 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5903 is_type_check_slow_path_fatal);
5904 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905
Roland Levillain0d5a2812015-11-13 10:07:31 +00005906 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005907 case TypeCheckKind::kExactCheck:
5908 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005909 NearLabel done;
5910 // Avoid null check if we know obj is not null.
5911 if (instruction->MustDoNullCheck()) {
5912 __ testl(obj, obj);
5913 __ j(kEqual, &done);
5914 }
5915
5916 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005917 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005918
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919 if (cls.IsRegister()) {
5920 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5921 } else {
5922 DCHECK(cls.IsStackSlot()) << cls;
5923 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5924 }
5925 // Jump to slow path for throwing the exception or doing a
5926 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005927 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005928 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 break;
5930 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005933 NearLabel done;
5934 // Avoid null check if we know obj is not null.
5935 if (instruction->MustDoNullCheck()) {
5936 __ testl(obj, obj);
5937 __ j(kEqual, &done);
5938 }
5939
5940 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005941 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005942
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 // If the class is abstract, we eagerly fetch the super class of the
5944 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005945 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005947 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005948 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005949
5950 // If the class reference currently in `temp` is not null, jump
5951 // to the `compare_classes` label to compare it with the checked
5952 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954 __ j(kNotEqual, &compare_classes);
5955 // Otherwise, jump to the slow path to throw the exception.
5956 //
5957 // But before, move back the object's class into `temp` before
5958 // going into the slow path, as it has been overwritten in the
5959 // meantime.
5960 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005961 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005962 __ jmp(type_check_slow_path->GetEntryLabel());
5963
5964 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005965 if (cls.IsRegister()) {
5966 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5967 } else {
5968 DCHECK(cls.IsStackSlot()) << cls;
5969 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5970 }
5971 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005972 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005973 break;
5974 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005977 NearLabel done;
5978 // Avoid null check if we know obj is not null.
5979 if (instruction->MustDoNullCheck()) {
5980 __ testl(obj, obj);
5981 __ j(kEqual, &done);
5982 }
5983
5984 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005985 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005988 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 __ Bind(&loop);
5990 if (cls.IsRegister()) {
5991 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5992 } else {
5993 DCHECK(cls.IsStackSlot()) << cls;
5994 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5995 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005996 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005997
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005999 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006000
6001 // If the class reference currently in `temp` is not null, jump
6002 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 __ testl(temp, temp);
6004 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006005 // Otherwise, jump to the slow path to throw the exception.
6006 //
6007 // But before, move back the object's class into `temp` before
6008 // going into the slow path, as it has been overwritten in the
6009 // meantime.
6010 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006011 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006013 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 break;
6015 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006016
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006017 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006018 // We cannot use a NearLabel here, as its range might be too
6019 // short in some cases when read barriers are enabled. This has
6020 // been observed for instance when the code emitted for this
6021 // case uses high x86-64 registers (R8-R15).
6022 Label done;
6023 // Avoid null check if we know obj is not null.
6024 if (instruction->MustDoNullCheck()) {
6025 __ testl(obj, obj);
6026 __ j(kEqual, &done);
6027 }
6028
6029 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006030 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006031
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006032 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006034 if (cls.IsRegister()) {
6035 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6036 } else {
6037 DCHECK(cls.IsStackSlot()) << cls;
6038 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6039 }
6040 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006041
6042 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006043 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006044 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045
6046 // If the component type is not null (i.e. the object is indeed
6047 // an array), jump to label `check_non_primitive_component_type`
6048 // to further check that this component type is not a primitive
6049 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006051 __ j(kNotEqual, &check_non_primitive_component_type);
6052 // Otherwise, jump to the slow path to throw the exception.
6053 //
6054 // But before, move back the object's class into `temp` before
6055 // going into the slow path, as it has been overwritten in the
6056 // meantime.
6057 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006058 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006059 __ jmp(type_check_slow_path->GetEntryLabel());
6060
6061 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006063 __ j(kEqual, &done);
6064 // Same comment as above regarding `temp` and the slow path.
6065 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006066 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006067 __ jmp(type_check_slow_path->GetEntryLabel());
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
Calin Juravle98893e12015-10-02 21:05:03 +01006072 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006073 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006074 NearLabel done;
6075 // Avoid null check if we know obj is not null.
6076 if (instruction->MustDoNullCheck()) {
6077 __ testl(obj, obj);
6078 __ j(kEqual, &done);
6079 }
6080
6081 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006082 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006083
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006084 // We always go into the type check slow path for the unresolved
6085 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 //
6087 // We cannot directly call the CheckCast runtime entry point
6088 // without resorting to a type checking slow path here (i.e. by
6089 // calling InvokeRuntime directly), as it would require to
6090 // assign fixed registers for the inputs of this HInstanceOf
6091 // instruction (following the runtime calling convention), which
6092 // might be cluttered by the potential first read barrier
6093 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006094 //
6095 // TODO: Introduce a new runtime entry point taking the object
6096 // to test (instead of its class) as argument, and let it deal
6097 // with the read barrier issues. This will let us refactor this
6098 // case of the `switch` code as it was previously (with a direct
6099 // call to the runtime not using a type checking slow path).
6100 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006101 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006102 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006103 break;
6104 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006107}
6108
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006109void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6110 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006111 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006112 InvokeRuntimeCallingConvention calling_convention;
6113 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6114}
6115
6116void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006117 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006118 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006119 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006120 if (instruction->IsEnter()) {
6121 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6122 } else {
6123 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6124 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006125}
6126
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006127void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6128void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6129void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6130
6131void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6132 LocationSummary* locations =
6133 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6134 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6135 || instruction->GetResultType() == Primitive::kPrimLong);
6136 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006137 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006138 locations->SetOut(Location::SameAsFirstInput());
6139}
6140
6141void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6142 HandleBitwiseOperation(instruction);
6143}
6144
6145void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6146 HandleBitwiseOperation(instruction);
6147}
6148
6149void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6150 HandleBitwiseOperation(instruction);
6151}
6152
6153void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6154 LocationSummary* locations = instruction->GetLocations();
6155 Location first = locations->InAt(0);
6156 Location second = locations->InAt(1);
6157 DCHECK(first.Equals(locations->Out()));
6158
6159 if (instruction->GetResultType() == Primitive::kPrimInt) {
6160 if (second.IsRegister()) {
6161 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006162 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006163 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006164 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006165 } else {
6166 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006167 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006168 }
6169 } else if (second.IsConstant()) {
6170 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6171 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006172 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006173 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006174 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006175 } else {
6176 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006177 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006178 }
6179 } else {
6180 Address address(CpuRegister(RSP), second.GetStackIndex());
6181 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006182 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006183 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006184 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006185 } else {
6186 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006187 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006188 }
6189 }
6190 } else {
6191 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006192 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6193 bool second_is_constant = false;
6194 int64_t value = 0;
6195 if (second.IsConstant()) {
6196 second_is_constant = true;
6197 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006198 }
Mark Mendell40741f32015-04-20 22:10:34 -04006199 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006200
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006201 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006202 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006203 if (is_int32_value) {
6204 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6205 } else {
6206 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6207 }
6208 } else if (second.IsDoubleStackSlot()) {
6209 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006210 } else {
6211 __ andq(first_reg, second.AsRegister<CpuRegister>());
6212 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006213 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006214 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006215 if (is_int32_value) {
6216 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6217 } else {
6218 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6219 }
6220 } else if (second.IsDoubleStackSlot()) {
6221 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006222 } else {
6223 __ orq(first_reg, second.AsRegister<CpuRegister>());
6224 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006225 } else {
6226 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006227 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006228 if (is_int32_value) {
6229 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6230 } else {
6231 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6232 }
6233 } else if (second.IsDoubleStackSlot()) {
6234 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006235 } else {
6236 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6237 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006238 }
6239 }
6240}
6241
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006242void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6243 Location out,
6244 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006245 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006246 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6247 if (kEmitCompilerReadBarrier) {
6248 if (kUseBakerReadBarrier) {
6249 // Load with fast path based Baker's read barrier.
6250 // /* HeapReference<Object> */ out = *(out + offset)
6251 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006252 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006253 } else {
6254 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006255 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006256 // in the following move operation, as we will need it for the
6257 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006258 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006259 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006260 // /* HeapReference<Object> */ out = *(out + offset)
6261 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006262 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006263 }
6264 } else {
6265 // Plain load with no read barrier.
6266 // /* HeapReference<Object> */ out = *(out + offset)
6267 __ movl(out_reg, Address(out_reg, offset));
6268 __ MaybeUnpoisonHeapReference(out_reg);
6269 }
6270}
6271
6272void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6273 Location out,
6274 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006275 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006276 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6277 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6278 if (kEmitCompilerReadBarrier) {
6279 if (kUseBakerReadBarrier) {
6280 // Load with fast path based Baker's read barrier.
6281 // /* HeapReference<Object> */ out = *(obj + offset)
6282 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006283 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006284 } else {
6285 // Load with slow path based read barrier.
6286 // /* HeapReference<Object> */ out = *(obj + offset)
6287 __ movl(out_reg, Address(obj_reg, offset));
6288 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6289 }
6290 } else {
6291 // Plain load with no read barrier.
6292 // /* HeapReference<Object> */ out = *(obj + offset)
6293 __ movl(out_reg, Address(obj_reg, offset));
6294 __ MaybeUnpoisonHeapReference(out_reg);
6295 }
6296}
6297
6298void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6299 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006300 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006301 Label* fixup_label,
6302 bool requires_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006303 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006304 if (requires_read_barrier) {
6305 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006306 if (kUseBakerReadBarrier) {
6307 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6308 // Baker's read barrier are used:
6309 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006310 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006311 // if (Thread::Current()->GetIsGcMarking()) {
6312 // root = ReadBarrier::Mark(root)
6313 // }
6314
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006315 // /* GcRoot<mirror::Object> */ root = *address
6316 __ movl(root_reg, address);
6317 if (fixup_label != nullptr) {
6318 __ Bind(fixup_label);
6319 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006320 static_assert(
6321 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6322 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6323 "have different sizes.");
6324 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6325 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6326 "have different sizes.");
6327
Vladimir Marko953437b2016-08-24 08:30:46 +00006328 // Slow path marking the GC root `root`.
6329 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6330 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006331 codegen_->AddSlowPath(slow_path);
6332
Andreas Gampe542451c2016-07-26 09:02:02 -07006333 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006334 /* no_rip */ true),
6335 Immediate(0));
6336 __ j(kNotEqual, slow_path->GetEntryLabel());
6337 __ Bind(slow_path->GetExitLabel());
6338 } else {
6339 // GC root loaded through a slow path for read barriers other
6340 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006341 // /* GcRoot<mirror::Object>* */ root = address
6342 __ leaq(root_reg, address);
6343 if (fixup_label != nullptr) {
6344 __ Bind(fixup_label);
6345 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006346 // /* mirror::Object* */ root = root->Read()
6347 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6348 }
6349 } else {
6350 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351 // /* GcRoot<mirror::Object> */ root = *address
6352 __ movl(root_reg, address);
6353 if (fixup_label != nullptr) {
6354 __ Bind(fixup_label);
6355 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006356 // Note that GC roots are not affected by heap poisoning, thus we
6357 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006358 }
6359}
6360
6361void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6362 Location ref,
6363 CpuRegister obj,
6364 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006365 bool needs_null_check) {
6366 DCHECK(kEmitCompilerReadBarrier);
6367 DCHECK(kUseBakerReadBarrier);
6368
6369 // /* HeapReference<Object> */ ref = *(obj + offset)
6370 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006371 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006372}
6373
6374void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6375 Location ref,
6376 CpuRegister obj,
6377 uint32_t data_offset,
6378 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006379 bool needs_null_check) {
6380 DCHECK(kEmitCompilerReadBarrier);
6381 DCHECK(kUseBakerReadBarrier);
6382
Roland Levillain3d312422016-06-23 13:53:42 +01006383 static_assert(
6384 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6385 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006386 // /* HeapReference<Object> */ ref =
6387 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6388 Address src = index.IsConstant() ?
6389 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6390 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006391 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006392}
6393
6394void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6395 Location ref,
6396 CpuRegister obj,
6397 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006398 bool needs_null_check) {
6399 DCHECK(kEmitCompilerReadBarrier);
6400 DCHECK(kUseBakerReadBarrier);
6401
6402 // In slow path based read barriers, the read barrier call is
6403 // inserted after the original load. However, in fast path based
6404 // Baker's read barriers, we need to perform the load of
6405 // mirror::Object::monitor_ *before* the original reference load.
6406 // This load-load ordering is required by the read barrier.
6407 // The fast path/slow path (for Baker's algorithm) should look like:
6408 //
6409 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6410 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6411 // HeapReference<Object> ref = *src; // Original reference load.
6412 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6413 // if (is_gray) {
6414 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6415 // }
6416 //
6417 // Note: the original implementation in ReadBarrier::Barrier is
6418 // slightly more complex as:
6419 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006420 // the high-bits of rb_state, which are expected to be all zeroes
6421 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6422 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006423 // - it performs additional checks that we do not do here for
6424 // performance reasons.
6425
6426 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006427 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6428
Vladimir Marko953437b2016-08-24 08:30:46 +00006429 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6430 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6431 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6432 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6433 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6434 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6435 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6436
6437 // if (rb_state == ReadBarrier::gray_ptr_)
6438 // ref = ReadBarrier::Mark(ref);
6439 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6440 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006441 if (needs_null_check) {
6442 MaybeRecordImplicitNullCheck(instruction);
6443 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006444
6445 // Load fence to prevent load-load reordering.
6446 // Note that this is a no-op, thanks to the x86-64 memory model.
6447 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6448
6449 // The actual reference load.
6450 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006451 __ movl(ref_reg, src); // Flags are unaffected.
6452
6453 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6454 // Slow path marking the object `ref` when it is gray.
6455 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6456 instruction, ref, /* unpoison */ true);
6457 AddSlowPath(slow_path);
6458
6459 // We have done the "if" of the gray bit check above, now branch based on the flags.
6460 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006461
6462 // Object* ref = ref_addr->AsMirrorPtr()
6463 __ MaybeUnpoisonHeapReference(ref_reg);
6464
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006465 __ Bind(slow_path->GetExitLabel());
6466}
6467
6468void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6469 Location out,
6470 Location ref,
6471 Location obj,
6472 uint32_t offset,
6473 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474 DCHECK(kEmitCompilerReadBarrier);
6475
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006476 // Insert a slow path based read barrier *after* the reference load.
6477 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006478 // If heap poisoning is enabled, the unpoisoning of the loaded
6479 // reference will be carried out by the runtime within the slow
6480 // path.
6481 //
6482 // Note that `ref` currently does not get unpoisoned (when heap
6483 // poisoning is enabled), which is alright as the `ref` argument is
6484 // not used by the artReadBarrierSlow entry point.
6485 //
6486 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6487 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6488 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6489 AddSlowPath(slow_path);
6490
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 __ jmp(slow_path->GetEntryLabel());
6492 __ Bind(slow_path->GetExitLabel());
6493}
6494
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006495void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6496 Location out,
6497 Location ref,
6498 Location obj,
6499 uint32_t offset,
6500 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006501 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006502 // Baker's read barriers shall be handled by the fast path
6503 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6504 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 // If heap poisoning is enabled, unpoisoning will be taken care of
6506 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006507 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 } else if (kPoisonHeapReferences) {
6509 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6510 }
6511}
6512
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006513void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6514 Location out,
6515 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 DCHECK(kEmitCompilerReadBarrier);
6517
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006518 // Insert a slow path based read barrier *after* the GC root load.
6519 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 // Note that GC roots are not affected by heap poisoning, so we do
6521 // not need to do anything special for this here.
6522 SlowPathCode* slow_path =
6523 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6524 AddSlowPath(slow_path);
6525
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526 __ jmp(slow_path->GetEntryLabel());
6527 __ Bind(slow_path->GetExitLabel());
6528}
6529
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006530void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006531 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006532 LOG(FATAL) << "Unreachable";
6533}
6534
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006535void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006536 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006537 LOG(FATAL) << "Unreachable";
6538}
6539
Mark Mendellfe57faa2015-09-18 09:26:15 -04006540// Simple implementation of packed switch - generate cascaded compare/jumps.
6541void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6542 LocationSummary* locations =
6543 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6544 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006545 locations->AddTemp(Location::RequiresRegister());
6546 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006547}
6548
6549void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6550 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006551 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006552 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006553 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6554 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6555 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006556 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6557
6558 // Should we generate smaller inline compare/jumps?
6559 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6560 // Figure out the correct compare values and jump conditions.
6561 // Handle the first compare/branch as a special case because it might
6562 // jump to the default case.
6563 DCHECK_GT(num_entries, 2u);
6564 Condition first_condition;
6565 uint32_t index;
6566 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6567 if (lower_bound != 0) {
6568 first_condition = kLess;
6569 __ cmpl(value_reg_in, Immediate(lower_bound));
6570 __ j(first_condition, codegen_->GetLabelOf(default_block));
6571 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6572
6573 index = 1;
6574 } else {
6575 // Handle all the compare/jumps below.
6576 first_condition = kBelow;
6577 index = 0;
6578 }
6579
6580 // Handle the rest of the compare/jumps.
6581 for (; index + 1 < num_entries; index += 2) {
6582 int32_t compare_to_value = lower_bound + index + 1;
6583 __ cmpl(value_reg_in, Immediate(compare_to_value));
6584 // Jump to successors[index] if value < case_value[index].
6585 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6586 // Jump to successors[index + 1] if value == case_value[index + 1].
6587 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6588 }
6589
6590 if (index != num_entries) {
6591 // There are an odd number of entries. Handle the last one.
6592 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006593 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006594 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6595 }
6596
6597 // And the default for any other value.
6598 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6599 __ jmp(codegen_->GetLabelOf(default_block));
6600 }
6601 return;
6602 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006603
6604 // Remove the bias, if needed.
6605 Register value_reg_out = value_reg_in.AsRegister();
6606 if (lower_bound != 0) {
6607 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6608 value_reg_out = temp_reg.AsRegister();
6609 }
6610 CpuRegister value_reg(value_reg_out);
6611
6612 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006613 __ cmpl(value_reg, Immediate(num_entries - 1));
6614 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006615
Mark Mendell9c86b482015-09-18 13:36:07 -04006616 // We are in the range of the table.
6617 // Load the address of the jump table in the constant area.
6618 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006619
Mark Mendell9c86b482015-09-18 13:36:07 -04006620 // Load the (signed) offset from the jump table.
6621 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6622
6623 // Add the offset to the address of the table base.
6624 __ addq(temp_reg, base_reg);
6625
6626 // And jump.
6627 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006628}
6629
Aart Bikc5d47542016-01-27 17:00:35 -08006630void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6631 if (value == 0) {
6632 __ xorl(dest, dest);
6633 } else {
6634 __ movl(dest, Immediate(value));
6635 }
6636}
6637
Mark Mendell92e83bf2015-05-07 11:25:03 -04006638void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6639 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006640 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006641 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006642 } else if (IsUint<32>(value)) {
6643 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006644 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6645 } else {
6646 __ movq(dest, Immediate(value));
6647 }
6648}
6649
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006650void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6651 if (value == 0) {
6652 __ xorps(dest, dest);
6653 } else {
6654 __ movss(dest, LiteralInt32Address(value));
6655 }
6656}
6657
6658void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6659 if (value == 0) {
6660 __ xorpd(dest, dest);
6661 } else {
6662 __ movsd(dest, LiteralInt64Address(value));
6663 }
6664}
6665
6666void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6667 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6668}
6669
6670void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6671 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6672}
6673
Aart Bika19616e2016-02-01 18:57:58 -08006674void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6675 if (value == 0) {
6676 __ testl(dest, dest);
6677 } else {
6678 __ cmpl(dest, Immediate(value));
6679 }
6680}
6681
6682void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6683 if (IsInt<32>(value)) {
6684 if (value == 0) {
6685 __ testq(dest, dest);
6686 } else {
6687 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6688 }
6689 } else {
6690 // Value won't fit in an int.
6691 __ cmpq(dest, LiteralInt64Address(value));
6692 }
6693}
6694
Mark Mendellcfa410b2015-05-25 16:02:44 -04006695void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6696 DCHECK(dest.IsDoubleStackSlot());
6697 if (IsInt<32>(value)) {
6698 // Can move directly as an int32 constant.
6699 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6700 Immediate(static_cast<int32_t>(value)));
6701 } else {
6702 Load64BitValue(CpuRegister(TMP), value);
6703 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6704 }
6705}
6706
Mark Mendell9c86b482015-09-18 13:36:07 -04006707/**
6708 * Class to handle late fixup of offsets into constant area.
6709 */
6710class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6711 public:
6712 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6713 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6714
6715 protected:
6716 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6717
6718 CodeGeneratorX86_64* codegen_;
6719
6720 private:
6721 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6722 // Patch the correct offset for the instruction. We use the address of the
6723 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6724 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6725 int32_t relative_position = constant_offset - pos;
6726
6727 // Patch in the right value.
6728 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6729 }
6730
6731 // Location in constant area that the fixup refers to.
6732 size_t offset_into_constant_area_;
6733};
6734
6735/**
6736 t * Class to handle late fixup of offsets to a jump table that will be created in the
6737 * constant area.
6738 */
6739class JumpTableRIPFixup : public RIPFixup {
6740 public:
6741 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6742 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6743
6744 void CreateJumpTable() {
6745 X86_64Assembler* assembler = codegen_->GetAssembler();
6746
6747 // Ensure that the reference to the jump table has the correct offset.
6748 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6749 SetOffset(offset_in_constant_table);
6750
6751 // Compute the offset from the start of the function to this jump table.
6752 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6753
6754 // Populate the jump table with the correct values for the jump table.
6755 int32_t num_entries = switch_instr_->GetNumEntries();
6756 HBasicBlock* block = switch_instr_->GetBlock();
6757 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6758 // The value that we want is the target offset - the position of the table.
6759 for (int32_t i = 0; i < num_entries; i++) {
6760 HBasicBlock* b = successors[i];
6761 Label* l = codegen_->GetLabelOf(b);
6762 DCHECK(l->IsBound());
6763 int32_t offset_to_block = l->Position() - current_table_offset;
6764 assembler->AppendInt32(offset_to_block);
6765 }
6766 }
6767
6768 private:
6769 const HPackedSwitch* switch_instr_;
6770};
6771
Mark Mendellf55c3e02015-03-26 21:07:46 -04006772void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6773 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006774 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006775 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6776 // 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 -04006777 assembler->Align(4, 0);
6778 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006779
6780 // Populate any jump tables.
6781 for (auto jump_table : fixups_to_jump_tables_) {
6782 jump_table->CreateJumpTable();
6783 }
6784
6785 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006786 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006787 }
6788
6789 // And finish up.
6790 CodeGenerator::Finalize(allocator);
6791}
6792
Mark Mendellf55c3e02015-03-26 21:07:46 -04006793Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6794 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6795 return Address::RIP(fixup);
6796}
6797
6798Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6799 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6800 return Address::RIP(fixup);
6801}
6802
6803Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6804 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6805 return Address::RIP(fixup);
6806}
6807
6808Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6809 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6810 return Address::RIP(fixup);
6811}
6812
Andreas Gampe85b62f22015-09-09 13:15:38 -07006813// TODO: trg as memory.
6814void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6815 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006816 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006817 return;
6818 }
6819
6820 DCHECK_NE(type, Primitive::kPrimVoid);
6821
6822 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6823 if (trg.Equals(return_loc)) {
6824 return;
6825 }
6826
6827 // Let the parallel move resolver take care of all of this.
6828 HParallelMove parallel_move(GetGraph()->GetArena());
6829 parallel_move.AddMove(return_loc, trg, type, nullptr);
6830 GetMoveResolver()->EmitNativeCode(&parallel_move);
6831}
6832
Mark Mendell9c86b482015-09-18 13:36:07 -04006833Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6834 // Create a fixup to be used to create and address the jump table.
6835 JumpTableRIPFixup* table_fixup =
6836 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6837
6838 // We have to populate the jump tables.
6839 fixups_to_jump_tables_.push_back(table_fixup);
6840 return Address::RIP(table_fixup);
6841}
6842
Mark Mendellea5af682015-10-22 17:35:49 -04006843void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6844 const Address& addr_high,
6845 int64_t v,
6846 HInstruction* instruction) {
6847 if (IsInt<32>(v)) {
6848 int32_t v_32 = v;
6849 __ movq(addr_low, Immediate(v_32));
6850 MaybeRecordImplicitNullCheck(instruction);
6851 } else {
6852 // Didn't fit in a register. Do it in pieces.
6853 int32_t low_v = Low32Bits(v);
6854 int32_t high_v = High32Bits(v);
6855 __ movl(addr_low, Immediate(low_v));
6856 MaybeRecordImplicitNullCheck(instruction);
6857 __ movl(addr_high, Immediate(high_v));
6858 }
6859}
6860
Roland Levillain4d027112015-07-01 15:41:14 +01006861#undef __
6862
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006863} // namespace x86_64
6864} // namespace art