blob: 9a9cb26ccb6413fbc0d262d737d3c9a9220c339e [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 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000069 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
70 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 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000095 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
96 instruction_,
97 instruction_->GetDexPc(),
98 this);
Roland Levillain888d0672015-11-23 18:53:50 +000099 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000100 }
101
Alexandre Rames8158f282015-08-07 10:26:17 +0100102 bool IsFatal() const OVERRIDE { return true; }
103
Alexandre Rames9931f312015-06-19 14:47:01 +0100104 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
105
Calin Juravled0d48522014-11-04 16:40:20 +0000106 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
108};
109
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
113 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000114
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000116 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 if (is_div_) {
119 __ negl(cpu_reg_);
120 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400121 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 }
123
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 } else {
125 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negq(cpu_reg_);
128 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400129 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000130 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 }
Calin Juravled0d48522014-11-04 16:40:20 +0000132 __ jmp(GetExitLabel());
133 }
134
Alexandre Rames9931f312015-06-19 14:47:01 +0100135 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
136
Calin Juravled0d48522014-11-04 16:40:20 +0000137 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000139 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000140 const bool is_div_;
141 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000142};
143
Andreas Gampe85b62f22015-09-09 13:15:38 -0700144class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100146 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000150 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 __ Bind(GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000152 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
153 instruction_,
154 instruction_->GetDexPc(),
155 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000156 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 if (successor_ == nullptr) {
158 __ jmp(GetReturnLabel());
159 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000160 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162 }
163
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100164 Label* GetReturnLabel() {
165 DCHECK(successor_ == nullptr);
166 return &return_label_;
167 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100169 HBasicBlock* GetSuccessor() const {
170 return successor_;
171 }
172
Alexandre Rames9931f312015-06-19 14:47:01 +0100173 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
174
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 Label return_label_;
178
179 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
180};
181
Andreas Gampe85b62f22015-09-09 13:15:38 -0700182class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100184 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000185 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000187 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100188 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000189 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000191 if (instruction_->CanThrowIntoCatchBlock()) {
192 // Live registers will be restored in the catch block if caught.
193 SaveLiveRegisters(codegen, instruction_->GetLocations());
194 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400195 // Are we using an array length from memory?
196 HInstruction* array_length = instruction_->InputAt(1);
197 Location length_loc = locations->InAt(1);
198 InvokeRuntimeCallingConvention calling_convention;
199 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
200 // Load the array length into our temporary.
201 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
202 Location array_loc = array_length->GetLocations()->InAt(0);
203 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
204 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
205 // Check for conflicts with index.
206 if (length_loc.Equals(locations->InAt(0))) {
207 // We know we aren't using parameter 2.
208 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
209 }
210 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
211 }
212
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000213 // We're moving two locations to locations that could overlap, so we need a parallel
214 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000215 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100216 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000217 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100218 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400219 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100220 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
221 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100222 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
223 ? QUICK_ENTRY_POINT(pThrowStringBounds)
224 : QUICK_ENTRY_POINT(pThrowArrayBounds);
225 x86_64_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000226 instruction_,
227 instruction_->GetDexPc(),
228 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100229 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000230 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100231 }
232
Alexandre Rames8158f282015-08-07 10:26:17 +0100233 bool IsFatal() const OVERRIDE { return true; }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
236
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100237 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100238 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
239};
240
Andreas Gampe85b62f22015-09-09 13:15:38 -0700241class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100242 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 LoadClassSlowPathX86_64(HLoadClass* cls,
244 HInstruction* at,
245 uint32_t dex_pc,
246 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000247 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
249 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000253 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100254 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100258 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000260 x86_64_codegen->InvokeRuntime(do_clinit_ ?
261 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
262 QUICK_ENTRY_POINT(pInitializeType),
263 at_,
264 dex_pc_,
265 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000266 if (do_clinit_) {
267 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
268 } else {
269 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
270 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 if (out.IsValid()) {
275 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000276 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 }
278
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100280 __ jmp(GetExitLabel());
281 }
282
Alexandre Rames9931f312015-06-19 14:47:01 +0100283 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
284
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 // The class this slow path will load.
287 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000289 // The instruction where this slow path is happening.
290 // (Might be the load class or an initialization check).
291 HInstruction* const at_;
292
293 // The dex PC of `at_`.
294 const uint32_t dex_pc_;
295
296 // Whether to initialize the class.
297 const bool do_clinit_;
298
299 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100300};
301
Andreas Gampe85b62f22015-09-09 13:15:38 -0700302class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000303 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000304 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000305
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000307 LocationSummary* locations = instruction_->GetLocations();
308 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
309
Roland Levillain0d5a2812015-11-13 10:07:31 +0000310 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000311 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000313
314 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000315 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
316 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000317 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
318 instruction_,
319 instruction_->GetDexPc(),
320 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000321 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000322 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000323 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000324 __ jmp(GetExitLabel());
325 }
326
Alexandre Rames9931f312015-06-19 14:47:01 +0100327 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
328
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000329 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000330 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
331};
332
Andreas Gampe85b62f22015-09-09 13:15:38 -0700333class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000336 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000338 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100340 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
341 : locations->Out();
342 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 DCHECK(instruction_->IsCheckCast()
344 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
Roland Levillain0d5a2812015-11-13 10:07:31 +0000346 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000349 if (!is_fatal_) {
350 SaveLiveRegisters(codegen, locations);
351 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352
353 // We're moving two locations to locations that could overlap, so we need a parallel
354 // move resolver.
355 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000356 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100359 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000363
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000365 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
366 instruction_,
367 dex_pc,
368 this);
369 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700370 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000371 } else {
372 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000373 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
374 instruction_,
375 dex_pc,
376 this);
377 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000378 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000379
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000380 if (!is_fatal_) {
381 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000382 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000383 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000384
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 RestoreLiveRegisters(codegen, locations);
386 __ jmp(GetExitLabel());
387 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000388 }
389
Alexandre Rames9931f312015-06-19 14:47:01 +0100390 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
391
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000392 bool IsFatal() const OVERRIDE { return is_fatal_; }
393
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000394 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000395 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000396
397 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
398};
399
Andreas Gampe85b62f22015-09-09 13:15:38 -0700400class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700401 public:
Aart Bik42249c32016-01-07 15:33:50 -0800402 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000406 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000409 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800410 instruction_,
411 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000412 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000413 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700414 }
415
Alexandre Rames9931f312015-06-19 14:47:01 +0100416 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
417
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700418 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700419 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
420};
421
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422class ArraySetSlowPathX86_64 : public SlowPathCode {
423 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000424 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425
426 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
427 LocationSummary* locations = instruction_->GetLocations();
428 __ Bind(GetEntryLabel());
429 SaveLiveRegisters(codegen, locations);
430
431 InvokeRuntimeCallingConvention calling_convention;
432 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
433 parallel_move.AddMove(
434 locations->InAt(0),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
436 Primitive::kPrimNot,
437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(1),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
441 Primitive::kPrimInt,
442 nullptr);
443 parallel_move.AddMove(
444 locations->InAt(2),
445 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
446 Primitive::kPrimNot,
447 nullptr);
448 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
449
Roland Levillain0d5a2812015-11-13 10:07:31 +0000450 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
451 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
452 instruction_,
453 instruction_->GetDexPc(),
454 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000455 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100456 RestoreLiveRegisters(codegen, locations);
457 __ jmp(GetExitLabel());
458 }
459
460 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
461
462 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100463 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
464};
465
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000466// Slow path marking an object during a read barrier.
467class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
468 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000469 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj, bool unpoison)
470 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000471 DCHECK(kEmitCompilerReadBarrier);
472 }
473
474 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
475
476 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
477 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100478 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000479 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100480 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000481 DCHECK(instruction_->IsInstanceFieldGet() ||
482 instruction_->IsStaticFieldGet() ||
483 instruction_->IsArrayGet() ||
484 instruction_->IsLoadClass() ||
485 instruction_->IsLoadString() ||
486 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100487 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100488 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
489 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000490 << "Unexpected instruction in read barrier marking slow path: "
491 << instruction_->DebugName();
492
493 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000494 if (unpoison_) {
495 // Object* ref = ref_addr->AsMirrorPtr()
496 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
497 }
Roland Levillain4359e612016-07-20 11:32:19 +0100498 // No need to save live registers; it's taken care of by the
499 // entrypoint. Also, there is no need to update the stack mask,
500 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000501 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100502 DCHECK_NE(reg, RSP);
503 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
504 // "Compact" slow path, saving two moves.
505 //
506 // Instead of using the standard runtime calling convention (input
507 // and output in R0):
508 //
509 // RDI <- obj
510 // RAX <- ReadBarrierMark(RDI)
511 // obj <- RAX
512 //
513 // we just use rX (the register holding `obj`) as input and output
514 // of a dedicated entrypoint:
515 //
516 // rX <- ReadBarrierMarkRegX(rX)
517 //
518 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700519 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100520 // This runtime call does not require a stack map.
521 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000522 __ jmp(GetExitLabel());
523 }
524
525 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000526 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000527 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000528
529 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
530};
531
Roland Levillain0d5a2812015-11-13 10:07:31 +0000532// Slow path generating a read barrier for a heap reference.
533class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
534 public:
535 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
536 Location out,
537 Location ref,
538 Location obj,
539 uint32_t offset,
540 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000541 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000542 out_(out),
543 ref_(ref),
544 obj_(obj),
545 offset_(offset),
546 index_(index) {
547 DCHECK(kEmitCompilerReadBarrier);
548 // If `obj` is equal to `out` or `ref`, it means the initial
549 // object has been overwritten by (or after) the heap object
550 // reference load to be instrumented, e.g.:
551 //
552 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000553 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000554 //
555 // In that case, we have lost the information about the original
556 // object, and the emitted read barrier cannot work properly.
557 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
558 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
559}
560
561 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
562 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
563 LocationSummary* locations = instruction_->GetLocations();
564 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
565 DCHECK(locations->CanCall());
566 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100567 DCHECK(instruction_->IsInstanceFieldGet() ||
568 instruction_->IsStaticFieldGet() ||
569 instruction_->IsArrayGet() ||
570 instruction_->IsInstanceOf() ||
571 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100572 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000573 << "Unexpected instruction in read barrier for heap reference slow path: "
574 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000575
576 __ Bind(GetEntryLabel());
577 SaveLiveRegisters(codegen, locations);
578
579 // We may have to change the index's value, but as `index_` is a
580 // constant member (like other "inputs" of this slow path),
581 // introduce a copy of it, `index`.
582 Location index = index_;
583 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100584 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000585 if (instruction_->IsArrayGet()) {
586 // Compute real offset and store it in index_.
587 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
588 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
589 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
590 // We are about to change the value of `index_reg` (see the
591 // calls to art::x86_64::X86_64Assembler::shll and
592 // art::x86_64::X86_64Assembler::AddImmediate below), but it
593 // has not been saved by the previous call to
594 // art::SlowPathCode::SaveLiveRegisters, as it is a
595 // callee-save register --
596 // art::SlowPathCode::SaveLiveRegisters does not consider
597 // callee-save registers, as it has been designed with the
598 // assumption that callee-save registers are supposed to be
599 // handled by the called function. So, as a callee-save
600 // register, `index_reg` _would_ eventually be saved onto
601 // the stack, but it would be too late: we would have
602 // changed its value earlier. Therefore, we manually save
603 // it here into another freely available register,
604 // `free_reg`, chosen of course among the caller-save
605 // registers (as a callee-save `free_reg` register would
606 // exhibit the same problem).
607 //
608 // Note we could have requested a temporary register from
609 // the register allocator instead; but we prefer not to, as
610 // this is a slow path, and we know we can find a
611 // caller-save register that is available.
612 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
613 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
614 index_reg = free_reg;
615 index = Location::RegisterLocation(index_reg);
616 } else {
617 // The initial register stored in `index_` has already been
618 // saved in the call to art::SlowPathCode::SaveLiveRegisters
619 // (as it is not a callee-save register), so we can freely
620 // use it.
621 }
622 // Shifting the index value contained in `index_reg` by the
623 // scale factor (2) cannot overflow in practice, as the
624 // runtime is unable to allocate object arrays with a size
625 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
626 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
627 static_assert(
628 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
629 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
630 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
631 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100632 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
633 // intrinsics, `index_` is not shifted by a scale factor of 2
634 // (as in the case of ArrayGet), as it is actually an offset
635 // to an object field within an object.
636 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000637 DCHECK(instruction_->GetLocations()->Intrinsified());
638 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
639 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
640 << instruction_->AsInvoke()->GetIntrinsic();
641 DCHECK_EQ(offset_, 0U);
642 DCHECK(index_.IsRegister());
643 }
644 }
645
646 // We're moving two or three locations to locations that could
647 // overlap, so we need a parallel move resolver.
648 InvokeRuntimeCallingConvention calling_convention;
649 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
650 parallel_move.AddMove(ref_,
651 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
652 Primitive::kPrimNot,
653 nullptr);
654 parallel_move.AddMove(obj_,
655 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
656 Primitive::kPrimNot,
657 nullptr);
658 if (index.IsValid()) {
659 parallel_move.AddMove(index,
660 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
661 Primitive::kPrimInt,
662 nullptr);
663 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
664 } else {
665 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
666 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
667 }
668 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
669 instruction_,
670 instruction_->GetDexPc(),
671 this);
672 CheckEntrypointTypes<
673 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
674 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
675
676 RestoreLiveRegisters(codegen, locations);
677 __ jmp(GetExitLabel());
678 }
679
680 const char* GetDescription() const OVERRIDE {
681 return "ReadBarrierForHeapReferenceSlowPathX86_64";
682 }
683
684 private:
685 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
686 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
687 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
688 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
689 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
690 return static_cast<CpuRegister>(i);
691 }
692 }
693 // We shall never fail to find a free caller-save register, as
694 // there are more than two core caller-save registers on x86-64
695 // (meaning it is possible to find one which is different from
696 // `ref` and `obj`).
697 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
698 LOG(FATAL) << "Could not find a free caller-save register";
699 UNREACHABLE();
700 }
701
Roland Levillain0d5a2812015-11-13 10:07:31 +0000702 const Location out_;
703 const Location ref_;
704 const Location obj_;
705 const uint32_t offset_;
706 // An additional location containing an index to an array.
707 // Only used for HArrayGet and the UnsafeGetObject &
708 // UnsafeGetObjectVolatile intrinsics.
709 const Location index_;
710
711 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
712};
713
714// Slow path generating a read barrier for a GC root.
715class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
716 public:
717 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000718 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000719 DCHECK(kEmitCompilerReadBarrier);
720 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000721
722 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
723 LocationSummary* locations = instruction_->GetLocations();
724 DCHECK(locations->CanCall());
725 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000726 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
727 << "Unexpected instruction in read barrier for GC root slow path: "
728 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000729
730 __ Bind(GetEntryLabel());
731 SaveLiveRegisters(codegen, locations);
732
733 InvokeRuntimeCallingConvention calling_convention;
734 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
735 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
736 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
737 instruction_,
738 instruction_->GetDexPc(),
739 this);
740 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
741 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
742
743 RestoreLiveRegisters(codegen, locations);
744 __ jmp(GetExitLabel());
745 }
746
747 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
748
749 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000750 const Location out_;
751 const Location root_;
752
753 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
754};
755
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100756#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100757// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
758#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100759
Roland Levillain4fa13f62015-07-06 18:11:54 +0100760inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700761 switch (cond) {
762 case kCondEQ: return kEqual;
763 case kCondNE: return kNotEqual;
764 case kCondLT: return kLess;
765 case kCondLE: return kLessEqual;
766 case kCondGT: return kGreater;
767 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700768 case kCondB: return kBelow;
769 case kCondBE: return kBelowEqual;
770 case kCondA: return kAbove;
771 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700772 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100773 LOG(FATAL) << "Unreachable";
774 UNREACHABLE();
775}
776
Aart Bike9f37602015-10-09 11:15:55 -0700777// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100778inline Condition X86_64FPCondition(IfCondition cond) {
779 switch (cond) {
780 case kCondEQ: return kEqual;
781 case kCondNE: return kNotEqual;
782 case kCondLT: return kBelow;
783 case kCondLE: return kBelowEqual;
784 case kCondGT: return kAbove;
785 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700786 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100787 };
788 LOG(FATAL) << "Unreachable";
789 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700790}
791
Vladimir Markodc151b22015-10-15 18:02:30 +0100792HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
793 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
794 MethodReference target_method ATTRIBUTE_UNUSED) {
795 switch (desired_dispatch_info.code_ptr_location) {
796 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
797 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
798 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
799 return HInvokeStaticOrDirect::DispatchInfo {
800 desired_dispatch_info.method_load_kind,
801 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
802 desired_dispatch_info.method_load_data,
803 0u
804 };
805 default:
806 return desired_dispatch_info;
807 }
808}
809
Serguei Katkov288c7a82016-05-16 11:53:15 +0600810Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
811 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800812 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000813 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
814 switch (invoke->GetMethodLoadKind()) {
815 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
816 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000817 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000818 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000819 break;
820 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000821 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000822 break;
823 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
824 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
825 break;
826 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
827 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
828 method_patches_.emplace_back(invoke->GetTargetMethod());
829 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
830 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000831 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000832 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000833 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000834 // Bind a new fixup label at the end of the "movl" insn.
835 uint32_t offset = invoke->GetDexCacheArrayOffset();
836 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000837 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000838 }
Vladimir Marko58155012015-08-19 12:49:41 +0000839 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000840 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000841 Register method_reg;
842 CpuRegister reg = temp.AsRegister<CpuRegister>();
843 if (current_method.IsRegister()) {
844 method_reg = current_method.AsRegister<Register>();
845 } else {
846 DCHECK(invoke->GetLocations()->Intrinsified());
847 DCHECK(!current_method.IsValid());
848 method_reg = reg.AsRegister();
849 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
850 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000851 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100852 __ movq(reg,
853 Address(CpuRegister(method_reg),
854 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100855 // temp = temp[index_in_cache];
856 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
857 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000858 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
859 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100860 }
Vladimir Marko58155012015-08-19 12:49:41 +0000861 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600862 return callee_method;
863}
864
865void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
866 Location temp) {
867 // All registers are assumed to be correctly set up.
868 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000869
870 switch (invoke->GetCodePtrLocation()) {
871 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
872 __ call(&frame_entry_label_);
873 break;
874 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
875 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
876 Label* label = &relative_call_patches_.back().label;
877 __ call(label); // Bind to the patch label, override at link time.
878 __ Bind(label); // Bind the label at the end of the "call" insn.
879 break;
880 }
881 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
882 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100883 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
884 LOG(FATAL) << "Unsupported";
885 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000886 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
887 // (callee_method + offset_of_quick_compiled_code)()
888 __ call(Address(callee_method.AsRegister<CpuRegister>(),
889 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700890 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000891 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000892 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800893
894 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800895}
896
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000897void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
898 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
899 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
900 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000901
902 // Use the calling convention instead of the location of the receiver, as
903 // intrinsics may have put the receiver in a different register. In the intrinsics
904 // slow path, the arguments have been moved to the right place, so here we are
905 // guaranteed that the receiver is the first register of the calling convention.
906 InvokeDexCallingConvention calling_convention;
907 Register receiver = calling_convention.GetRegisterAt(0);
908
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000909 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000910 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000911 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000912 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000913 // Instead of simply (possibly) unpoisoning `temp` here, we should
914 // emit a read barrier for the previous class reference load.
915 // However this is not required in practice, as this is an
916 // intermediate/temporary reference and because the current
917 // concurrent copying collector keeps the from-space memory
918 // intact/accessible until the end of the marking phase (the
919 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000920 __ MaybeUnpoisonHeapReference(temp);
921 // temp = temp->GetMethodAt(method_offset);
922 __ movq(temp, Address(temp, method_offset));
923 // call temp->GetEntryPoint();
924 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700925 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000926}
927
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000928void CodeGeneratorX86_64::RecordSimplePatch() {
929 if (GetCompilerOptions().GetIncludePatchInformation()) {
930 simple_patches_.emplace_back();
931 __ Bind(&simple_patches_.back());
932 }
933}
934
935void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
936 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
937 __ Bind(&string_patches_.back().label);
938}
939
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100940void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
941 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
942 __ Bind(&type_patches_.back().label);
943}
944
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000945Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
946 uint32_t element_offset) {
947 // Add a patch entry and return the label.
948 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
949 return &pc_relative_dex_cache_patches_.back().label;
950}
951
Vladimir Marko58155012015-08-19 12:49:41 +0000952void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
953 DCHECK(linker_patches->empty());
954 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000955 method_patches_.size() +
956 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000957 pc_relative_dex_cache_patches_.size() +
958 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100959 string_patches_.size() +
960 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000961 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000962 // The label points to the end of the "movl" insn but the literal offset for method
963 // patch needs to point to the embedded constant which occupies the last 4 bytes.
964 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000965 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000966 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000967 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
968 info.target_method.dex_file,
969 info.target_method.dex_method_index));
970 }
971 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000972 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000973 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
974 info.target_method.dex_file,
975 info.target_method.dex_method_index));
976 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000977 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
978 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000979 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
980 &info.target_dex_file,
981 info.label.Position(),
982 info.element_offset));
983 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000984 for (const Label& label : simple_patches_) {
985 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
986 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
987 }
988 for (const StringPatchInfo<Label>& info : string_patches_) {
989 // These are always PC-relative, see GetSupportedLoadStringKind().
990 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
991 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
992 &info.dex_file,
993 info.label.Position(),
994 info.string_index));
995 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100996 for (const TypePatchInfo<Label>& info : type_patches_) {
997 // These are always PC-relative, see GetSupportedLoadClassKind().
998 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
999 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
1000 &info.dex_file,
1001 info.label.Position(),
1002 info.type_index));
1003 }
Vladimir Marko58155012015-08-19 12:49:41 +00001004}
1005
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001007 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001008}
1009
1010void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001011 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001012}
1013
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001014size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1015 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1016 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001017}
1018
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001019size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1020 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1021 return kX86_64WordSize;
1022}
1023
1024size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1025 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1026 return kX86_64WordSize;
1027}
1028
1029size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1030 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1031 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001032}
1033
Calin Juravle175dc732015-08-25 15:42:32 +01001034void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1035 HInstruction* instruction,
1036 uint32_t dex_pc,
1037 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001038 InvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001039 instruction,
1040 dex_pc,
1041 slow_path);
1042}
1043
1044void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +01001045 HInstruction* instruction,
1046 uint32_t dex_pc,
1047 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001048 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001049 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +01001050 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +01001051}
1052
Roland Levillaindec8f632016-07-22 17:10:06 +01001053void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1054 HInstruction* instruction,
1055 SlowPathCode* slow_path) {
1056 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1057 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1058}
1059
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001060static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001061// Use a fake return address register to mimic Quick.
1062static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001063CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001064 const X86_64InstructionSetFeatures& isa_features,
1065 const CompilerOptions& compiler_options,
1066 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001067 : CodeGenerator(graph,
1068 kNumberOfCpuRegisters,
1069 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001070 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001071 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1072 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001073 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001074 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1075 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001076 compiler_options,
1077 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001078 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001079 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001080 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001081 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001082 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001083 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001084 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001085 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1086 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001087 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001088 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1089 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001090 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001091 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001092 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1093}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001095InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1096 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001097 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098 assembler_(codegen->GetAssembler()),
1099 codegen_(codegen) {}
1100
David Brazdil58282f42016-01-14 12:45:10 +00001101void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001102 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001103 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001105 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001106 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001107}
1108
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001109static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001110 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001111}
David Srbecky9d8606d2015-04-12 09:35:32 +01001112
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001113static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001114 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001115}
1116
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001118 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001119 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001120 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001121 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001122 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001123
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001124 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001125 __ testq(CpuRegister(RAX), Address(
1126 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001127 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001128 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001129
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001130 if (HasEmptyFrame()) {
1131 return;
1132 }
1133
Nicolas Geoffray98893962015-01-21 12:32:32 +00001134 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001135 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001136 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001137 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001138 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1139 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001140 }
1141 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001142
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001143 int adjust = GetFrameSize() - GetCoreSpillSize();
1144 __ subq(CpuRegister(RSP), Immediate(adjust));
1145 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001146 uint32_t xmm_spill_location = GetFpuSpillStart();
1147 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001148
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001149 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1150 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001151 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1152 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1153 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001154 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001155 }
1156
Mathieu Chartiere401d142015-04-22 13:56:20 -07001157 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001158 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001159}
1160
1161void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001162 __ cfi().RememberState();
1163 if (!HasEmptyFrame()) {
1164 uint32_t xmm_spill_location = GetFpuSpillStart();
1165 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1166 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1167 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1168 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1169 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1170 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1171 }
1172 }
1173
1174 int adjust = GetFrameSize() - GetCoreSpillSize();
1175 __ addq(CpuRegister(RSP), Immediate(adjust));
1176 __ cfi().AdjustCFAOffset(-adjust);
1177
1178 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1179 Register reg = kCoreCalleeSaves[i];
1180 if (allocated_registers_.ContainsCoreRegister(reg)) {
1181 __ popq(CpuRegister(reg));
1182 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1183 __ cfi().Restore(DWARFReg(reg));
1184 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001185 }
1186 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001187 __ ret();
1188 __ cfi().RestoreState();
1189 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190}
1191
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001192void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1193 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001194}
1195
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196void CodeGeneratorX86_64::Move(Location destination, Location source) {
1197 if (source.Equals(destination)) {
1198 return;
1199 }
1200 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001201 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001203 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001205 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001207 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1208 } else if (source.IsConstant()) {
1209 HConstant* constant = source.GetConstant();
1210 if (constant->IsLongConstant()) {
1211 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1212 } else {
1213 Load32BitValue(dest, GetInt32ValueOf(constant));
1214 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215 } else {
1216 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001217 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 }
1219 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001220 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001222 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001224 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1225 } else if (source.IsConstant()) {
1226 HConstant* constant = source.GetConstant();
1227 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1228 if (constant->IsFloatConstant()) {
1229 Load32BitValue(dest, static_cast<int32_t>(value));
1230 } else {
1231 Load64BitValue(dest, value);
1232 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001234 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 } else {
1236 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001237 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001238 }
1239 } else if (destination.IsStackSlot()) {
1240 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else if (source.IsFpuRegister()) {
1244 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001245 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001246 } else if (source.IsConstant()) {
1247 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001248 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001249 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001251 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001252 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1253 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001254 }
1255 } else {
1256 DCHECK(destination.IsDoubleStackSlot());
1257 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001258 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001259 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001260 } else if (source.IsFpuRegister()) {
1261 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001262 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001263 } else if (source.IsConstant()) {
1264 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001265 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001266 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001267 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001268 } else {
1269 DCHECK(constant->IsLongConstant());
1270 value = constant->AsLongConstant()->GetValue();
1271 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001272 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273 } else {
1274 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001275 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1276 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277 }
1278 }
1279}
1280
Calin Juravle175dc732015-08-25 15:42:32 +01001281void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1282 DCHECK(location.IsRegister());
1283 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1284}
1285
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286void CodeGeneratorX86_64::MoveLocation(
1287 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1288 Move(dst, src);
1289}
1290
1291void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1292 if (location.IsRegister()) {
1293 locations->AddTemp(location);
1294 } else {
1295 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1296 }
1297}
1298
David Brazdilfc6a86a2015-06-26 10:33:45 +00001299void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001300 DCHECK(!successor->IsExitBlock());
1301
1302 HBasicBlock* block = got->GetBlock();
1303 HInstruction* previous = got->GetPrevious();
1304
1305 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001306 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001307 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1308 return;
1309 }
1310
1311 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1312 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1313 }
1314 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001315 __ jmp(codegen_->GetLabelOf(successor));
1316 }
1317}
1318
David Brazdilfc6a86a2015-06-26 10:33:45 +00001319void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1320 got->SetLocations(nullptr);
1321}
1322
1323void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1324 HandleGoto(got, got->GetSuccessor());
1325}
1326
1327void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1328 try_boundary->SetLocations(nullptr);
1329}
1330
1331void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1332 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1333 if (!successor->IsExitBlock()) {
1334 HandleGoto(try_boundary, successor);
1335 }
1336}
1337
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001338void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1339 exit->SetLocations(nullptr);
1340}
1341
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001342void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001343}
1344
Mark Mendell152408f2015-12-31 12:28:50 -05001345template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001346void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001347 LabelType* true_label,
1348 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001349 if (cond->IsFPConditionTrueIfNaN()) {
1350 __ j(kUnordered, true_label);
1351 } else if (cond->IsFPConditionFalseIfNaN()) {
1352 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001353 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001355}
1356
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001357void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001358 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001359
Mark Mendellc4701932015-04-10 13:18:51 -04001360 Location left = locations->InAt(0);
1361 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001362 Primitive::Type type = condition->InputAt(0)->GetType();
1363 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001364 case Primitive::kPrimBoolean:
1365 case Primitive::kPrimByte:
1366 case Primitive::kPrimChar:
1367 case Primitive::kPrimShort:
1368 case Primitive::kPrimInt:
1369 case Primitive::kPrimNot: {
1370 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1371 if (right.IsConstant()) {
1372 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1373 if (value == 0) {
1374 __ testl(left_reg, left_reg);
1375 } else {
1376 __ cmpl(left_reg, Immediate(value));
1377 }
1378 } else if (right.IsStackSlot()) {
1379 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1380 } else {
1381 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1382 }
1383 break;
1384 }
Mark Mendellc4701932015-04-10 13:18:51 -04001385 case Primitive::kPrimLong: {
1386 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1387 if (right.IsConstant()) {
1388 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001389 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001390 } else if (right.IsDoubleStackSlot()) {
1391 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1392 } else {
1393 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1394 }
Mark Mendellc4701932015-04-10 13:18:51 -04001395 break;
1396 }
1397 case Primitive::kPrimFloat: {
1398 if (right.IsFpuRegister()) {
1399 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1400 } else if (right.IsConstant()) {
1401 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1402 codegen_->LiteralFloatAddress(
1403 right.GetConstant()->AsFloatConstant()->GetValue()));
1404 } else {
1405 DCHECK(right.IsStackSlot());
1406 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1407 Address(CpuRegister(RSP), right.GetStackIndex()));
1408 }
Mark Mendellc4701932015-04-10 13:18:51 -04001409 break;
1410 }
1411 case Primitive::kPrimDouble: {
1412 if (right.IsFpuRegister()) {
1413 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1414 } else if (right.IsConstant()) {
1415 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1416 codegen_->LiteralDoubleAddress(
1417 right.GetConstant()->AsDoubleConstant()->GetValue()));
1418 } else {
1419 DCHECK(right.IsDoubleStackSlot());
1420 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1421 Address(CpuRegister(RSP), right.GetStackIndex()));
1422 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001423 break;
1424 }
1425 default:
1426 LOG(FATAL) << "Unexpected condition type " << type;
1427 }
1428}
1429
1430template<class LabelType>
1431void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1432 LabelType* true_target_in,
1433 LabelType* false_target_in) {
1434 // Generated branching requires both targets to be explicit. If either of the
1435 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1436 LabelType fallthrough_target;
1437 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1438 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1439
1440 // Generate the comparison to set the CC.
1441 GenerateCompareTest(condition);
1442
1443 // Now generate the correct jump(s).
1444 Primitive::Type type = condition->InputAt(0)->GetType();
1445 switch (type) {
1446 case Primitive::kPrimLong: {
1447 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1448 break;
1449 }
1450 case Primitive::kPrimFloat: {
1451 GenerateFPJumps(condition, true_target, false_target);
1452 break;
1453 }
1454 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001455 GenerateFPJumps(condition, true_target, false_target);
1456 break;
1457 }
1458 default:
1459 LOG(FATAL) << "Unexpected condition type " << type;
1460 }
1461
David Brazdil0debae72015-11-12 18:37:00 +00001462 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001463 __ jmp(false_target);
1464 }
David Brazdil0debae72015-11-12 18:37:00 +00001465
1466 if (fallthrough_target.IsLinked()) {
1467 __ Bind(&fallthrough_target);
1468 }
Mark Mendellc4701932015-04-10 13:18:51 -04001469}
1470
David Brazdil0debae72015-11-12 18:37:00 +00001471static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1472 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1473 // are set only strictly before `branch`. We can't use the eflags on long
1474 // conditions if they are materialized due to the complex branching.
1475 return cond->IsCondition() &&
1476 cond->GetNext() == branch &&
1477 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1478}
1479
Mark Mendell152408f2015-12-31 12:28:50 -05001480template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001481void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001482 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001483 LabelType* true_target,
1484 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001485 HInstruction* cond = instruction->InputAt(condition_input_index);
1486
1487 if (true_target == nullptr && false_target == nullptr) {
1488 // Nothing to do. The code always falls through.
1489 return;
1490 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001491 // Constant condition, statically compared against "true" (integer value 1).
1492 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001493 if (true_target != nullptr) {
1494 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001495 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001496 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001497 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001498 if (false_target != nullptr) {
1499 __ jmp(false_target);
1500 }
1501 }
1502 return;
1503 }
1504
1505 // The following code generates these patterns:
1506 // (1) true_target == nullptr && false_target != nullptr
1507 // - opposite condition true => branch to false_target
1508 // (2) true_target != nullptr && false_target == nullptr
1509 // - condition true => branch to true_target
1510 // (3) true_target != nullptr && false_target != nullptr
1511 // - condition true => branch to true_target
1512 // - branch to false_target
1513 if (IsBooleanValueOrMaterializedCondition(cond)) {
1514 if (AreEflagsSetFrom(cond, instruction)) {
1515 if (true_target == nullptr) {
1516 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1517 } else {
1518 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1519 }
1520 } else {
1521 // Materialized condition, compare against 0.
1522 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1523 if (lhs.IsRegister()) {
1524 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1525 } else {
1526 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1527 }
1528 if (true_target == nullptr) {
1529 __ j(kEqual, false_target);
1530 } else {
1531 __ j(kNotEqual, true_target);
1532 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001533 }
1534 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001535 // Condition has not been materialized, use its inputs as the
1536 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001537 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001538
David Brazdil0debae72015-11-12 18:37:00 +00001539 // If this is a long or FP comparison that has been folded into
1540 // the HCondition, generate the comparison directly.
1541 Primitive::Type type = condition->InputAt(0)->GetType();
1542 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1543 GenerateCompareTestAndBranch(condition, true_target, false_target);
1544 return;
1545 }
1546
1547 Location lhs = condition->GetLocations()->InAt(0);
1548 Location rhs = condition->GetLocations()->InAt(1);
1549 if (rhs.IsRegister()) {
1550 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1551 } else if (rhs.IsConstant()) {
1552 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001553 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001554 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001555 __ cmpl(lhs.AsRegister<CpuRegister>(),
1556 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1557 }
1558 if (true_target == nullptr) {
1559 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1560 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001561 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001562 }
Dave Allison20dfc792014-06-16 20:44:29 -07001563 }
David Brazdil0debae72015-11-12 18:37:00 +00001564
1565 // If neither branch falls through (case 3), the conditional branch to `true_target`
1566 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1567 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001568 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001569 }
1570}
1571
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001572void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1574 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001575 locations->SetInAt(0, Location::Any());
1576 }
1577}
1578
1579void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001580 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1581 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1582 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1583 nullptr : codegen_->GetLabelOf(true_successor);
1584 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1585 nullptr : codegen_->GetLabelOf(false_successor);
1586 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587}
1588
1589void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1590 LocationSummary* locations = new (GetGraph()->GetArena())
1591 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001592 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001593 locations->SetInAt(0, Location::Any());
1594 }
1595}
1596
1597void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001598 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001599 GenerateTestAndBranch<Label>(deoptimize,
1600 /* condition_input_index */ 0,
1601 slow_path->GetEntryLabel(),
1602 /* false_target */ nullptr);
1603}
1604
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001605static bool SelectCanUseCMOV(HSelect* select) {
1606 // There are no conditional move instructions for XMMs.
1607 if (Primitive::IsFloatingPointType(select->GetType())) {
1608 return false;
1609 }
1610
1611 // A FP condition doesn't generate the single CC that we need.
1612 HInstruction* condition = select->GetCondition();
1613 if (condition->IsCondition() &&
1614 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1615 return false;
1616 }
1617
1618 // We can generate a CMOV for this Select.
1619 return true;
1620}
1621
David Brazdil74eb1b22015-12-14 11:44:01 +00001622void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1623 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1624 if (Primitive::IsFloatingPointType(select->GetType())) {
1625 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001626 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001627 } else {
1628 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001629 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001630 if (select->InputAt(1)->IsConstant()) {
1631 locations->SetInAt(1, Location::RequiresRegister());
1632 } else {
1633 locations->SetInAt(1, Location::Any());
1634 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001635 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001636 locations->SetInAt(1, Location::Any());
1637 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001638 }
1639 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1640 locations->SetInAt(2, Location::RequiresRegister());
1641 }
1642 locations->SetOut(Location::SameAsFirstInput());
1643}
1644
1645void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1646 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001647 if (SelectCanUseCMOV(select)) {
1648 // If both the condition and the source types are integer, we can generate
1649 // a CMOV to implement Select.
1650 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001651 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001652 DCHECK(locations->InAt(0).Equals(locations->Out()));
1653
1654 HInstruction* select_condition = select->GetCondition();
1655 Condition cond = kNotEqual;
1656
1657 // Figure out how to test the 'condition'.
1658 if (select_condition->IsCondition()) {
1659 HCondition* condition = select_condition->AsCondition();
1660 if (!condition->IsEmittedAtUseSite()) {
1661 // This was a previously materialized condition.
1662 // Can we use the existing condition code?
1663 if (AreEflagsSetFrom(condition, select)) {
1664 // Materialization was the previous instruction. Condition codes are right.
1665 cond = X86_64IntegerCondition(condition->GetCondition());
1666 } else {
1667 // No, we have to recreate the condition code.
1668 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1669 __ testl(cond_reg, cond_reg);
1670 }
1671 } else {
1672 GenerateCompareTest(condition);
1673 cond = X86_64IntegerCondition(condition->GetCondition());
1674 }
1675 } else {
1676 // Must be a boolean condition, which needs to be compared to 0.
1677 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1678 __ testl(cond_reg, cond_reg);
1679 }
1680
1681 // If the condition is true, overwrite the output, which already contains false.
1682 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001683 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1684 if (value_true_loc.IsRegister()) {
1685 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1686 } else {
1687 __ cmov(cond,
1688 value_false,
1689 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1690 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001691 } else {
1692 NearLabel false_target;
1693 GenerateTestAndBranch<NearLabel>(select,
1694 /* condition_input_index */ 2,
1695 /* true_target */ nullptr,
1696 &false_target);
1697 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1698 __ Bind(&false_target);
1699 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700}
1701
David Srbecky0cf44932015-12-09 14:09:59 +00001702void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1703 new (GetGraph()->GetArena()) LocationSummary(info);
1704}
1705
David Srbeckyd28f4a02016-03-14 17:14:24 +00001706void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1707 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001708}
1709
1710void CodeGeneratorX86_64::GenerateNop() {
1711 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001712}
1713
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001715 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001716 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001717 // Handle the long/FP comparisons made in instruction simplification.
1718 switch (cond->InputAt(0)->GetType()) {
1719 case Primitive::kPrimLong:
1720 locations->SetInAt(0, Location::RequiresRegister());
1721 locations->SetInAt(1, Location::Any());
1722 break;
1723 case Primitive::kPrimFloat:
1724 case Primitive::kPrimDouble:
1725 locations->SetInAt(0, Location::RequiresFpuRegister());
1726 locations->SetInAt(1, Location::Any());
1727 break;
1728 default:
1729 locations->SetInAt(0, Location::RequiresRegister());
1730 locations->SetInAt(1, Location::Any());
1731 break;
1732 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001733 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001734 locations->SetOut(Location::RequiresRegister());
1735 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001736}
1737
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001739 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001740 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001741 }
Mark Mendellc4701932015-04-10 13:18:51 -04001742
1743 LocationSummary* locations = cond->GetLocations();
1744 Location lhs = locations->InAt(0);
1745 Location rhs = locations->InAt(1);
1746 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001747 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001748
1749 switch (cond->InputAt(0)->GetType()) {
1750 default:
1751 // Integer case.
1752
1753 // Clear output register: setcc only sets the low byte.
1754 __ xorl(reg, reg);
1755
1756 if (rhs.IsRegister()) {
1757 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1758 } else if (rhs.IsConstant()) {
1759 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001760 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001761 } else {
1762 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1763 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001764 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001765 return;
1766 case Primitive::kPrimLong:
1767 // Clear output register: setcc only sets the low byte.
1768 __ xorl(reg, reg);
1769
1770 if (rhs.IsRegister()) {
1771 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1772 } else if (rhs.IsConstant()) {
1773 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001774 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001775 } else {
1776 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1777 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001778 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001779 return;
1780 case Primitive::kPrimFloat: {
1781 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1782 if (rhs.IsConstant()) {
1783 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1784 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1785 } else if (rhs.IsStackSlot()) {
1786 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1787 } else {
1788 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1789 }
1790 GenerateFPJumps(cond, &true_label, &false_label);
1791 break;
1792 }
1793 case Primitive::kPrimDouble: {
1794 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1795 if (rhs.IsConstant()) {
1796 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1797 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1798 } else if (rhs.IsDoubleStackSlot()) {
1799 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1800 } else {
1801 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1802 }
1803 GenerateFPJumps(cond, &true_label, &false_label);
1804 break;
1805 }
1806 }
1807
1808 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001809 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001810
Roland Levillain4fa13f62015-07-06 18:11:54 +01001811 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001812 __ Bind(&false_label);
1813 __ xorl(reg, reg);
1814 __ jmp(&done_label);
1815
Roland Levillain4fa13f62015-07-06 18:11:54 +01001816 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001817 __ Bind(&true_label);
1818 __ movl(reg, Immediate(1));
1819 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001820}
1821
1822void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001824}
1825
1826void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001828}
1829
1830void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001832}
1833
1834void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001836}
1837
1838void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001840}
1841
1842void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001844}
1845
1846void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001847 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001848}
1849
1850void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001851 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001852}
1853
1854void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001856}
1857
1858void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001859 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001860}
1861
1862void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001863 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001864}
1865
1866void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001867 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001868}
1869
Aart Bike9f37602015-10-09 11:15:55 -07001870void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001871 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001872}
1873
1874void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001875 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001876}
1877
1878void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001879 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001880}
1881
1882void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001883 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001884}
1885
1886void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001887 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001888}
1889
1890void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001891 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001892}
1893
1894void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001895 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001896}
1897
1898void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001899 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001900}
1901
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001902void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001903 LocationSummary* locations =
1904 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001905 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001906 case Primitive::kPrimBoolean:
1907 case Primitive::kPrimByte:
1908 case Primitive::kPrimShort:
1909 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001910 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001911 case Primitive::kPrimLong: {
1912 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001913 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1915 break;
1916 }
1917 case Primitive::kPrimFloat:
1918 case Primitive::kPrimDouble: {
1919 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001920 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001921 locations->SetOut(Location::RequiresRegister());
1922 break;
1923 }
1924 default:
1925 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1926 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001927}
1928
1929void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001930 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001931 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001932 Location left = locations->InAt(0);
1933 Location right = locations->InAt(1);
1934
Mark Mendell0c9497d2015-08-21 09:30:05 -04001935 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001936 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001937 Condition less_cond = kLess;
1938
Calin Juravleddb7df22014-11-25 20:56:51 +00001939 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001940 case Primitive::kPrimBoolean:
1941 case Primitive::kPrimByte:
1942 case Primitive::kPrimShort:
1943 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001944 case Primitive::kPrimInt: {
1945 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1946 if (right.IsConstant()) {
1947 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1948 codegen_->Compare32BitValue(left_reg, value);
1949 } else if (right.IsStackSlot()) {
1950 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1951 } else {
1952 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1953 }
1954 break;
1955 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001956 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001957 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1958 if (right.IsConstant()) {
1959 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001960 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001961 } else if (right.IsDoubleStackSlot()) {
1962 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001963 } else {
1964 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1965 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001966 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001967 }
1968 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001969 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1970 if (right.IsConstant()) {
1971 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1972 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1973 } else if (right.IsStackSlot()) {
1974 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1975 } else {
1976 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1977 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001978 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001979 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001980 break;
1981 }
1982 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001983 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1984 if (right.IsConstant()) {
1985 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1986 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1987 } else if (right.IsDoubleStackSlot()) {
1988 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1989 } else {
1990 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1991 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001992 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001993 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001994 break;
1995 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001996 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001997 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001998 }
Aart Bika19616e2016-02-01 18:57:58 -08001999
Calin Juravleddb7df22014-11-25 20:56:51 +00002000 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002001 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002002 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002003
Calin Juravle91debbc2014-11-26 19:01:09 +00002004 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002005 __ movl(out, Immediate(1));
2006 __ jmp(&done);
2007
2008 __ Bind(&less);
2009 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002010
2011 __ Bind(&done);
2012}
2013
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 LocationSummary* locations =
2016 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002017 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002018}
2019
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002020void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002021 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002022}
2023
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002024void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2025 LocationSummary* locations =
2026 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2027 locations->SetOut(Location::ConstantLocation(constant));
2028}
2029
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002030void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002031 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002032}
2033
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002034void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002035 LocationSummary* locations =
2036 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002037 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002038}
2039
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002040void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002041 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002042}
2043
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002044void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2045 LocationSummary* locations =
2046 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2047 locations->SetOut(Location::ConstantLocation(constant));
2048}
2049
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002050void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002051 // Will be generated at use site.
2052}
2053
2054void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2055 LocationSummary* locations =
2056 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2057 locations->SetOut(Location::ConstantLocation(constant));
2058}
2059
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002060void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2061 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002062 // Will be generated at use site.
2063}
2064
Calin Juravle27df7582015-04-17 19:12:31 +01002065void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2066 memory_barrier->SetLocations(nullptr);
2067}
2068
2069void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002070 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002071}
2072
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002073void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2074 ret->SetLocations(nullptr);
2075}
2076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002077void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002078 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002079}
2080
2081void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002082 LocationSummary* locations =
2083 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002084 switch (ret->InputAt(0)->GetType()) {
2085 case Primitive::kPrimBoolean:
2086 case Primitive::kPrimByte:
2087 case Primitive::kPrimChar:
2088 case Primitive::kPrimShort:
2089 case Primitive::kPrimInt:
2090 case Primitive::kPrimNot:
2091 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002092 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002093 break;
2094
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002095 case Primitive::kPrimFloat:
2096 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002097 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002098 break;
2099
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002100 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002101 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002102 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103}
2104
2105void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2106 if (kIsDebugBuild) {
2107 switch (ret->InputAt(0)->GetType()) {
2108 case Primitive::kPrimBoolean:
2109 case Primitive::kPrimByte:
2110 case Primitive::kPrimChar:
2111 case Primitive::kPrimShort:
2112 case Primitive::kPrimInt:
2113 case Primitive::kPrimNot:
2114 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002115 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002116 break;
2117
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 case Primitive::kPrimFloat:
2119 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 XMM0);
2122 break;
2123
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002124 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002125 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002126 }
2127 }
2128 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002129}
2130
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002131Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2132 switch (type) {
2133 case Primitive::kPrimBoolean:
2134 case Primitive::kPrimByte:
2135 case Primitive::kPrimChar:
2136 case Primitive::kPrimShort:
2137 case Primitive::kPrimInt:
2138 case Primitive::kPrimNot:
2139 case Primitive::kPrimLong:
2140 return Location::RegisterLocation(RAX);
2141
2142 case Primitive::kPrimVoid:
2143 return Location::NoLocation();
2144
2145 case Primitive::kPrimDouble:
2146 case Primitive::kPrimFloat:
2147 return Location::FpuRegisterLocation(XMM0);
2148 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002149
2150 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002151}
2152
2153Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2154 return Location::RegisterLocation(kMethodRegisterArgument);
2155}
2156
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002157Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002158 switch (type) {
2159 case Primitive::kPrimBoolean:
2160 case Primitive::kPrimByte:
2161 case Primitive::kPrimChar:
2162 case Primitive::kPrimShort:
2163 case Primitive::kPrimInt:
2164 case Primitive::kPrimNot: {
2165 uint32_t index = gp_index_++;
2166 stack_index_++;
2167 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002168 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002169 } else {
2170 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2171 }
2172 }
2173
2174 case Primitive::kPrimLong: {
2175 uint32_t index = gp_index_;
2176 stack_index_ += 2;
2177 if (index < calling_convention.GetNumberOfRegisters()) {
2178 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002179 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002180 } else {
2181 gp_index_ += 2;
2182 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2183 }
2184 }
2185
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002186 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002187 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002188 stack_index_++;
2189 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002190 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002191 } else {
2192 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2193 }
2194 }
2195
2196 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002197 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002198 stack_index_ += 2;
2199 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002200 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002201 } else {
2202 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2203 }
2204 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205
2206 case Primitive::kPrimVoid:
2207 LOG(FATAL) << "Unexpected parameter type " << type;
2208 break;
2209 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002210 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002211}
2212
Calin Juravle175dc732015-08-25 15:42:32 +01002213void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2214 // The trampoline uses the same calling convention as dex calling conventions,
2215 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2216 // the method_idx.
2217 HandleInvoke(invoke);
2218}
2219
2220void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2221 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2222}
2223
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002224void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002225 // Explicit clinit checks triggered by static invokes must have been pruned by
2226 // art::PrepareForRegisterAllocation.
2227 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002228
Mark Mendellfb8d2792015-03-31 22:16:59 -04002229 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002230 if (intrinsic.TryDispatch(invoke)) {
2231 return;
2232 }
2233
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002234 HandleInvoke(invoke);
2235}
2236
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002237static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2238 if (invoke->GetLocations()->Intrinsified()) {
2239 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2240 intrinsic.Dispatch(invoke);
2241 return true;
2242 }
2243 return false;
2244}
2245
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002246void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002247 // Explicit clinit checks triggered by static invokes must have been pruned by
2248 // art::PrepareForRegisterAllocation.
2249 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002250
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002251 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2252 return;
2253 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002254
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002255 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002256 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002257 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002258 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002259}
2260
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002261void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002262 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002263 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002264}
2265
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002267 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002268 if (intrinsic.TryDispatch(invoke)) {
2269 return;
2270 }
2271
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002272 HandleInvoke(invoke);
2273}
2274
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002275void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002276 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2277 return;
2278 }
2279
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002280 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002281 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002282 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002283}
2284
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002285void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2286 HandleInvoke(invoke);
2287 // Add the hidden argument.
2288 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2289}
2290
2291void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2292 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002293 LocationSummary* locations = invoke->GetLocations();
2294 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2295 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002296 Location receiver = locations->InAt(0);
2297 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2298
Roland Levillain0d5a2812015-11-13 10:07:31 +00002299 // Set the hidden argument. This is safe to do this here, as RAX
2300 // won't be modified thereafter, before the `call` instruction.
2301 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002302 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002303
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002304 if (receiver.IsStackSlot()) {
2305 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002306 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002307 __ movl(temp, Address(temp, class_offset));
2308 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002309 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002310 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002311 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002312 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002313 // Instead of simply (possibly) unpoisoning `temp` here, we should
2314 // emit a read barrier for the previous class reference load.
2315 // However this is not required in practice, as this is an
2316 // intermediate/temporary reference and because the current
2317 // concurrent copying collector keeps the from-space memory
2318 // intact/accessible until the end of the marking phase (the
2319 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002320 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002321 // temp = temp->GetAddressOfIMT()
2322 __ movq(temp,
2323 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2324 // temp = temp->GetImtEntryAt(method_offset);
2325 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002326 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002327 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002328 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002329 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002330 __ call(Address(
2331 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002332
2333 DCHECK(!codegen_->IsLeafMethod());
2334 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2335}
2336
Roland Levillain88cb1752014-10-20 16:36:47 +01002337void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2338 LocationSummary* locations =
2339 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2340 switch (neg->GetResultType()) {
2341 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002342 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002343 locations->SetInAt(0, Location::RequiresRegister());
2344 locations->SetOut(Location::SameAsFirstInput());
2345 break;
2346
Roland Levillain88cb1752014-10-20 16:36:47 +01002347 case Primitive::kPrimFloat:
2348 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002349 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002350 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002351 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2356 }
2357}
2358
2359void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2360 LocationSummary* locations = neg->GetLocations();
2361 Location out = locations->Out();
2362 Location in = locations->InAt(0);
2363 switch (neg->GetResultType()) {
2364 case Primitive::kPrimInt:
2365 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002366 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002367 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002368 break;
2369
2370 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002371 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002372 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002373 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002374 break;
2375
Roland Levillain5368c212014-11-27 15:03:41 +00002376 case Primitive::kPrimFloat: {
2377 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002378 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002379 // Implement float negation with an exclusive or with value
2380 // 0x80000000 (mask for bit 31, representing the sign of a
2381 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002382 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002384 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002385 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002386
Roland Levillain5368c212014-11-27 15:03:41 +00002387 case Primitive::kPrimDouble: {
2388 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002389 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002390 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002391 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002392 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002393 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002394 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002395 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002396 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002397
2398 default:
2399 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2400 }
2401}
2402
Roland Levillaindff1f282014-11-05 14:15:05 +00002403void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2404 LocationSummary* locations =
2405 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2406 Primitive::Type result_type = conversion->GetResultType();
2407 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002408 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002409
David Brazdilb2bd1c52015-03-25 11:17:37 +00002410 // The Java language does not allow treating boolean as an integral type but
2411 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002412
Roland Levillaindff1f282014-11-05 14:15:05 +00002413 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002414 case Primitive::kPrimByte:
2415 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002416 case Primitive::kPrimLong:
2417 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002418 case Primitive::kPrimBoolean:
2419 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002420 case Primitive::kPrimShort:
2421 case Primitive::kPrimInt:
2422 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002423 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002424 locations->SetInAt(0, Location::Any());
2425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2426 break;
2427
2428 default:
2429 LOG(FATAL) << "Unexpected type conversion from " << input_type
2430 << " to " << result_type;
2431 }
2432 break;
2433
Roland Levillain01a8d712014-11-14 16:27:39 +00002434 case Primitive::kPrimShort:
2435 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002436 case Primitive::kPrimLong:
2437 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002438 case Primitive::kPrimBoolean:
2439 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002440 case Primitive::kPrimByte:
2441 case Primitive::kPrimInt:
2442 case Primitive::kPrimChar:
2443 // Processing a Dex `int-to-short' instruction.
2444 locations->SetInAt(0, Location::Any());
2445 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2446 break;
2447
2448 default:
2449 LOG(FATAL) << "Unexpected type conversion from " << input_type
2450 << " to " << result_type;
2451 }
2452 break;
2453
Roland Levillain946e1432014-11-11 17:35:19 +00002454 case Primitive::kPrimInt:
2455 switch (input_type) {
2456 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002457 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002458 locations->SetInAt(0, Location::Any());
2459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2460 break;
2461
2462 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002463 // Processing a Dex `float-to-int' instruction.
2464 locations->SetInAt(0, Location::RequiresFpuRegister());
2465 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002466 break;
2467
Roland Levillain946e1432014-11-11 17:35:19 +00002468 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002469 // Processing a Dex `double-to-int' instruction.
2470 locations->SetInAt(0, Location::RequiresFpuRegister());
2471 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002472 break;
2473
2474 default:
2475 LOG(FATAL) << "Unexpected type conversion from " << input_type
2476 << " to " << result_type;
2477 }
2478 break;
2479
Roland Levillaindff1f282014-11-05 14:15:05 +00002480 case Primitive::kPrimLong:
2481 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002482 case Primitive::kPrimBoolean:
2483 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002484 case Primitive::kPrimByte:
2485 case Primitive::kPrimShort:
2486 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002487 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002488 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 // TODO: We would benefit from a (to-be-implemented)
2490 // Location::RegisterOrStackSlot requirement for this input.
2491 locations->SetInAt(0, Location::RequiresRegister());
2492 locations->SetOut(Location::RequiresRegister());
2493 break;
2494
2495 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002496 // Processing a Dex `float-to-long' instruction.
2497 locations->SetInAt(0, Location::RequiresFpuRegister());
2498 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002499 break;
2500
Roland Levillaindff1f282014-11-05 14:15:05 +00002501 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002502 // Processing a Dex `double-to-long' instruction.
2503 locations->SetInAt(0, Location::RequiresFpuRegister());
2504 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unexpected type conversion from " << input_type
2509 << " to " << result_type;
2510 }
2511 break;
2512
Roland Levillain981e4542014-11-14 11:47:14 +00002513 case Primitive::kPrimChar:
2514 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002515 case Primitive::kPrimLong:
2516 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002517 case Primitive::kPrimBoolean:
2518 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002519 case Primitive::kPrimByte:
2520 case Primitive::kPrimShort:
2521 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002522 // Processing a Dex `int-to-char' instruction.
2523 locations->SetInAt(0, Location::Any());
2524 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2525 break;
2526
2527 default:
2528 LOG(FATAL) << "Unexpected type conversion from " << input_type
2529 << " to " << result_type;
2530 }
2531 break;
2532
Roland Levillaindff1f282014-11-05 14:15:05 +00002533 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002534 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002535 case Primitive::kPrimBoolean:
2536 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002537 case Primitive::kPrimByte:
2538 case Primitive::kPrimShort:
2539 case Primitive::kPrimInt:
2540 case Primitive::kPrimChar:
2541 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002542 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002543 locations->SetOut(Location::RequiresFpuRegister());
2544 break;
2545
2546 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002547 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002548 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002549 locations->SetOut(Location::RequiresFpuRegister());
2550 break;
2551
Roland Levillaincff13742014-11-17 14:32:17 +00002552 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002553 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002554 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 };
2562 break;
2563
Roland Levillaindff1f282014-11-05 14:15:05 +00002564 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002565 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002566 case Primitive::kPrimBoolean:
2567 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002568 case Primitive::kPrimByte:
2569 case Primitive::kPrimShort:
2570 case Primitive::kPrimInt:
2571 case Primitive::kPrimChar:
2572 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002573 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002574 locations->SetOut(Location::RequiresFpuRegister());
2575 break;
2576
2577 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002578 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002579 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002580 locations->SetOut(Location::RequiresFpuRegister());
2581 break;
2582
Roland Levillaincff13742014-11-17 14:32:17 +00002583 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002584 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002585 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002586 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002587 break;
2588
2589 default:
2590 LOG(FATAL) << "Unexpected type conversion from " << input_type
2591 << " to " << result_type;
2592 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unexpected type conversion from " << input_type
2597 << " to " << result_type;
2598 }
2599}
2600
2601void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2602 LocationSummary* locations = conversion->GetLocations();
2603 Location out = locations->Out();
2604 Location in = locations->InAt(0);
2605 Primitive::Type result_type = conversion->GetResultType();
2606 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002607 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002608 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002609 case Primitive::kPrimByte:
2610 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002611 case Primitive::kPrimLong:
2612 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002613 case Primitive::kPrimBoolean:
2614 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002615 case Primitive::kPrimShort:
2616 case Primitive::kPrimInt:
2617 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002618 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002619 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002620 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002621 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002622 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002623 Address(CpuRegister(RSP), in.GetStackIndex()));
2624 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002625 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002626 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002627 }
2628 break;
2629
2630 default:
2631 LOG(FATAL) << "Unexpected type conversion from " << input_type
2632 << " to " << result_type;
2633 }
2634 break;
2635
Roland Levillain01a8d712014-11-14 16:27:39 +00002636 case Primitive::kPrimShort:
2637 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002638 case Primitive::kPrimLong:
2639 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002640 case Primitive::kPrimBoolean:
2641 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002642 case Primitive::kPrimByte:
2643 case Primitive::kPrimInt:
2644 case Primitive::kPrimChar:
2645 // Processing a Dex `int-to-short' instruction.
2646 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002648 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002649 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002650 Address(CpuRegister(RSP), in.GetStackIndex()));
2651 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002652 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002653 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002654 }
2655 break;
2656
2657 default:
2658 LOG(FATAL) << "Unexpected type conversion from " << input_type
2659 << " to " << result_type;
2660 }
2661 break;
2662
Roland Levillain946e1432014-11-11 17:35:19 +00002663 case Primitive::kPrimInt:
2664 switch (input_type) {
2665 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002666 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002667 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002669 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002671 Address(CpuRegister(RSP), in.GetStackIndex()));
2672 } else {
2673 DCHECK(in.IsConstant());
2674 DCHECK(in.GetConstant()->IsLongConstant());
2675 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002676 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002677 }
2678 break;
2679
Roland Levillain3f8f9362014-12-02 17:45:01 +00002680 case Primitive::kPrimFloat: {
2681 // Processing a Dex `float-to-int' instruction.
2682 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2683 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002684 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002685
2686 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002687 // if input >= (float)INT_MAX goto done
2688 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002689 __ j(kAboveEqual, &done);
2690 // if input == NaN goto nan
2691 __ j(kUnordered, &nan);
2692 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002693 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002694 __ jmp(&done);
2695 __ Bind(&nan);
2696 // output = 0
2697 __ xorl(output, output);
2698 __ Bind(&done);
2699 break;
2700 }
2701
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002702 case Primitive::kPrimDouble: {
2703 // Processing a Dex `double-to-int' instruction.
2704 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2705 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002706 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002707
2708 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002709 // if input >= (double)INT_MAX goto done
2710 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002711 __ j(kAboveEqual, &done);
2712 // if input == NaN goto nan
2713 __ j(kUnordered, &nan);
2714 // output = double-to-int-truncate(input)
2715 __ cvttsd2si(output, input);
2716 __ jmp(&done);
2717 __ Bind(&nan);
2718 // output = 0
2719 __ xorl(output, output);
2720 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002721 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002722 }
Roland Levillain946e1432014-11-11 17:35:19 +00002723
2724 default:
2725 LOG(FATAL) << "Unexpected type conversion from " << input_type
2726 << " to " << result_type;
2727 }
2728 break;
2729
Roland Levillaindff1f282014-11-05 14:15:05 +00002730 case Primitive::kPrimLong:
2731 switch (input_type) {
2732 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002733 case Primitive::kPrimBoolean:
2734 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002735 case Primitive::kPrimByte:
2736 case Primitive::kPrimShort:
2737 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002738 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002739 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002740 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002741 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002742 break;
2743
Roland Levillain624279f2014-12-04 11:54:28 +00002744 case Primitive::kPrimFloat: {
2745 // Processing a Dex `float-to-long' instruction.
2746 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2747 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002748 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002749
Mark Mendell92e83bf2015-05-07 11:25:03 -04002750 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002751 // if input >= (float)LONG_MAX goto done
2752 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002753 __ j(kAboveEqual, &done);
2754 // if input == NaN goto nan
2755 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002756 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002757 __ cvttss2si(output, input, true);
2758 __ jmp(&done);
2759 __ Bind(&nan);
2760 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002761 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002762 __ Bind(&done);
2763 break;
2764 }
2765
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002766 case Primitive::kPrimDouble: {
2767 // Processing a Dex `double-to-long' instruction.
2768 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2769 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002770 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002771
Mark Mendell92e83bf2015-05-07 11:25:03 -04002772 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002773 // if input >= (double)LONG_MAX goto done
2774 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002775 __ j(kAboveEqual, &done);
2776 // if input == NaN goto nan
2777 __ j(kUnordered, &nan);
2778 // output = double-to-long-truncate(input)
2779 __ cvttsd2si(output, input, true);
2780 __ jmp(&done);
2781 __ Bind(&nan);
2782 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002783 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002784 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002786 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002787
2788 default:
2789 LOG(FATAL) << "Unexpected type conversion from " << input_type
2790 << " to " << result_type;
2791 }
2792 break;
2793
Roland Levillain981e4542014-11-14 11:47:14 +00002794 case Primitive::kPrimChar:
2795 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002796 case Primitive::kPrimLong:
2797 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002798 case Primitive::kPrimBoolean:
2799 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002800 case Primitive::kPrimByte:
2801 case Primitive::kPrimShort:
2802 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002803 // Processing a Dex `int-to-char' instruction.
2804 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002805 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002806 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002807 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002808 Address(CpuRegister(RSP), in.GetStackIndex()));
2809 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002810 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002811 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002812 }
2813 break;
2814
2815 default:
2816 LOG(FATAL) << "Unexpected type conversion from " << input_type
2817 << " to " << result_type;
2818 }
2819 break;
2820
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002822 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002823 case Primitive::kPrimBoolean:
2824 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002825 case Primitive::kPrimByte:
2826 case Primitive::kPrimShort:
2827 case Primitive::kPrimInt:
2828 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002829 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002830 if (in.IsRegister()) {
2831 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2832 } else if (in.IsConstant()) {
2833 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2834 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002835 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002836 } else {
2837 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2838 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2839 }
Roland Levillaincff13742014-11-17 14:32:17 +00002840 break;
2841
2842 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002843 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002844 if (in.IsRegister()) {
2845 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2846 } else if (in.IsConstant()) {
2847 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2848 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002849 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002850 } else {
2851 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2852 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2853 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 break;
2855
Roland Levillaincff13742014-11-17 14:32:17 +00002856 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002857 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002858 if (in.IsFpuRegister()) {
2859 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2860 } else if (in.IsConstant()) {
2861 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2862 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002863 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002864 } else {
2865 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2866 Address(CpuRegister(RSP), in.GetStackIndex()));
2867 }
Roland Levillaincff13742014-11-17 14:32:17 +00002868 break;
2869
2870 default:
2871 LOG(FATAL) << "Unexpected type conversion from " << input_type
2872 << " to " << result_type;
2873 };
2874 break;
2875
Roland Levillaindff1f282014-11-05 14:15:05 +00002876 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002877 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002878 case Primitive::kPrimBoolean:
2879 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002880 case Primitive::kPrimByte:
2881 case Primitive::kPrimShort:
2882 case Primitive::kPrimInt:
2883 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002884 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002885 if (in.IsRegister()) {
2886 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2887 } else if (in.IsConstant()) {
2888 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2889 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002890 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002891 } else {
2892 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2893 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2894 }
Roland Levillaincff13742014-11-17 14:32:17 +00002895 break;
2896
2897 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002898 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002899 if (in.IsRegister()) {
2900 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2901 } else if (in.IsConstant()) {
2902 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2903 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002904 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002905 } else {
2906 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2907 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2908 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002909 break;
2910
Roland Levillaincff13742014-11-17 14:32:17 +00002911 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002912 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002913 if (in.IsFpuRegister()) {
2914 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2915 } else if (in.IsConstant()) {
2916 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2917 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002918 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002919 } else {
2920 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2921 Address(CpuRegister(RSP), in.GetStackIndex()));
2922 }
Roland Levillaincff13742014-11-17 14:32:17 +00002923 break;
2924
2925 default:
2926 LOG(FATAL) << "Unexpected type conversion from " << input_type
2927 << " to " << result_type;
2928 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002929 break;
2930
2931 default:
2932 LOG(FATAL) << "Unexpected type conversion from " << input_type
2933 << " to " << result_type;
2934 }
2935}
2936
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002937void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002938 LocationSummary* locations =
2939 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002940 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002941 case Primitive::kPrimInt: {
2942 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002943 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2944 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002945 break;
2946 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002947
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002948 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002949 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002950 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002951 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002953 break;
2954 }
2955
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002956 case Primitive::kPrimDouble:
2957 case Primitive::kPrimFloat: {
2958 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002959 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002960 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002961 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002962 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002963
2964 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002966 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002967}
2968
2969void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2970 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002971 Location first = locations->InAt(0);
2972 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002973 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002974
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002975 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002976 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002977 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002978 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2979 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002980 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2981 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002982 } else {
2983 __ leal(out.AsRegister<CpuRegister>(), Address(
2984 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2985 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002987 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2988 __ addl(out.AsRegister<CpuRegister>(),
2989 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2990 } else {
2991 __ leal(out.AsRegister<CpuRegister>(), Address(
2992 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2993 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002994 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002995 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002997 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002998 break;
2999 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003000
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003001 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003002 if (second.IsRegister()) {
3003 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3004 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003005 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3006 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003007 } else {
3008 __ leaq(out.AsRegister<CpuRegister>(), Address(
3009 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3010 }
3011 } else {
3012 DCHECK(second.IsConstant());
3013 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3014 int32_t int32_value = Low32Bits(value);
3015 DCHECK_EQ(int32_value, value);
3016 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3017 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3018 } else {
3019 __ leaq(out.AsRegister<CpuRegister>(), Address(
3020 first.AsRegister<CpuRegister>(), int32_value));
3021 }
3022 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003023 break;
3024 }
3025
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003026 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003027 if (second.IsFpuRegister()) {
3028 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3029 } else if (second.IsConstant()) {
3030 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003031 codegen_->LiteralFloatAddress(
3032 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003033 } else {
3034 DCHECK(second.IsStackSlot());
3035 __ addss(first.AsFpuRegister<XmmRegister>(),
3036 Address(CpuRegister(RSP), second.GetStackIndex()));
3037 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003038 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003039 }
3040
3041 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003042 if (second.IsFpuRegister()) {
3043 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3044 } else if (second.IsConstant()) {
3045 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003046 codegen_->LiteralDoubleAddress(
3047 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003048 } else {
3049 DCHECK(second.IsDoubleStackSlot());
3050 __ addsd(first.AsFpuRegister<XmmRegister>(),
3051 Address(CpuRegister(RSP), second.GetStackIndex()));
3052 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003053 break;
3054 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003055
3056 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003057 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 }
3059}
3060
3061void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003062 LocationSummary* locations =
3063 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003064 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003065 case Primitive::kPrimInt: {
3066 locations->SetInAt(0, Location::RequiresRegister());
3067 locations->SetInAt(1, Location::Any());
3068 locations->SetOut(Location::SameAsFirstInput());
3069 break;
3070 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003071 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003072 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003073 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003074 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003075 break;
3076 }
Calin Juravle11351682014-10-23 15:38:15 +01003077 case Primitive::kPrimFloat:
3078 case Primitive::kPrimDouble: {
3079 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003080 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003081 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003082 break;
Calin Juravle11351682014-10-23 15:38:15 +01003083 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003084 default:
Calin Juravle11351682014-10-23 15:38:15 +01003085 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003086 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003087}
3088
3089void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3090 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003091 Location first = locations->InAt(0);
3092 Location second = locations->InAt(1);
3093 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003094 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003095 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003096 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003098 } else if (second.IsConstant()) {
3099 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003100 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003101 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003103 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003104 break;
3105 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003106 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003107 if (second.IsConstant()) {
3108 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3109 DCHECK(IsInt<32>(value));
3110 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3111 } else {
3112 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3113 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003114 break;
3115 }
3116
Calin Juravle11351682014-10-23 15:38:15 +01003117 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003118 if (second.IsFpuRegister()) {
3119 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3120 } else if (second.IsConstant()) {
3121 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003122 codegen_->LiteralFloatAddress(
3123 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003124 } else {
3125 DCHECK(second.IsStackSlot());
3126 __ subss(first.AsFpuRegister<XmmRegister>(),
3127 Address(CpuRegister(RSP), second.GetStackIndex()));
3128 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003129 break;
Calin Juravle11351682014-10-23 15:38:15 +01003130 }
3131
3132 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003133 if (second.IsFpuRegister()) {
3134 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3135 } else if (second.IsConstant()) {
3136 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003137 codegen_->LiteralDoubleAddress(
3138 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003139 } else {
3140 DCHECK(second.IsDoubleStackSlot());
3141 __ subsd(first.AsFpuRegister<XmmRegister>(),
3142 Address(CpuRegister(RSP), second.GetStackIndex()));
3143 }
Calin Juravle11351682014-10-23 15:38:15 +01003144 break;
3145 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003146
3147 default:
Calin Juravle11351682014-10-23 15:38:15 +01003148 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003149 }
3150}
3151
Calin Juravle34bacdf2014-10-07 20:23:36 +01003152void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3153 LocationSummary* locations =
3154 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3155 switch (mul->GetResultType()) {
3156 case Primitive::kPrimInt: {
3157 locations->SetInAt(0, Location::RequiresRegister());
3158 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003159 if (mul->InputAt(1)->IsIntConstant()) {
3160 // Can use 3 operand multiply.
3161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3162 } else {
3163 locations->SetOut(Location::SameAsFirstInput());
3164 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 break;
3166 }
3167 case Primitive::kPrimLong: {
3168 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003169 locations->SetInAt(1, Location::Any());
3170 if (mul->InputAt(1)->IsLongConstant() &&
3171 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003172 // Can use 3 operand multiply.
3173 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3174 } else {
3175 locations->SetOut(Location::SameAsFirstInput());
3176 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003177 break;
3178 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003179 case Primitive::kPrimFloat:
3180 case Primitive::kPrimDouble: {
3181 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003182 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003183 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003185 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003186
3187 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003188 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189 }
3190}
3191
3192void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3193 LocationSummary* locations = mul->GetLocations();
3194 Location first = locations->InAt(0);
3195 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003196 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003198 case Primitive::kPrimInt:
3199 // The constant may have ended up in a register, so test explicitly to avoid
3200 // problems where the output may not be the same as the first operand.
3201 if (mul->InputAt(1)->IsIntConstant()) {
3202 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3203 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3204 } else if (second.IsRegister()) {
3205 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003206 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003208 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003209 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003210 __ imull(first.AsRegister<CpuRegister>(),
3211 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003212 }
3213 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003214 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003215 // The constant may have ended up in a register, so test explicitly to avoid
3216 // problems where the output may not be the same as the first operand.
3217 if (mul->InputAt(1)->IsLongConstant()) {
3218 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3219 if (IsInt<32>(value)) {
3220 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3221 Immediate(static_cast<int32_t>(value)));
3222 } else {
3223 // Have to use the constant area.
3224 DCHECK(first.Equals(out));
3225 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3226 }
3227 } else if (second.IsRegister()) {
3228 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003229 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003230 } else {
3231 DCHECK(second.IsDoubleStackSlot());
3232 DCHECK(first.Equals(out));
3233 __ imulq(first.AsRegister<CpuRegister>(),
3234 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003235 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003236 break;
3237 }
3238
Calin Juravleb5bfa962014-10-21 18:02:24 +01003239 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003240 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003241 if (second.IsFpuRegister()) {
3242 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3243 } else if (second.IsConstant()) {
3244 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003245 codegen_->LiteralFloatAddress(
3246 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003247 } else {
3248 DCHECK(second.IsStackSlot());
3249 __ mulss(first.AsFpuRegister<XmmRegister>(),
3250 Address(CpuRegister(RSP), second.GetStackIndex()));
3251 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003253 }
3254
3255 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003256 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003257 if (second.IsFpuRegister()) {
3258 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3259 } else if (second.IsConstant()) {
3260 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003261 codegen_->LiteralDoubleAddress(
3262 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003263 } else {
3264 DCHECK(second.IsDoubleStackSlot());
3265 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3266 Address(CpuRegister(RSP), second.GetStackIndex()));
3267 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003268 break;
3269 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003270
3271 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003272 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003273 }
3274}
3275
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003276void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3277 uint32_t stack_adjustment, bool is_float) {
3278 if (source.IsStackSlot()) {
3279 DCHECK(is_float);
3280 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3281 } else if (source.IsDoubleStackSlot()) {
3282 DCHECK(!is_float);
3283 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3284 } else {
3285 // Write the value to the temporary location on the stack and load to FP stack.
3286 if (is_float) {
3287 Location stack_temp = Location::StackSlot(temp_offset);
3288 codegen_->Move(stack_temp, source);
3289 __ flds(Address(CpuRegister(RSP), temp_offset));
3290 } else {
3291 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3292 codegen_->Move(stack_temp, source);
3293 __ fldl(Address(CpuRegister(RSP), temp_offset));
3294 }
3295 }
3296}
3297
3298void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3299 Primitive::Type type = rem->GetResultType();
3300 bool is_float = type == Primitive::kPrimFloat;
3301 size_t elem_size = Primitive::ComponentSize(type);
3302 LocationSummary* locations = rem->GetLocations();
3303 Location first = locations->InAt(0);
3304 Location second = locations->InAt(1);
3305 Location out = locations->Out();
3306
3307 // Create stack space for 2 elements.
3308 // TODO: enhance register allocator to ask for stack temporaries.
3309 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3310
3311 // Load the values to the FP stack in reverse order, using temporaries if needed.
3312 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3313 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3314
3315 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003316 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003317 __ Bind(&retry);
3318 __ fprem();
3319
3320 // Move FP status to AX.
3321 __ fstsw();
3322
3323 // And see if the argument reduction is complete. This is signaled by the
3324 // C2 FPU flag bit set to 0.
3325 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3326 __ j(kNotEqual, &retry);
3327
3328 // We have settled on the final value. Retrieve it into an XMM register.
3329 // Store FP top of stack to real stack.
3330 if (is_float) {
3331 __ fsts(Address(CpuRegister(RSP), 0));
3332 } else {
3333 __ fstl(Address(CpuRegister(RSP), 0));
3334 }
3335
3336 // Pop the 2 items from the FP stack.
3337 __ fucompp();
3338
3339 // Load the value from the stack into an XMM register.
3340 DCHECK(out.IsFpuRegister()) << out;
3341 if (is_float) {
3342 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3343 } else {
3344 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3345 }
3346
3347 // And remove the temporary stack space we allocated.
3348 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3349}
3350
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3352 DCHECK(instruction->IsDiv() || instruction->IsRem());
3353
3354 LocationSummary* locations = instruction->GetLocations();
3355 Location second = locations->InAt(1);
3356 DCHECK(second.IsConstant());
3357
3358 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3359 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003360 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003361
3362 DCHECK(imm == 1 || imm == -1);
3363
3364 switch (instruction->GetResultType()) {
3365 case Primitive::kPrimInt: {
3366 if (instruction->IsRem()) {
3367 __ xorl(output_register, output_register);
3368 } else {
3369 __ movl(output_register, input_register);
3370 if (imm == -1) {
3371 __ negl(output_register);
3372 }
3373 }
3374 break;
3375 }
3376
3377 case Primitive::kPrimLong: {
3378 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003379 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003380 } else {
3381 __ movq(output_register, input_register);
3382 if (imm == -1) {
3383 __ negq(output_register);
3384 }
3385 }
3386 break;
3387 }
3388
3389 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003390 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003391 }
3392}
3393
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003394void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003395 LocationSummary* locations = instruction->GetLocations();
3396 Location second = locations->InAt(1);
3397
3398 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3399 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3400
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003401 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003402 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3403 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003404
3405 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3406
3407 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003408 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003409 __ testl(numerator, numerator);
3410 __ cmov(kGreaterEqual, tmp, numerator);
3411 int shift = CTZ(imm);
3412 __ sarl(tmp, Immediate(shift));
3413
3414 if (imm < 0) {
3415 __ negl(tmp);
3416 }
3417
3418 __ movl(output_register, tmp);
3419 } else {
3420 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3421 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3422
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003423 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003424 __ addq(rdx, numerator);
3425 __ testq(numerator, numerator);
3426 __ cmov(kGreaterEqual, rdx, numerator);
3427 int shift = CTZ(imm);
3428 __ sarq(rdx, Immediate(shift));
3429
3430 if (imm < 0) {
3431 __ negq(rdx);
3432 }
3433
3434 __ movq(output_register, rdx);
3435 }
3436}
3437
3438void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3439 DCHECK(instruction->IsDiv() || instruction->IsRem());
3440
3441 LocationSummary* locations = instruction->GetLocations();
3442 Location second = locations->InAt(1);
3443
3444 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3445 : locations->GetTemp(0).AsRegister<CpuRegister>();
3446 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3447 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3448 : locations->Out().AsRegister<CpuRegister>();
3449 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3450
3451 DCHECK_EQ(RAX, eax.AsRegister());
3452 DCHECK_EQ(RDX, edx.AsRegister());
3453 if (instruction->IsDiv()) {
3454 DCHECK_EQ(RAX, out.AsRegister());
3455 } else {
3456 DCHECK_EQ(RDX, out.AsRegister());
3457 }
3458
3459 int64_t magic;
3460 int shift;
3461
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003462 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003463 if (instruction->GetResultType() == Primitive::kPrimInt) {
3464 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3465
3466 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3467
3468 __ movl(numerator, eax);
3469
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003470 __ movl(eax, Immediate(magic));
3471 __ imull(numerator);
3472
3473 if (imm > 0 && magic < 0) {
3474 __ addl(edx, numerator);
3475 } else if (imm < 0 && magic > 0) {
3476 __ subl(edx, numerator);
3477 }
3478
3479 if (shift != 0) {
3480 __ sarl(edx, Immediate(shift));
3481 }
3482
3483 __ movl(eax, edx);
3484 __ shrl(edx, Immediate(31));
3485 __ addl(edx, eax);
3486
3487 if (instruction->IsRem()) {
3488 __ movl(eax, numerator);
3489 __ imull(edx, Immediate(imm));
3490 __ subl(eax, edx);
3491 __ movl(edx, eax);
3492 } else {
3493 __ movl(eax, edx);
3494 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003495 } else {
3496 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3497
3498 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3499
3500 CpuRegister rax = eax;
3501 CpuRegister rdx = edx;
3502
3503 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3504
3505 // Save the numerator.
3506 __ movq(numerator, rax);
3507
3508 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003509 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510
3511 // RDX:RAX = magic * numerator
3512 __ imulq(numerator);
3513
3514 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003515 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003516 __ addq(rdx, numerator);
3517 } else if (imm < 0 && magic > 0) {
3518 // RDX -= numerator
3519 __ subq(rdx, numerator);
3520 }
3521
3522 // Shift if needed.
3523 if (shift != 0) {
3524 __ sarq(rdx, Immediate(shift));
3525 }
3526
3527 // RDX += 1 if RDX < 0
3528 __ movq(rax, rdx);
3529 __ shrq(rdx, Immediate(63));
3530 __ addq(rdx, rax);
3531
3532 if (instruction->IsRem()) {
3533 __ movq(rax, numerator);
3534
3535 if (IsInt<32>(imm)) {
3536 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3537 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003538 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539 }
3540
3541 __ subq(rax, rdx);
3542 __ movq(rdx, rax);
3543 } else {
3544 __ movq(rax, rdx);
3545 }
3546 }
3547}
3548
Calin Juravlebacfec32014-11-14 15:54:36 +00003549void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3550 DCHECK(instruction->IsDiv() || instruction->IsRem());
3551 Primitive::Type type = instruction->GetResultType();
3552 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3553
3554 bool is_div = instruction->IsDiv();
3555 LocationSummary* locations = instruction->GetLocations();
3556
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3558 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003559
Roland Levillain271ab9c2014-11-27 15:23:57 +00003560 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003561 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003562
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003563 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003564 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003565
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003566 if (imm == 0) {
3567 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3568 } else if (imm == 1 || imm == -1) {
3569 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003570 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003571 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003572 } else {
3573 DCHECK(imm <= -2 || imm >= 2);
3574 GenerateDivRemWithAnyConstant(instruction);
3575 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003576 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003577 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003578 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003579 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003581
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003582 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3583 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3584 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3585 // so it's safe to just use negl instead of more complex comparisons.
3586 if (type == Primitive::kPrimInt) {
3587 __ cmpl(second_reg, Immediate(-1));
3588 __ j(kEqual, slow_path->GetEntryLabel());
3589 // edx:eax <- sign-extended of eax
3590 __ cdq();
3591 // eax = quotient, edx = remainder
3592 __ idivl(second_reg);
3593 } else {
3594 __ cmpq(second_reg, Immediate(-1));
3595 __ j(kEqual, slow_path->GetEntryLabel());
3596 // rdx:rax <- sign-extended of rax
3597 __ cqo();
3598 // rax = quotient, rdx = remainder
3599 __ idivq(second_reg);
3600 }
3601 __ Bind(slow_path->GetExitLabel());
3602 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003603}
3604
Calin Juravle7c4954d2014-10-28 16:57:40 +00003605void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3606 LocationSummary* locations =
3607 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3608 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003609 case Primitive::kPrimInt:
3610 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003611 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003612 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003613 locations->SetOut(Location::SameAsFirstInput());
3614 // Intel uses edx:eax as the dividend.
3615 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003616 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3617 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3618 // output and request another temp.
3619 if (div->InputAt(1)->IsConstant()) {
3620 locations->AddTemp(Location::RequiresRegister());
3621 }
Calin Juravled0d48522014-11-04 16:40:20 +00003622 break;
3623 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003624
Calin Juravle7c4954d2014-10-28 16:57:40 +00003625 case Primitive::kPrimFloat:
3626 case Primitive::kPrimDouble: {
3627 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003628 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003629 locations->SetOut(Location::SameAsFirstInput());
3630 break;
3631 }
3632
3633 default:
3634 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3635 }
3636}
3637
3638void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3639 LocationSummary* locations = div->GetLocations();
3640 Location first = locations->InAt(0);
3641 Location second = locations->InAt(1);
3642 DCHECK(first.Equals(locations->Out()));
3643
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003644 Primitive::Type type = div->GetResultType();
3645 switch (type) {
3646 case Primitive::kPrimInt:
3647 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003649 break;
3650 }
3651
Calin Juravle7c4954d2014-10-28 16:57:40 +00003652 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003653 if (second.IsFpuRegister()) {
3654 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3655 } else if (second.IsConstant()) {
3656 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003657 codegen_->LiteralFloatAddress(
3658 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003659 } else {
3660 DCHECK(second.IsStackSlot());
3661 __ divss(first.AsFpuRegister<XmmRegister>(),
3662 Address(CpuRegister(RSP), second.GetStackIndex()));
3663 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664 break;
3665 }
3666
3667 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003668 if (second.IsFpuRegister()) {
3669 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3670 } else if (second.IsConstant()) {
3671 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003672 codegen_->LiteralDoubleAddress(
3673 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003674 } else {
3675 DCHECK(second.IsDoubleStackSlot());
3676 __ divsd(first.AsFpuRegister<XmmRegister>(),
3677 Address(CpuRegister(RSP), second.GetStackIndex()));
3678 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003679 break;
3680 }
3681
3682 default:
3683 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3684 }
3685}
3686
Calin Juravlebacfec32014-11-14 15:54:36 +00003687void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003688 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003689 LocationSummary* locations =
3690 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003691
3692 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003693 case Primitive::kPrimInt:
3694 case Primitive::kPrimLong: {
3695 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003696 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003697 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3698 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003699 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3700 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3701 // output and request another temp.
3702 if (rem->InputAt(1)->IsConstant()) {
3703 locations->AddTemp(Location::RequiresRegister());
3704 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003705 break;
3706 }
3707
3708 case Primitive::kPrimFloat:
3709 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003710 locations->SetInAt(0, Location::Any());
3711 locations->SetInAt(1, Location::Any());
3712 locations->SetOut(Location::RequiresFpuRegister());
3713 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003714 break;
3715 }
3716
3717 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003718 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003719 }
3720}
3721
3722void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3723 Primitive::Type type = rem->GetResultType();
3724 switch (type) {
3725 case Primitive::kPrimInt:
3726 case Primitive::kPrimLong: {
3727 GenerateDivRemIntegral(rem);
3728 break;
3729 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003730 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003731 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003732 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003733 break;
3734 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003735 default:
3736 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3737 }
3738}
3739
Calin Juravled0d48522014-11-04 16:40:20 +00003740void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003741 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3742 ? LocationSummary::kCallOnSlowPath
3743 : LocationSummary::kNoCall;
3744 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003745 locations->SetInAt(0, Location::Any());
3746 if (instruction->HasUses()) {
3747 locations->SetOut(Location::SameAsFirstInput());
3748 }
3749}
3750
3751void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003752 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003753 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3754 codegen_->AddSlowPath(slow_path);
3755
3756 LocationSummary* locations = instruction->GetLocations();
3757 Location value = locations->InAt(0);
3758
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003759 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003760 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003761 case Primitive::kPrimByte:
3762 case Primitive::kPrimChar:
3763 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003764 case Primitive::kPrimInt: {
3765 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003767 __ j(kEqual, slow_path->GetEntryLabel());
3768 } else if (value.IsStackSlot()) {
3769 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3770 __ j(kEqual, slow_path->GetEntryLabel());
3771 } else {
3772 DCHECK(value.IsConstant()) << value;
3773 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3774 __ jmp(slow_path->GetEntryLabel());
3775 }
3776 }
3777 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003778 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003779 case Primitive::kPrimLong: {
3780 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003781 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003782 __ j(kEqual, slow_path->GetEntryLabel());
3783 } else if (value.IsDoubleStackSlot()) {
3784 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3785 __ j(kEqual, slow_path->GetEntryLabel());
3786 } else {
3787 DCHECK(value.IsConstant()) << value;
3788 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3789 __ jmp(slow_path->GetEntryLabel());
3790 }
3791 }
3792 break;
3793 }
3794 default:
3795 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003796 }
Calin Juravled0d48522014-11-04 16:40:20 +00003797}
3798
Calin Juravle9aec02f2014-11-18 23:06:35 +00003799void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3800 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3801
3802 LocationSummary* locations =
3803 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3804
3805 switch (op->GetResultType()) {
3806 case Primitive::kPrimInt:
3807 case Primitive::kPrimLong: {
3808 locations->SetInAt(0, Location::RequiresRegister());
3809 // The shift count needs to be in CL.
3810 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3811 locations->SetOut(Location::SameAsFirstInput());
3812 break;
3813 }
3814 default:
3815 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3816 }
3817}
3818
3819void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3820 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3821
3822 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003823 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003824 Location second = locations->InAt(1);
3825
3826 switch (op->GetResultType()) {
3827 case Primitive::kPrimInt: {
3828 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003829 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830 if (op->IsShl()) {
3831 __ shll(first_reg, second_reg);
3832 } else if (op->IsShr()) {
3833 __ sarl(first_reg, second_reg);
3834 } else {
3835 __ shrl(first_reg, second_reg);
3836 }
3837 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003838 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003839 if (op->IsShl()) {
3840 __ shll(first_reg, imm);
3841 } else if (op->IsShr()) {
3842 __ sarl(first_reg, imm);
3843 } else {
3844 __ shrl(first_reg, imm);
3845 }
3846 }
3847 break;
3848 }
3849 case Primitive::kPrimLong: {
3850 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003852 if (op->IsShl()) {
3853 __ shlq(first_reg, second_reg);
3854 } else if (op->IsShr()) {
3855 __ sarq(first_reg, second_reg);
3856 } else {
3857 __ shrq(first_reg, second_reg);
3858 }
3859 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003860 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861 if (op->IsShl()) {
3862 __ shlq(first_reg, imm);
3863 } else if (op->IsShr()) {
3864 __ sarq(first_reg, imm);
3865 } else {
3866 __ shrq(first_reg, imm);
3867 }
3868 }
3869 break;
3870 }
3871 default:
3872 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003873 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003874 }
3875}
3876
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003877void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3878 LocationSummary* locations =
3879 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3880
3881 switch (ror->GetResultType()) {
3882 case Primitive::kPrimInt:
3883 case Primitive::kPrimLong: {
3884 locations->SetInAt(0, Location::RequiresRegister());
3885 // The shift count needs to be in CL (unless it is a constant).
3886 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3887 locations->SetOut(Location::SameAsFirstInput());
3888 break;
3889 }
3890 default:
3891 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3892 UNREACHABLE();
3893 }
3894}
3895
3896void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3897 LocationSummary* locations = ror->GetLocations();
3898 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3899 Location second = locations->InAt(1);
3900
3901 switch (ror->GetResultType()) {
3902 case Primitive::kPrimInt:
3903 if (second.IsRegister()) {
3904 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3905 __ rorl(first_reg, second_reg);
3906 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003907 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003908 __ rorl(first_reg, imm);
3909 }
3910 break;
3911 case Primitive::kPrimLong:
3912 if (second.IsRegister()) {
3913 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3914 __ rorq(first_reg, second_reg);
3915 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003916 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003917 __ rorq(first_reg, imm);
3918 }
3919 break;
3920 default:
3921 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3922 UNREACHABLE();
3923 }
3924}
3925
Calin Juravle9aec02f2014-11-18 23:06:35 +00003926void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3927 HandleShift(shl);
3928}
3929
3930void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3931 HandleShift(shl);
3932}
3933
3934void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3935 HandleShift(shr);
3936}
3937
3938void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3939 HandleShift(shr);
3940}
3941
3942void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3943 HandleShift(ushr);
3944}
3945
3946void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3947 HandleShift(ushr);
3948}
3949
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003950void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003951 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003953 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003954 if (instruction->IsStringAlloc()) {
3955 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3956 } else {
3957 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3958 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3959 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003960 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003961}
3962
3963void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003964 // Note: if heap poisoning is enabled, the entry point takes cares
3965 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003966 if (instruction->IsStringAlloc()) {
3967 // String is allocated through StringFactory. Call NewEmptyString entry point.
3968 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003969 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003970 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3971 __ call(Address(temp, code_offset.SizeValue()));
3972 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3973 } else {
3974 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3975 instruction,
3976 instruction->GetDexPc(),
3977 nullptr);
3978 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3979 DCHECK(!codegen_->IsLeafMethod());
3980 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003981}
3982
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003983void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3984 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003986 InvokeRuntimeCallingConvention calling_convention;
3987 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003988 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003989 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003990 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003991}
3992
3993void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3994 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003995 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3996 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003997 // Note: if heap poisoning is enabled, the entry point takes cares
3998 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003999 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4000 instruction,
4001 instruction->GetDexPc(),
4002 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004003 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004004
4005 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004006}
4007
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004008void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004009 LocationSummary* locations =
4010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004011 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4012 if (location.IsStackSlot()) {
4013 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4014 } else if (location.IsDoubleStackSlot()) {
4015 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4016 }
4017 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004018}
4019
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004020void InstructionCodeGeneratorX86_64::VisitParameterValue(
4021 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004022 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004023}
4024
4025void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4026 LocationSummary* locations =
4027 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4028 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4029}
4030
4031void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4032 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4033 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004034}
4035
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004036void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4037 LocationSummary* locations =
4038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4039 locations->SetInAt(0, Location::RequiresRegister());
4040 locations->SetOut(Location::RequiresRegister());
4041}
4042
4043void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4044 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004045 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004046 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004047 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004048 __ movq(locations->Out().AsRegister<CpuRegister>(),
4049 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004050 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004051 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004052 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004053 __ movq(locations->Out().AsRegister<CpuRegister>(),
4054 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4055 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004056 __ movq(locations->Out().AsRegister<CpuRegister>(),
4057 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004058 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004059}
4060
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004061void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004062 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004063 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004064 locations->SetInAt(0, Location::RequiresRegister());
4065 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004066}
4067
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004068void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4069 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4071 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004072 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004073 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004074 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004076 break;
4077
4078 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004079 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004080 break;
4081
4082 default:
4083 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4084 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004085}
4086
David Brazdil66d126e2015-04-03 16:02:44 +01004087void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4088 LocationSummary* locations =
4089 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4090 locations->SetInAt(0, Location::RequiresRegister());
4091 locations->SetOut(Location::SameAsFirstInput());
4092}
4093
4094void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004095 LocationSummary* locations = bool_not->GetLocations();
4096 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4097 locations->Out().AsRegister<CpuRegister>().AsRegister());
4098 Location out = locations->Out();
4099 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4100}
4101
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004102void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004103 LocationSummary* locations =
4104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004105 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004106 locations->SetInAt(i, Location::Any());
4107 }
4108 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004109}
4110
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004111void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004112 LOG(FATAL) << "Unimplemented";
4113}
4114
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004115void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004116 /*
4117 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004118 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004119 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4120 */
4121 switch (kind) {
4122 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004123 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004124 break;
4125 }
4126 case MemBarrierKind::kAnyStore:
4127 case MemBarrierKind::kLoadAny:
4128 case MemBarrierKind::kStoreStore: {
4129 // nop
4130 break;
4131 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004132 case MemBarrierKind::kNTStoreStore:
4133 // Non-Temporal Store/Store needs an explicit fence.
4134 MemoryFence(/* non-temporal */ true);
4135 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004136 }
4137}
4138
4139void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4140 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4141
Roland Levillain0d5a2812015-11-13 10:07:31 +00004142 bool object_field_get_with_read_barrier =
4143 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004144 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004145 new (GetGraph()->GetArena()) LocationSummary(instruction,
4146 object_field_get_with_read_barrier ?
4147 LocationSummary::kCallOnSlowPath :
4148 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004149 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4150 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4151 }
Calin Juravle52c48962014-12-16 17:02:57 +00004152 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004153 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4154 locations->SetOut(Location::RequiresFpuRegister());
4155 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004156 // The output overlaps for an object field get when read barriers
4157 // are enabled: we do not want the move to overwrite the object's
4158 // location, as we need it to emit the read barrier.
4159 locations->SetOut(
4160 Location::RequiresRegister(),
4161 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004162 }
Calin Juravle52c48962014-12-16 17:02:57 +00004163}
4164
4165void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4166 const FieldInfo& field_info) {
4167 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4168
4169 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004170 Location base_loc = locations->InAt(0);
4171 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004172 Location out = locations->Out();
4173 bool is_volatile = field_info.IsVolatile();
4174 Primitive::Type field_type = field_info.GetFieldType();
4175 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4176
4177 switch (field_type) {
4178 case Primitive::kPrimBoolean: {
4179 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4180 break;
4181 }
4182
4183 case Primitive::kPrimByte: {
4184 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4185 break;
4186 }
4187
4188 case Primitive::kPrimShort: {
4189 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4190 break;
4191 }
4192
4193 case Primitive::kPrimChar: {
4194 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4195 break;
4196 }
4197
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004198 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004199 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4200 break;
4201 }
4202
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004203 case Primitive::kPrimNot: {
4204 // /* HeapReference<Object> */ out = *(base + offset)
4205 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004206 // Note that a potential implicit null check is handled in this
4207 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4208 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004209 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004210 if (is_volatile) {
4211 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4212 }
4213 } else {
4214 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4215 codegen_->MaybeRecordImplicitNullCheck(instruction);
4216 if (is_volatile) {
4217 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4218 }
4219 // If read barriers are enabled, emit read barriers other than
4220 // Baker's using a slow path (and also unpoison the loaded
4221 // reference, if heap poisoning is enabled).
4222 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4223 }
4224 break;
4225 }
4226
Calin Juravle52c48962014-12-16 17:02:57 +00004227 case Primitive::kPrimLong: {
4228 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4229 break;
4230 }
4231
4232 case Primitive::kPrimFloat: {
4233 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4234 break;
4235 }
4236
4237 case Primitive::kPrimDouble: {
4238 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4239 break;
4240 }
4241
4242 case Primitive::kPrimVoid:
4243 LOG(FATAL) << "Unreachable type " << field_type;
4244 UNREACHABLE();
4245 }
4246
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004247 if (field_type == Primitive::kPrimNot) {
4248 // Potential implicit null checks, in the case of reference
4249 // fields, are handled in the previous switch statement.
4250 } else {
4251 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004252 }
Roland Levillain4d027112015-07-01 15:41:14 +01004253
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004254 if (is_volatile) {
4255 if (field_type == Primitive::kPrimNot) {
4256 // Memory barriers, in the case of references, are also handled
4257 // in the previous switch statement.
4258 } else {
4259 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4260 }
Roland Levillain4d027112015-07-01 15:41:14 +01004261 }
Calin Juravle52c48962014-12-16 17:02:57 +00004262}
4263
4264void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4265 const FieldInfo& field_info) {
4266 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4267
4268 LocationSummary* locations =
4269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004270 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004271 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004272 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004273 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004274
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004275 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004276 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004277 if (is_volatile) {
4278 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4279 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4280 } else {
4281 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4282 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004283 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004284 if (is_volatile) {
4285 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4286 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4287 } else {
4288 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4289 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004290 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004291 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004292 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004293 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004294 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004295 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4296 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004297 locations->AddTemp(Location::RequiresRegister());
4298 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299}
4300
Calin Juravle52c48962014-12-16 17:02:57 +00004301void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004302 const FieldInfo& field_info,
4303 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004304 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4305
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004307 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4308 Location value = locations->InAt(1);
4309 bool is_volatile = field_info.IsVolatile();
4310 Primitive::Type field_type = field_info.GetFieldType();
4311 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4312
4313 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004314 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004315 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316
Mark Mendellea5af682015-10-22 17:35:49 -04004317 bool maybe_record_implicit_null_check_done = false;
4318
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319 switch (field_type) {
4320 case Primitive::kPrimBoolean:
4321 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004322 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004323 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004324 __ movb(Address(base, offset), Immediate(v));
4325 } else {
4326 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4327 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004328 break;
4329 }
4330
4331 case Primitive::kPrimShort:
4332 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004333 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004334 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004335 __ movw(Address(base, offset), Immediate(v));
4336 } else {
4337 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4338 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339 break;
4340 }
4341
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004342 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004343 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004344 if (value.IsConstant()) {
4345 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004346 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4347 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4348 // Note: if heap poisoning is enabled, no need to poison
4349 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004350 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004351 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004352 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4353 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4354 __ movl(temp, value.AsRegister<CpuRegister>());
4355 __ PoisonHeapReference(temp);
4356 __ movl(Address(base, offset), temp);
4357 } else {
4358 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4359 }
Mark Mendell40741f32015-04-20 22:10:34 -04004360 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361 break;
4362 }
4363
4364 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004365 if (value.IsConstant()) {
4366 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004367 codegen_->MoveInt64ToAddress(Address(base, offset),
4368 Address(base, offset + sizeof(int32_t)),
4369 v,
4370 instruction);
4371 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004372 } else {
4373 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4374 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375 break;
4376 }
4377
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004378 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004379 if (value.IsConstant()) {
4380 int32_t v =
4381 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4382 __ movl(Address(base, offset), Immediate(v));
4383 } else {
4384 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4385 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004386 break;
4387 }
4388
4389 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004390 if (value.IsConstant()) {
4391 int64_t v =
4392 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4393 codegen_->MoveInt64ToAddress(Address(base, offset),
4394 Address(base, offset + sizeof(int32_t)),
4395 v,
4396 instruction);
4397 maybe_record_implicit_null_check_done = true;
4398 } else {
4399 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4400 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004401 break;
4402 }
4403
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404 case Primitive::kPrimVoid:
4405 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004406 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407 }
Calin Juravle52c48962014-12-16 17:02:57 +00004408
Mark Mendellea5af682015-10-22 17:35:49 -04004409 if (!maybe_record_implicit_null_check_done) {
4410 codegen_->MaybeRecordImplicitNullCheck(instruction);
4411 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004412
4413 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4414 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4415 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004416 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004417 }
4418
Calin Juravle52c48962014-12-16 17:02:57 +00004419 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004420 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004421 }
4422}
4423
4424void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4425 HandleFieldSet(instruction, instruction->GetFieldInfo());
4426}
4427
4428void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004429 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430}
4431
4432void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004433 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004434}
4435
4436void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004437 HandleFieldGet(instruction, instruction->GetFieldInfo());
4438}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439
Calin Juravle52c48962014-12-16 17:02:57 +00004440void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4441 HandleFieldGet(instruction);
4442}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443
Calin Juravle52c48962014-12-16 17:02:57 +00004444void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4445 HandleFieldGet(instruction, instruction->GetFieldInfo());
4446}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447
Calin Juravle52c48962014-12-16 17:02:57 +00004448void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4449 HandleFieldSet(instruction, instruction->GetFieldInfo());
4450}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004451
Calin Juravle52c48962014-12-16 17:02:57 +00004452void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004453 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004454}
4455
Calin Juravlee460d1d2015-09-29 04:52:17 +01004456void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4457 HUnresolvedInstanceFieldGet* instruction) {
4458 FieldAccessCallingConventionX86_64 calling_convention;
4459 codegen_->CreateUnresolvedFieldLocationSummary(
4460 instruction, instruction->GetFieldType(), calling_convention);
4461}
4462
4463void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4464 HUnresolvedInstanceFieldGet* instruction) {
4465 FieldAccessCallingConventionX86_64 calling_convention;
4466 codegen_->GenerateUnresolvedFieldAccess(instruction,
4467 instruction->GetFieldType(),
4468 instruction->GetFieldIndex(),
4469 instruction->GetDexPc(),
4470 calling_convention);
4471}
4472
4473void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4474 HUnresolvedInstanceFieldSet* instruction) {
4475 FieldAccessCallingConventionX86_64 calling_convention;
4476 codegen_->CreateUnresolvedFieldLocationSummary(
4477 instruction, instruction->GetFieldType(), calling_convention);
4478}
4479
4480void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4481 HUnresolvedInstanceFieldSet* instruction) {
4482 FieldAccessCallingConventionX86_64 calling_convention;
4483 codegen_->GenerateUnresolvedFieldAccess(instruction,
4484 instruction->GetFieldType(),
4485 instruction->GetFieldIndex(),
4486 instruction->GetDexPc(),
4487 calling_convention);
4488}
4489
4490void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4491 HUnresolvedStaticFieldGet* instruction) {
4492 FieldAccessCallingConventionX86_64 calling_convention;
4493 codegen_->CreateUnresolvedFieldLocationSummary(
4494 instruction, instruction->GetFieldType(), calling_convention);
4495}
4496
4497void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4498 HUnresolvedStaticFieldGet* instruction) {
4499 FieldAccessCallingConventionX86_64 calling_convention;
4500 codegen_->GenerateUnresolvedFieldAccess(instruction,
4501 instruction->GetFieldType(),
4502 instruction->GetFieldIndex(),
4503 instruction->GetDexPc(),
4504 calling_convention);
4505}
4506
4507void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4508 HUnresolvedStaticFieldSet* instruction) {
4509 FieldAccessCallingConventionX86_64 calling_convention;
4510 codegen_->CreateUnresolvedFieldLocationSummary(
4511 instruction, instruction->GetFieldType(), calling_convention);
4512}
4513
4514void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4515 HUnresolvedStaticFieldSet* instruction) {
4516 FieldAccessCallingConventionX86_64 calling_convention;
4517 codegen_->GenerateUnresolvedFieldAccess(instruction,
4518 instruction->GetFieldType(),
4519 instruction->GetFieldIndex(),
4520 instruction->GetDexPc(),
4521 calling_convention);
4522}
4523
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004524void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004525 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4526 ? LocationSummary::kCallOnSlowPath
4527 : LocationSummary::kNoCall;
4528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4529 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004530 ? Location::RequiresRegister()
4531 : Location::Any();
4532 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004533 if (instruction->HasUses()) {
4534 locations->SetOut(Location::SameAsFirstInput());
4535 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536}
4537
Calin Juravle2ae48182016-03-16 14:05:09 +00004538void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4539 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004540 return;
4541 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004542 LocationSummary* locations = instruction->GetLocations();
4543 Location obj = locations->InAt(0);
4544
4545 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004546 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004547}
4548
Calin Juravle2ae48182016-03-16 14:05:09 +00004549void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004550 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004551 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552
4553 LocationSummary* locations = instruction->GetLocations();
4554 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555
4556 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004557 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004558 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004559 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004560 } else {
4561 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004562 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004563 __ jmp(slow_path->GetEntryLabel());
4564 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004565 }
4566 __ j(kEqual, slow_path->GetEntryLabel());
4567}
4568
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004569void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004570 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004571}
4572
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004573void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004574 bool object_array_get_with_read_barrier =
4575 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004576 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004577 new (GetGraph()->GetArena()) LocationSummary(instruction,
4578 object_array_get_with_read_barrier ?
4579 LocationSummary::kCallOnSlowPath :
4580 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004581 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4582 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4583 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004584 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004585 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004586 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4587 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4588 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004589 // The output overlaps for an object array get when read barriers
4590 // are enabled: we do not want the move to overwrite the array's
4591 // location, as we need it to emit the read barrier.
4592 locations->SetOut(
4593 Location::RequiresRegister(),
4594 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004595 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596}
4597
4598void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4599 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004600 Location obj_loc = locations->InAt(0);
4601 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004603 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004604 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004606 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004607 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004608 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004609 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 if (index.IsConstant()) {
4611 __ movzxb(out, Address(obj,
4612 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4613 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004614 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004615 }
4616 break;
4617 }
4618
4619 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004620 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621 if (index.IsConstant()) {
4622 __ movsxb(out, Address(obj,
4623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4624 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004625 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 }
4627 break;
4628 }
4629
4630 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004631 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 if (index.IsConstant()) {
4633 __ movsxw(out, Address(obj,
4634 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4635 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004636 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637 }
4638 break;
4639 }
4640
4641 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004642 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 if (index.IsConstant()) {
4644 __ movzxw(out, Address(obj,
4645 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4646 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004647 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004648 }
4649 break;
4650 }
4651
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004652 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004653 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004654 if (index.IsConstant()) {
4655 __ movl(out, Address(obj,
4656 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4657 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004658 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004659 }
4660 break;
4661 }
4662
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004663 case Primitive::kPrimNot: {
4664 static_assert(
4665 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4666 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004667 // /* HeapReference<Object> */ out =
4668 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4669 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004670 // Note that a potential implicit null check is handled in this
4671 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4672 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004673 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004674 } else {
4675 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4676 if (index.IsConstant()) {
4677 uint32_t offset =
4678 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4679 __ movl(out, Address(obj, offset));
4680 codegen_->MaybeRecordImplicitNullCheck(instruction);
4681 // If read barriers are enabled, emit read barriers other than
4682 // Baker's using a slow path (and also unpoison the loaded
4683 // reference, if heap poisoning is enabled).
4684 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4685 } else {
4686 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4687 codegen_->MaybeRecordImplicitNullCheck(instruction);
4688 // If read barriers are enabled, emit read barriers other than
4689 // Baker's using a slow path (and also unpoison the loaded
4690 // reference, if heap poisoning is enabled).
4691 codegen_->MaybeGenerateReadBarrierSlow(
4692 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4693 }
4694 }
4695 break;
4696 }
4697
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004699 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700 if (index.IsConstant()) {
4701 __ movq(out, Address(obj,
4702 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4703 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004704 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004705 }
4706 break;
4707 }
4708
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004709 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004710 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004711 if (index.IsConstant()) {
4712 __ movss(out, Address(obj,
4713 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4714 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004715 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004716 }
4717 break;
4718 }
4719
4720 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004721 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004722 if (index.IsConstant()) {
4723 __ movsd(out, Address(obj,
4724 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4725 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004726 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004727 }
4728 break;
4729 }
4730
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004731 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004732 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004733 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004734 }
Roland Levillain4d027112015-07-01 15:41:14 +01004735
4736 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004737 // Potential implicit null checks, in the case of reference
4738 // arrays, are handled in the previous switch statement.
4739 } else {
4740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004741 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004742}
4743
4744void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004745 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004746
4747 bool needs_write_barrier =
4748 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004749 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004750
Nicolas Geoffray39468442014-09-02 15:17:15 +01004751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004752 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004753 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004754 LocationSummary::kCallOnSlowPath :
4755 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004756
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004757 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004758 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4759 if (Primitive::IsFloatingPointType(value_type)) {
4760 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004761 } else {
4762 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4763 }
4764
4765 if (needs_write_barrier) {
4766 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004767
4768 // This first temporary register is possibly used for heap
4769 // reference poisoning and/or read barrier emission too.
4770 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004771 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004772 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773}
4774
4775void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4776 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004777 Location array_loc = locations->InAt(0);
4778 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004780 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004781 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004782 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004783 bool needs_write_barrier =
4784 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004785 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4786 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4787 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004788
4789 switch (value_type) {
4790 case Primitive::kPrimBoolean:
4791 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004792 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4793 Address address = index.IsConstant()
4794 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4795 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4796 if (value.IsRegister()) {
4797 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004801 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004802 break;
4803 }
4804
4805 case Primitive::kPrimShort:
4806 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004807 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4808 Address address = index.IsConstant()
4809 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4810 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4811 if (value.IsRegister()) {
4812 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004813 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004814 DCHECK(value.IsConstant()) << value;
4815 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004816 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818 break;
4819 }
4820
4821 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004822 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4823 Address address = index.IsConstant()
4824 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4825 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004826
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 if (!value.IsRegister()) {
4828 // Just setting null.
4829 DCHECK(instruction->InputAt(2)->IsNullConstant());
4830 DCHECK(value.IsConstant()) << value;
4831 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004833 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004834 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004836 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837
4838 DCHECK(needs_write_barrier);
4839 CpuRegister register_value = value.AsRegister<CpuRegister>();
4840 NearLabel done, not_null, do_put;
4841 SlowPathCode* slow_path = nullptr;
4842 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004843 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4845 codegen_->AddSlowPath(slow_path);
4846 if (instruction->GetValueCanBeNull()) {
4847 __ testl(register_value, register_value);
4848 __ j(kNotEqual, &not_null);
4849 __ movl(address, Immediate(0));
4850 codegen_->MaybeRecordImplicitNullCheck(instruction);
4851 __ jmp(&done);
4852 __ Bind(&not_null);
4853 }
4854
Roland Levillain0d5a2812015-11-13 10:07:31 +00004855 if (kEmitCompilerReadBarrier) {
4856 // When read barriers are enabled, the type checking
4857 // instrumentation requires two read barriers:
4858 //
4859 // __ movl(temp2, temp);
4860 // // /* HeapReference<Class> */ temp = temp->component_type_
4861 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004862 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004863 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4864 //
4865 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4866 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004867 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004868 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4869 //
4870 // __ cmpl(temp, temp2);
4871 //
4872 // However, the second read barrier may trash `temp`, as it
4873 // is a temporary register, and as such would not be saved
4874 // along with live registers before calling the runtime (nor
4875 // restored afterwards). So in this case, we bail out and
4876 // delegate the work to the array set slow path.
4877 //
4878 // TODO: Extend the register allocator to support a new
4879 // "(locally) live temp" location so as to avoid always
4880 // going into the slow path when read barriers are enabled.
4881 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004882 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004883 // /* HeapReference<Class> */ temp = array->klass_
4884 __ movl(temp, Address(array, class_offset));
4885 codegen_->MaybeRecordImplicitNullCheck(instruction);
4886 __ MaybeUnpoisonHeapReference(temp);
4887
4888 // /* HeapReference<Class> */ temp = temp->component_type_
4889 __ movl(temp, Address(temp, component_offset));
4890 // If heap poisoning is enabled, no need to unpoison `temp`
4891 // nor the object reference in `register_value->klass`, as
4892 // we are comparing two poisoned references.
4893 __ cmpl(temp, Address(register_value, class_offset));
4894
4895 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4896 __ j(kEqual, &do_put);
4897 // If heap poisoning is enabled, the `temp` reference has
4898 // not been unpoisoned yet; unpoison it now.
4899 __ MaybeUnpoisonHeapReference(temp);
4900
4901 // /* HeapReference<Class> */ temp = temp->super_class_
4902 __ movl(temp, Address(temp, super_offset));
4903 // If heap poisoning is enabled, no need to unpoison
4904 // `temp`, as we are comparing against null below.
4905 __ testl(temp, temp);
4906 __ j(kNotEqual, slow_path->GetEntryLabel());
4907 __ Bind(&do_put);
4908 } else {
4909 __ j(kNotEqual, slow_path->GetEntryLabel());
4910 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 }
4912 }
4913
4914 if (kPoisonHeapReferences) {
4915 __ movl(temp, register_value);
4916 __ PoisonHeapReference(temp);
4917 __ movl(address, temp);
4918 } else {
4919 __ movl(address, register_value);
4920 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004921 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004922 codegen_->MaybeRecordImplicitNullCheck(instruction);
4923 }
4924
4925 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4926 codegen_->MarkGCCard(
4927 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4928 __ Bind(&done);
4929
4930 if (slow_path != nullptr) {
4931 __ Bind(slow_path->GetExitLabel());
4932 }
4933
4934 break;
4935 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004936
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004937 case Primitive::kPrimInt: {
4938 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4939 Address address = index.IsConstant()
4940 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4941 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4942 if (value.IsRegister()) {
4943 __ movl(address, value.AsRegister<CpuRegister>());
4944 } else {
4945 DCHECK(value.IsConstant()) << value;
4946 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4947 __ movl(address, Immediate(v));
4948 }
4949 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004950 break;
4951 }
4952
4953 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004954 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4955 Address address = index.IsConstant()
4956 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4957 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4958 if (value.IsRegister()) {
4959 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004960 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004961 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004962 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004963 Address address_high = index.IsConstant()
4964 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4965 offset + sizeof(int32_t))
4966 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4967 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968 }
4969 break;
4970 }
4971
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004972 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004973 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4974 Address address = index.IsConstant()
4975 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4976 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004977 if (value.IsFpuRegister()) {
4978 __ movss(address, value.AsFpuRegister<XmmRegister>());
4979 } else {
4980 DCHECK(value.IsConstant());
4981 int32_t v =
4982 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4983 __ movl(address, Immediate(v));
4984 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004985 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004986 break;
4987 }
4988
4989 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004990 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4991 Address address = index.IsConstant()
4992 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4993 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004994 if (value.IsFpuRegister()) {
4995 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4996 codegen_->MaybeRecordImplicitNullCheck(instruction);
4997 } else {
4998 int64_t v =
4999 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5000 Address address_high = index.IsConstant()
5001 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5002 offset + sizeof(int32_t))
5003 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5004 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5005 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005006 break;
5007 }
5008
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 case Primitive::kPrimVoid:
5010 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005011 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005012 }
5013}
5014
5015void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005016 LocationSummary* locations =
5017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005018 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005019 if (!instruction->IsEmittedAtUseSite()) {
5020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5021 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005022}
5023
5024void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005025 if (instruction->IsEmittedAtUseSite()) {
5026 return;
5027 }
5028
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005030 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005031 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5032 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005033 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005034 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035}
5036
5037void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005038 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5039 ? LocationSummary::kCallOnSlowPath
5040 : LocationSummary::kNoCall;
5041 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005042 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005043 HInstruction* length = instruction->InputAt(1);
5044 if (!length->IsEmittedAtUseSite()) {
5045 locations->SetInAt(1, Location::RegisterOrConstant(length));
5046 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005047 if (instruction->HasUses()) {
5048 locations->SetOut(Location::SameAsFirstInput());
5049 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005050}
5051
5052void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5053 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005054 Location index_loc = locations->InAt(0);
5055 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005056 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057
Mark Mendell99dbd682015-04-22 16:18:52 -04005058 if (length_loc.IsConstant()) {
5059 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5060 if (index_loc.IsConstant()) {
5061 // BCE will remove the bounds check if we are guarenteed to pass.
5062 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5063 if (index < 0 || index >= length) {
5064 codegen_->AddSlowPath(slow_path);
5065 __ jmp(slow_path->GetEntryLabel());
5066 } else {
5067 // Some optimization after BCE may have generated this, and we should not
5068 // generate a bounds check if it is a valid range.
5069 }
5070 return;
5071 }
5072
5073 // We have to reverse the jump condition because the length is the constant.
5074 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5075 __ cmpl(index_reg, Immediate(length));
5076 codegen_->AddSlowPath(slow_path);
5077 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005078 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005079 HInstruction* array_length = instruction->InputAt(1);
5080 if (array_length->IsEmittedAtUseSite()) {
5081 // Address the length field in the array.
5082 DCHECK(array_length->IsArrayLength());
5083 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5084 Location array_loc = array_length->GetLocations()->InAt(0);
5085 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
5086 if (index_loc.IsConstant()) {
5087 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5088 __ cmpl(array_len, Immediate(value));
5089 } else {
5090 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5091 }
5092 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005093 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005094 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5095 if (index_loc.IsConstant()) {
5096 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5097 __ cmpl(length, Immediate(value));
5098 } else {
5099 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5100 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005101 }
5102 codegen_->AddSlowPath(slow_path);
5103 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005104 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105}
5106
5107void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5108 CpuRegister card,
5109 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005110 CpuRegister value,
5111 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005112 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005113 if (value_can_be_null) {
5114 __ testl(value, value);
5115 __ j(kEqual, &is_null);
5116 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005117 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005118 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005119 __ movq(temp, object);
5120 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005121 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005122 if (value_can_be_null) {
5123 __ Bind(&is_null);
5124 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125}
5126
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005127void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005128 LOG(FATAL) << "Unimplemented";
5129}
5130
5131void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005132 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5133}
5134
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005135void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005136 LocationSummary* locations =
5137 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5138 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005139}
5140
5141void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005142 HBasicBlock* block = instruction->GetBlock();
5143 if (block->GetLoopInformation() != nullptr) {
5144 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5145 // The back edge will generate the suspend check.
5146 return;
5147 }
5148 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5149 // The goto will generate the suspend check.
5150 return;
5151 }
5152 GenerateSuspendCheck(instruction, nullptr);
5153}
5154
5155void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5156 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005157 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005158 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5159 if (slow_path == nullptr) {
5160 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5161 instruction->SetSlowPath(slow_path);
5162 codegen_->AddSlowPath(slow_path);
5163 if (successor != nullptr) {
5164 DCHECK(successor->IsLoopHeader());
5165 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5166 }
5167 } else {
5168 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5169 }
5170
Andreas Gampe542451c2016-07-26 09:02:02 -07005171 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005172 /* no_rip */ true),
5173 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005174 if (successor == nullptr) {
5175 __ j(kNotEqual, slow_path->GetEntryLabel());
5176 __ Bind(slow_path->GetReturnLabel());
5177 } else {
5178 __ j(kEqual, codegen_->GetLabelOf(successor));
5179 __ jmp(slow_path->GetEntryLabel());
5180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005181}
5182
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005183X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5184 return codegen_->GetAssembler();
5185}
5186
5187void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005188 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005189 Location source = move->GetSource();
5190 Location destination = move->GetDestination();
5191
5192 if (source.IsRegister()) {
5193 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005194 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005195 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005196 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005197 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005198 } else {
5199 DCHECK(destination.IsDoubleStackSlot());
5200 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005201 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005202 }
5203 } else if (source.IsStackSlot()) {
5204 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005205 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005206 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005207 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005208 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005210 } else {
5211 DCHECK(destination.IsStackSlot());
5212 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5213 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5214 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005215 } else if (source.IsDoubleStackSlot()) {
5216 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005217 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005218 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005220 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5221 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005222 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005223 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005224 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5225 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5226 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005227 } else if (source.IsConstant()) {
5228 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005229 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5230 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005231 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005232 if (value == 0) {
5233 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5234 } else {
5235 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5236 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005237 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005238 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005239 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005240 }
5241 } else if (constant->IsLongConstant()) {
5242 int64_t value = constant->AsLongConstant()->GetValue();
5243 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005244 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005245 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005246 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005247 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005248 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005249 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005250 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005251 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005252 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005253 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005254 } else {
5255 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005256 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005257 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5258 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005259 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005261 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005262 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005263 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005264 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005265 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005266 } else {
5267 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005268 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005269 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005270 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005271 } else if (source.IsFpuRegister()) {
5272 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005273 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005274 } else if (destination.IsStackSlot()) {
5275 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005276 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005277 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005278 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005279 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005280 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005282 }
5283}
5284
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005285void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005286 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005287 __ movl(Address(CpuRegister(RSP), mem), reg);
5288 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005289}
5290
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005291void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005292 ScratchRegisterScope ensure_scratch(
5293 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5294
5295 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5296 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5297 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5298 Address(CpuRegister(RSP), mem2 + stack_offset));
5299 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5300 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5301 CpuRegister(ensure_scratch.GetRegister()));
5302}
5303
Mark Mendell8a1c7282015-06-29 15:41:28 -04005304void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5305 __ movq(CpuRegister(TMP), reg1);
5306 __ movq(reg1, reg2);
5307 __ movq(reg2, CpuRegister(TMP));
5308}
5309
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005310void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5311 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5312 __ movq(Address(CpuRegister(RSP), mem), reg);
5313 __ movq(reg, CpuRegister(TMP));
5314}
5315
5316void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5317 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005318 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005319
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005320 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5321 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5322 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5323 Address(CpuRegister(RSP), mem2 + stack_offset));
5324 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5325 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5326 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005327}
5328
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005329void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5330 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5331 __ movss(Address(CpuRegister(RSP), mem), reg);
5332 __ movd(reg, CpuRegister(TMP));
5333}
5334
5335void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5336 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5337 __ movsd(Address(CpuRegister(RSP), mem), reg);
5338 __ movd(reg, CpuRegister(TMP));
5339}
5340
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005341void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005342 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005343 Location source = move->GetSource();
5344 Location destination = move->GetDestination();
5345
5346 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005347 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005348 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005349 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005350 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005351 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005352 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005353 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5354 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005355 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005356 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005357 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005358 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5359 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005360 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005361 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5362 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5363 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005364 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005365 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005366 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005367 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005368 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005369 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005370 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005371 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005372 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005373 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005374 }
5375}
5376
5377
5378void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5379 __ pushq(CpuRegister(reg));
5380}
5381
5382
5383void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5384 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005385}
5386
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005387void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005388 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005389 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5390 Immediate(mirror::Class::kStatusInitialized));
5391 __ j(kLess, slow_path->GetEntryLabel());
5392 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005393 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005394}
5395
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005396HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5397 HLoadClass::LoadKind desired_class_load_kind) {
5398 if (kEmitCompilerReadBarrier) {
5399 switch (desired_class_load_kind) {
5400 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5401 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5402 case HLoadClass::LoadKind::kBootImageAddress:
5403 // TODO: Implement for read barrier.
5404 return HLoadClass::LoadKind::kDexCacheViaMethod;
5405 default:
5406 break;
5407 }
5408 }
5409 switch (desired_class_load_kind) {
5410 case HLoadClass::LoadKind::kReferrersClass:
5411 break;
5412 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5413 DCHECK(!GetCompilerOptions().GetCompilePic());
5414 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5415 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5416 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5417 DCHECK(GetCompilerOptions().GetCompilePic());
5418 break;
5419 case HLoadClass::LoadKind::kBootImageAddress:
5420 break;
5421 case HLoadClass::LoadKind::kDexCacheAddress:
5422 DCHECK(Runtime::Current()->UseJitCompilation());
5423 break;
5424 case HLoadClass::LoadKind::kDexCachePcRelative:
5425 DCHECK(!Runtime::Current()->UseJitCompilation());
5426 break;
5427 case HLoadClass::LoadKind::kDexCacheViaMethod:
5428 break;
5429 }
5430 return desired_class_load_kind;
5431}
5432
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005433void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005434 if (cls->NeedsAccessCheck()) {
5435 InvokeRuntimeCallingConvention calling_convention;
5436 CodeGenerator::CreateLoadClassLocationSummary(
5437 cls,
5438 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5439 Location::RegisterLocation(RAX),
5440 /* code_generator_supports_read_barrier */ true);
5441 return;
5442 }
5443
5444 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5445 ? LocationSummary::kCallOnSlowPath
5446 : LocationSummary::kNoCall;
5447 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005448 if (kUseBakerReadBarrier && !cls->NeedsEnvironment()) {
5449 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5450 }
5451
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005452 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5453 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5454 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5455 locations->SetInAt(0, Location::RequiresRegister());
5456 }
5457 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005458}
5459
5460void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005461 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005462 if (cls->NeedsAccessCheck()) {
5463 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5464 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5465 cls,
5466 cls->GetDexPc(),
5467 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005468 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005469 return;
5470 }
5471
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472 Location out_loc = locations->Out();
5473 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005474
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005475 bool generate_null_check = false;
5476 switch (cls->GetLoadKind()) {
5477 case HLoadClass::LoadKind::kReferrersClass: {
5478 DCHECK(!cls->CanCallRuntime());
5479 DCHECK(!cls->MustGenerateClinitCheck());
5480 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5481 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5482 GenerateGcRootFieldLoad(
5483 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5484 break;
5485 }
5486 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5487 DCHECK(!kEmitCompilerReadBarrier);
5488 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5489 codegen_->RecordTypePatch(cls);
5490 break;
5491 case HLoadClass::LoadKind::kBootImageAddress: {
5492 DCHECK(!kEmitCompilerReadBarrier);
5493 DCHECK_NE(cls->GetAddress(), 0u);
5494 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5495 __ movl(out, Immediate(address)); // Zero-extended.
5496 codegen_->RecordSimplePatch();
5497 break;
5498 }
5499 case HLoadClass::LoadKind::kDexCacheAddress: {
5500 DCHECK_NE(cls->GetAddress(), 0u);
5501 // /* GcRoot<mirror::Class> */ out = *address
5502 if (IsUint<32>(cls->GetAddress())) {
5503 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5504 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005505 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005506 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5507 __ movq(out, Immediate(cls->GetAddress()));
5508 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005509 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005510 generate_null_check = !cls->IsInDexCache();
5511 break;
5512 }
5513 case HLoadClass::LoadKind::kDexCachePcRelative: {
5514 uint32_t offset = cls->GetDexCacheElementOffset();
5515 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5516 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5517 /* no_rip */ false);
5518 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5519 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5520 generate_null_check = !cls->IsInDexCache();
5521 break;
5522 }
5523 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5524 // /* GcRoot<mirror::Class>[] */ out =
5525 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5526 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5527 __ movq(out,
5528 Address(current_method,
5529 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5530 // /* GcRoot<mirror::Class> */ out = out[type_index]
5531 GenerateGcRootFieldLoad(
5532 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5533 generate_null_check = !cls->IsInDexCache();
5534 break;
5535 }
5536 default:
5537 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5538 UNREACHABLE();
5539 }
5540
5541 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5542 DCHECK(cls->CanCallRuntime());
5543 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5544 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5545 codegen_->AddSlowPath(slow_path);
5546 if (generate_null_check) {
5547 __ testl(out, out);
5548 __ j(kEqual, slow_path->GetEntryLabel());
5549 }
5550 if (cls->MustGenerateClinitCheck()) {
5551 GenerateClassInitializationCheck(slow_path, out);
5552 } else {
5553 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005554 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005555 }
5556}
5557
5558void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5559 LocationSummary* locations =
5560 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5561 locations->SetInAt(0, Location::RequiresRegister());
5562 if (check->HasUses()) {
5563 locations->SetOut(Location::SameAsFirstInput());
5564 }
5565}
5566
5567void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005568 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005569 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005570 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005571 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005572 GenerateClassInitializationCheck(slow_path,
5573 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005574}
5575
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005576HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5577 HLoadString::LoadKind desired_string_load_kind) {
5578 if (kEmitCompilerReadBarrier) {
5579 switch (desired_string_load_kind) {
5580 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5581 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5582 case HLoadString::LoadKind::kBootImageAddress:
5583 // TODO: Implement for read barrier.
5584 return HLoadString::LoadKind::kDexCacheViaMethod;
5585 default:
5586 break;
5587 }
5588 }
5589 switch (desired_string_load_kind) {
5590 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5591 DCHECK(!GetCompilerOptions().GetCompilePic());
5592 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5593 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5594 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5595 DCHECK(GetCompilerOptions().GetCompilePic());
5596 break;
5597 case HLoadString::LoadKind::kBootImageAddress:
5598 break;
5599 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005600 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005601 break;
5602 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005603 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005604 break;
5605 case HLoadString::LoadKind::kDexCacheViaMethod:
5606 break;
5607 }
5608 return desired_string_load_kind;
5609}
5610
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005611void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005612 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005613 ? LocationSummary::kCallOnSlowPath
5614 : LocationSummary::kNoCall;
5615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005616 if (kUseBakerReadBarrier && !load->NeedsEnvironment()) {
5617 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5618 }
5619
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005620 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5621 locations->SetInAt(0, Location::RequiresRegister());
5622 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005623 locations->SetOut(Location::RequiresRegister());
5624}
5625
5626void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005627 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005628 Location out_loc = locations->Out();
5629 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005630
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005631 switch (load->GetLoadKind()) {
5632 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5633 DCHECK(!kEmitCompilerReadBarrier);
5634 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5635 codegen_->RecordStringPatch(load);
5636 return; // No dex cache slow path.
5637 }
5638 case HLoadString::LoadKind::kBootImageAddress: {
5639 DCHECK(!kEmitCompilerReadBarrier);
5640 DCHECK_NE(load->GetAddress(), 0u);
5641 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5642 __ movl(out, Immediate(address)); // Zero-extended.
5643 codegen_->RecordSimplePatch();
5644 return; // No dex cache slow path.
5645 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005646 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005647 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005648 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005649
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005650 // TODO: Re-add the compiler code to do string dex cache lookup again.
5651 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5652 codegen_->AddSlowPath(slow_path);
5653 __ jmp(slow_path->GetEntryLabel());
5654 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005655}
5656
David Brazdilcb1c0552015-08-04 16:22:25 +01005657static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005658 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005659 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005660}
5661
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005662void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5663 LocationSummary* locations =
5664 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5665 locations->SetOut(Location::RequiresRegister());
5666}
5667
5668void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005669 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5670}
5671
5672void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5673 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5674}
5675
5676void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5677 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005678}
5679
5680void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5681 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005683 InvokeRuntimeCallingConvention calling_convention;
5684 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5685}
5686
5687void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005688 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5689 instruction,
5690 instruction->GetDexPc(),
5691 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005692 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005693}
5694
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005695static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5696 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005697 !kUseBakerReadBarrier &&
5698 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005699 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5700 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5701}
5702
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005703void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005704 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005705 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005706 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005707 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 case TypeCheckKind::kExactCheck:
5709 case TypeCheckKind::kAbstractClassCheck:
5710 case TypeCheckKind::kClassHierarchyCheck:
5711 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005712 call_kind =
5713 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005714 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 break;
5716 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005717 case TypeCheckKind::kUnresolvedCheck:
5718 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005719 call_kind = LocationSummary::kCallOnSlowPath;
5720 break;
5721 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005724 if (baker_read_barrier_slow_path) {
5725 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5726 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005727 locations->SetInAt(0, Location::RequiresRegister());
5728 locations->SetInAt(1, Location::Any());
5729 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5730 locations->SetOut(Location::RequiresRegister());
5731 // When read barriers are enabled, we need a temporary register for
5732 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005733 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005734 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005735 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005736}
5737
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005738void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005739 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005740 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005741 Location obj_loc = locations->InAt(0);
5742 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005743 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005744 Location out_loc = locations->Out();
5745 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005746 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005747 locations->GetTemp(0) :
5748 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005749 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005750 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5751 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5752 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005753 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005755
5756 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005757 // Avoid null check if we know obj is not null.
5758 if (instruction->MustDoNullCheck()) {
5759 __ testl(obj, obj);
5760 __ j(kEqual, &zero);
5761 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005762
Roland Levillain0d5a2812015-11-13 10:07:31 +00005763 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005764 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005766 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005767 case TypeCheckKind::kExactCheck: {
5768 if (cls.IsRegister()) {
5769 __ cmpl(out, cls.AsRegister<CpuRegister>());
5770 } else {
5771 DCHECK(cls.IsStackSlot()) << cls;
5772 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5773 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005774 if (zero.IsLinked()) {
5775 // Classes must be equal for the instanceof to succeed.
5776 __ j(kNotEqual, &zero);
5777 __ movl(out, Immediate(1));
5778 __ jmp(&done);
5779 } else {
5780 __ setcc(kEqual, out);
5781 // setcc only sets the low byte.
5782 __ andl(out, Immediate(1));
5783 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005784 break;
5785 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 case TypeCheckKind::kAbstractClassCheck: {
5788 // If the class is abstract, we eagerly fetch the super class of the
5789 // object to avoid doing a comparison we know will fail.
5790 NearLabel loop, success;
5791 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005792 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005793 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 __ testl(out, out);
5795 // If `out` is null, we use it for the result, and jump to `done`.
5796 __ j(kEqual, &done);
5797 if (cls.IsRegister()) {
5798 __ cmpl(out, cls.AsRegister<CpuRegister>());
5799 } else {
5800 DCHECK(cls.IsStackSlot()) << cls;
5801 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5802 }
5803 __ j(kNotEqual, &loop);
5804 __ movl(out, Immediate(1));
5805 if (zero.IsLinked()) {
5806 __ jmp(&done);
5807 }
5808 break;
5809 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005810
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005811 case TypeCheckKind::kClassHierarchyCheck: {
5812 // Walk over the class hierarchy to find a match.
5813 NearLabel loop, success;
5814 __ Bind(&loop);
5815 if (cls.IsRegister()) {
5816 __ cmpl(out, cls.AsRegister<CpuRegister>());
5817 } else {
5818 DCHECK(cls.IsStackSlot()) << cls;
5819 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5820 }
5821 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005823 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005824 __ testl(out, out);
5825 __ j(kNotEqual, &loop);
5826 // If `out` is null, we use it for the result, and jump to `done`.
5827 __ jmp(&done);
5828 __ Bind(&success);
5829 __ movl(out, Immediate(1));
5830 if (zero.IsLinked()) {
5831 __ jmp(&done);
5832 }
5833 break;
5834 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005835
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005837 // Do an exact check.
5838 NearLabel exact_check;
5839 if (cls.IsRegister()) {
5840 __ cmpl(out, cls.AsRegister<CpuRegister>());
5841 } else {
5842 DCHECK(cls.IsStackSlot()) << cls;
5843 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5844 }
5845 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005846 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005848 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005849 __ testl(out, out);
5850 // If `out` is null, we use it for the result, and jump to `done`.
5851 __ j(kEqual, &done);
5852 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5853 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005854 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 __ movl(out, Immediate(1));
5856 __ jmp(&done);
5857 break;
5858 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 case TypeCheckKind::kArrayCheck: {
5861 if (cls.IsRegister()) {
5862 __ cmpl(out, cls.AsRegister<CpuRegister>());
5863 } else {
5864 DCHECK(cls.IsStackSlot()) << cls;
5865 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5866 }
5867 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005868 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5869 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 codegen_->AddSlowPath(slow_path);
5871 __ j(kNotEqual, slow_path->GetEntryLabel());
5872 __ movl(out, Immediate(1));
5873 if (zero.IsLinked()) {
5874 __ jmp(&done);
5875 }
5876 break;
5877 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878
Calin Juravle98893e12015-10-02 21:05:03 +01005879 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880 case TypeCheckKind::kInterfaceCheck: {
5881 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005882 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883 // cases.
5884 //
5885 // We cannot directly call the InstanceofNonTrivial runtime
5886 // entry point without resorting to a type checking slow path
5887 // here (i.e. by calling InvokeRuntime directly), as it would
5888 // require to assign fixed registers for the inputs of this
5889 // HInstanceOf instruction (following the runtime calling
5890 // convention), which might be cluttered by the potential first
5891 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005892 //
5893 // TODO: Introduce a new runtime entry point taking the object
5894 // to test (instead of its class) as argument, and let it deal
5895 // with the read barrier issues. This will let us refactor this
5896 // case of the `switch` code as it was previously (with a direct
5897 // call to the runtime not using a type checking slow path).
5898 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 DCHECK(locations->OnlyCallsOnSlowPath());
5900 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5901 /* is_fatal */ false);
5902 codegen_->AddSlowPath(slow_path);
5903 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005904 if (zero.IsLinked()) {
5905 __ jmp(&done);
5906 }
5907 break;
5908 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005909 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005910
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005911 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005912 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 __ xorl(out, out);
5914 }
5915
5916 if (done.IsLinked()) {
5917 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005918 }
5919
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005920 if (slow_path != nullptr) {
5921 __ Bind(slow_path->GetExitLabel());
5922 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005923}
5924
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005925void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005926 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5927 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005929 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 case TypeCheckKind::kExactCheck:
5932 case TypeCheckKind::kAbstractClassCheck:
5933 case TypeCheckKind::kClassHierarchyCheck:
5934 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005935 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5936 LocationSummary::kCallOnSlowPath :
5937 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01005938 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005939 break;
5940 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005941 case TypeCheckKind::kUnresolvedCheck:
5942 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 call_kind = LocationSummary::kCallOnSlowPath;
5944 break;
5945 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005947 if (baker_read_barrier_slow_path) {
5948 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5949 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950 locations->SetInAt(0, Location::RequiresRegister());
5951 locations->SetInAt(1, Location::Any());
5952 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5953 locations->AddTemp(Location::RequiresRegister());
5954 // When read barriers are enabled, we need an additional temporary
5955 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005956 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005958 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005959}
5960
5961void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005962 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964 Location obj_loc = locations->InAt(0);
5965 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005966 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005967 Location temp_loc = locations->GetTemp(0);
5968 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005969 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005970 locations->GetTemp(1) :
5971 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5973 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5974 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5975 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976
Roland Levillain0d5a2812015-11-13 10:07:31 +00005977 bool is_type_check_slow_path_fatal =
5978 (type_check_kind == TypeCheckKind::kExactCheck ||
5979 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5980 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5981 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5982 !instruction->CanThrowIntoCatchBlock();
5983 SlowPathCode* type_check_slow_path =
5984 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5985 is_type_check_slow_path_fatal);
5986 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987
Roland Levillain0d5a2812015-11-13 10:07:31 +00005988 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 case TypeCheckKind::kExactCheck:
5990 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005991 NearLabel done;
5992 // Avoid null check if we know obj is not null.
5993 if (instruction->MustDoNullCheck()) {
5994 __ testl(obj, obj);
5995 __ j(kEqual, &done);
5996 }
5997
5998 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005999 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006000
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006001 if (cls.IsRegister()) {
6002 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6003 } else {
6004 DCHECK(cls.IsStackSlot()) << cls;
6005 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6006 }
6007 // Jump to slow path for throwing the exception or doing a
6008 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006010 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 break;
6012 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006015 NearLabel done;
6016 // Avoid null check if we know obj is not null.
6017 if (instruction->MustDoNullCheck()) {
6018 __ testl(obj, obj);
6019 __ j(kEqual, &done);
6020 }
6021
6022 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006023 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006024
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 // If the class is abstract, we eagerly fetch the super class of the
6026 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006027 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006029 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006030 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031
6032 // If the class reference currently in `temp` is not null, jump
6033 // to the `compare_classes` label to compare it with the checked
6034 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006035 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006036 __ j(kNotEqual, &compare_classes);
6037 // Otherwise, jump to the slow path to throw the exception.
6038 //
6039 // But before, move back the object's class into `temp` before
6040 // going into the slow path, as it has been overwritten in the
6041 // meantime.
6042 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006043 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044 __ jmp(type_check_slow_path->GetEntryLabel());
6045
6046 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 if (cls.IsRegister()) {
6048 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6049 } else {
6050 DCHECK(cls.IsStackSlot()) << cls;
6051 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6052 }
6053 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006054 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006055 break;
6056 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006057
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006058 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006059 NearLabel done;
6060 // Avoid null check if we know obj is not null.
6061 if (instruction->MustDoNullCheck()) {
6062 __ testl(obj, obj);
6063 __ j(kEqual, &done);
6064 }
6065
6066 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006067 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006068
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006069 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006070 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006071 __ Bind(&loop);
6072 if (cls.IsRegister()) {
6073 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6074 } else {
6075 DCHECK(cls.IsStackSlot()) << cls;
6076 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6077 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006078 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079
Roland Levillain0d5a2812015-11-13 10:07:31 +00006080 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006081 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006082
6083 // If the class reference currently in `temp` is not null, jump
6084 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085 __ testl(temp, temp);
6086 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087 // Otherwise, jump to the slow path to throw the exception.
6088 //
6089 // But before, move back the object's class into `temp` before
6090 // going into the slow path, as it has been overwritten in the
6091 // meantime.
6092 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006093 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006094 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006095 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006096 break;
6097 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006098
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006099 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006100 // We cannot use a NearLabel here, as its range might be too
6101 // short in some cases when read barriers are enabled. This has
6102 // been observed for instance when the code emitted for this
6103 // case uses high x86-64 registers (R8-R15).
6104 Label done;
6105 // Avoid null check if we know obj is not null.
6106 if (instruction->MustDoNullCheck()) {
6107 __ testl(obj, obj);
6108 __ j(kEqual, &done);
6109 }
6110
6111 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006112 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006113
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006114 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006115 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006116 if (cls.IsRegister()) {
6117 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6118 } else {
6119 DCHECK(cls.IsStackSlot()) << cls;
6120 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6121 }
6122 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123
6124 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006125 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006126 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006127
6128 // If the component type is not null (i.e. the object is indeed
6129 // an array), jump to label `check_non_primitive_component_type`
6130 // to further check that this component type is not a primitive
6131 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006132 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006133 __ j(kNotEqual, &check_non_primitive_component_type);
6134 // Otherwise, jump to the slow path to throw the exception.
6135 //
6136 // But before, move back the object's class into `temp` before
6137 // going into the slow path, as it has been overwritten in the
6138 // meantime.
6139 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006140 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141 __ jmp(type_check_slow_path->GetEntryLabel());
6142
6143 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006144 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006145 __ j(kEqual, &done);
6146 // Same comment as above regarding `temp` and the slow path.
6147 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006148 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006150 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006151 break;
6152 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153
Calin Juravle98893e12015-10-02 21:05:03 +01006154 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006156 NearLabel done;
6157 // Avoid null check if we know obj is not null.
6158 if (instruction->MustDoNullCheck()) {
6159 __ testl(obj, obj);
6160 __ j(kEqual, &done);
6161 }
6162
6163 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006164 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006165
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006166 // We always go into the type check slow path for the unresolved
6167 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 //
6169 // We cannot directly call the CheckCast runtime entry point
6170 // without resorting to a type checking slow path here (i.e. by
6171 // calling InvokeRuntime directly), as it would require to
6172 // assign fixed registers for the inputs of this HInstanceOf
6173 // instruction (following the runtime calling convention), which
6174 // might be cluttered by the potential first read barrier
6175 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006176 //
6177 // TODO: Introduce a new runtime entry point taking the object
6178 // to test (instead of its class) as argument, and let it deal
6179 // with the read barrier issues. This will let us refactor this
6180 // case of the `switch` code as it was previously (with a direct
6181 // call to the runtime not using a type checking slow path).
6182 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006184 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006185 break;
6186 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006187
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006189}
6190
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006191void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6192 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006193 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006194 InvokeRuntimeCallingConvention calling_convention;
6195 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6196}
6197
6198void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006199 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6200 : QUICK_ENTRY_POINT(pUnlockObject),
6201 instruction,
6202 instruction->GetDexPc(),
6203 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006204 if (instruction->IsEnter()) {
6205 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6206 } else {
6207 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6208 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006209}
6210
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006211void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6212void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6213void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6214
6215void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6216 LocationSummary* locations =
6217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6218 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6219 || instruction->GetResultType() == Primitive::kPrimLong);
6220 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006221 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006222 locations->SetOut(Location::SameAsFirstInput());
6223}
6224
6225void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6226 HandleBitwiseOperation(instruction);
6227}
6228
6229void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6230 HandleBitwiseOperation(instruction);
6231}
6232
6233void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6234 HandleBitwiseOperation(instruction);
6235}
6236
6237void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6238 LocationSummary* locations = instruction->GetLocations();
6239 Location first = locations->InAt(0);
6240 Location second = locations->InAt(1);
6241 DCHECK(first.Equals(locations->Out()));
6242
6243 if (instruction->GetResultType() == Primitive::kPrimInt) {
6244 if (second.IsRegister()) {
6245 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006246 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006247 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006248 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006249 } else {
6250 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006251 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006252 }
6253 } else if (second.IsConstant()) {
6254 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6255 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006256 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006257 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006258 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006259 } else {
6260 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006261 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006262 }
6263 } else {
6264 Address address(CpuRegister(RSP), second.GetStackIndex());
6265 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006266 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006267 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006268 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006269 } else {
6270 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006271 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006272 }
6273 }
6274 } else {
6275 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006276 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6277 bool second_is_constant = false;
6278 int64_t value = 0;
6279 if (second.IsConstant()) {
6280 second_is_constant = true;
6281 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006282 }
Mark Mendell40741f32015-04-20 22:10:34 -04006283 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006284
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006285 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006286 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006287 if (is_int32_value) {
6288 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6289 } else {
6290 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6291 }
6292 } else if (second.IsDoubleStackSlot()) {
6293 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006294 } else {
6295 __ andq(first_reg, second.AsRegister<CpuRegister>());
6296 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006297 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006298 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006299 if (is_int32_value) {
6300 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6301 } else {
6302 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6303 }
6304 } else if (second.IsDoubleStackSlot()) {
6305 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006306 } else {
6307 __ orq(first_reg, second.AsRegister<CpuRegister>());
6308 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006309 } else {
6310 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006311 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006312 if (is_int32_value) {
6313 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6314 } else {
6315 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6316 }
6317 } else if (second.IsDoubleStackSlot()) {
6318 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006319 } else {
6320 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6321 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006322 }
6323 }
6324}
6325
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006326void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6327 Location out,
6328 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006329 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006330 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6331 if (kEmitCompilerReadBarrier) {
6332 if (kUseBakerReadBarrier) {
6333 // Load with fast path based Baker's read barrier.
6334 // /* HeapReference<Object> */ out = *(out + offset)
6335 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006336 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006337 } else {
6338 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006339 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006340 // in the following move operation, as we will need it for the
6341 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006342 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006343 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006344 // /* HeapReference<Object> */ out = *(out + offset)
6345 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006346 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006347 }
6348 } else {
6349 // Plain load with no read barrier.
6350 // /* HeapReference<Object> */ out = *(out + offset)
6351 __ movl(out_reg, Address(out_reg, offset));
6352 __ MaybeUnpoisonHeapReference(out_reg);
6353 }
6354}
6355
6356void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6357 Location out,
6358 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006359 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006360 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6361 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6362 if (kEmitCompilerReadBarrier) {
6363 if (kUseBakerReadBarrier) {
6364 // Load with fast path based Baker's read barrier.
6365 // /* HeapReference<Object> */ out = *(obj + offset)
6366 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006367 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006368 } else {
6369 // Load with slow path based read barrier.
6370 // /* HeapReference<Object> */ out = *(obj + offset)
6371 __ movl(out_reg, Address(obj_reg, offset));
6372 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6373 }
6374 } else {
6375 // Plain load with no read barrier.
6376 // /* HeapReference<Object> */ out = *(obj + offset)
6377 __ movl(out_reg, Address(obj_reg, offset));
6378 __ MaybeUnpoisonHeapReference(out_reg);
6379 }
6380}
6381
6382void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6383 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006384 const Address& address,
6385 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006386 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6387 if (kEmitCompilerReadBarrier) {
6388 if (kUseBakerReadBarrier) {
6389 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6390 // Baker's read barrier are used:
6391 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006392 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006393 // if (Thread::Current()->GetIsGcMarking()) {
6394 // root = ReadBarrier::Mark(root)
6395 // }
6396
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397 // /* GcRoot<mirror::Object> */ root = *address
6398 __ movl(root_reg, address);
6399 if (fixup_label != nullptr) {
6400 __ Bind(fixup_label);
6401 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006402 static_assert(
6403 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6404 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6405 "have different sizes.");
6406 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6407 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6408 "have different sizes.");
6409
Vladimir Marko953437b2016-08-24 08:30:46 +00006410 // Slow path marking the GC root `root`.
6411 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6412 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006413 codegen_->AddSlowPath(slow_path);
6414
Andreas Gampe542451c2016-07-26 09:02:02 -07006415 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006416 /* no_rip */ true),
6417 Immediate(0));
6418 __ j(kNotEqual, slow_path->GetEntryLabel());
6419 __ Bind(slow_path->GetExitLabel());
6420 } else {
6421 // GC root loaded through a slow path for read barriers other
6422 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 // /* GcRoot<mirror::Object>* */ root = address
6424 __ leaq(root_reg, address);
6425 if (fixup_label != nullptr) {
6426 __ Bind(fixup_label);
6427 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006428 // /* mirror::Object* */ root = root->Read()
6429 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6430 }
6431 } else {
6432 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006433 // /* GcRoot<mirror::Object> */ root = *address
6434 __ movl(root_reg, address);
6435 if (fixup_label != nullptr) {
6436 __ Bind(fixup_label);
6437 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006438 // Note that GC roots are not affected by heap poisoning, thus we
6439 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006440 }
6441}
6442
6443void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6444 Location ref,
6445 CpuRegister obj,
6446 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006447 bool needs_null_check) {
6448 DCHECK(kEmitCompilerReadBarrier);
6449 DCHECK(kUseBakerReadBarrier);
6450
6451 // /* HeapReference<Object> */ ref = *(obj + offset)
6452 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006453 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006454}
6455
6456void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6457 Location ref,
6458 CpuRegister obj,
6459 uint32_t data_offset,
6460 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006461 bool needs_null_check) {
6462 DCHECK(kEmitCompilerReadBarrier);
6463 DCHECK(kUseBakerReadBarrier);
6464
Roland Levillain3d312422016-06-23 13:53:42 +01006465 static_assert(
6466 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6467 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006468 // /* HeapReference<Object> */ ref =
6469 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6470 Address src = index.IsConstant() ?
6471 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6472 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006473 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006474}
6475
6476void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6477 Location ref,
6478 CpuRegister obj,
6479 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006480 bool needs_null_check) {
6481 DCHECK(kEmitCompilerReadBarrier);
6482 DCHECK(kUseBakerReadBarrier);
6483
6484 // In slow path based read barriers, the read barrier call is
6485 // inserted after the original load. However, in fast path based
6486 // Baker's read barriers, we need to perform the load of
6487 // mirror::Object::monitor_ *before* the original reference load.
6488 // This load-load ordering is required by the read barrier.
6489 // The fast path/slow path (for Baker's algorithm) should look like:
6490 //
6491 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6492 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6493 // HeapReference<Object> ref = *src; // Original reference load.
6494 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6495 // if (is_gray) {
6496 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6497 // }
6498 //
6499 // Note: the original implementation in ReadBarrier::Barrier is
6500 // slightly more complex as:
6501 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006502 // the high-bits of rb_state, which are expected to be all zeroes
6503 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6504 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006505 // - it performs additional checks that we do not do here for
6506 // performance reasons.
6507
6508 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006509 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6510
Vladimir Marko953437b2016-08-24 08:30:46 +00006511 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6512 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6513 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6514 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6515 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6516 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6517 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6518
6519 // if (rb_state == ReadBarrier::gray_ptr_)
6520 // ref = ReadBarrier::Mark(ref);
6521 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6522 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006523 if (needs_null_check) {
6524 MaybeRecordImplicitNullCheck(instruction);
6525 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006526
6527 // Load fence to prevent load-load reordering.
6528 // Note that this is a no-op, thanks to the x86-64 memory model.
6529 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6530
6531 // The actual reference load.
6532 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006533 __ movl(ref_reg, src); // Flags are unaffected.
6534
6535 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6536 // Slow path marking the object `ref` when it is gray.
6537 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6538 instruction, ref, /* unpoison */ true);
6539 AddSlowPath(slow_path);
6540
6541 // We have done the "if" of the gray bit check above, now branch based on the flags.
6542 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006543
6544 // Object* ref = ref_addr->AsMirrorPtr()
6545 __ MaybeUnpoisonHeapReference(ref_reg);
6546
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006547 __ Bind(slow_path->GetExitLabel());
6548}
6549
6550void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6551 Location out,
6552 Location ref,
6553 Location obj,
6554 uint32_t offset,
6555 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006556 DCHECK(kEmitCompilerReadBarrier);
6557
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006558 // Insert a slow path based read barrier *after* the reference load.
6559 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 // If heap poisoning is enabled, the unpoisoning of the loaded
6561 // reference will be carried out by the runtime within the slow
6562 // path.
6563 //
6564 // Note that `ref` currently does not get unpoisoned (when heap
6565 // poisoning is enabled), which is alright as the `ref` argument is
6566 // not used by the artReadBarrierSlow entry point.
6567 //
6568 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6569 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6570 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6571 AddSlowPath(slow_path);
6572
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573 __ jmp(slow_path->GetEntryLabel());
6574 __ Bind(slow_path->GetExitLabel());
6575}
6576
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006577void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6578 Location out,
6579 Location ref,
6580 Location obj,
6581 uint32_t offset,
6582 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006583 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006584 // Baker's read barriers shall be handled by the fast path
6585 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6586 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 // If heap poisoning is enabled, unpoisoning will be taken care of
6588 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006589 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006590 } else if (kPoisonHeapReferences) {
6591 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6592 }
6593}
6594
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006595void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6596 Location out,
6597 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006598 DCHECK(kEmitCompilerReadBarrier);
6599
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006600 // Insert a slow path based read barrier *after* the GC root load.
6601 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602 // Note that GC roots are not affected by heap poisoning, so we do
6603 // not need to do anything special for this here.
6604 SlowPathCode* slow_path =
6605 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6606 AddSlowPath(slow_path);
6607
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 __ jmp(slow_path->GetEntryLabel());
6609 __ Bind(slow_path->GetExitLabel());
6610}
6611
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006612void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006613 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006614 LOG(FATAL) << "Unreachable";
6615}
6616
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006617void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006618 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006619 LOG(FATAL) << "Unreachable";
6620}
6621
Mark Mendellfe57faa2015-09-18 09:26:15 -04006622// Simple implementation of packed switch - generate cascaded compare/jumps.
6623void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6624 LocationSummary* locations =
6625 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6626 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006627 locations->AddTemp(Location::RequiresRegister());
6628 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006629}
6630
6631void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6632 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006633 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006634 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006635 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6636 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6637 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006638 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6639
6640 // Should we generate smaller inline compare/jumps?
6641 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6642 // Figure out the correct compare values and jump conditions.
6643 // Handle the first compare/branch as a special case because it might
6644 // jump to the default case.
6645 DCHECK_GT(num_entries, 2u);
6646 Condition first_condition;
6647 uint32_t index;
6648 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6649 if (lower_bound != 0) {
6650 first_condition = kLess;
6651 __ cmpl(value_reg_in, Immediate(lower_bound));
6652 __ j(first_condition, codegen_->GetLabelOf(default_block));
6653 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6654
6655 index = 1;
6656 } else {
6657 // Handle all the compare/jumps below.
6658 first_condition = kBelow;
6659 index = 0;
6660 }
6661
6662 // Handle the rest of the compare/jumps.
6663 for (; index + 1 < num_entries; index += 2) {
6664 int32_t compare_to_value = lower_bound + index + 1;
6665 __ cmpl(value_reg_in, Immediate(compare_to_value));
6666 // Jump to successors[index] if value < case_value[index].
6667 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6668 // Jump to successors[index + 1] if value == case_value[index + 1].
6669 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6670 }
6671
6672 if (index != num_entries) {
6673 // There are an odd number of entries. Handle the last one.
6674 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006675 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006676 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6677 }
6678
6679 // And the default for any other value.
6680 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6681 __ jmp(codegen_->GetLabelOf(default_block));
6682 }
6683 return;
6684 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006685
6686 // Remove the bias, if needed.
6687 Register value_reg_out = value_reg_in.AsRegister();
6688 if (lower_bound != 0) {
6689 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6690 value_reg_out = temp_reg.AsRegister();
6691 }
6692 CpuRegister value_reg(value_reg_out);
6693
6694 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006695 __ cmpl(value_reg, Immediate(num_entries - 1));
6696 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006697
Mark Mendell9c86b482015-09-18 13:36:07 -04006698 // We are in the range of the table.
6699 // Load the address of the jump table in the constant area.
6700 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006701
Mark Mendell9c86b482015-09-18 13:36:07 -04006702 // Load the (signed) offset from the jump table.
6703 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6704
6705 // Add the offset to the address of the table base.
6706 __ addq(temp_reg, base_reg);
6707
6708 // And jump.
6709 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006710}
6711
Aart Bikc5d47542016-01-27 17:00:35 -08006712void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6713 if (value == 0) {
6714 __ xorl(dest, dest);
6715 } else {
6716 __ movl(dest, Immediate(value));
6717 }
6718}
6719
Mark Mendell92e83bf2015-05-07 11:25:03 -04006720void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6721 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006722 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006723 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006724 } else if (IsUint<32>(value)) {
6725 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006726 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6727 } else {
6728 __ movq(dest, Immediate(value));
6729 }
6730}
6731
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006732void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6733 if (value == 0) {
6734 __ xorps(dest, dest);
6735 } else {
6736 __ movss(dest, LiteralInt32Address(value));
6737 }
6738}
6739
6740void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6741 if (value == 0) {
6742 __ xorpd(dest, dest);
6743 } else {
6744 __ movsd(dest, LiteralInt64Address(value));
6745 }
6746}
6747
6748void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6749 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6750}
6751
6752void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6753 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6754}
6755
Aart Bika19616e2016-02-01 18:57:58 -08006756void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6757 if (value == 0) {
6758 __ testl(dest, dest);
6759 } else {
6760 __ cmpl(dest, Immediate(value));
6761 }
6762}
6763
6764void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6765 if (IsInt<32>(value)) {
6766 if (value == 0) {
6767 __ testq(dest, dest);
6768 } else {
6769 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6770 }
6771 } else {
6772 // Value won't fit in an int.
6773 __ cmpq(dest, LiteralInt64Address(value));
6774 }
6775}
6776
Mark Mendellcfa410b2015-05-25 16:02:44 -04006777void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6778 DCHECK(dest.IsDoubleStackSlot());
6779 if (IsInt<32>(value)) {
6780 // Can move directly as an int32 constant.
6781 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6782 Immediate(static_cast<int32_t>(value)));
6783 } else {
6784 Load64BitValue(CpuRegister(TMP), value);
6785 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6786 }
6787}
6788
Mark Mendell9c86b482015-09-18 13:36:07 -04006789/**
6790 * Class to handle late fixup of offsets into constant area.
6791 */
6792class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6793 public:
6794 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6795 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6796
6797 protected:
6798 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6799
6800 CodeGeneratorX86_64* codegen_;
6801
6802 private:
6803 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6804 // Patch the correct offset for the instruction. We use the address of the
6805 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6806 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6807 int32_t relative_position = constant_offset - pos;
6808
6809 // Patch in the right value.
6810 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6811 }
6812
6813 // Location in constant area that the fixup refers to.
6814 size_t offset_into_constant_area_;
6815};
6816
6817/**
6818 t * Class to handle late fixup of offsets to a jump table that will be created in the
6819 * constant area.
6820 */
6821class JumpTableRIPFixup : public RIPFixup {
6822 public:
6823 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6824 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6825
6826 void CreateJumpTable() {
6827 X86_64Assembler* assembler = codegen_->GetAssembler();
6828
6829 // Ensure that the reference to the jump table has the correct offset.
6830 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6831 SetOffset(offset_in_constant_table);
6832
6833 // Compute the offset from the start of the function to this jump table.
6834 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6835
6836 // Populate the jump table with the correct values for the jump table.
6837 int32_t num_entries = switch_instr_->GetNumEntries();
6838 HBasicBlock* block = switch_instr_->GetBlock();
6839 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6840 // The value that we want is the target offset - the position of the table.
6841 for (int32_t i = 0; i < num_entries; i++) {
6842 HBasicBlock* b = successors[i];
6843 Label* l = codegen_->GetLabelOf(b);
6844 DCHECK(l->IsBound());
6845 int32_t offset_to_block = l->Position() - current_table_offset;
6846 assembler->AppendInt32(offset_to_block);
6847 }
6848 }
6849
6850 private:
6851 const HPackedSwitch* switch_instr_;
6852};
6853
Mark Mendellf55c3e02015-03-26 21:07:46 -04006854void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6855 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006856 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006857 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6858 // 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 -04006859 assembler->Align(4, 0);
6860 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006861
6862 // Populate any jump tables.
6863 for (auto jump_table : fixups_to_jump_tables_) {
6864 jump_table->CreateJumpTable();
6865 }
6866
6867 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006868 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006869 }
6870
6871 // And finish up.
6872 CodeGenerator::Finalize(allocator);
6873}
6874
Mark Mendellf55c3e02015-03-26 21:07:46 -04006875Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6876 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6877 return Address::RIP(fixup);
6878}
6879
6880Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6881 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6882 return Address::RIP(fixup);
6883}
6884
6885Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6886 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6887 return Address::RIP(fixup);
6888}
6889
6890Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6891 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6892 return Address::RIP(fixup);
6893}
6894
Andreas Gampe85b62f22015-09-09 13:15:38 -07006895// TODO: trg as memory.
6896void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6897 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006898 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006899 return;
6900 }
6901
6902 DCHECK_NE(type, Primitive::kPrimVoid);
6903
6904 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6905 if (trg.Equals(return_loc)) {
6906 return;
6907 }
6908
6909 // Let the parallel move resolver take care of all of this.
6910 HParallelMove parallel_move(GetGraph()->GetArena());
6911 parallel_move.AddMove(return_loc, trg, type, nullptr);
6912 GetMoveResolver()->EmitNativeCode(&parallel_move);
6913}
6914
Mark Mendell9c86b482015-09-18 13:36:07 -04006915Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6916 // Create a fixup to be used to create and address the jump table.
6917 JumpTableRIPFixup* table_fixup =
6918 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6919
6920 // We have to populate the jump tables.
6921 fixups_to_jump_tables_.push_back(table_fixup);
6922 return Address::RIP(table_fixup);
6923}
6924
Mark Mendellea5af682015-10-22 17:35:49 -04006925void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6926 const Address& addr_high,
6927 int64_t v,
6928 HInstruction* instruction) {
6929 if (IsInt<32>(v)) {
6930 int32_t v_32 = v;
6931 __ movq(addr_low, Immediate(v_32));
6932 MaybeRecordImplicitNullCheck(instruction);
6933 } else {
6934 // Didn't fit in a register. Do it in pieces.
6935 int32_t low_v = Low32Bits(v);
6936 int32_t high_v = High32Bits(v);
6937 __ movl(addr_low, Immediate(low_v));
6938 MaybeRecordImplicitNullCheck(instruction);
6939 __ movl(addr_high, Immediate(high_v));
6940 }
6941}
6942
Roland Levillain4d027112015-07-01 15:41:14 +01006943#undef __
6944
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006945} // namespace x86_64
6946} // namespace art