blob: cf01a791ee5b0083ea54ec934d55bd48d606dcad [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:
Roland Levillain02b75802016-07-13 11:54:35 +0100469 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj)
470 : SlowPathCode(instruction), obj_(obj) {
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 Levillaindec8f632016-07-22 17:10:06 +0100488 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000489 << "Unexpected instruction in read barrier marking slow path: "
490 << instruction_->DebugName();
491
492 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100493 // No need to save live registers; it's taken care of by the
494 // entrypoint. Also, there is no need to update the stack mask,
495 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000496 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100497 DCHECK_NE(reg, RSP);
498 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
499 // "Compact" slow path, saving two moves.
500 //
501 // Instead of using the standard runtime calling convention (input
502 // and output in R0):
503 //
504 // RDI <- obj
505 // RAX <- ReadBarrierMark(RDI)
506 // obj <- RAX
507 //
508 // we just use rX (the register holding `obj`) as input and output
509 // of a dedicated entrypoint:
510 //
511 // rX <- ReadBarrierMarkRegX(rX)
512 //
513 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700514 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100515 // This runtime call does not require a stack map.
516 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000517 __ jmp(GetExitLabel());
518 }
519
520 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000521 const Location obj_;
522
523 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
524};
525
Roland Levillain0d5a2812015-11-13 10:07:31 +0000526// Slow path generating a read barrier for a heap reference.
527class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
528 public:
529 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
530 Location out,
531 Location ref,
532 Location obj,
533 uint32_t offset,
534 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000535 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000536 out_(out),
537 ref_(ref),
538 obj_(obj),
539 offset_(offset),
540 index_(index) {
541 DCHECK(kEmitCompilerReadBarrier);
542 // If `obj` is equal to `out` or `ref`, it means the initial
543 // object has been overwritten by (or after) the heap object
544 // reference load to be instrumented, e.g.:
545 //
546 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000547 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000548 //
549 // In that case, we have lost the information about the original
550 // object, and the emitted read barrier cannot work properly.
551 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
552 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
553}
554
555 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
556 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
557 LocationSummary* locations = instruction_->GetLocations();
558 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
559 DCHECK(locations->CanCall());
560 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100561 DCHECK(instruction_->IsInstanceFieldGet() ||
562 instruction_->IsStaticFieldGet() ||
563 instruction_->IsArrayGet() ||
564 instruction_->IsInstanceOf() ||
565 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100566 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000567 << "Unexpected instruction in read barrier for heap reference slow path: "
568 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000569
570 __ Bind(GetEntryLabel());
571 SaveLiveRegisters(codegen, locations);
572
573 // We may have to change the index's value, but as `index_` is a
574 // constant member (like other "inputs" of this slow path),
575 // introduce a copy of it, `index`.
576 Location index = index_;
577 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100578 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000579 if (instruction_->IsArrayGet()) {
580 // Compute real offset and store it in index_.
581 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
582 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
583 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
584 // We are about to change the value of `index_reg` (see the
585 // calls to art::x86_64::X86_64Assembler::shll and
586 // art::x86_64::X86_64Assembler::AddImmediate below), but it
587 // has not been saved by the previous call to
588 // art::SlowPathCode::SaveLiveRegisters, as it is a
589 // callee-save register --
590 // art::SlowPathCode::SaveLiveRegisters does not consider
591 // callee-save registers, as it has been designed with the
592 // assumption that callee-save registers are supposed to be
593 // handled by the called function. So, as a callee-save
594 // register, `index_reg` _would_ eventually be saved onto
595 // the stack, but it would be too late: we would have
596 // changed its value earlier. Therefore, we manually save
597 // it here into another freely available register,
598 // `free_reg`, chosen of course among the caller-save
599 // registers (as a callee-save `free_reg` register would
600 // exhibit the same problem).
601 //
602 // Note we could have requested a temporary register from
603 // the register allocator instead; but we prefer not to, as
604 // this is a slow path, and we know we can find a
605 // caller-save register that is available.
606 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
607 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
608 index_reg = free_reg;
609 index = Location::RegisterLocation(index_reg);
610 } else {
611 // The initial register stored in `index_` has already been
612 // saved in the call to art::SlowPathCode::SaveLiveRegisters
613 // (as it is not a callee-save register), so we can freely
614 // use it.
615 }
616 // Shifting the index value contained in `index_reg` by the
617 // scale factor (2) cannot overflow in practice, as the
618 // runtime is unable to allocate object arrays with a size
619 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
620 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
621 static_assert(
622 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
623 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
624 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
625 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100626 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
627 // intrinsics, `index_` is not shifted by a scale factor of 2
628 // (as in the case of ArrayGet), as it is actually an offset
629 // to an object field within an object.
630 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000631 DCHECK(instruction_->GetLocations()->Intrinsified());
632 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
633 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
634 << instruction_->AsInvoke()->GetIntrinsic();
635 DCHECK_EQ(offset_, 0U);
636 DCHECK(index_.IsRegister());
637 }
638 }
639
640 // We're moving two or three locations to locations that could
641 // overlap, so we need a parallel move resolver.
642 InvokeRuntimeCallingConvention calling_convention;
643 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
644 parallel_move.AddMove(ref_,
645 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
646 Primitive::kPrimNot,
647 nullptr);
648 parallel_move.AddMove(obj_,
649 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
650 Primitive::kPrimNot,
651 nullptr);
652 if (index.IsValid()) {
653 parallel_move.AddMove(index,
654 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
655 Primitive::kPrimInt,
656 nullptr);
657 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
658 } else {
659 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
660 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
661 }
662 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
663 instruction_,
664 instruction_->GetDexPc(),
665 this);
666 CheckEntrypointTypes<
667 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
668 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
669
670 RestoreLiveRegisters(codegen, locations);
671 __ jmp(GetExitLabel());
672 }
673
674 const char* GetDescription() const OVERRIDE {
675 return "ReadBarrierForHeapReferenceSlowPathX86_64";
676 }
677
678 private:
679 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
680 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
681 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
682 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
683 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
684 return static_cast<CpuRegister>(i);
685 }
686 }
687 // We shall never fail to find a free caller-save register, as
688 // there are more than two core caller-save registers on x86-64
689 // (meaning it is possible to find one which is different from
690 // `ref` and `obj`).
691 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
692 LOG(FATAL) << "Could not find a free caller-save register";
693 UNREACHABLE();
694 }
695
Roland Levillain0d5a2812015-11-13 10:07:31 +0000696 const Location out_;
697 const Location ref_;
698 const Location obj_;
699 const uint32_t offset_;
700 // An additional location containing an index to an array.
701 // Only used for HArrayGet and the UnsafeGetObject &
702 // UnsafeGetObjectVolatile intrinsics.
703 const Location index_;
704
705 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
706};
707
708// Slow path generating a read barrier for a GC root.
709class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
710 public:
711 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000712 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000713 DCHECK(kEmitCompilerReadBarrier);
714 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000715
716 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
717 LocationSummary* locations = instruction_->GetLocations();
718 DCHECK(locations->CanCall());
719 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000720 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
721 << "Unexpected instruction in read barrier for GC root slow path: "
722 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000723
724 __ Bind(GetEntryLabel());
725 SaveLiveRegisters(codegen, locations);
726
727 InvokeRuntimeCallingConvention calling_convention;
728 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
729 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
730 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
731 instruction_,
732 instruction_->GetDexPc(),
733 this);
734 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
735 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
736
737 RestoreLiveRegisters(codegen, locations);
738 __ jmp(GetExitLabel());
739 }
740
741 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
742
743 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000744 const Location out_;
745 const Location root_;
746
747 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
748};
749
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100750#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100751// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
752#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100753
Roland Levillain4fa13f62015-07-06 18:11:54 +0100754inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700755 switch (cond) {
756 case kCondEQ: return kEqual;
757 case kCondNE: return kNotEqual;
758 case kCondLT: return kLess;
759 case kCondLE: return kLessEqual;
760 case kCondGT: return kGreater;
761 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700762 case kCondB: return kBelow;
763 case kCondBE: return kBelowEqual;
764 case kCondA: return kAbove;
765 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700766 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100767 LOG(FATAL) << "Unreachable";
768 UNREACHABLE();
769}
770
Aart Bike9f37602015-10-09 11:15:55 -0700771// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100772inline Condition X86_64FPCondition(IfCondition cond) {
773 switch (cond) {
774 case kCondEQ: return kEqual;
775 case kCondNE: return kNotEqual;
776 case kCondLT: return kBelow;
777 case kCondLE: return kBelowEqual;
778 case kCondGT: return kAbove;
779 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700780 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100781 };
782 LOG(FATAL) << "Unreachable";
783 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700784}
785
Vladimir Markodc151b22015-10-15 18:02:30 +0100786HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
787 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
788 MethodReference target_method ATTRIBUTE_UNUSED) {
789 switch (desired_dispatch_info.code_ptr_location) {
790 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
791 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
792 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
793 return HInvokeStaticOrDirect::DispatchInfo {
794 desired_dispatch_info.method_load_kind,
795 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
796 desired_dispatch_info.method_load_data,
797 0u
798 };
799 default:
800 return desired_dispatch_info;
801 }
802}
803
Serguei Katkov288c7a82016-05-16 11:53:15 +0600804Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
805 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800806 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000807 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
808 switch (invoke->GetMethodLoadKind()) {
809 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
810 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000811 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000812 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000813 break;
814 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000815 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000816 break;
817 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
818 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
819 break;
820 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
821 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
822 method_patches_.emplace_back(invoke->GetTargetMethod());
823 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
824 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000825 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000826 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000827 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000828 // Bind a new fixup label at the end of the "movl" insn.
829 uint32_t offset = invoke->GetDexCacheArrayOffset();
830 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000831 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000832 }
Vladimir Marko58155012015-08-19 12:49:41 +0000833 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000834 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000835 Register method_reg;
836 CpuRegister reg = temp.AsRegister<CpuRegister>();
837 if (current_method.IsRegister()) {
838 method_reg = current_method.AsRegister<Register>();
839 } else {
840 DCHECK(invoke->GetLocations()->Intrinsified());
841 DCHECK(!current_method.IsValid());
842 method_reg = reg.AsRegister();
843 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
844 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000845 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100846 __ movq(reg,
847 Address(CpuRegister(method_reg),
848 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100849 // temp = temp[index_in_cache];
850 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
851 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000852 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
853 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100854 }
Vladimir Marko58155012015-08-19 12:49:41 +0000855 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600856 return callee_method;
857}
858
859void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
860 Location temp) {
861 // All registers are assumed to be correctly set up.
862 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000863
864 switch (invoke->GetCodePtrLocation()) {
865 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
866 __ call(&frame_entry_label_);
867 break;
868 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
869 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
870 Label* label = &relative_call_patches_.back().label;
871 __ call(label); // Bind to the patch label, override at link time.
872 __ Bind(label); // Bind the label at the end of the "call" insn.
873 break;
874 }
875 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
876 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100877 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
878 LOG(FATAL) << "Unsupported";
879 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000880 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
881 // (callee_method + offset_of_quick_compiled_code)()
882 __ call(Address(callee_method.AsRegister<CpuRegister>(),
883 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700884 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000885 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000886 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800887
888 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800889}
890
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000891void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
892 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
893 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
894 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000895
896 // Use the calling convention instead of the location of the receiver, as
897 // intrinsics may have put the receiver in a different register. In the intrinsics
898 // slow path, the arguments have been moved to the right place, so here we are
899 // guaranteed that the receiver is the first register of the calling convention.
900 InvokeDexCallingConvention calling_convention;
901 Register receiver = calling_convention.GetRegisterAt(0);
902
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000903 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000905 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000906 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000907 // Instead of simply (possibly) unpoisoning `temp` here, we should
908 // emit a read barrier for the previous class reference load.
909 // However this is not required in practice, as this is an
910 // intermediate/temporary reference and because the current
911 // concurrent copying collector keeps the from-space memory
912 // intact/accessible until the end of the marking phase (the
913 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000914 __ MaybeUnpoisonHeapReference(temp);
915 // temp = temp->GetMethodAt(method_offset);
916 __ movq(temp, Address(temp, method_offset));
917 // call temp->GetEntryPoint();
918 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700919 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000920}
921
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000922void CodeGeneratorX86_64::RecordSimplePatch() {
923 if (GetCompilerOptions().GetIncludePatchInformation()) {
924 simple_patches_.emplace_back();
925 __ Bind(&simple_patches_.back());
926 }
927}
928
929void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
930 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
931 __ Bind(&string_patches_.back().label);
932}
933
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100934void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
935 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
936 __ Bind(&type_patches_.back().label);
937}
938
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
940 uint32_t element_offset) {
941 // Add a patch entry and return the label.
942 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
943 return &pc_relative_dex_cache_patches_.back().label;
944}
945
Vladimir Marko58155012015-08-19 12:49:41 +0000946void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
947 DCHECK(linker_patches->empty());
948 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000949 method_patches_.size() +
950 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000951 pc_relative_dex_cache_patches_.size() +
952 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100953 string_patches_.size() +
954 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000955 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000956 // The label points to the end of the "movl" insn but the literal offset for method
957 // patch needs to point to the embedded constant which occupies the last 4 bytes.
958 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000959 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000960 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000961 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
962 info.target_method.dex_file,
963 info.target_method.dex_method_index));
964 }
965 for (const MethodPatchInfo<Label>& info : relative_call_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::RelativeCodePatch(literal_offset,
968 info.target_method.dex_file,
969 info.target_method.dex_method_index));
970 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000971 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
972 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000973 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
974 &info.target_dex_file,
975 info.label.Position(),
976 info.element_offset));
977 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000978 for (const Label& label : simple_patches_) {
979 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
980 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
981 }
982 for (const StringPatchInfo<Label>& info : string_patches_) {
983 // These are always PC-relative, see GetSupportedLoadStringKind().
984 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
985 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
986 &info.dex_file,
987 info.label.Position(),
988 info.string_index));
989 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100990 for (const TypePatchInfo<Label>& info : type_patches_) {
991 // These are always PC-relative, see GetSupportedLoadClassKind().
992 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
993 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
994 &info.dex_file,
995 info.label.Position(),
996 info.type_index));
997 }
Vladimir Marko58155012015-08-19 12:49:41 +0000998}
999
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001000void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001001 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002}
1003
1004void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001005 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006}
1007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001008size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1009 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1010 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001011}
1012
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001013size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1014 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1015 return kX86_64WordSize;
1016}
1017
1018size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1019 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1020 return kX86_64WordSize;
1021}
1022
1023size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1024 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1025 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001026}
1027
Calin Juravle175dc732015-08-25 15:42:32 +01001028void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1029 HInstruction* instruction,
1030 uint32_t dex_pc,
1031 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001032 InvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001033 instruction,
1034 dex_pc,
1035 slow_path);
1036}
1037
1038void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +01001039 HInstruction* instruction,
1040 uint32_t dex_pc,
1041 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001042 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001043 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +01001044 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +01001045}
1046
Roland Levillaindec8f632016-07-22 17:10:06 +01001047void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1048 HInstruction* instruction,
1049 SlowPathCode* slow_path) {
1050 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1051 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1052}
1053
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001054static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001055// Use a fake return address register to mimic Quick.
1056static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001057CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001058 const X86_64InstructionSetFeatures& isa_features,
1059 const CompilerOptions& compiler_options,
1060 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001061 : CodeGenerator(graph,
1062 kNumberOfCpuRegisters,
1063 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001064 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001065 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1066 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001067 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001068 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1069 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001070 compiler_options,
1071 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001072 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001073 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001074 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001075 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001076 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001077 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001078 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001079 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1080 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001081 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001082 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1083 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001084 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001085 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001086 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1087}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001089InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1090 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001091 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092 assembler_(codegen->GetAssembler()),
1093 codegen_(codegen) {}
1094
David Brazdil58282f42016-01-14 12:45:10 +00001095void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001096 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001097 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001099 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001100 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001101}
1102
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001103static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001104 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001105}
David Srbecky9d8606d2015-04-12 09:35:32 +01001106
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001107static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001108 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001109}
1110
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001111void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001112 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001113 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001114 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001115 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001116 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001117
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001118 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001119 __ testq(CpuRegister(RAX), Address(
1120 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001121 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001122 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001123
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001124 if (HasEmptyFrame()) {
1125 return;
1126 }
1127
Nicolas Geoffray98893962015-01-21 12:32:32 +00001128 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001129 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001130 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001131 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001132 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1133 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001134 }
1135 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001136
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001137 int adjust = GetFrameSize() - GetCoreSpillSize();
1138 __ subq(CpuRegister(RSP), Immediate(adjust));
1139 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001140 uint32_t xmm_spill_location = GetFpuSpillStart();
1141 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001142
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001143 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1144 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001145 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1146 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1147 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001148 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001149 }
1150
Mathieu Chartiere401d142015-04-22 13:56:20 -07001151 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001152 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001153}
1154
1155void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001156 __ cfi().RememberState();
1157 if (!HasEmptyFrame()) {
1158 uint32_t xmm_spill_location = GetFpuSpillStart();
1159 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1160 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1161 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1162 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1163 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1164 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1165 }
1166 }
1167
1168 int adjust = GetFrameSize() - GetCoreSpillSize();
1169 __ addq(CpuRegister(RSP), Immediate(adjust));
1170 __ cfi().AdjustCFAOffset(-adjust);
1171
1172 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1173 Register reg = kCoreCalleeSaves[i];
1174 if (allocated_registers_.ContainsCoreRegister(reg)) {
1175 __ popq(CpuRegister(reg));
1176 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1177 __ cfi().Restore(DWARFReg(reg));
1178 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001179 }
1180 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001181 __ ret();
1182 __ cfi().RestoreState();
1183 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001184}
1185
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001186void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1187 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188}
1189
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190void CodeGeneratorX86_64::Move(Location destination, Location source) {
1191 if (source.Equals(destination)) {
1192 return;
1193 }
1194 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001195 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001197 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001198 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001199 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001200 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001201 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1202 } else if (source.IsConstant()) {
1203 HConstant* constant = source.GetConstant();
1204 if (constant->IsLongConstant()) {
1205 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1206 } else {
1207 Load32BitValue(dest, GetInt32ValueOf(constant));
1208 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001209 } else {
1210 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001211 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 }
1213 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001214 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001216 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001218 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1219 } else if (source.IsConstant()) {
1220 HConstant* constant = source.GetConstant();
1221 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1222 if (constant->IsFloatConstant()) {
1223 Load32BitValue(dest, static_cast<int32_t>(value));
1224 } else {
1225 Load64BitValue(dest, value);
1226 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001228 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 } else {
1230 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001231 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001232 }
1233 } else if (destination.IsStackSlot()) {
1234 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 } else if (source.IsFpuRegister()) {
1238 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001240 } else if (source.IsConstant()) {
1241 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001242 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001243 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001244 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001245 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001246 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1247 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001248 }
1249 } else {
1250 DCHECK(destination.IsDoubleStackSlot());
1251 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001253 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001254 } else if (source.IsFpuRegister()) {
1255 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001256 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001257 } else if (source.IsConstant()) {
1258 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001259 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001260 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001261 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001262 } else {
1263 DCHECK(constant->IsLongConstant());
1264 value = constant->AsLongConstant()->GetValue();
1265 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001266 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001267 } else {
1268 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001269 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1270 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001271 }
1272 }
1273}
1274
Calin Juravle175dc732015-08-25 15:42:32 +01001275void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1276 DCHECK(location.IsRegister());
1277 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1278}
1279
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280void CodeGeneratorX86_64::MoveLocation(
1281 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1282 Move(dst, src);
1283}
1284
1285void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1286 if (location.IsRegister()) {
1287 locations->AddTemp(location);
1288 } else {
1289 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1290 }
1291}
1292
David Brazdilfc6a86a2015-06-26 10:33:45 +00001293void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001294 DCHECK(!successor->IsExitBlock());
1295
1296 HBasicBlock* block = got->GetBlock();
1297 HInstruction* previous = got->GetPrevious();
1298
1299 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001300 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001301 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1302 return;
1303 }
1304
1305 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1306 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1307 }
1308 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001309 __ jmp(codegen_->GetLabelOf(successor));
1310 }
1311}
1312
David Brazdilfc6a86a2015-06-26 10:33:45 +00001313void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1314 got->SetLocations(nullptr);
1315}
1316
1317void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1318 HandleGoto(got, got->GetSuccessor());
1319}
1320
1321void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1322 try_boundary->SetLocations(nullptr);
1323}
1324
1325void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1326 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1327 if (!successor->IsExitBlock()) {
1328 HandleGoto(try_boundary, successor);
1329 }
1330}
1331
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001332void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1333 exit->SetLocations(nullptr);
1334}
1335
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001336void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001337}
1338
Mark Mendell152408f2015-12-31 12:28:50 -05001339template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001340void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001341 LabelType* true_label,
1342 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343 if (cond->IsFPConditionTrueIfNaN()) {
1344 __ j(kUnordered, true_label);
1345 } else if (cond->IsFPConditionFalseIfNaN()) {
1346 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001347 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001348 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001349}
1350
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001351void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001352 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001353
Mark Mendellc4701932015-04-10 13:18:51 -04001354 Location left = locations->InAt(0);
1355 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001356 Primitive::Type type = condition->InputAt(0)->GetType();
1357 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001358 case Primitive::kPrimBoolean:
1359 case Primitive::kPrimByte:
1360 case Primitive::kPrimChar:
1361 case Primitive::kPrimShort:
1362 case Primitive::kPrimInt:
1363 case Primitive::kPrimNot: {
1364 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1365 if (right.IsConstant()) {
1366 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1367 if (value == 0) {
1368 __ testl(left_reg, left_reg);
1369 } else {
1370 __ cmpl(left_reg, Immediate(value));
1371 }
1372 } else if (right.IsStackSlot()) {
1373 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1374 } else {
1375 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1376 }
1377 break;
1378 }
Mark Mendellc4701932015-04-10 13:18:51 -04001379 case Primitive::kPrimLong: {
1380 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1381 if (right.IsConstant()) {
1382 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001383 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001384 } else if (right.IsDoubleStackSlot()) {
1385 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1386 } else {
1387 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1388 }
Mark Mendellc4701932015-04-10 13:18:51 -04001389 break;
1390 }
1391 case Primitive::kPrimFloat: {
1392 if (right.IsFpuRegister()) {
1393 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1394 } else if (right.IsConstant()) {
1395 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1396 codegen_->LiteralFloatAddress(
1397 right.GetConstant()->AsFloatConstant()->GetValue()));
1398 } else {
1399 DCHECK(right.IsStackSlot());
1400 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1401 Address(CpuRegister(RSP), right.GetStackIndex()));
1402 }
Mark Mendellc4701932015-04-10 13:18:51 -04001403 break;
1404 }
1405 case Primitive::kPrimDouble: {
1406 if (right.IsFpuRegister()) {
1407 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1408 } else if (right.IsConstant()) {
1409 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1410 codegen_->LiteralDoubleAddress(
1411 right.GetConstant()->AsDoubleConstant()->GetValue()));
1412 } else {
1413 DCHECK(right.IsDoubleStackSlot());
1414 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1415 Address(CpuRegister(RSP), right.GetStackIndex()));
1416 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001417 break;
1418 }
1419 default:
1420 LOG(FATAL) << "Unexpected condition type " << type;
1421 }
1422}
1423
1424template<class LabelType>
1425void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1426 LabelType* true_target_in,
1427 LabelType* false_target_in) {
1428 // Generated branching requires both targets to be explicit. If either of the
1429 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1430 LabelType fallthrough_target;
1431 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1432 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1433
1434 // Generate the comparison to set the CC.
1435 GenerateCompareTest(condition);
1436
1437 // Now generate the correct jump(s).
1438 Primitive::Type type = condition->InputAt(0)->GetType();
1439 switch (type) {
1440 case Primitive::kPrimLong: {
1441 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1442 break;
1443 }
1444 case Primitive::kPrimFloat: {
1445 GenerateFPJumps(condition, true_target, false_target);
1446 break;
1447 }
1448 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001449 GenerateFPJumps(condition, true_target, false_target);
1450 break;
1451 }
1452 default:
1453 LOG(FATAL) << "Unexpected condition type " << type;
1454 }
1455
David Brazdil0debae72015-11-12 18:37:00 +00001456 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001457 __ jmp(false_target);
1458 }
David Brazdil0debae72015-11-12 18:37:00 +00001459
1460 if (fallthrough_target.IsLinked()) {
1461 __ Bind(&fallthrough_target);
1462 }
Mark Mendellc4701932015-04-10 13:18:51 -04001463}
1464
David Brazdil0debae72015-11-12 18:37:00 +00001465static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1466 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1467 // are set only strictly before `branch`. We can't use the eflags on long
1468 // conditions if they are materialized due to the complex branching.
1469 return cond->IsCondition() &&
1470 cond->GetNext() == branch &&
1471 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1472}
1473
Mark Mendell152408f2015-12-31 12:28:50 -05001474template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001475void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001476 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001477 LabelType* true_target,
1478 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001479 HInstruction* cond = instruction->InputAt(condition_input_index);
1480
1481 if (true_target == nullptr && false_target == nullptr) {
1482 // Nothing to do. The code always falls through.
1483 return;
1484 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001485 // Constant condition, statically compared against "true" (integer value 1).
1486 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001487 if (true_target != nullptr) {
1488 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001489 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001490 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001491 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001492 if (false_target != nullptr) {
1493 __ jmp(false_target);
1494 }
1495 }
1496 return;
1497 }
1498
1499 // The following code generates these patterns:
1500 // (1) true_target == nullptr && false_target != nullptr
1501 // - opposite condition true => branch to false_target
1502 // (2) true_target != nullptr && false_target == nullptr
1503 // - condition true => branch to true_target
1504 // (3) true_target != nullptr && false_target != nullptr
1505 // - condition true => branch to true_target
1506 // - branch to false_target
1507 if (IsBooleanValueOrMaterializedCondition(cond)) {
1508 if (AreEflagsSetFrom(cond, instruction)) {
1509 if (true_target == nullptr) {
1510 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1511 } else {
1512 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1513 }
1514 } else {
1515 // Materialized condition, compare against 0.
1516 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1517 if (lhs.IsRegister()) {
1518 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1519 } else {
1520 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1521 }
1522 if (true_target == nullptr) {
1523 __ j(kEqual, false_target);
1524 } else {
1525 __ j(kNotEqual, true_target);
1526 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001527 }
1528 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001529 // Condition has not been materialized, use its inputs as the
1530 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001531 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001532
David Brazdil0debae72015-11-12 18:37:00 +00001533 // If this is a long or FP comparison that has been folded into
1534 // the HCondition, generate the comparison directly.
1535 Primitive::Type type = condition->InputAt(0)->GetType();
1536 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1537 GenerateCompareTestAndBranch(condition, true_target, false_target);
1538 return;
1539 }
1540
1541 Location lhs = condition->GetLocations()->InAt(0);
1542 Location rhs = condition->GetLocations()->InAt(1);
1543 if (rhs.IsRegister()) {
1544 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1545 } else if (rhs.IsConstant()) {
1546 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001547 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001548 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001549 __ cmpl(lhs.AsRegister<CpuRegister>(),
1550 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1551 }
1552 if (true_target == nullptr) {
1553 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1554 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001555 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001556 }
Dave Allison20dfc792014-06-16 20:44:29 -07001557 }
David Brazdil0debae72015-11-12 18:37:00 +00001558
1559 // If neither branch falls through (case 3), the conditional branch to `true_target`
1560 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1561 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001562 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001563 }
1564}
1565
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001566void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1568 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001569 locations->SetInAt(0, Location::Any());
1570 }
1571}
1572
1573void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001574 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1575 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1576 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1577 nullptr : codegen_->GetLabelOf(true_successor);
1578 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1579 nullptr : codegen_->GetLabelOf(false_successor);
1580 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001581}
1582
1583void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1584 LocationSummary* locations = new (GetGraph()->GetArena())
1585 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001586 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587 locations->SetInAt(0, Location::Any());
1588 }
1589}
1590
1591void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001592 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001593 GenerateTestAndBranch<Label>(deoptimize,
1594 /* condition_input_index */ 0,
1595 slow_path->GetEntryLabel(),
1596 /* false_target */ nullptr);
1597}
1598
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001599static bool SelectCanUseCMOV(HSelect* select) {
1600 // There are no conditional move instructions for XMMs.
1601 if (Primitive::IsFloatingPointType(select->GetType())) {
1602 return false;
1603 }
1604
1605 // A FP condition doesn't generate the single CC that we need.
1606 HInstruction* condition = select->GetCondition();
1607 if (condition->IsCondition() &&
1608 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1609 return false;
1610 }
1611
1612 // We can generate a CMOV for this Select.
1613 return true;
1614}
1615
David Brazdil74eb1b22015-12-14 11:44:01 +00001616void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1618 if (Primitive::IsFloatingPointType(select->GetType())) {
1619 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001620 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001621 } else {
1622 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001623 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001624 if (select->InputAt(1)->IsConstant()) {
1625 locations->SetInAt(1, Location::RequiresRegister());
1626 } else {
1627 locations->SetInAt(1, Location::Any());
1628 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001629 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001630 locations->SetInAt(1, Location::Any());
1631 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001632 }
1633 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1634 locations->SetInAt(2, Location::RequiresRegister());
1635 }
1636 locations->SetOut(Location::SameAsFirstInput());
1637}
1638
1639void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1640 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001641 if (SelectCanUseCMOV(select)) {
1642 // If both the condition and the source types are integer, we can generate
1643 // a CMOV to implement Select.
1644 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001645 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001646 DCHECK(locations->InAt(0).Equals(locations->Out()));
1647
1648 HInstruction* select_condition = select->GetCondition();
1649 Condition cond = kNotEqual;
1650
1651 // Figure out how to test the 'condition'.
1652 if (select_condition->IsCondition()) {
1653 HCondition* condition = select_condition->AsCondition();
1654 if (!condition->IsEmittedAtUseSite()) {
1655 // This was a previously materialized condition.
1656 // Can we use the existing condition code?
1657 if (AreEflagsSetFrom(condition, select)) {
1658 // Materialization was the previous instruction. Condition codes are right.
1659 cond = X86_64IntegerCondition(condition->GetCondition());
1660 } else {
1661 // No, we have to recreate the condition code.
1662 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1663 __ testl(cond_reg, cond_reg);
1664 }
1665 } else {
1666 GenerateCompareTest(condition);
1667 cond = X86_64IntegerCondition(condition->GetCondition());
1668 }
1669 } else {
1670 // Must be a boolean condition, which needs to be compared to 0.
1671 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1672 __ testl(cond_reg, cond_reg);
1673 }
1674
1675 // If the condition is true, overwrite the output, which already contains false.
1676 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001677 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1678 if (value_true_loc.IsRegister()) {
1679 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1680 } else {
1681 __ cmov(cond,
1682 value_false,
1683 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1684 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001685 } else {
1686 NearLabel false_target;
1687 GenerateTestAndBranch<NearLabel>(select,
1688 /* condition_input_index */ 2,
1689 /* true_target */ nullptr,
1690 &false_target);
1691 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1692 __ Bind(&false_target);
1693 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001694}
1695
David Srbecky0cf44932015-12-09 14:09:59 +00001696void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1697 new (GetGraph()->GetArena()) LocationSummary(info);
1698}
1699
David Srbeckyd28f4a02016-03-14 17:14:24 +00001700void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1701 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001702}
1703
1704void CodeGeneratorX86_64::GenerateNop() {
1705 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001706}
1707
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001708void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001709 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001710 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001711 // Handle the long/FP comparisons made in instruction simplification.
1712 switch (cond->InputAt(0)->GetType()) {
1713 case Primitive::kPrimLong:
1714 locations->SetInAt(0, Location::RequiresRegister());
1715 locations->SetInAt(1, Location::Any());
1716 break;
1717 case Primitive::kPrimFloat:
1718 case Primitive::kPrimDouble:
1719 locations->SetInAt(0, Location::RequiresFpuRegister());
1720 locations->SetInAt(1, Location::Any());
1721 break;
1722 default:
1723 locations->SetInAt(0, Location::RequiresRegister());
1724 locations->SetInAt(1, Location::Any());
1725 break;
1726 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001727 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001728 locations->SetOut(Location::RequiresRegister());
1729 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001730}
1731
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001732void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001733 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001734 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001735 }
Mark Mendellc4701932015-04-10 13:18:51 -04001736
1737 LocationSummary* locations = cond->GetLocations();
1738 Location lhs = locations->InAt(0);
1739 Location rhs = locations->InAt(1);
1740 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001741 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001742
1743 switch (cond->InputAt(0)->GetType()) {
1744 default:
1745 // Integer case.
1746
1747 // Clear output register: setcc only sets the low byte.
1748 __ xorl(reg, reg);
1749
1750 if (rhs.IsRegister()) {
1751 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1752 } else if (rhs.IsConstant()) {
1753 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001754 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001755 } else {
1756 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1757 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001758 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001759 return;
1760 case Primitive::kPrimLong:
1761 // Clear output register: setcc only sets the low byte.
1762 __ xorl(reg, reg);
1763
1764 if (rhs.IsRegister()) {
1765 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1766 } else if (rhs.IsConstant()) {
1767 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001768 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001769 } else {
1770 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1771 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001772 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001773 return;
1774 case Primitive::kPrimFloat: {
1775 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1776 if (rhs.IsConstant()) {
1777 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1778 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1779 } else if (rhs.IsStackSlot()) {
1780 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1781 } else {
1782 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1783 }
1784 GenerateFPJumps(cond, &true_label, &false_label);
1785 break;
1786 }
1787 case Primitive::kPrimDouble: {
1788 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1789 if (rhs.IsConstant()) {
1790 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1791 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1792 } else if (rhs.IsDoubleStackSlot()) {
1793 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1794 } else {
1795 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1796 }
1797 GenerateFPJumps(cond, &true_label, &false_label);
1798 break;
1799 }
1800 }
1801
1802 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001803 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001804
Roland Levillain4fa13f62015-07-06 18:11:54 +01001805 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001806 __ Bind(&false_label);
1807 __ xorl(reg, reg);
1808 __ jmp(&done_label);
1809
Roland Levillain4fa13f62015-07-06 18:11:54 +01001810 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001811 __ Bind(&true_label);
1812 __ movl(reg, Immediate(1));
1813 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001814}
1815
1816void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001818}
1819
1820void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001822}
1823
1824void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001826}
1827
1828void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001830}
1831
1832void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001834}
1835
1836void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001838}
1839
1840void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001842}
1843
1844void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001846}
1847
1848void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001849 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001850}
1851
1852void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001853 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001854}
1855
1856void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001857 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001858}
1859
1860void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001861 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001862}
1863
Aart Bike9f37602015-10-09 11:15:55 -07001864void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001865 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001866}
1867
1868void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001869 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001870}
1871
1872void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001873 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001874}
1875
1876void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001877 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001878}
1879
1880void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001881 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001882}
1883
1884void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001885 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001886}
1887
1888void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001889 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001890}
1891
1892void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001893 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001894}
1895
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001896void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001897 LocationSummary* locations =
1898 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001899 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001900 case Primitive::kPrimBoolean:
1901 case Primitive::kPrimByte:
1902 case Primitive::kPrimShort:
1903 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001904 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001905 case Primitive::kPrimLong: {
1906 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001907 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1909 break;
1910 }
1911 case Primitive::kPrimFloat:
1912 case Primitive::kPrimDouble: {
1913 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001914 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001915 locations->SetOut(Location::RequiresRegister());
1916 break;
1917 }
1918 default:
1919 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1920 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001921}
1922
1923void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001924 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001925 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001926 Location left = locations->InAt(0);
1927 Location right = locations->InAt(1);
1928
Mark Mendell0c9497d2015-08-21 09:30:05 -04001929 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001931 Condition less_cond = kLess;
1932
Calin Juravleddb7df22014-11-25 20:56:51 +00001933 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001934 case Primitive::kPrimBoolean:
1935 case Primitive::kPrimByte:
1936 case Primitive::kPrimShort:
1937 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001938 case Primitive::kPrimInt: {
1939 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1940 if (right.IsConstant()) {
1941 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1942 codegen_->Compare32BitValue(left_reg, value);
1943 } else if (right.IsStackSlot()) {
1944 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1945 } else {
1946 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1947 }
1948 break;
1949 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001950 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001951 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1952 if (right.IsConstant()) {
1953 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001954 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001955 } else if (right.IsDoubleStackSlot()) {
1956 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001957 } else {
1958 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1959 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001960 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001961 }
1962 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001963 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1964 if (right.IsConstant()) {
1965 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1966 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1967 } else if (right.IsStackSlot()) {
1968 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1969 } else {
1970 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1971 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001972 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001973 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001974 break;
1975 }
1976 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001977 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1978 if (right.IsConstant()) {
1979 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1980 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1981 } else if (right.IsDoubleStackSlot()) {
1982 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1983 } else {
1984 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1985 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001986 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001987 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001988 break;
1989 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001990 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001991 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001992 }
Aart Bika19616e2016-02-01 18:57:58 -08001993
Calin Juravleddb7df22014-11-25 20:56:51 +00001994 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001995 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001996 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001997
Calin Juravle91debbc2014-11-26 19:01:09 +00001998 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001999 __ movl(out, Immediate(1));
2000 __ jmp(&done);
2001
2002 __ Bind(&less);
2003 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002004
2005 __ Bind(&done);
2006}
2007
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002011 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002012}
2013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002014void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002015 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016}
2017
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002018void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2021 locations->SetOut(Location::ConstantLocation(constant));
2022}
2023
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002024void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002025 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002026}
2027
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002028void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002029 LocationSummary* locations =
2030 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002031 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002032}
2033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002035 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036}
2037
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002038void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2039 LocationSummary* locations =
2040 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2041 locations->SetOut(Location::ConstantLocation(constant));
2042}
2043
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002044void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002045 // Will be generated at use site.
2046}
2047
2048void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2049 LocationSummary* locations =
2050 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2051 locations->SetOut(Location::ConstantLocation(constant));
2052}
2053
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002054void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2055 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002056 // Will be generated at use site.
2057}
2058
Calin Juravle27df7582015-04-17 19:12:31 +01002059void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2060 memory_barrier->SetLocations(nullptr);
2061}
2062
2063void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002064 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002065}
2066
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002067void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2068 ret->SetLocations(nullptr);
2069}
2070
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002071void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002072 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002073}
2074
2075void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002076 LocationSummary* locations =
2077 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002078 switch (ret->InputAt(0)->GetType()) {
2079 case Primitive::kPrimBoolean:
2080 case Primitive::kPrimByte:
2081 case Primitive::kPrimChar:
2082 case Primitive::kPrimShort:
2083 case Primitive::kPrimInt:
2084 case Primitive::kPrimNot:
2085 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002086 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002087 break;
2088
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002089 case Primitive::kPrimFloat:
2090 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002091 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002092 break;
2093
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002094 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002095 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002096 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002097}
2098
2099void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2100 if (kIsDebugBuild) {
2101 switch (ret->InputAt(0)->GetType()) {
2102 case Primitive::kPrimBoolean:
2103 case Primitive::kPrimByte:
2104 case Primitive::kPrimChar:
2105 case Primitive::kPrimShort:
2106 case Primitive::kPrimInt:
2107 case Primitive::kPrimNot:
2108 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002109 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002110 break;
2111
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002112 case Primitive::kPrimFloat:
2113 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002115 XMM0);
2116 break;
2117
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002118 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002119 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002120 }
2121 }
2122 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002123}
2124
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002125Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2126 switch (type) {
2127 case Primitive::kPrimBoolean:
2128 case Primitive::kPrimByte:
2129 case Primitive::kPrimChar:
2130 case Primitive::kPrimShort:
2131 case Primitive::kPrimInt:
2132 case Primitive::kPrimNot:
2133 case Primitive::kPrimLong:
2134 return Location::RegisterLocation(RAX);
2135
2136 case Primitive::kPrimVoid:
2137 return Location::NoLocation();
2138
2139 case Primitive::kPrimDouble:
2140 case Primitive::kPrimFloat:
2141 return Location::FpuRegisterLocation(XMM0);
2142 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002143
2144 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002145}
2146
2147Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2148 return Location::RegisterLocation(kMethodRegisterArgument);
2149}
2150
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002151Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002152 switch (type) {
2153 case Primitive::kPrimBoolean:
2154 case Primitive::kPrimByte:
2155 case Primitive::kPrimChar:
2156 case Primitive::kPrimShort:
2157 case Primitive::kPrimInt:
2158 case Primitive::kPrimNot: {
2159 uint32_t index = gp_index_++;
2160 stack_index_++;
2161 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002162 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002163 } else {
2164 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2165 }
2166 }
2167
2168 case Primitive::kPrimLong: {
2169 uint32_t index = gp_index_;
2170 stack_index_ += 2;
2171 if (index < calling_convention.GetNumberOfRegisters()) {
2172 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002173 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002174 } else {
2175 gp_index_ += 2;
2176 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2177 }
2178 }
2179
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002180 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002181 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002182 stack_index_++;
2183 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002184 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002185 } else {
2186 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2187 }
2188 }
2189
2190 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002191 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002192 stack_index_ += 2;
2193 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002194 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002195 } else {
2196 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2197 }
2198 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199
2200 case Primitive::kPrimVoid:
2201 LOG(FATAL) << "Unexpected parameter type " << type;
2202 break;
2203 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002204 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205}
2206
Calin Juravle175dc732015-08-25 15:42:32 +01002207void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2208 // The trampoline uses the same calling convention as dex calling conventions,
2209 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2210 // the method_idx.
2211 HandleInvoke(invoke);
2212}
2213
2214void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2215 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2216}
2217
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002218void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002219 // Explicit clinit checks triggered by static invokes must have been pruned by
2220 // art::PrepareForRegisterAllocation.
2221 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002222
Mark Mendellfb8d2792015-03-31 22:16:59 -04002223 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002224 if (intrinsic.TryDispatch(invoke)) {
2225 return;
2226 }
2227
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002228 HandleInvoke(invoke);
2229}
2230
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002231static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2232 if (invoke->GetLocations()->Intrinsified()) {
2233 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2234 intrinsic.Dispatch(invoke);
2235 return true;
2236 }
2237 return false;
2238}
2239
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002240void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002241 // Explicit clinit checks triggered by static invokes must have been pruned by
2242 // art::PrepareForRegisterAllocation.
2243 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002244
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002245 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2246 return;
2247 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002248
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002249 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002250 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002251 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002252 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002253}
2254
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002255void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002256 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002257 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002258}
2259
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002261 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002262 if (intrinsic.TryDispatch(invoke)) {
2263 return;
2264 }
2265
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266 HandleInvoke(invoke);
2267}
2268
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002269void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002270 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2271 return;
2272 }
2273
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002274 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002275 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002276 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002277}
2278
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2280 HandleInvoke(invoke);
2281 // Add the hidden argument.
2282 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2283}
2284
2285void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2286 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002287 LocationSummary* locations = invoke->GetLocations();
2288 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2289 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002290 Location receiver = locations->InAt(0);
2291 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2292
Roland Levillain0d5a2812015-11-13 10:07:31 +00002293 // Set the hidden argument. This is safe to do this here, as RAX
2294 // won't be modified thereafter, before the `call` instruction.
2295 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002296 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002297
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002298 if (receiver.IsStackSlot()) {
2299 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002300 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002301 __ movl(temp, Address(temp, class_offset));
2302 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002303 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002304 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002305 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002306 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002307 // Instead of simply (possibly) unpoisoning `temp` here, we should
2308 // emit a read barrier for the previous class reference load.
2309 // However this is not required in practice, as this is an
2310 // intermediate/temporary reference and because the current
2311 // concurrent copying collector keeps the from-space memory
2312 // intact/accessible until the end of the marking phase (the
2313 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002314 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002315 // temp = temp->GetAddressOfIMT()
2316 __ movq(temp,
2317 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2318 // temp = temp->GetImtEntryAt(method_offset);
2319 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002320 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002321 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002322 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002323 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002324 __ call(Address(
2325 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002326
2327 DCHECK(!codegen_->IsLeafMethod());
2328 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2329}
2330
Roland Levillain88cb1752014-10-20 16:36:47 +01002331void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2332 LocationSummary* locations =
2333 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2334 switch (neg->GetResultType()) {
2335 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002336 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002337 locations->SetInAt(0, Location::RequiresRegister());
2338 locations->SetOut(Location::SameAsFirstInput());
2339 break;
2340
Roland Levillain88cb1752014-10-20 16:36:47 +01002341 case Primitive::kPrimFloat:
2342 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002343 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002344 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002345 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002346 break;
2347
2348 default:
2349 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2350 }
2351}
2352
2353void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2354 LocationSummary* locations = neg->GetLocations();
2355 Location out = locations->Out();
2356 Location in = locations->InAt(0);
2357 switch (neg->GetResultType()) {
2358 case Primitive::kPrimInt:
2359 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002360 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002362 break;
2363
2364 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002365 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002366 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002367 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002368 break;
2369
Roland Levillain5368c212014-11-27 15:03:41 +00002370 case Primitive::kPrimFloat: {
2371 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002372 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002373 // Implement float negation with an exclusive or with value
2374 // 0x80000000 (mask for bit 31, representing the sign of a
2375 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002376 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002377 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002378 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002379 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002380
Roland Levillain5368c212014-11-27 15:03:41 +00002381 case Primitive::kPrimDouble: {
2382 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002383 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002384 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002385 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002386 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002387 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002388 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002389 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002390 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002391
2392 default:
2393 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2394 }
2395}
2396
Roland Levillaindff1f282014-11-05 14:15:05 +00002397void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2398 LocationSummary* locations =
2399 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2400 Primitive::Type result_type = conversion->GetResultType();
2401 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002402 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002403
David Brazdilb2bd1c52015-03-25 11:17:37 +00002404 // The Java language does not allow treating boolean as an integral type but
2405 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002406
Roland Levillaindff1f282014-11-05 14:15:05 +00002407 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002408 case Primitive::kPrimByte:
2409 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002410 case Primitive::kPrimLong:
2411 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002412 case Primitive::kPrimBoolean:
2413 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002414 case Primitive::kPrimShort:
2415 case Primitive::kPrimInt:
2416 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002417 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002418 locations->SetInAt(0, Location::Any());
2419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 }
2426 break;
2427
Roland Levillain01a8d712014-11-14 16:27:39 +00002428 case Primitive::kPrimShort:
2429 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002430 case Primitive::kPrimLong:
2431 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002432 case Primitive::kPrimBoolean:
2433 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002434 case Primitive::kPrimByte:
2435 case Primitive::kPrimInt:
2436 case Primitive::kPrimChar:
2437 // Processing a Dex `int-to-short' instruction.
2438 locations->SetInAt(0, Location::Any());
2439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2440 break;
2441
2442 default:
2443 LOG(FATAL) << "Unexpected type conversion from " << input_type
2444 << " to " << result_type;
2445 }
2446 break;
2447
Roland Levillain946e1432014-11-11 17:35:19 +00002448 case Primitive::kPrimInt:
2449 switch (input_type) {
2450 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002451 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002452 locations->SetInAt(0, Location::Any());
2453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2454 break;
2455
2456 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002457 // Processing a Dex `float-to-int' instruction.
2458 locations->SetInAt(0, Location::RequiresFpuRegister());
2459 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002460 break;
2461
Roland Levillain946e1432014-11-11 17:35:19 +00002462 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002463 // Processing a Dex `double-to-int' instruction.
2464 locations->SetInAt(0, Location::RequiresFpuRegister());
2465 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002466 break;
2467
2468 default:
2469 LOG(FATAL) << "Unexpected type conversion from " << input_type
2470 << " to " << result_type;
2471 }
2472 break;
2473
Roland Levillaindff1f282014-11-05 14:15:05 +00002474 case Primitive::kPrimLong:
2475 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002476 case Primitive::kPrimBoolean:
2477 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002478 case Primitive::kPrimByte:
2479 case Primitive::kPrimShort:
2480 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002481 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002482 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002483 // TODO: We would benefit from a (to-be-implemented)
2484 // Location::RegisterOrStackSlot requirement for this input.
2485 locations->SetInAt(0, Location::RequiresRegister());
2486 locations->SetOut(Location::RequiresRegister());
2487 break;
2488
2489 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002490 // Processing a Dex `float-to-long' instruction.
2491 locations->SetInAt(0, Location::RequiresFpuRegister());
2492 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002493 break;
2494
Roland Levillaindff1f282014-11-05 14:15:05 +00002495 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002496 // Processing a Dex `double-to-long' instruction.
2497 locations->SetInAt(0, Location::RequiresFpuRegister());
2498 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002499 break;
2500
2501 default:
2502 LOG(FATAL) << "Unexpected type conversion from " << input_type
2503 << " to " << result_type;
2504 }
2505 break;
2506
Roland Levillain981e4542014-11-14 11:47:14 +00002507 case Primitive::kPrimChar:
2508 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002509 case Primitive::kPrimLong:
2510 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002511 case Primitive::kPrimBoolean:
2512 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002513 case Primitive::kPrimByte:
2514 case Primitive::kPrimShort:
2515 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002516 // Processing a Dex `int-to-char' instruction.
2517 locations->SetInAt(0, Location::Any());
2518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2519 break;
2520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 }
2525 break;
2526
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002528 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002529 case Primitive::kPrimBoolean:
2530 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002531 case Primitive::kPrimByte:
2532 case Primitive::kPrimShort:
2533 case Primitive::kPrimInt:
2534 case Primitive::kPrimChar:
2535 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002536 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002537 locations->SetOut(Location::RequiresFpuRegister());
2538 break;
2539
2540 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002541 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002542 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002543 locations->SetOut(Location::RequiresFpuRegister());
2544 break;
2545
Roland Levillaincff13742014-11-17 14:32:17 +00002546 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002547 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002548 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002550 break;
2551
2552 default:
2553 LOG(FATAL) << "Unexpected type conversion from " << input_type
2554 << " to " << result_type;
2555 };
2556 break;
2557
Roland Levillaindff1f282014-11-05 14:15:05 +00002558 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002559 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002560 case Primitive::kPrimBoolean:
2561 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002562 case Primitive::kPrimByte:
2563 case Primitive::kPrimShort:
2564 case Primitive::kPrimInt:
2565 case Primitive::kPrimChar:
2566 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002567 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002568 locations->SetOut(Location::RequiresFpuRegister());
2569 break;
2570
2571 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002572 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002573 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002574 locations->SetOut(Location::RequiresFpuRegister());
2575 break;
2576
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002578 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002579 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 break;
2588
2589 default:
2590 LOG(FATAL) << "Unexpected type conversion from " << input_type
2591 << " to " << result_type;
2592 }
2593}
2594
2595void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2596 LocationSummary* locations = conversion->GetLocations();
2597 Location out = locations->Out();
2598 Location in = locations->InAt(0);
2599 Primitive::Type result_type = conversion->GetResultType();
2600 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002601 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002603 case Primitive::kPrimByte:
2604 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002605 case Primitive::kPrimLong:
2606 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002607 case Primitive::kPrimBoolean:
2608 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002609 case Primitive::kPrimShort:
2610 case Primitive::kPrimInt:
2611 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002612 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002613 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002614 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002615 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002616 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002617 Address(CpuRegister(RSP), in.GetStackIndex()));
2618 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002619 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002620 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002621 }
2622 break;
2623
2624 default:
2625 LOG(FATAL) << "Unexpected type conversion from " << input_type
2626 << " to " << result_type;
2627 }
2628 break;
2629
Roland Levillain01a8d712014-11-14 16:27:39 +00002630 case Primitive::kPrimShort:
2631 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002632 case Primitive::kPrimLong:
2633 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002634 case Primitive::kPrimBoolean:
2635 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002636 case Primitive::kPrimByte:
2637 case Primitive::kPrimInt:
2638 case Primitive::kPrimChar:
2639 // Processing a Dex `int-to-short' instruction.
2640 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002641 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002642 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002643 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002644 Address(CpuRegister(RSP), in.GetStackIndex()));
2645 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002646 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002647 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002648 }
2649 break;
2650
2651 default:
2652 LOG(FATAL) << "Unexpected type conversion from " << input_type
2653 << " to " << result_type;
2654 }
2655 break;
2656
Roland Levillain946e1432014-11-11 17:35:19 +00002657 case Primitive::kPrimInt:
2658 switch (input_type) {
2659 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002660 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002661 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002662 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002663 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002665 Address(CpuRegister(RSP), in.GetStackIndex()));
2666 } else {
2667 DCHECK(in.IsConstant());
2668 DCHECK(in.GetConstant()->IsLongConstant());
2669 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002671 }
2672 break;
2673
Roland Levillain3f8f9362014-12-02 17:45:01 +00002674 case Primitive::kPrimFloat: {
2675 // Processing a Dex `float-to-int' instruction.
2676 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2677 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002678 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002679
2680 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002681 // if input >= (float)INT_MAX goto done
2682 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002683 __ j(kAboveEqual, &done);
2684 // if input == NaN goto nan
2685 __ j(kUnordered, &nan);
2686 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002687 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002688 __ jmp(&done);
2689 __ Bind(&nan);
2690 // output = 0
2691 __ xorl(output, output);
2692 __ Bind(&done);
2693 break;
2694 }
2695
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002696 case Primitive::kPrimDouble: {
2697 // Processing a Dex `double-to-int' instruction.
2698 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2699 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002700 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002701
2702 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002703 // if input >= (double)INT_MAX goto done
2704 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002705 __ j(kAboveEqual, &done);
2706 // if input == NaN goto nan
2707 __ j(kUnordered, &nan);
2708 // output = double-to-int-truncate(input)
2709 __ cvttsd2si(output, input);
2710 __ jmp(&done);
2711 __ Bind(&nan);
2712 // output = 0
2713 __ xorl(output, output);
2714 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002715 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002716 }
Roland Levillain946e1432014-11-11 17:35:19 +00002717
2718 default:
2719 LOG(FATAL) << "Unexpected type conversion from " << input_type
2720 << " to " << result_type;
2721 }
2722 break;
2723
Roland Levillaindff1f282014-11-05 14:15:05 +00002724 case Primitive::kPrimLong:
2725 switch (input_type) {
2726 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002727 case Primitive::kPrimBoolean:
2728 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002729 case Primitive::kPrimByte:
2730 case Primitive::kPrimShort:
2731 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002732 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002733 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002734 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002735 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002736 break;
2737
Roland Levillain624279f2014-12-04 11:54:28 +00002738 case Primitive::kPrimFloat: {
2739 // Processing a Dex `float-to-long' instruction.
2740 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2741 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002742 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002743
Mark Mendell92e83bf2015-05-07 11:25:03 -04002744 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002745 // if input >= (float)LONG_MAX goto done
2746 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002747 __ j(kAboveEqual, &done);
2748 // if input == NaN goto nan
2749 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002750 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002751 __ cvttss2si(output, input, true);
2752 __ jmp(&done);
2753 __ Bind(&nan);
2754 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002755 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002756 __ Bind(&done);
2757 break;
2758 }
2759
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002760 case Primitive::kPrimDouble: {
2761 // Processing a Dex `double-to-long' instruction.
2762 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2763 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002764 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002765
Mark Mendell92e83bf2015-05-07 11:25:03 -04002766 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002767 // if input >= (double)LONG_MAX goto done
2768 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002769 __ j(kAboveEqual, &done);
2770 // if input == NaN goto nan
2771 __ j(kUnordered, &nan);
2772 // output = double-to-long-truncate(input)
2773 __ cvttsd2si(output, input, true);
2774 __ jmp(&done);
2775 __ Bind(&nan);
2776 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002777 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002778 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002779 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002780 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002781
2782 default:
2783 LOG(FATAL) << "Unexpected type conversion from " << input_type
2784 << " to " << result_type;
2785 }
2786 break;
2787
Roland Levillain981e4542014-11-14 11:47:14 +00002788 case Primitive::kPrimChar:
2789 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002790 case Primitive::kPrimLong:
2791 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002792 case Primitive::kPrimBoolean:
2793 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002794 case Primitive::kPrimByte:
2795 case Primitive::kPrimShort:
2796 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002797 // Processing a Dex `int-to-char' instruction.
2798 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002799 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002800 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002802 Address(CpuRegister(RSP), in.GetStackIndex()));
2803 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002804 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002805 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002806 }
2807 break;
2808
2809 default:
2810 LOG(FATAL) << "Unexpected type conversion from " << input_type
2811 << " to " << result_type;
2812 }
2813 break;
2814
Roland Levillaindff1f282014-11-05 14:15:05 +00002815 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002817 case Primitive::kPrimBoolean:
2818 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002819 case Primitive::kPrimByte:
2820 case Primitive::kPrimShort:
2821 case Primitive::kPrimInt:
2822 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002823 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002824 if (in.IsRegister()) {
2825 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2826 } else if (in.IsConstant()) {
2827 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2828 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002829 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002830 } else {
2831 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2832 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2833 }
Roland Levillaincff13742014-11-17 14:32:17 +00002834 break;
2835
2836 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002837 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002838 if (in.IsRegister()) {
2839 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2840 } else if (in.IsConstant()) {
2841 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2842 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002843 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002844 } else {
2845 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2846 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2847 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002848 break;
2849
Roland Levillaincff13742014-11-17 14:32:17 +00002850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002851 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002852 if (in.IsFpuRegister()) {
2853 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2854 } else if (in.IsConstant()) {
2855 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2856 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002857 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002858 } else {
2859 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2860 Address(CpuRegister(RSP), in.GetStackIndex()));
2861 }
Roland Levillaincff13742014-11-17 14:32:17 +00002862 break;
2863
2864 default:
2865 LOG(FATAL) << "Unexpected type conversion from " << input_type
2866 << " to " << result_type;
2867 };
2868 break;
2869
Roland Levillaindff1f282014-11-05 14:15:05 +00002870 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002871 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002872 case Primitive::kPrimBoolean:
2873 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002874 case Primitive::kPrimByte:
2875 case Primitive::kPrimShort:
2876 case Primitive::kPrimInt:
2877 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002878 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002879 if (in.IsRegister()) {
2880 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2881 } else if (in.IsConstant()) {
2882 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2883 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002884 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002885 } else {
2886 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2887 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2888 }
Roland Levillaincff13742014-11-17 14:32:17 +00002889 break;
2890
2891 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002892 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002893 if (in.IsRegister()) {
2894 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2895 } else if (in.IsConstant()) {
2896 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2897 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002898 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002899 } else {
2900 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2901 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2902 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002903 break;
2904
Roland Levillaincff13742014-11-17 14:32:17 +00002905 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002906 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002907 if (in.IsFpuRegister()) {
2908 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2909 } else if (in.IsConstant()) {
2910 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2911 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002912 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002913 } else {
2914 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2915 Address(CpuRegister(RSP), in.GetStackIndex()));
2916 }
Roland Levillaincff13742014-11-17 14:32:17 +00002917 break;
2918
2919 default:
2920 LOG(FATAL) << "Unexpected type conversion from " << input_type
2921 << " to " << result_type;
2922 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002923 break;
2924
2925 default:
2926 LOG(FATAL) << "Unexpected type conversion from " << input_type
2927 << " to " << result_type;
2928 }
2929}
2930
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002931void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002932 LocationSummary* locations =
2933 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002934 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002935 case Primitive::kPrimInt: {
2936 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002937 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002939 break;
2940 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002941
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002942 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002943 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002944 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002945 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002947 break;
2948 }
2949
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 case Primitive::kPrimDouble:
2951 case Primitive::kPrimFloat: {
2952 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002953 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002954 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002955 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002956 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002957
2958 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002959 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002960 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002961}
2962
2963void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2964 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 Location first = locations->InAt(0);
2966 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002967 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002968
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002969 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002970 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002971 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002972 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2973 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002974 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2975 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002976 } else {
2977 __ leal(out.AsRegister<CpuRegister>(), Address(
2978 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2979 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002980 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002981 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2982 __ addl(out.AsRegister<CpuRegister>(),
2983 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2984 } else {
2985 __ leal(out.AsRegister<CpuRegister>(), Address(
2986 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2987 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002988 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002989 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002990 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002991 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002992 break;
2993 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002994
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002995 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002996 if (second.IsRegister()) {
2997 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2998 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002999 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3000 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003001 } else {
3002 __ leaq(out.AsRegister<CpuRegister>(), Address(
3003 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3004 }
3005 } else {
3006 DCHECK(second.IsConstant());
3007 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3008 int32_t int32_value = Low32Bits(value);
3009 DCHECK_EQ(int32_value, value);
3010 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3011 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3012 } else {
3013 __ leaq(out.AsRegister<CpuRegister>(), Address(
3014 first.AsRegister<CpuRegister>(), int32_value));
3015 }
3016 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003017 break;
3018 }
3019
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003020 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003021 if (second.IsFpuRegister()) {
3022 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3023 } else if (second.IsConstant()) {
3024 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003025 codegen_->LiteralFloatAddress(
3026 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003027 } else {
3028 DCHECK(second.IsStackSlot());
3029 __ addss(first.AsFpuRegister<XmmRegister>(),
3030 Address(CpuRegister(RSP), second.GetStackIndex()));
3031 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003032 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003033 }
3034
3035 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003036 if (second.IsFpuRegister()) {
3037 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3038 } else if (second.IsConstant()) {
3039 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003040 codegen_->LiteralDoubleAddress(
3041 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003042 } else {
3043 DCHECK(second.IsDoubleStackSlot());
3044 __ addsd(first.AsFpuRegister<XmmRegister>(),
3045 Address(CpuRegister(RSP), second.GetStackIndex()));
3046 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003047 break;
3048 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003049
3050 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003051 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003052 }
3053}
3054
3055void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003056 LocationSummary* locations =
3057 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003059 case Primitive::kPrimInt: {
3060 locations->SetInAt(0, Location::RequiresRegister());
3061 locations->SetInAt(1, Location::Any());
3062 locations->SetOut(Location::SameAsFirstInput());
3063 break;
3064 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003065 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003066 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003067 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003068 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003069 break;
3070 }
Calin Juravle11351682014-10-23 15:38:15 +01003071 case Primitive::kPrimFloat:
3072 case Primitive::kPrimDouble: {
3073 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003074 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003075 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003076 break;
Calin Juravle11351682014-10-23 15:38:15 +01003077 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003078 default:
Calin Juravle11351682014-10-23 15:38:15 +01003079 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003080 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003081}
3082
3083void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3084 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003085 Location first = locations->InAt(0);
3086 Location second = locations->InAt(1);
3087 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003088 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003089 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003090 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003091 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003092 } else if (second.IsConstant()) {
3093 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003095 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003096 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003097 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003098 break;
3099 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003100 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003101 if (second.IsConstant()) {
3102 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3103 DCHECK(IsInt<32>(value));
3104 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3105 } else {
3106 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3107 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003108 break;
3109 }
3110
Calin Juravle11351682014-10-23 15:38:15 +01003111 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003112 if (second.IsFpuRegister()) {
3113 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3114 } else if (second.IsConstant()) {
3115 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003116 codegen_->LiteralFloatAddress(
3117 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003118 } else {
3119 DCHECK(second.IsStackSlot());
3120 __ subss(first.AsFpuRegister<XmmRegister>(),
3121 Address(CpuRegister(RSP), second.GetStackIndex()));
3122 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003123 break;
Calin Juravle11351682014-10-23 15:38:15 +01003124 }
3125
3126 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003127 if (second.IsFpuRegister()) {
3128 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3129 } else if (second.IsConstant()) {
3130 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003131 codegen_->LiteralDoubleAddress(
3132 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003133 } else {
3134 DCHECK(second.IsDoubleStackSlot());
3135 __ subsd(first.AsFpuRegister<XmmRegister>(),
3136 Address(CpuRegister(RSP), second.GetStackIndex()));
3137 }
Calin Juravle11351682014-10-23 15:38:15 +01003138 break;
3139 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003140
3141 default:
Calin Juravle11351682014-10-23 15:38:15 +01003142 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003143 }
3144}
3145
Calin Juravle34bacdf2014-10-07 20:23:36 +01003146void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3147 LocationSummary* locations =
3148 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3149 switch (mul->GetResultType()) {
3150 case Primitive::kPrimInt: {
3151 locations->SetInAt(0, Location::RequiresRegister());
3152 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003153 if (mul->InputAt(1)->IsIntConstant()) {
3154 // Can use 3 operand multiply.
3155 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3156 } else {
3157 locations->SetOut(Location::SameAsFirstInput());
3158 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003159 break;
3160 }
3161 case Primitive::kPrimLong: {
3162 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003163 locations->SetInAt(1, Location::Any());
3164 if (mul->InputAt(1)->IsLongConstant() &&
3165 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003166 // Can use 3 operand multiply.
3167 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3168 } else {
3169 locations->SetOut(Location::SameAsFirstInput());
3170 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 break;
3172 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003173 case Primitive::kPrimFloat:
3174 case Primitive::kPrimDouble: {
3175 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003176 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003177 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003179 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003180
3181 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003182 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 }
3184}
3185
3186void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3187 LocationSummary* locations = mul->GetLocations();
3188 Location first = locations->InAt(0);
3189 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003190 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003192 case Primitive::kPrimInt:
3193 // The constant may have ended up in a register, so test explicitly to avoid
3194 // problems where the output may not be the same as the first operand.
3195 if (mul->InputAt(1)->IsIntConstant()) {
3196 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3197 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3198 } else if (second.IsRegister()) {
3199 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003200 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003201 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003202 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003204 __ imull(first.AsRegister<CpuRegister>(),
3205 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206 }
3207 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003208 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003209 // The constant may have ended up in a register, so test explicitly to avoid
3210 // problems where the output may not be the same as the first operand.
3211 if (mul->InputAt(1)->IsLongConstant()) {
3212 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3213 if (IsInt<32>(value)) {
3214 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3215 Immediate(static_cast<int32_t>(value)));
3216 } else {
3217 // Have to use the constant area.
3218 DCHECK(first.Equals(out));
3219 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3220 }
3221 } else if (second.IsRegister()) {
3222 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003223 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003224 } else {
3225 DCHECK(second.IsDoubleStackSlot());
3226 DCHECK(first.Equals(out));
3227 __ imulq(first.AsRegister<CpuRegister>(),
3228 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003229 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 break;
3231 }
3232
Calin Juravleb5bfa962014-10-21 18:02:24 +01003233 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003234 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003235 if (second.IsFpuRegister()) {
3236 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3237 } else if (second.IsConstant()) {
3238 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003239 codegen_->LiteralFloatAddress(
3240 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003241 } else {
3242 DCHECK(second.IsStackSlot());
3243 __ mulss(first.AsFpuRegister<XmmRegister>(),
3244 Address(CpuRegister(RSP), second.GetStackIndex()));
3245 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003247 }
3248
3249 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003250 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003251 if (second.IsFpuRegister()) {
3252 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3253 } else if (second.IsConstant()) {
3254 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003255 codegen_->LiteralDoubleAddress(
3256 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003257 } else {
3258 DCHECK(second.IsDoubleStackSlot());
3259 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3260 Address(CpuRegister(RSP), second.GetStackIndex()));
3261 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003262 break;
3263 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003264
3265 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003266 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003267 }
3268}
3269
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003270void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3271 uint32_t stack_adjustment, bool is_float) {
3272 if (source.IsStackSlot()) {
3273 DCHECK(is_float);
3274 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3275 } else if (source.IsDoubleStackSlot()) {
3276 DCHECK(!is_float);
3277 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3278 } else {
3279 // Write the value to the temporary location on the stack and load to FP stack.
3280 if (is_float) {
3281 Location stack_temp = Location::StackSlot(temp_offset);
3282 codegen_->Move(stack_temp, source);
3283 __ flds(Address(CpuRegister(RSP), temp_offset));
3284 } else {
3285 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3286 codegen_->Move(stack_temp, source);
3287 __ fldl(Address(CpuRegister(RSP), temp_offset));
3288 }
3289 }
3290}
3291
3292void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3293 Primitive::Type type = rem->GetResultType();
3294 bool is_float = type == Primitive::kPrimFloat;
3295 size_t elem_size = Primitive::ComponentSize(type);
3296 LocationSummary* locations = rem->GetLocations();
3297 Location first = locations->InAt(0);
3298 Location second = locations->InAt(1);
3299 Location out = locations->Out();
3300
3301 // Create stack space for 2 elements.
3302 // TODO: enhance register allocator to ask for stack temporaries.
3303 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3304
3305 // Load the values to the FP stack in reverse order, using temporaries if needed.
3306 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3307 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3308
3309 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003310 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003311 __ Bind(&retry);
3312 __ fprem();
3313
3314 // Move FP status to AX.
3315 __ fstsw();
3316
3317 // And see if the argument reduction is complete. This is signaled by the
3318 // C2 FPU flag bit set to 0.
3319 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3320 __ j(kNotEqual, &retry);
3321
3322 // We have settled on the final value. Retrieve it into an XMM register.
3323 // Store FP top of stack to real stack.
3324 if (is_float) {
3325 __ fsts(Address(CpuRegister(RSP), 0));
3326 } else {
3327 __ fstl(Address(CpuRegister(RSP), 0));
3328 }
3329
3330 // Pop the 2 items from the FP stack.
3331 __ fucompp();
3332
3333 // Load the value from the stack into an XMM register.
3334 DCHECK(out.IsFpuRegister()) << out;
3335 if (is_float) {
3336 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3337 } else {
3338 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3339 }
3340
3341 // And remove the temporary stack space we allocated.
3342 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3343}
3344
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003345void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3346 DCHECK(instruction->IsDiv() || instruction->IsRem());
3347
3348 LocationSummary* locations = instruction->GetLocations();
3349 Location second = locations->InAt(1);
3350 DCHECK(second.IsConstant());
3351
3352 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3353 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003354 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003355
3356 DCHECK(imm == 1 || imm == -1);
3357
3358 switch (instruction->GetResultType()) {
3359 case Primitive::kPrimInt: {
3360 if (instruction->IsRem()) {
3361 __ xorl(output_register, output_register);
3362 } else {
3363 __ movl(output_register, input_register);
3364 if (imm == -1) {
3365 __ negl(output_register);
3366 }
3367 }
3368 break;
3369 }
3370
3371 case Primitive::kPrimLong: {
3372 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003373 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003374 } else {
3375 __ movq(output_register, input_register);
3376 if (imm == -1) {
3377 __ negq(output_register);
3378 }
3379 }
3380 break;
3381 }
3382
3383 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003384 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003385 }
3386}
3387
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003388void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003389 LocationSummary* locations = instruction->GetLocations();
3390 Location second = locations->InAt(1);
3391
3392 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3393 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3394
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003395 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003396 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3397 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003398
3399 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3400
3401 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003402 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003403 __ testl(numerator, numerator);
3404 __ cmov(kGreaterEqual, tmp, numerator);
3405 int shift = CTZ(imm);
3406 __ sarl(tmp, Immediate(shift));
3407
3408 if (imm < 0) {
3409 __ negl(tmp);
3410 }
3411
3412 __ movl(output_register, tmp);
3413 } else {
3414 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3415 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3416
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003417 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003418 __ addq(rdx, numerator);
3419 __ testq(numerator, numerator);
3420 __ cmov(kGreaterEqual, rdx, numerator);
3421 int shift = CTZ(imm);
3422 __ sarq(rdx, Immediate(shift));
3423
3424 if (imm < 0) {
3425 __ negq(rdx);
3426 }
3427
3428 __ movq(output_register, rdx);
3429 }
3430}
3431
3432void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3433 DCHECK(instruction->IsDiv() || instruction->IsRem());
3434
3435 LocationSummary* locations = instruction->GetLocations();
3436 Location second = locations->InAt(1);
3437
3438 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3439 : locations->GetTemp(0).AsRegister<CpuRegister>();
3440 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3441 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3442 : locations->Out().AsRegister<CpuRegister>();
3443 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3444
3445 DCHECK_EQ(RAX, eax.AsRegister());
3446 DCHECK_EQ(RDX, edx.AsRegister());
3447 if (instruction->IsDiv()) {
3448 DCHECK_EQ(RAX, out.AsRegister());
3449 } else {
3450 DCHECK_EQ(RDX, out.AsRegister());
3451 }
3452
3453 int64_t magic;
3454 int shift;
3455
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003456 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003457 if (instruction->GetResultType() == Primitive::kPrimInt) {
3458 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3459
3460 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3461
3462 __ movl(numerator, eax);
3463
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464 __ movl(eax, Immediate(magic));
3465 __ imull(numerator);
3466
3467 if (imm > 0 && magic < 0) {
3468 __ addl(edx, numerator);
3469 } else if (imm < 0 && magic > 0) {
3470 __ subl(edx, numerator);
3471 }
3472
3473 if (shift != 0) {
3474 __ sarl(edx, Immediate(shift));
3475 }
3476
3477 __ movl(eax, edx);
3478 __ shrl(edx, Immediate(31));
3479 __ addl(edx, eax);
3480
3481 if (instruction->IsRem()) {
3482 __ movl(eax, numerator);
3483 __ imull(edx, Immediate(imm));
3484 __ subl(eax, edx);
3485 __ movl(edx, eax);
3486 } else {
3487 __ movl(eax, edx);
3488 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003489 } else {
3490 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3491
3492 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3493
3494 CpuRegister rax = eax;
3495 CpuRegister rdx = edx;
3496
3497 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3498
3499 // Save the numerator.
3500 __ movq(numerator, rax);
3501
3502 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003503 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504
3505 // RDX:RAX = magic * numerator
3506 __ imulq(numerator);
3507
3508 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003509 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 __ addq(rdx, numerator);
3511 } else if (imm < 0 && magic > 0) {
3512 // RDX -= numerator
3513 __ subq(rdx, numerator);
3514 }
3515
3516 // Shift if needed.
3517 if (shift != 0) {
3518 __ sarq(rdx, Immediate(shift));
3519 }
3520
3521 // RDX += 1 if RDX < 0
3522 __ movq(rax, rdx);
3523 __ shrq(rdx, Immediate(63));
3524 __ addq(rdx, rax);
3525
3526 if (instruction->IsRem()) {
3527 __ movq(rax, numerator);
3528
3529 if (IsInt<32>(imm)) {
3530 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3531 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003532 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533 }
3534
3535 __ subq(rax, rdx);
3536 __ movq(rdx, rax);
3537 } else {
3538 __ movq(rax, rdx);
3539 }
3540 }
3541}
3542
Calin Juravlebacfec32014-11-14 15:54:36 +00003543void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3544 DCHECK(instruction->IsDiv() || instruction->IsRem());
3545 Primitive::Type type = instruction->GetResultType();
3546 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3547
3548 bool is_div = instruction->IsDiv();
3549 LocationSummary* locations = instruction->GetLocations();
3550
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003551 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3552 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003553
Roland Levillain271ab9c2014-11-27 15:23:57 +00003554 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003556
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003558 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003559
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003560 if (imm == 0) {
3561 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3562 } else if (imm == 1 || imm == -1) {
3563 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003564 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003565 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003566 } else {
3567 DCHECK(imm <= -2 || imm >= 2);
3568 GenerateDivRemWithAnyConstant(instruction);
3569 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003570 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003571 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003572 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003573 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003575
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3577 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3578 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3579 // so it's safe to just use negl instead of more complex comparisons.
3580 if (type == Primitive::kPrimInt) {
3581 __ cmpl(second_reg, Immediate(-1));
3582 __ j(kEqual, slow_path->GetEntryLabel());
3583 // edx:eax <- sign-extended of eax
3584 __ cdq();
3585 // eax = quotient, edx = remainder
3586 __ idivl(second_reg);
3587 } else {
3588 __ cmpq(second_reg, Immediate(-1));
3589 __ j(kEqual, slow_path->GetEntryLabel());
3590 // rdx:rax <- sign-extended of rax
3591 __ cqo();
3592 // rax = quotient, rdx = remainder
3593 __ idivq(second_reg);
3594 }
3595 __ Bind(slow_path->GetExitLabel());
3596 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003597}
3598
Calin Juravle7c4954d2014-10-28 16:57:40 +00003599void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3600 LocationSummary* locations =
3601 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3602 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003603 case Primitive::kPrimInt:
3604 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003605 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003607 locations->SetOut(Location::SameAsFirstInput());
3608 // Intel uses edx:eax as the dividend.
3609 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003610 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3611 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3612 // output and request another temp.
3613 if (div->InputAt(1)->IsConstant()) {
3614 locations->AddTemp(Location::RequiresRegister());
3615 }
Calin Juravled0d48522014-11-04 16:40:20 +00003616 break;
3617 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003618
Calin Juravle7c4954d2014-10-28 16:57:40 +00003619 case Primitive::kPrimFloat:
3620 case Primitive::kPrimDouble: {
3621 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003622 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003623 locations->SetOut(Location::SameAsFirstInput());
3624 break;
3625 }
3626
3627 default:
3628 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3629 }
3630}
3631
3632void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3633 LocationSummary* locations = div->GetLocations();
3634 Location first = locations->InAt(0);
3635 Location second = locations->InAt(1);
3636 DCHECK(first.Equals(locations->Out()));
3637
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 Primitive::Type type = div->GetResultType();
3639 switch (type) {
3640 case Primitive::kPrimInt:
3641 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003642 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003643 break;
3644 }
3645
Calin Juravle7c4954d2014-10-28 16:57:40 +00003646 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003647 if (second.IsFpuRegister()) {
3648 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3649 } else if (second.IsConstant()) {
3650 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003651 codegen_->LiteralFloatAddress(
3652 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003653 } else {
3654 DCHECK(second.IsStackSlot());
3655 __ divss(first.AsFpuRegister<XmmRegister>(),
3656 Address(CpuRegister(RSP), second.GetStackIndex()));
3657 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003658 break;
3659 }
3660
3661 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003662 if (second.IsFpuRegister()) {
3663 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3664 } else if (second.IsConstant()) {
3665 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003666 codegen_->LiteralDoubleAddress(
3667 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003668 } else {
3669 DCHECK(second.IsDoubleStackSlot());
3670 __ divsd(first.AsFpuRegister<XmmRegister>(),
3671 Address(CpuRegister(RSP), second.GetStackIndex()));
3672 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003673 break;
3674 }
3675
3676 default:
3677 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3678 }
3679}
3680
Calin Juravlebacfec32014-11-14 15:54:36 +00003681void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003682 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003683 LocationSummary* locations =
3684 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003685
3686 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003687 case Primitive::kPrimInt:
3688 case Primitive::kPrimLong: {
3689 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003690 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003691 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3692 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003693 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3694 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3695 // output and request another temp.
3696 if (rem->InputAt(1)->IsConstant()) {
3697 locations->AddTemp(Location::RequiresRegister());
3698 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003699 break;
3700 }
3701
3702 case Primitive::kPrimFloat:
3703 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003704 locations->SetInAt(0, Location::Any());
3705 locations->SetInAt(1, Location::Any());
3706 locations->SetOut(Location::RequiresFpuRegister());
3707 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003708 break;
3709 }
3710
3711 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003712 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003713 }
3714}
3715
3716void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3717 Primitive::Type type = rem->GetResultType();
3718 switch (type) {
3719 case Primitive::kPrimInt:
3720 case Primitive::kPrimLong: {
3721 GenerateDivRemIntegral(rem);
3722 break;
3723 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003724 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003725 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003726 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003727 break;
3728 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003729 default:
3730 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3731 }
3732}
3733
Calin Juravled0d48522014-11-04 16:40:20 +00003734void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003735 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3736 ? LocationSummary::kCallOnSlowPath
3737 : LocationSummary::kNoCall;
3738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003739 locations->SetInAt(0, Location::Any());
3740 if (instruction->HasUses()) {
3741 locations->SetOut(Location::SameAsFirstInput());
3742 }
3743}
3744
3745void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003746 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003747 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3748 codegen_->AddSlowPath(slow_path);
3749
3750 LocationSummary* locations = instruction->GetLocations();
3751 Location value = locations->InAt(0);
3752
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003753 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003754 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003755 case Primitive::kPrimByte:
3756 case Primitive::kPrimChar:
3757 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003758 case Primitive::kPrimInt: {
3759 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003761 __ j(kEqual, slow_path->GetEntryLabel());
3762 } else if (value.IsStackSlot()) {
3763 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3764 __ j(kEqual, slow_path->GetEntryLabel());
3765 } else {
3766 DCHECK(value.IsConstant()) << value;
3767 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3768 __ jmp(slow_path->GetEntryLabel());
3769 }
3770 }
3771 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003772 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003773 case Primitive::kPrimLong: {
3774 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003776 __ j(kEqual, slow_path->GetEntryLabel());
3777 } else if (value.IsDoubleStackSlot()) {
3778 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3779 __ j(kEqual, slow_path->GetEntryLabel());
3780 } else {
3781 DCHECK(value.IsConstant()) << value;
3782 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3783 __ jmp(slow_path->GetEntryLabel());
3784 }
3785 }
3786 break;
3787 }
3788 default:
3789 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003790 }
Calin Juravled0d48522014-11-04 16:40:20 +00003791}
3792
Calin Juravle9aec02f2014-11-18 23:06:35 +00003793void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3794 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3795
3796 LocationSummary* locations =
3797 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3798
3799 switch (op->GetResultType()) {
3800 case Primitive::kPrimInt:
3801 case Primitive::kPrimLong: {
3802 locations->SetInAt(0, Location::RequiresRegister());
3803 // The shift count needs to be in CL.
3804 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3805 locations->SetOut(Location::SameAsFirstInput());
3806 break;
3807 }
3808 default:
3809 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3810 }
3811}
3812
3813void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3814 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3815
3816 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003818 Location second = locations->InAt(1);
3819
3820 switch (op->GetResultType()) {
3821 case Primitive::kPrimInt: {
3822 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003823 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003824 if (op->IsShl()) {
3825 __ shll(first_reg, second_reg);
3826 } else if (op->IsShr()) {
3827 __ sarl(first_reg, second_reg);
3828 } else {
3829 __ shrl(first_reg, second_reg);
3830 }
3831 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003832 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003833 if (op->IsShl()) {
3834 __ shll(first_reg, imm);
3835 } else if (op->IsShr()) {
3836 __ sarl(first_reg, imm);
3837 } else {
3838 __ shrl(first_reg, imm);
3839 }
3840 }
3841 break;
3842 }
3843 case Primitive::kPrimLong: {
3844 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003845 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003846 if (op->IsShl()) {
3847 __ shlq(first_reg, second_reg);
3848 } else if (op->IsShr()) {
3849 __ sarq(first_reg, second_reg);
3850 } else {
3851 __ shrq(first_reg, second_reg);
3852 }
3853 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003854 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855 if (op->IsShl()) {
3856 __ shlq(first_reg, imm);
3857 } else if (op->IsShr()) {
3858 __ sarq(first_reg, imm);
3859 } else {
3860 __ shrq(first_reg, imm);
3861 }
3862 }
3863 break;
3864 }
3865 default:
3866 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003867 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868 }
3869}
3870
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003871void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3872 LocationSummary* locations =
3873 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3874
3875 switch (ror->GetResultType()) {
3876 case Primitive::kPrimInt:
3877 case Primitive::kPrimLong: {
3878 locations->SetInAt(0, Location::RequiresRegister());
3879 // The shift count needs to be in CL (unless it is a constant).
3880 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3881 locations->SetOut(Location::SameAsFirstInput());
3882 break;
3883 }
3884 default:
3885 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3886 UNREACHABLE();
3887 }
3888}
3889
3890void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3891 LocationSummary* locations = ror->GetLocations();
3892 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3893 Location second = locations->InAt(1);
3894
3895 switch (ror->GetResultType()) {
3896 case Primitive::kPrimInt:
3897 if (second.IsRegister()) {
3898 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3899 __ rorl(first_reg, second_reg);
3900 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003901 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003902 __ rorl(first_reg, imm);
3903 }
3904 break;
3905 case Primitive::kPrimLong:
3906 if (second.IsRegister()) {
3907 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3908 __ rorq(first_reg, second_reg);
3909 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003910 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003911 __ rorq(first_reg, imm);
3912 }
3913 break;
3914 default:
3915 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3916 UNREACHABLE();
3917 }
3918}
3919
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3921 HandleShift(shl);
3922}
3923
3924void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3925 HandleShift(shl);
3926}
3927
3928void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3929 HandleShift(shr);
3930}
3931
3932void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3933 HandleShift(shr);
3934}
3935
3936void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3937 HandleShift(ushr);
3938}
3939
3940void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3941 HandleShift(ushr);
3942}
3943
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003944void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003945 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003946 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003947 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003948 if (instruction->IsStringAlloc()) {
3949 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3950 } else {
3951 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3952 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3953 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003954 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003955}
3956
3957void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003958 // Note: if heap poisoning is enabled, the entry point takes cares
3959 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003960 if (instruction->IsStringAlloc()) {
3961 // String is allocated through StringFactory. Call NewEmptyString entry point.
3962 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003963 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003964 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3965 __ call(Address(temp, code_offset.SizeValue()));
3966 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3967 } else {
3968 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3969 instruction,
3970 instruction->GetDexPc(),
3971 nullptr);
3972 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3973 DCHECK(!codegen_->IsLeafMethod());
3974 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003975}
3976
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003977void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3978 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003979 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003980 InvokeRuntimeCallingConvention calling_convention;
3981 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003982 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003983 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003984 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003985}
3986
3987void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3988 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003989 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3990 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003991 // Note: if heap poisoning is enabled, the entry point takes cares
3992 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003993 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3994 instruction,
3995 instruction->GetDexPc(),
3996 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003997 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003998
3999 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004000}
4001
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004002void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004003 LocationSummary* locations =
4004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004005 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4006 if (location.IsStackSlot()) {
4007 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4008 } else if (location.IsDoubleStackSlot()) {
4009 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4010 }
4011 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004012}
4013
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004014void InstructionCodeGeneratorX86_64::VisitParameterValue(
4015 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004016 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004017}
4018
4019void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4020 LocationSummary* locations =
4021 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4022 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4023}
4024
4025void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4026 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4027 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004028}
4029
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004030void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4031 LocationSummary* locations =
4032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4033 locations->SetInAt(0, Location::RequiresRegister());
4034 locations->SetOut(Location::RequiresRegister());
4035}
4036
4037void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4038 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004039 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004040 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004041 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004042 __ movq(locations->Out().AsRegister<CpuRegister>(),
4043 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004044 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004045 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004046 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004047 __ movq(locations->Out().AsRegister<CpuRegister>(),
4048 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4049 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004050 __ movq(locations->Out().AsRegister<CpuRegister>(),
4051 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004052 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004053}
4054
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004055void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004056 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004057 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004058 locations->SetInAt(0, Location::RequiresRegister());
4059 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004060}
4061
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004062void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4063 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004064 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4065 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004066 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004067 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004068 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004069 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004070 break;
4071
4072 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004073 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004074 break;
4075
4076 default:
4077 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4078 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004079}
4080
David Brazdil66d126e2015-04-03 16:02:44 +01004081void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4082 LocationSummary* locations =
4083 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4084 locations->SetInAt(0, Location::RequiresRegister());
4085 locations->SetOut(Location::SameAsFirstInput());
4086}
4087
4088void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004089 LocationSummary* locations = bool_not->GetLocations();
4090 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4091 locations->Out().AsRegister<CpuRegister>().AsRegister());
4092 Location out = locations->Out();
4093 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4094}
4095
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004096void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004097 LocationSummary* locations =
4098 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004099 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004100 locations->SetInAt(i, Location::Any());
4101 }
4102 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004103}
4104
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004105void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004106 LOG(FATAL) << "Unimplemented";
4107}
4108
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004109void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004110 /*
4111 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004112 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004113 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4114 */
4115 switch (kind) {
4116 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004117 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004118 break;
4119 }
4120 case MemBarrierKind::kAnyStore:
4121 case MemBarrierKind::kLoadAny:
4122 case MemBarrierKind::kStoreStore: {
4123 // nop
4124 break;
4125 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004126 case MemBarrierKind::kNTStoreStore:
4127 // Non-Temporal Store/Store needs an explicit fence.
4128 MemoryFence(/* non-temporal */ true);
4129 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004130 }
4131}
4132
4133void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4134 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4135
Roland Levillain0d5a2812015-11-13 10:07:31 +00004136 bool object_field_get_with_read_barrier =
4137 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004138 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004139 new (GetGraph()->GetArena()) LocationSummary(instruction,
4140 object_field_get_with_read_barrier ?
4141 LocationSummary::kCallOnSlowPath :
4142 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004143 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004144 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4145 locations->SetOut(Location::RequiresFpuRegister());
4146 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004147 // The output overlaps for an object field get when read barriers
4148 // are enabled: we do not want the move to overwrite the object's
4149 // location, as we need it to emit the read barrier.
4150 locations->SetOut(
4151 Location::RequiresRegister(),
4152 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004153 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004154 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4155 // We need a temporary register for the read barrier marking slow
4156 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4157 locations->AddTemp(Location::RequiresRegister());
4158 }
Calin Juravle52c48962014-12-16 17:02:57 +00004159}
4160
4161void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4162 const FieldInfo& field_info) {
4163 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4164
4165 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004166 Location base_loc = locations->InAt(0);
4167 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004168 Location out = locations->Out();
4169 bool is_volatile = field_info.IsVolatile();
4170 Primitive::Type field_type = field_info.GetFieldType();
4171 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4172
4173 switch (field_type) {
4174 case Primitive::kPrimBoolean: {
4175 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4176 break;
4177 }
4178
4179 case Primitive::kPrimByte: {
4180 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4181 break;
4182 }
4183
4184 case Primitive::kPrimShort: {
4185 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4186 break;
4187 }
4188
4189 case Primitive::kPrimChar: {
4190 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4191 break;
4192 }
4193
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004194 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004195 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4196 break;
4197 }
4198
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004199 case Primitive::kPrimNot: {
4200 // /* HeapReference<Object> */ out = *(base + offset)
4201 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4202 Location temp_loc = locations->GetTemp(0);
4203 // Note that a potential implicit null check is handled in this
4204 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4205 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4206 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4207 if (is_volatile) {
4208 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4209 }
4210 } else {
4211 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4212 codegen_->MaybeRecordImplicitNullCheck(instruction);
4213 if (is_volatile) {
4214 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4215 }
4216 // If read barriers are enabled, emit read barriers other than
4217 // Baker's using a slow path (and also unpoison the loaded
4218 // reference, if heap poisoning is enabled).
4219 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4220 }
4221 break;
4222 }
4223
Calin Juravle52c48962014-12-16 17:02:57 +00004224 case Primitive::kPrimLong: {
4225 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4226 break;
4227 }
4228
4229 case Primitive::kPrimFloat: {
4230 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4231 break;
4232 }
4233
4234 case Primitive::kPrimDouble: {
4235 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4236 break;
4237 }
4238
4239 case Primitive::kPrimVoid:
4240 LOG(FATAL) << "Unreachable type " << field_type;
4241 UNREACHABLE();
4242 }
4243
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004244 if (field_type == Primitive::kPrimNot) {
4245 // Potential implicit null checks, in the case of reference
4246 // fields, are handled in the previous switch statement.
4247 } else {
4248 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004249 }
Roland Levillain4d027112015-07-01 15:41:14 +01004250
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004251 if (is_volatile) {
4252 if (field_type == Primitive::kPrimNot) {
4253 // Memory barriers, in the case of references, are also handled
4254 // in the previous switch statement.
4255 } else {
4256 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4257 }
Roland Levillain4d027112015-07-01 15:41:14 +01004258 }
Calin Juravle52c48962014-12-16 17:02:57 +00004259}
4260
4261void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4262 const FieldInfo& field_info) {
4263 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4264
4265 LocationSummary* locations =
4266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004267 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004268 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004269 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004270 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004271
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004272 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004273 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004274 if (is_volatile) {
4275 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4276 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4277 } else {
4278 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4279 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004280 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004281 if (is_volatile) {
4282 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4283 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4284 } else {
4285 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4286 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004287 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004288 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004289 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004290 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004291 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004292 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4293 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004294 locations->AddTemp(Location::RequiresRegister());
4295 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004296}
4297
Calin Juravle52c48962014-12-16 17:02:57 +00004298void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004299 const FieldInfo& field_info,
4300 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4302
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004304 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4305 Location value = locations->InAt(1);
4306 bool is_volatile = field_info.IsVolatile();
4307 Primitive::Type field_type = field_info.GetFieldType();
4308 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4309
4310 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004311 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004312 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313
Mark Mendellea5af682015-10-22 17:35:49 -04004314 bool maybe_record_implicit_null_check_done = false;
4315
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316 switch (field_type) {
4317 case Primitive::kPrimBoolean:
4318 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004319 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004320 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004321 __ movb(Address(base, offset), Immediate(v));
4322 } else {
4323 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4324 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004325 break;
4326 }
4327
4328 case Primitive::kPrimShort:
4329 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004330 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004331 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004332 __ movw(Address(base, offset), Immediate(v));
4333 } else {
4334 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4335 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004336 break;
4337 }
4338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004339 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004340 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004341 if (value.IsConstant()) {
4342 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004343 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4344 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4345 // Note: if heap poisoning is enabled, no need to poison
4346 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004347 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004348 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004349 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4350 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4351 __ movl(temp, value.AsRegister<CpuRegister>());
4352 __ PoisonHeapReference(temp);
4353 __ movl(Address(base, offset), temp);
4354 } else {
4355 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4356 }
Mark Mendell40741f32015-04-20 22:10:34 -04004357 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358 break;
4359 }
4360
4361 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004362 if (value.IsConstant()) {
4363 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004364 codegen_->MoveInt64ToAddress(Address(base, offset),
4365 Address(base, offset + sizeof(int32_t)),
4366 v,
4367 instruction);
4368 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004369 } else {
4370 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4371 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372 break;
4373 }
4374
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004375 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004376 if (value.IsConstant()) {
4377 int32_t v =
4378 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4379 __ movl(Address(base, offset), Immediate(v));
4380 } else {
4381 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4382 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004383 break;
4384 }
4385
4386 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004387 if (value.IsConstant()) {
4388 int64_t v =
4389 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4390 codegen_->MoveInt64ToAddress(Address(base, offset),
4391 Address(base, offset + sizeof(int32_t)),
4392 v,
4393 instruction);
4394 maybe_record_implicit_null_check_done = true;
4395 } else {
4396 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4397 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004398 break;
4399 }
4400
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401 case Primitive::kPrimVoid:
4402 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004403 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404 }
Calin Juravle52c48962014-12-16 17:02:57 +00004405
Mark Mendellea5af682015-10-22 17:35:49 -04004406 if (!maybe_record_implicit_null_check_done) {
4407 codegen_->MaybeRecordImplicitNullCheck(instruction);
4408 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004409
4410 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4411 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4412 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004413 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004414 }
4415
Calin Juravle52c48962014-12-16 17:02:57 +00004416 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004417 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004418 }
4419}
4420
4421void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4422 HandleFieldSet(instruction, instruction->GetFieldInfo());
4423}
4424
4425void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004426 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004427}
4428
4429void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004430 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004431}
4432
4433void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004434 HandleFieldGet(instruction, instruction->GetFieldInfo());
4435}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436
Calin Juravle52c48962014-12-16 17:02:57 +00004437void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4438 HandleFieldGet(instruction);
4439}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440
Calin Juravle52c48962014-12-16 17:02:57 +00004441void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4442 HandleFieldGet(instruction, instruction->GetFieldInfo());
4443}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004444
Calin Juravle52c48962014-12-16 17:02:57 +00004445void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4446 HandleFieldSet(instruction, instruction->GetFieldInfo());
4447}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448
Calin Juravle52c48962014-12-16 17:02:57 +00004449void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004450 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004451}
4452
Calin Juravlee460d1d2015-09-29 04:52:17 +01004453void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4454 HUnresolvedInstanceFieldGet* instruction) {
4455 FieldAccessCallingConventionX86_64 calling_convention;
4456 codegen_->CreateUnresolvedFieldLocationSummary(
4457 instruction, instruction->GetFieldType(), calling_convention);
4458}
4459
4460void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4461 HUnresolvedInstanceFieldGet* instruction) {
4462 FieldAccessCallingConventionX86_64 calling_convention;
4463 codegen_->GenerateUnresolvedFieldAccess(instruction,
4464 instruction->GetFieldType(),
4465 instruction->GetFieldIndex(),
4466 instruction->GetDexPc(),
4467 calling_convention);
4468}
4469
4470void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4471 HUnresolvedInstanceFieldSet* instruction) {
4472 FieldAccessCallingConventionX86_64 calling_convention;
4473 codegen_->CreateUnresolvedFieldLocationSummary(
4474 instruction, instruction->GetFieldType(), calling_convention);
4475}
4476
4477void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4478 HUnresolvedInstanceFieldSet* instruction) {
4479 FieldAccessCallingConventionX86_64 calling_convention;
4480 codegen_->GenerateUnresolvedFieldAccess(instruction,
4481 instruction->GetFieldType(),
4482 instruction->GetFieldIndex(),
4483 instruction->GetDexPc(),
4484 calling_convention);
4485}
4486
4487void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4488 HUnresolvedStaticFieldGet* instruction) {
4489 FieldAccessCallingConventionX86_64 calling_convention;
4490 codegen_->CreateUnresolvedFieldLocationSummary(
4491 instruction, instruction->GetFieldType(), calling_convention);
4492}
4493
4494void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4495 HUnresolvedStaticFieldGet* instruction) {
4496 FieldAccessCallingConventionX86_64 calling_convention;
4497 codegen_->GenerateUnresolvedFieldAccess(instruction,
4498 instruction->GetFieldType(),
4499 instruction->GetFieldIndex(),
4500 instruction->GetDexPc(),
4501 calling_convention);
4502}
4503
4504void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4505 HUnresolvedStaticFieldSet* instruction) {
4506 FieldAccessCallingConventionX86_64 calling_convention;
4507 codegen_->CreateUnresolvedFieldLocationSummary(
4508 instruction, instruction->GetFieldType(), calling_convention);
4509}
4510
4511void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4512 HUnresolvedStaticFieldSet* instruction) {
4513 FieldAccessCallingConventionX86_64 calling_convention;
4514 codegen_->GenerateUnresolvedFieldAccess(instruction,
4515 instruction->GetFieldType(),
4516 instruction->GetFieldIndex(),
4517 instruction->GetDexPc(),
4518 calling_convention);
4519}
4520
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004522 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4523 ? LocationSummary::kCallOnSlowPath
4524 : LocationSummary::kNoCall;
4525 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4526 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004527 ? Location::RequiresRegister()
4528 : Location::Any();
4529 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004530 if (instruction->HasUses()) {
4531 locations->SetOut(Location::SameAsFirstInput());
4532 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533}
4534
Calin Juravle2ae48182016-03-16 14:05:09 +00004535void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4536 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004537 return;
4538 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004539 LocationSummary* locations = instruction->GetLocations();
4540 Location obj = locations->InAt(0);
4541
4542 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004543 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004544}
4545
Calin Juravle2ae48182016-03-16 14:05:09 +00004546void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004547 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004548 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549
4550 LocationSummary* locations = instruction->GetLocations();
4551 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552
4553 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004554 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004555 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004557 } else {
4558 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004559 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004560 __ jmp(slow_path->GetEntryLabel());
4561 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004562 }
4563 __ j(kEqual, slow_path->GetEntryLabel());
4564}
4565
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004566void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004567 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004568}
4569
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004571 bool object_array_get_with_read_barrier =
4572 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004573 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004574 new (GetGraph()->GetArena()) LocationSummary(instruction,
4575 object_array_get_with_read_barrier ?
4576 LocationSummary::kCallOnSlowPath :
4577 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004578 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004579 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004580 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4581 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4582 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004583 // The output overlaps for an object array get when read barriers
4584 // are enabled: we do not want the move to overwrite the array's
4585 // location, as we need it to emit the read barrier.
4586 locations->SetOut(
4587 Location::RequiresRegister(),
4588 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004589 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004590 // We need a temporary register for the read barrier marking slow
4591 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4592 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4593 locations->AddTemp(Location::RequiresRegister());
4594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595}
4596
4597void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4598 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004599 Location obj_loc = locations->InAt(0);
4600 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004602 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004603 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004605 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004606 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004608 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 if (index.IsConstant()) {
4610 __ movzxb(out, Address(obj,
4611 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4612 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004613 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004614 }
4615 break;
4616 }
4617
4618 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004619 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004620 if (index.IsConstant()) {
4621 __ movsxb(out, Address(obj,
4622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4623 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004624 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 }
4626 break;
4627 }
4628
4629 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004630 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004631 if (index.IsConstant()) {
4632 __ movsxw(out, Address(obj,
4633 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4634 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004635 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004636 }
4637 break;
4638 }
4639
4640 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004641 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004642 if (index.IsConstant()) {
4643 __ movzxw(out, Address(obj,
4644 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4645 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004646 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004647 }
4648 break;
4649 }
4650
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004651 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004652 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004653 if (index.IsConstant()) {
4654 __ movl(out, Address(obj,
4655 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4656 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 }
4659 break;
4660 }
4661
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004662 case Primitive::kPrimNot: {
4663 static_assert(
4664 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4665 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004666 // /* HeapReference<Object> */ out =
4667 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4668 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4669 Location temp = locations->GetTemp(0);
4670 // Note that a potential implicit null check is handled in this
4671 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4672 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4673 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4674 } 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();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004750 bool object_array_set_with_read_barrier =
4751 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004752
Nicolas Geoffray39468442014-09-02 15:17:15 +01004753 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004754 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004755 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004756 LocationSummary::kCallOnSlowPath :
4757 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004758
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004760 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4761 if (Primitive::IsFloatingPointType(value_type)) {
4762 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004763 } else {
4764 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4765 }
4766
4767 if (needs_write_barrier) {
4768 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004769
4770 // This first temporary register is possibly used for heap
4771 // reference poisoning and/or read barrier emission too.
4772 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004775}
4776
4777void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4778 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004779 Location array_loc = locations->InAt(0);
4780 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004781 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004782 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004783 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004784 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004785 bool needs_write_barrier =
4786 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004787 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4788 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4789 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790
4791 switch (value_type) {
4792 case Primitive::kPrimBoolean:
4793 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4795 Address address = index.IsConstant()
4796 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4797 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4798 if (value.IsRegister()) {
4799 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004802 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004804 break;
4805 }
4806
4807 case Primitive::kPrimShort:
4808 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004809 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4810 Address address = index.IsConstant()
4811 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4812 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4813 if (value.IsRegister()) {
4814 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004816 DCHECK(value.IsConstant()) << value;
4817 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004820 break;
4821 }
4822
4823 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004824 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4825 Address address = index.IsConstant()
4826 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4827 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004828
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004829 if (!value.IsRegister()) {
4830 // Just setting null.
4831 DCHECK(instruction->InputAt(2)->IsNullConstant());
4832 DCHECK(value.IsConstant()) << value;
4833 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004836 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004838 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004839
4840 DCHECK(needs_write_barrier);
4841 CpuRegister register_value = value.AsRegister<CpuRegister>();
4842 NearLabel done, not_null, do_put;
4843 SlowPathCode* slow_path = nullptr;
4844 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004845 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4847 codegen_->AddSlowPath(slow_path);
4848 if (instruction->GetValueCanBeNull()) {
4849 __ testl(register_value, register_value);
4850 __ j(kNotEqual, &not_null);
4851 __ movl(address, Immediate(0));
4852 codegen_->MaybeRecordImplicitNullCheck(instruction);
4853 __ jmp(&done);
4854 __ Bind(&not_null);
4855 }
4856
Roland Levillain0d5a2812015-11-13 10:07:31 +00004857 if (kEmitCompilerReadBarrier) {
4858 // When read barriers are enabled, the type checking
4859 // instrumentation requires two read barriers:
4860 //
4861 // __ movl(temp2, temp);
4862 // // /* HeapReference<Class> */ temp = temp->component_type_
4863 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004864 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004865 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4866 //
4867 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4868 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004869 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004870 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4871 //
4872 // __ cmpl(temp, temp2);
4873 //
4874 // However, the second read barrier may trash `temp`, as it
4875 // is a temporary register, and as such would not be saved
4876 // along with live registers before calling the runtime (nor
4877 // restored afterwards). So in this case, we bail out and
4878 // delegate the work to the array set slow path.
4879 //
4880 // TODO: Extend the register allocator to support a new
4881 // "(locally) live temp" location so as to avoid always
4882 // going into the slow path when read barriers are enabled.
4883 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004884 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004885 // /* HeapReference<Class> */ temp = array->klass_
4886 __ movl(temp, Address(array, class_offset));
4887 codegen_->MaybeRecordImplicitNullCheck(instruction);
4888 __ MaybeUnpoisonHeapReference(temp);
4889
4890 // /* HeapReference<Class> */ temp = temp->component_type_
4891 __ movl(temp, Address(temp, component_offset));
4892 // If heap poisoning is enabled, no need to unpoison `temp`
4893 // nor the object reference in `register_value->klass`, as
4894 // we are comparing two poisoned references.
4895 __ cmpl(temp, Address(register_value, class_offset));
4896
4897 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4898 __ j(kEqual, &do_put);
4899 // If heap poisoning is enabled, the `temp` reference has
4900 // not been unpoisoned yet; unpoison it now.
4901 __ MaybeUnpoisonHeapReference(temp);
4902
4903 // /* HeapReference<Class> */ temp = temp->super_class_
4904 __ movl(temp, Address(temp, super_offset));
4905 // If heap poisoning is enabled, no need to unpoison
4906 // `temp`, as we are comparing against null below.
4907 __ testl(temp, temp);
4908 __ j(kNotEqual, slow_path->GetEntryLabel());
4909 __ Bind(&do_put);
4910 } else {
4911 __ j(kNotEqual, slow_path->GetEntryLabel());
4912 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004913 }
4914 }
4915
4916 if (kPoisonHeapReferences) {
4917 __ movl(temp, register_value);
4918 __ PoisonHeapReference(temp);
4919 __ movl(address, temp);
4920 } else {
4921 __ movl(address, register_value);
4922 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004923 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924 codegen_->MaybeRecordImplicitNullCheck(instruction);
4925 }
4926
4927 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4928 codegen_->MarkGCCard(
4929 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4930 __ Bind(&done);
4931
4932 if (slow_path != nullptr) {
4933 __ Bind(slow_path->GetExitLabel());
4934 }
4935
4936 break;
4937 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004938
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004939 case Primitive::kPrimInt: {
4940 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4941 Address address = index.IsConstant()
4942 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4943 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4944 if (value.IsRegister()) {
4945 __ movl(address, value.AsRegister<CpuRegister>());
4946 } else {
4947 DCHECK(value.IsConstant()) << value;
4948 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4949 __ movl(address, Immediate(v));
4950 }
4951 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004952 break;
4953 }
4954
4955 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004956 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4957 Address address = index.IsConstant()
4958 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4959 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4960 if (value.IsRegister()) {
4961 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004962 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004963 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004964 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004965 Address address_high = index.IsConstant()
4966 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4967 offset + sizeof(int32_t))
4968 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4969 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004970 }
4971 break;
4972 }
4973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004974 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004975 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4976 Address address = index.IsConstant()
4977 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4978 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004979 if (value.IsFpuRegister()) {
4980 __ movss(address, value.AsFpuRegister<XmmRegister>());
4981 } else {
4982 DCHECK(value.IsConstant());
4983 int32_t v =
4984 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4985 __ movl(address, Immediate(v));
4986 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004987 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004988 break;
4989 }
4990
4991 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004992 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4993 Address address = index.IsConstant()
4994 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4995 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004996 if (value.IsFpuRegister()) {
4997 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4998 codegen_->MaybeRecordImplicitNullCheck(instruction);
4999 } else {
5000 int64_t v =
5001 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5002 Address address_high = index.IsConstant()
5003 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5004 offset + sizeof(int32_t))
5005 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5006 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5007 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005008 break;
5009 }
5010
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 case Primitive::kPrimVoid:
5012 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005013 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005014 }
5015}
5016
5017void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005018 LocationSummary* locations =
5019 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005020 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005021 if (!instruction->IsEmittedAtUseSite()) {
5022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5023 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005024}
5025
5026void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005027 if (instruction->IsEmittedAtUseSite()) {
5028 return;
5029 }
5030
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005032 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005033 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5034 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005036 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037}
5038
5039void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005040 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5041 ? LocationSummary::kCallOnSlowPath
5042 : LocationSummary::kNoCall;
5043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005044 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005045 HInstruction* length = instruction->InputAt(1);
5046 if (!length->IsEmittedAtUseSite()) {
5047 locations->SetInAt(1, Location::RegisterOrConstant(length));
5048 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005049 if (instruction->HasUses()) {
5050 locations->SetOut(Location::SameAsFirstInput());
5051 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005052}
5053
5054void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5055 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005056 Location index_loc = locations->InAt(0);
5057 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005058 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005059
Mark Mendell99dbd682015-04-22 16:18:52 -04005060 if (length_loc.IsConstant()) {
5061 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5062 if (index_loc.IsConstant()) {
5063 // BCE will remove the bounds check if we are guarenteed to pass.
5064 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5065 if (index < 0 || index >= length) {
5066 codegen_->AddSlowPath(slow_path);
5067 __ jmp(slow_path->GetEntryLabel());
5068 } else {
5069 // Some optimization after BCE may have generated this, and we should not
5070 // generate a bounds check if it is a valid range.
5071 }
5072 return;
5073 }
5074
5075 // We have to reverse the jump condition because the length is the constant.
5076 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5077 __ cmpl(index_reg, Immediate(length));
5078 codegen_->AddSlowPath(slow_path);
5079 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005080 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005081 HInstruction* array_length = instruction->InputAt(1);
5082 if (array_length->IsEmittedAtUseSite()) {
5083 // Address the length field in the array.
5084 DCHECK(array_length->IsArrayLength());
5085 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5086 Location array_loc = array_length->GetLocations()->InAt(0);
5087 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
5088 if (index_loc.IsConstant()) {
5089 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5090 __ cmpl(array_len, Immediate(value));
5091 } else {
5092 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5093 }
5094 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005095 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005096 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5097 if (index_loc.IsConstant()) {
5098 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5099 __ cmpl(length, Immediate(value));
5100 } else {
5101 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5102 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005103 }
5104 codegen_->AddSlowPath(slow_path);
5105 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005106 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107}
5108
5109void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5110 CpuRegister card,
5111 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005112 CpuRegister value,
5113 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005114 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005115 if (value_can_be_null) {
5116 __ testl(value, value);
5117 __ j(kEqual, &is_null);
5118 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005119 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005120 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121 __ movq(temp, object);
5122 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005123 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005124 if (value_can_be_null) {
5125 __ Bind(&is_null);
5126 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127}
5128
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005129void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005130 LOG(FATAL) << "Unimplemented";
5131}
5132
5133void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005134 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5135}
5136
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005137void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5139}
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);
5448 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5449 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5450 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5451 locations->SetInAt(0, Location::RequiresRegister());
5452 }
5453 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005454}
5455
5456void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005457 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005458 if (cls->NeedsAccessCheck()) {
5459 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5460 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5461 cls,
5462 cls->GetDexPc(),
5463 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005464 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005465 return;
5466 }
5467
Roland Levillain0d5a2812015-11-13 10:07:31 +00005468 Location out_loc = locations->Out();
5469 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005470
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005471 bool generate_null_check = false;
5472 switch (cls->GetLoadKind()) {
5473 case HLoadClass::LoadKind::kReferrersClass: {
5474 DCHECK(!cls->CanCallRuntime());
5475 DCHECK(!cls->MustGenerateClinitCheck());
5476 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5477 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5478 GenerateGcRootFieldLoad(
5479 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5480 break;
5481 }
5482 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5483 DCHECK(!kEmitCompilerReadBarrier);
5484 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5485 codegen_->RecordTypePatch(cls);
5486 break;
5487 case HLoadClass::LoadKind::kBootImageAddress: {
5488 DCHECK(!kEmitCompilerReadBarrier);
5489 DCHECK_NE(cls->GetAddress(), 0u);
5490 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5491 __ movl(out, Immediate(address)); // Zero-extended.
5492 codegen_->RecordSimplePatch();
5493 break;
5494 }
5495 case HLoadClass::LoadKind::kDexCacheAddress: {
5496 DCHECK_NE(cls->GetAddress(), 0u);
5497 // /* GcRoot<mirror::Class> */ out = *address
5498 if (IsUint<32>(cls->GetAddress())) {
5499 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5500 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005501 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005502 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5503 __ movq(out, Immediate(cls->GetAddress()));
5504 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005505 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005506 generate_null_check = !cls->IsInDexCache();
5507 break;
5508 }
5509 case HLoadClass::LoadKind::kDexCachePcRelative: {
5510 uint32_t offset = cls->GetDexCacheElementOffset();
5511 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5512 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5513 /* no_rip */ false);
5514 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5515 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5516 generate_null_check = !cls->IsInDexCache();
5517 break;
5518 }
5519 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5520 // /* GcRoot<mirror::Class>[] */ out =
5521 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5522 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5523 __ movq(out,
5524 Address(current_method,
5525 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5526 // /* GcRoot<mirror::Class> */ out = out[type_index]
5527 GenerateGcRootFieldLoad(
5528 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5529 generate_null_check = !cls->IsInDexCache();
5530 break;
5531 }
5532 default:
5533 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5534 UNREACHABLE();
5535 }
5536
5537 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5538 DCHECK(cls->CanCallRuntime());
5539 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5540 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5541 codegen_->AddSlowPath(slow_path);
5542 if (generate_null_check) {
5543 __ testl(out, out);
5544 __ j(kEqual, slow_path->GetEntryLabel());
5545 }
5546 if (cls->MustGenerateClinitCheck()) {
5547 GenerateClassInitializationCheck(slow_path, out);
5548 } else {
5549 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005550 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005551 }
5552}
5553
5554void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5555 LocationSummary* locations =
5556 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5557 locations->SetInAt(0, Location::RequiresRegister());
5558 if (check->HasUses()) {
5559 locations->SetOut(Location::SameAsFirstInput());
5560 }
5561}
5562
5563void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005564 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005565 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005566 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005567 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005568 GenerateClassInitializationCheck(slow_path,
5569 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570}
5571
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005572HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5573 HLoadString::LoadKind desired_string_load_kind) {
5574 if (kEmitCompilerReadBarrier) {
5575 switch (desired_string_load_kind) {
5576 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5577 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5578 case HLoadString::LoadKind::kBootImageAddress:
5579 // TODO: Implement for read barrier.
5580 return HLoadString::LoadKind::kDexCacheViaMethod;
5581 default:
5582 break;
5583 }
5584 }
5585 switch (desired_string_load_kind) {
5586 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5587 DCHECK(!GetCompilerOptions().GetCompilePic());
5588 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5589 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5590 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5591 DCHECK(GetCompilerOptions().GetCompilePic());
5592 break;
5593 case HLoadString::LoadKind::kBootImageAddress:
5594 break;
5595 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005596 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005597 break;
5598 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005599 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 break;
5601 case HLoadString::LoadKind::kDexCacheViaMethod:
5602 break;
5603 }
5604 return desired_string_load_kind;
5605}
5606
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005607void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005608 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005609 ? LocationSummary::kCallOnSlowPath
5610 : LocationSummary::kNoCall;
5611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005612 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5613 locations->SetInAt(0, Location::RequiresRegister());
5614 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005615 locations->SetOut(Location::RequiresRegister());
5616}
5617
5618void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005619 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005620 Location out_loc = locations->Out();
5621 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005622
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005623 switch (load->GetLoadKind()) {
5624 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5625 DCHECK(!kEmitCompilerReadBarrier);
5626 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5627 codegen_->RecordStringPatch(load);
5628 return; // No dex cache slow path.
5629 }
5630 case HLoadString::LoadKind::kBootImageAddress: {
5631 DCHECK(!kEmitCompilerReadBarrier);
5632 DCHECK_NE(load->GetAddress(), 0u);
5633 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5634 __ movl(out, Immediate(address)); // Zero-extended.
5635 codegen_->RecordSimplePatch();
5636 return; // No dex cache slow path.
5637 }
5638 case HLoadString::LoadKind::kDexCacheAddress: {
5639 DCHECK_NE(load->GetAddress(), 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005640 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005641 if (IsUint<32>(load->GetAddress())) {
5642 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5643 GenerateGcRootFieldLoad(load, out_loc, address);
5644 } else {
5645 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5646 __ movq(out, Immediate(load->GetAddress()));
5647 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5648 }
5649 break;
5650 }
5651 case HLoadString::LoadKind::kDexCachePcRelative: {
5652 uint32_t offset = load->GetDexCacheElementOffset();
5653 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5654 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5655 /* no_rip */ false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005656 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005657 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5658 break;
5659 }
5660 case HLoadString::LoadKind::kDexCacheViaMethod: {
5661 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5662
5663 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5664 GenerateGcRootFieldLoad(
5665 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5666 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5667 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5668 // /* GcRoot<mirror::String> */ out = out[string_index]
5669 GenerateGcRootFieldLoad(
5670 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5671 break;
5672 }
5673 default:
5674 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5675 UNREACHABLE();
5676 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005677
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005678 if (!load->IsInDexCache()) {
5679 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5680 codegen_->AddSlowPath(slow_path);
5681 __ testl(out, out);
5682 __ j(kEqual, slow_path->GetEntryLabel());
5683 __ Bind(slow_path->GetExitLabel());
5684 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005685}
5686
David Brazdilcb1c0552015-08-04 16:22:25 +01005687static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005688 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005689 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005690}
5691
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005692void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5693 LocationSummary* locations =
5694 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5695 locations->SetOut(Location::RequiresRegister());
5696}
5697
5698void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005699 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5700}
5701
5702void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5703 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5704}
5705
5706void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5707 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005708}
5709
5710void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5711 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005712 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005713 InvokeRuntimeCallingConvention calling_convention;
5714 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5715}
5716
5717void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005718 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5719 instruction,
5720 instruction->GetDexPc(),
5721 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005722 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005723}
5724
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005725static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5726 return kEmitCompilerReadBarrier &&
5727 (kUseBakerReadBarrier ||
5728 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5729 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5730 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5731}
5732
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005733void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005734 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005735 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5736 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005737 case TypeCheckKind::kExactCheck:
5738 case TypeCheckKind::kAbstractClassCheck:
5739 case TypeCheckKind::kClassHierarchyCheck:
5740 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005741 call_kind =
5742 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005743 break;
5744 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005745 case TypeCheckKind::kUnresolvedCheck:
5746 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005747 call_kind = LocationSummary::kCallOnSlowPath;
5748 break;
5749 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005750
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005752 locations->SetInAt(0, Location::RequiresRegister());
5753 locations->SetInAt(1, Location::Any());
5754 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5755 locations->SetOut(Location::RequiresRegister());
5756 // When read barriers are enabled, we need a temporary register for
5757 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005758 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005759 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005760 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005761}
5762
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005763void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005764 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005765 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005766 Location obj_loc = locations->InAt(0);
5767 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005768 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005769 Location out_loc = locations->Out();
5770 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005771 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005772 locations->GetTemp(0) :
5773 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005774 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5776 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5777 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005778 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005779 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005780
5781 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005782 // Avoid null check if we know obj is not null.
5783 if (instruction->MustDoNullCheck()) {
5784 __ testl(obj, obj);
5785 __ j(kEqual, &zero);
5786 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787
Roland Levillain0d5a2812015-11-13 10:07:31 +00005788 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005789 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005791 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005792 case TypeCheckKind::kExactCheck: {
5793 if (cls.IsRegister()) {
5794 __ cmpl(out, cls.AsRegister<CpuRegister>());
5795 } else {
5796 DCHECK(cls.IsStackSlot()) << cls;
5797 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5798 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005799 if (zero.IsLinked()) {
5800 // Classes must be equal for the instanceof to succeed.
5801 __ j(kNotEqual, &zero);
5802 __ movl(out, Immediate(1));
5803 __ jmp(&done);
5804 } else {
5805 __ setcc(kEqual, out);
5806 // setcc only sets the low byte.
5807 __ andl(out, Immediate(1));
5808 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 break;
5810 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005811
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 case TypeCheckKind::kAbstractClassCheck: {
5813 // If the class is abstract, we eagerly fetch the super class of the
5814 // object to avoid doing a comparison we know will fail.
5815 NearLabel loop, success;
5816 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005818 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 __ testl(out, out);
5820 // If `out` is null, we use it for the result, and jump to `done`.
5821 __ j(kEqual, &done);
5822 if (cls.IsRegister()) {
5823 __ cmpl(out, cls.AsRegister<CpuRegister>());
5824 } else {
5825 DCHECK(cls.IsStackSlot()) << cls;
5826 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5827 }
5828 __ j(kNotEqual, &loop);
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::kClassHierarchyCheck: {
5837 // Walk over the class hierarchy to find a match.
5838 NearLabel loop, success;
5839 __ Bind(&loop);
5840 if (cls.IsRegister()) {
5841 __ cmpl(out, cls.AsRegister<CpuRegister>());
5842 } else {
5843 DCHECK(cls.IsStackSlot()) << cls;
5844 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5845 }
5846 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005848 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005849 __ testl(out, out);
5850 __ j(kNotEqual, &loop);
5851 // If `out` is null, we use it for the result, and jump to `done`.
5852 __ jmp(&done);
5853 __ Bind(&success);
5854 __ movl(out, Immediate(1));
5855 if (zero.IsLinked()) {
5856 __ jmp(&done);
5857 }
5858 break;
5859 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005860
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005861 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005862 // Do an exact check.
5863 NearLabel exact_check;
5864 if (cls.IsRegister()) {
5865 __ cmpl(out, cls.AsRegister<CpuRegister>());
5866 } else {
5867 DCHECK(cls.IsStackSlot()) << cls;
5868 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5869 }
5870 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005871 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005873 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874 __ testl(out, out);
5875 // If `out` is null, we use it for the result, and jump to `done`.
5876 __ j(kEqual, &done);
5877 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5878 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005879 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880 __ movl(out, Immediate(1));
5881 __ jmp(&done);
5882 break;
5883 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 case TypeCheckKind::kArrayCheck: {
5886 if (cls.IsRegister()) {
5887 __ cmpl(out, cls.AsRegister<CpuRegister>());
5888 } else {
5889 DCHECK(cls.IsStackSlot()) << cls;
5890 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5891 }
5892 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5894 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005895 codegen_->AddSlowPath(slow_path);
5896 __ j(kNotEqual, slow_path->GetEntryLabel());
5897 __ movl(out, Immediate(1));
5898 if (zero.IsLinked()) {
5899 __ jmp(&done);
5900 }
5901 break;
5902 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903
Calin Juravle98893e12015-10-02 21:05:03 +01005904 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005905 case TypeCheckKind::kInterfaceCheck: {
5906 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005907 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908 // cases.
5909 //
5910 // We cannot directly call the InstanceofNonTrivial runtime
5911 // entry point without resorting to a type checking slow path
5912 // here (i.e. by calling InvokeRuntime directly), as it would
5913 // require to assign fixed registers for the inputs of this
5914 // HInstanceOf instruction (following the runtime calling
5915 // convention), which might be cluttered by the potential first
5916 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005917 //
5918 // TODO: Introduce a new runtime entry point taking the object
5919 // to test (instead of its class) as argument, and let it deal
5920 // with the read barrier issues. This will let us refactor this
5921 // case of the `switch` code as it was previously (with a direct
5922 // call to the runtime not using a type checking slow path).
5923 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005924 DCHECK(locations->OnlyCallsOnSlowPath());
5925 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5926 /* is_fatal */ false);
5927 codegen_->AddSlowPath(slow_path);
5928 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 if (zero.IsLinked()) {
5930 __ jmp(&done);
5931 }
5932 break;
5933 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005934 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005935
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005936 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005937 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 __ xorl(out, out);
5939 }
5940
5941 if (done.IsLinked()) {
5942 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005943 }
5944
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005945 if (slow_path != nullptr) {
5946 __ Bind(slow_path->GetExitLabel());
5947 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005948}
5949
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005950void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005951 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5952 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005953 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5954 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955 case TypeCheckKind::kExactCheck:
5956 case TypeCheckKind::kAbstractClassCheck:
5957 case TypeCheckKind::kClassHierarchyCheck:
5958 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005959 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5960 LocationSummary::kCallOnSlowPath :
5961 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005962 break;
5963 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964 case TypeCheckKind::kUnresolvedCheck:
5965 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005966 call_kind = LocationSummary::kCallOnSlowPath;
5967 break;
5968 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5970 locations->SetInAt(0, Location::RequiresRegister());
5971 locations->SetInAt(1, Location::Any());
5972 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5973 locations->AddTemp(Location::RequiresRegister());
5974 // When read barriers are enabled, we need an additional temporary
5975 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005976 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005978 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005979}
5980
5981void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005982 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005983 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984 Location obj_loc = locations->InAt(0);
5985 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005986 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005987 Location temp_loc = locations->GetTemp(0);
5988 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005989 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005990 locations->GetTemp(1) :
5991 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5993 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5994 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5995 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996
Roland Levillain0d5a2812015-11-13 10:07:31 +00005997 bool is_type_check_slow_path_fatal =
5998 (type_check_kind == TypeCheckKind::kExactCheck ||
5999 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6000 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6001 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6002 !instruction->CanThrowIntoCatchBlock();
6003 SlowPathCode* type_check_slow_path =
6004 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6005 is_type_check_slow_path_fatal);
6006 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006007
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006009 case TypeCheckKind::kExactCheck:
6010 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006011 NearLabel done;
6012 // Avoid null check if we know obj is not null.
6013 if (instruction->MustDoNullCheck()) {
6014 __ testl(obj, obj);
6015 __ j(kEqual, &done);
6016 }
6017
6018 // /* HeapReference<Class> */ temp = obj->klass_
6019 GenerateReferenceLoadTwoRegisters(
6020 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6021
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 if (cls.IsRegister()) {
6023 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6024 } else {
6025 DCHECK(cls.IsStackSlot()) << cls;
6026 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6027 }
6028 // Jump to slow path for throwing the exception or doing a
6029 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006030 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006031 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006032 break;
6033 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006034
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006035 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006036 NearLabel done;
6037 // Avoid null check if we know obj is not null.
6038 if (instruction->MustDoNullCheck()) {
6039 __ testl(obj, obj);
6040 __ j(kEqual, &done);
6041 }
6042
6043 // /* HeapReference<Class> */ temp = obj->klass_
6044 GenerateReferenceLoadTwoRegisters(
6045 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6046
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 // If the class is abstract, we eagerly fetch the super class of the
6048 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006051 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006052 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006053
6054 // If the class reference currently in `temp` is not null, jump
6055 // to the `compare_classes` label to compare it with the checked
6056 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006057 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006058 __ j(kNotEqual, &compare_classes);
6059 // Otherwise, jump to the slow path to throw the exception.
6060 //
6061 // But before, move back the object's class into `temp` before
6062 // going into the slow path, as it has been overwritten in the
6063 // meantime.
6064 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006065 GenerateReferenceLoadTwoRegisters(
6066 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006067 __ jmp(type_check_slow_path->GetEntryLabel());
6068
6069 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 if (cls.IsRegister()) {
6071 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6072 } else {
6073 DCHECK(cls.IsStackSlot()) << cls;
6074 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6075 }
6076 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006077 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006078 break;
6079 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006080
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006082 NearLabel done;
6083 // Avoid null check if we know obj is not null.
6084 if (instruction->MustDoNullCheck()) {
6085 __ testl(obj, obj);
6086 __ j(kEqual, &done);
6087 }
6088
6089 // /* HeapReference<Class> */ temp = obj->klass_
6090 GenerateReferenceLoadTwoRegisters(
6091 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6092
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006093 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006094 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006095 __ Bind(&loop);
6096 if (cls.IsRegister()) {
6097 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6098 } else {
6099 DCHECK(cls.IsStackSlot()) << cls;
6100 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6101 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006102 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006103
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006105 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106
6107 // If the class reference currently in `temp` is not null, jump
6108 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109 __ testl(temp, temp);
6110 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006111 // Otherwise, jump to the slow path to throw the exception.
6112 //
6113 // But before, move back the object's class into `temp` before
6114 // going into the slow path, as it has been overwritten in the
6115 // meantime.
6116 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006117 GenerateReferenceLoadTwoRegisters(
6118 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006119 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006120 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006121 break;
6122 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006124 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006125 // We cannot use a NearLabel here, as its range might be too
6126 // short in some cases when read barriers are enabled. This has
6127 // been observed for instance when the code emitted for this
6128 // case uses high x86-64 registers (R8-R15).
6129 Label done;
6130 // Avoid null check if we know obj is not null.
6131 if (instruction->MustDoNullCheck()) {
6132 __ testl(obj, obj);
6133 __ j(kEqual, &done);
6134 }
6135
6136 // /* HeapReference<Class> */ temp = obj->klass_
6137 GenerateReferenceLoadTwoRegisters(
6138 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6139
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006140 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006142 if (cls.IsRegister()) {
6143 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6144 } else {
6145 DCHECK(cls.IsStackSlot()) << cls;
6146 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6147 }
6148 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149
6150 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006151 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006152 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153
6154 // If the component type is not null (i.e. the object is indeed
6155 // an array), jump to label `check_non_primitive_component_type`
6156 // to further check that this component type is not a primitive
6157 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159 __ j(kNotEqual, &check_non_primitive_component_type);
6160 // Otherwise, jump to the slow path to throw the exception.
6161 //
6162 // But before, move back the object's class into `temp` before
6163 // going into the slow path, as it has been overwritten in the
6164 // meantime.
6165 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006166 GenerateReferenceLoadTwoRegisters(
6167 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 __ jmp(type_check_slow_path->GetEntryLabel());
6169
6170 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006172 __ j(kEqual, &done);
6173 // Same comment as above regarding `temp` and the slow path.
6174 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006175 GenerateReferenceLoadTwoRegisters(
6176 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006178 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 break;
6180 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006181
Calin Juravle98893e12015-10-02 21:05:03 +01006182 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006184 NearLabel done;
6185 // Avoid null check if we know obj is not null.
6186 if (instruction->MustDoNullCheck()) {
6187 __ testl(obj, obj);
6188 __ j(kEqual, &done);
6189 }
6190
6191 // /* HeapReference<Class> */ temp = obj->klass_
6192 GenerateReferenceLoadTwoRegisters(
6193 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6194
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006195 // We always go into the type check slow path for the unresolved
6196 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006197 //
6198 // We cannot directly call the CheckCast runtime entry point
6199 // without resorting to a type checking slow path here (i.e. by
6200 // calling InvokeRuntime directly), as it would require to
6201 // assign fixed registers for the inputs of this HInstanceOf
6202 // instruction (following the runtime calling convention), which
6203 // might be cluttered by the potential first read barrier
6204 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006205 //
6206 // TODO: Introduce a new runtime entry point taking the object
6207 // to test (instead of its class) as argument, and let it deal
6208 // with the read barrier issues. This will let us refactor this
6209 // case of the `switch` code as it was previously (with a direct
6210 // call to the runtime not using a type checking slow path).
6211 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006212 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006213 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006214 break;
6215 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216
Roland Levillain0d5a2812015-11-13 10:07:31 +00006217 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006218}
6219
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006220void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6221 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006223 InvokeRuntimeCallingConvention calling_convention;
6224 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6225}
6226
6227void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006228 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6229 : QUICK_ENTRY_POINT(pUnlockObject),
6230 instruction,
6231 instruction->GetDexPc(),
6232 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006233 if (instruction->IsEnter()) {
6234 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6235 } else {
6236 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6237 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006238}
6239
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006240void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6241void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6242void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6243
6244void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6245 LocationSummary* locations =
6246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6247 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6248 || instruction->GetResultType() == Primitive::kPrimLong);
6249 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006250 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006251 locations->SetOut(Location::SameAsFirstInput());
6252}
6253
6254void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6255 HandleBitwiseOperation(instruction);
6256}
6257
6258void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6259 HandleBitwiseOperation(instruction);
6260}
6261
6262void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6263 HandleBitwiseOperation(instruction);
6264}
6265
6266void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6267 LocationSummary* locations = instruction->GetLocations();
6268 Location first = locations->InAt(0);
6269 Location second = locations->InAt(1);
6270 DCHECK(first.Equals(locations->Out()));
6271
6272 if (instruction->GetResultType() == Primitive::kPrimInt) {
6273 if (second.IsRegister()) {
6274 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006275 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006276 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006277 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006278 } else {
6279 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006280 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006281 }
6282 } else if (second.IsConstant()) {
6283 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6284 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006285 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006286 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006287 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006288 } else {
6289 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006290 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006291 }
6292 } else {
6293 Address address(CpuRegister(RSP), second.GetStackIndex());
6294 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006295 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006296 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006297 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006298 } else {
6299 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006300 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006301 }
6302 }
6303 } else {
6304 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006305 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6306 bool second_is_constant = false;
6307 int64_t value = 0;
6308 if (second.IsConstant()) {
6309 second_is_constant = true;
6310 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006311 }
Mark Mendell40741f32015-04-20 22:10:34 -04006312 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006313
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006314 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006315 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006316 if (is_int32_value) {
6317 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6318 } else {
6319 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6320 }
6321 } else if (second.IsDoubleStackSlot()) {
6322 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006323 } else {
6324 __ andq(first_reg, second.AsRegister<CpuRegister>());
6325 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006326 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006327 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006328 if (is_int32_value) {
6329 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6330 } else {
6331 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6332 }
6333 } else if (second.IsDoubleStackSlot()) {
6334 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006335 } else {
6336 __ orq(first_reg, second.AsRegister<CpuRegister>());
6337 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006338 } else {
6339 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006340 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006341 if (is_int32_value) {
6342 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6343 } else {
6344 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6345 }
6346 } else if (second.IsDoubleStackSlot()) {
6347 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006348 } else {
6349 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6350 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006351 }
6352 }
6353}
6354
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006355void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6356 Location out,
6357 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006358 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006359 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6360 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006361 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006362 if (kUseBakerReadBarrier) {
6363 // Load with fast path based Baker's read barrier.
6364 // /* HeapReference<Object> */ out = *(out + offset)
6365 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006366 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006367 } else {
6368 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006369 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006370 // in the following move operation, as we will need it for the
6371 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006372 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006373 // /* HeapReference<Object> */ out = *(out + offset)
6374 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006375 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006376 }
6377 } else {
6378 // Plain load with no read barrier.
6379 // /* HeapReference<Object> */ out = *(out + offset)
6380 __ movl(out_reg, Address(out_reg, offset));
6381 __ MaybeUnpoisonHeapReference(out_reg);
6382 }
6383}
6384
6385void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6386 Location out,
6387 Location obj,
6388 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006389 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006390 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6391 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6392 if (kEmitCompilerReadBarrier) {
6393 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006394 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006395 // Load with fast path based Baker's read barrier.
6396 // /* HeapReference<Object> */ out = *(obj + offset)
6397 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006398 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006399 } else {
6400 // Load with slow path based read barrier.
6401 // /* HeapReference<Object> */ out = *(obj + offset)
6402 __ movl(out_reg, Address(obj_reg, offset));
6403 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6404 }
6405 } else {
6406 // Plain load with no read barrier.
6407 // /* HeapReference<Object> */ out = *(obj + offset)
6408 __ movl(out_reg, Address(obj_reg, offset));
6409 __ MaybeUnpoisonHeapReference(out_reg);
6410 }
6411}
6412
6413void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6414 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006415 const Address& address,
6416 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006417 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6418 if (kEmitCompilerReadBarrier) {
6419 if (kUseBakerReadBarrier) {
6420 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6421 // Baker's read barrier are used:
6422 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006424 // if (Thread::Current()->GetIsGcMarking()) {
6425 // root = ReadBarrier::Mark(root)
6426 // }
6427
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006428 // /* GcRoot<mirror::Object> */ root = *address
6429 __ movl(root_reg, address);
6430 if (fixup_label != nullptr) {
6431 __ Bind(fixup_label);
6432 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006433 static_assert(
6434 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6435 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6436 "have different sizes.");
6437 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6438 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6439 "have different sizes.");
6440
6441 // Slow path used to mark the GC root `root`.
6442 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006443 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006444 codegen_->AddSlowPath(slow_path);
6445
Andreas Gampe542451c2016-07-26 09:02:02 -07006446 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006447 /* no_rip */ true),
6448 Immediate(0));
6449 __ j(kNotEqual, slow_path->GetEntryLabel());
6450 __ Bind(slow_path->GetExitLabel());
6451 } else {
6452 // GC root loaded through a slow path for read barriers other
6453 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006454 // /* GcRoot<mirror::Object>* */ root = address
6455 __ leaq(root_reg, address);
6456 if (fixup_label != nullptr) {
6457 __ Bind(fixup_label);
6458 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006459 // /* mirror::Object* */ root = root->Read()
6460 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6461 }
6462 } else {
6463 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006464 // /* GcRoot<mirror::Object> */ root = *address
6465 __ movl(root_reg, address);
6466 if (fixup_label != nullptr) {
6467 __ Bind(fixup_label);
6468 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006469 // Note that GC roots are not affected by heap poisoning, thus we
6470 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006471 }
6472}
6473
6474void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6475 Location ref,
6476 CpuRegister obj,
6477 uint32_t offset,
6478 Location temp,
6479 bool needs_null_check) {
6480 DCHECK(kEmitCompilerReadBarrier);
6481 DCHECK(kUseBakerReadBarrier);
6482
6483 // /* HeapReference<Object> */ ref = *(obj + offset)
6484 Address src(obj, offset);
6485 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6486}
6487
6488void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6489 Location ref,
6490 CpuRegister obj,
6491 uint32_t data_offset,
6492 Location index,
6493 Location temp,
6494 bool needs_null_check) {
6495 DCHECK(kEmitCompilerReadBarrier);
6496 DCHECK(kUseBakerReadBarrier);
6497
Roland Levillain3d312422016-06-23 13:53:42 +01006498 static_assert(
6499 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6500 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006501 // /* HeapReference<Object> */ ref =
6502 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6503 Address src = index.IsConstant() ?
6504 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6505 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6506 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6507}
6508
6509void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6510 Location ref,
6511 CpuRegister obj,
6512 const Address& src,
6513 Location temp,
6514 bool needs_null_check) {
6515 DCHECK(kEmitCompilerReadBarrier);
6516 DCHECK(kUseBakerReadBarrier);
6517
6518 // In slow path based read barriers, the read barrier call is
6519 // inserted after the original load. However, in fast path based
6520 // Baker's read barriers, we need to perform the load of
6521 // mirror::Object::monitor_ *before* the original reference load.
6522 // This load-load ordering is required by the read barrier.
6523 // The fast path/slow path (for Baker's algorithm) should look like:
6524 //
6525 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6526 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6527 // HeapReference<Object> ref = *src; // Original reference load.
6528 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6529 // if (is_gray) {
6530 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6531 // }
6532 //
6533 // Note: the original implementation in ReadBarrier::Barrier is
6534 // slightly more complex as:
6535 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006536 // the high-bits of rb_state, which are expected to be all zeroes
6537 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6538 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006539 // - it performs additional checks that we do not do here for
6540 // performance reasons.
6541
6542 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6543 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6544 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6545
6546 // /* int32_t */ monitor = obj->monitor_
6547 __ movl(temp_reg, Address(obj, monitor_offset));
6548 if (needs_null_check) {
6549 MaybeRecordImplicitNullCheck(instruction);
6550 }
6551 // /* LockWord */ lock_word = LockWord(monitor)
6552 static_assert(sizeof(LockWord) == sizeof(int32_t),
6553 "art::LockWord and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006554
6555 // Load fence to prevent load-load reordering.
6556 // Note that this is a no-op, thanks to the x86-64 memory model.
6557 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6558
6559 // The actual reference load.
6560 // /* HeapReference<Object> */ ref = *src
6561 __ movl(ref_reg, src);
6562
6563 // Object* ref = ref_addr->AsMirrorPtr()
6564 __ MaybeUnpoisonHeapReference(ref_reg);
6565
6566 // Slow path used to mark the object `ref` when it is gray.
6567 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006568 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006569 AddSlowPath(slow_path);
6570
6571 // if (rb_state == ReadBarrier::gray_ptr_)
6572 // ref = ReadBarrier::Mark(ref);
Vladimir Marko961ea122016-08-11 14:16:57 +01006573 // Given the numeric representation, it's enough to check the low bit of the
6574 // rb_state. We do that by shifting the bit out of the lock word with SHR.
6575 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6576 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6577 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6578 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift + 1));
6579 __ j(kCarrySet, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006580 __ Bind(slow_path->GetExitLabel());
6581}
6582
6583void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6584 Location out,
6585 Location ref,
6586 Location obj,
6587 uint32_t offset,
6588 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006589 DCHECK(kEmitCompilerReadBarrier);
6590
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006591 // Insert a slow path based read barrier *after* the reference load.
6592 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006593 // If heap poisoning is enabled, the unpoisoning of the loaded
6594 // reference will be carried out by the runtime within the slow
6595 // path.
6596 //
6597 // Note that `ref` currently does not get unpoisoned (when heap
6598 // poisoning is enabled), which is alright as the `ref` argument is
6599 // not used by the artReadBarrierSlow entry point.
6600 //
6601 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6602 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6603 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6604 AddSlowPath(slow_path);
6605
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606 __ jmp(slow_path->GetEntryLabel());
6607 __ Bind(slow_path->GetExitLabel());
6608}
6609
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006610void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6611 Location out,
6612 Location ref,
6613 Location obj,
6614 uint32_t offset,
6615 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006616 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006617 // Baker's read barriers shall be handled by the fast path
6618 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6619 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620 // If heap poisoning is enabled, unpoisoning will be taken care of
6621 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006622 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623 } else if (kPoisonHeapReferences) {
6624 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6625 }
6626}
6627
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006628void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6629 Location out,
6630 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 DCHECK(kEmitCompilerReadBarrier);
6632
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006633 // Insert a slow path based read barrier *after* the GC root load.
6634 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006635 // Note that GC roots are not affected by heap poisoning, so we do
6636 // not need to do anything special for this here.
6637 SlowPathCode* slow_path =
6638 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6639 AddSlowPath(slow_path);
6640
Roland Levillain0d5a2812015-11-13 10:07:31 +00006641 __ jmp(slow_path->GetEntryLabel());
6642 __ Bind(slow_path->GetExitLabel());
6643}
6644
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006645void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006646 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006647 LOG(FATAL) << "Unreachable";
6648}
6649
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006650void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006651 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006652 LOG(FATAL) << "Unreachable";
6653}
6654
Mark Mendellfe57faa2015-09-18 09:26:15 -04006655// Simple implementation of packed switch - generate cascaded compare/jumps.
6656void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6657 LocationSummary* locations =
6658 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6659 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006660 locations->AddTemp(Location::RequiresRegister());
6661 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006662}
6663
6664void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6665 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006666 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006667 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006668 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6669 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6670 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006671 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6672
6673 // Should we generate smaller inline compare/jumps?
6674 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6675 // Figure out the correct compare values and jump conditions.
6676 // Handle the first compare/branch as a special case because it might
6677 // jump to the default case.
6678 DCHECK_GT(num_entries, 2u);
6679 Condition first_condition;
6680 uint32_t index;
6681 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6682 if (lower_bound != 0) {
6683 first_condition = kLess;
6684 __ cmpl(value_reg_in, Immediate(lower_bound));
6685 __ j(first_condition, codegen_->GetLabelOf(default_block));
6686 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6687
6688 index = 1;
6689 } else {
6690 // Handle all the compare/jumps below.
6691 first_condition = kBelow;
6692 index = 0;
6693 }
6694
6695 // Handle the rest of the compare/jumps.
6696 for (; index + 1 < num_entries; index += 2) {
6697 int32_t compare_to_value = lower_bound + index + 1;
6698 __ cmpl(value_reg_in, Immediate(compare_to_value));
6699 // Jump to successors[index] if value < case_value[index].
6700 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6701 // Jump to successors[index + 1] if value == case_value[index + 1].
6702 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6703 }
6704
6705 if (index != num_entries) {
6706 // There are an odd number of entries. Handle the last one.
6707 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006708 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006709 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6710 }
6711
6712 // And the default for any other value.
6713 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6714 __ jmp(codegen_->GetLabelOf(default_block));
6715 }
6716 return;
6717 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006718
6719 // Remove the bias, if needed.
6720 Register value_reg_out = value_reg_in.AsRegister();
6721 if (lower_bound != 0) {
6722 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6723 value_reg_out = temp_reg.AsRegister();
6724 }
6725 CpuRegister value_reg(value_reg_out);
6726
6727 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006728 __ cmpl(value_reg, Immediate(num_entries - 1));
6729 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006730
Mark Mendell9c86b482015-09-18 13:36:07 -04006731 // We are in the range of the table.
6732 // Load the address of the jump table in the constant area.
6733 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006734
Mark Mendell9c86b482015-09-18 13:36:07 -04006735 // Load the (signed) offset from the jump table.
6736 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6737
6738 // Add the offset to the address of the table base.
6739 __ addq(temp_reg, base_reg);
6740
6741 // And jump.
6742 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006743}
6744
Aart Bikc5d47542016-01-27 17:00:35 -08006745void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6746 if (value == 0) {
6747 __ xorl(dest, dest);
6748 } else {
6749 __ movl(dest, Immediate(value));
6750 }
6751}
6752
Mark Mendell92e83bf2015-05-07 11:25:03 -04006753void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6754 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006755 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006756 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006757 } else if (IsUint<32>(value)) {
6758 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006759 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6760 } else {
6761 __ movq(dest, Immediate(value));
6762 }
6763}
6764
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006765void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6766 if (value == 0) {
6767 __ xorps(dest, dest);
6768 } else {
6769 __ movss(dest, LiteralInt32Address(value));
6770 }
6771}
6772
6773void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6774 if (value == 0) {
6775 __ xorpd(dest, dest);
6776 } else {
6777 __ movsd(dest, LiteralInt64Address(value));
6778 }
6779}
6780
6781void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6782 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6783}
6784
6785void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6786 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6787}
6788
Aart Bika19616e2016-02-01 18:57:58 -08006789void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6790 if (value == 0) {
6791 __ testl(dest, dest);
6792 } else {
6793 __ cmpl(dest, Immediate(value));
6794 }
6795}
6796
6797void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6798 if (IsInt<32>(value)) {
6799 if (value == 0) {
6800 __ testq(dest, dest);
6801 } else {
6802 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6803 }
6804 } else {
6805 // Value won't fit in an int.
6806 __ cmpq(dest, LiteralInt64Address(value));
6807 }
6808}
6809
Mark Mendellcfa410b2015-05-25 16:02:44 -04006810void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6811 DCHECK(dest.IsDoubleStackSlot());
6812 if (IsInt<32>(value)) {
6813 // Can move directly as an int32 constant.
6814 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6815 Immediate(static_cast<int32_t>(value)));
6816 } else {
6817 Load64BitValue(CpuRegister(TMP), value);
6818 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6819 }
6820}
6821
Mark Mendell9c86b482015-09-18 13:36:07 -04006822/**
6823 * Class to handle late fixup of offsets into constant area.
6824 */
6825class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6826 public:
6827 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6828 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6829
6830 protected:
6831 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6832
6833 CodeGeneratorX86_64* codegen_;
6834
6835 private:
6836 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6837 // Patch the correct offset for the instruction. We use the address of the
6838 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6839 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6840 int32_t relative_position = constant_offset - pos;
6841
6842 // Patch in the right value.
6843 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6844 }
6845
6846 // Location in constant area that the fixup refers to.
6847 size_t offset_into_constant_area_;
6848};
6849
6850/**
6851 t * Class to handle late fixup of offsets to a jump table that will be created in the
6852 * constant area.
6853 */
6854class JumpTableRIPFixup : public RIPFixup {
6855 public:
6856 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6857 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6858
6859 void CreateJumpTable() {
6860 X86_64Assembler* assembler = codegen_->GetAssembler();
6861
6862 // Ensure that the reference to the jump table has the correct offset.
6863 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6864 SetOffset(offset_in_constant_table);
6865
6866 // Compute the offset from the start of the function to this jump table.
6867 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6868
6869 // Populate the jump table with the correct values for the jump table.
6870 int32_t num_entries = switch_instr_->GetNumEntries();
6871 HBasicBlock* block = switch_instr_->GetBlock();
6872 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6873 // The value that we want is the target offset - the position of the table.
6874 for (int32_t i = 0; i < num_entries; i++) {
6875 HBasicBlock* b = successors[i];
6876 Label* l = codegen_->GetLabelOf(b);
6877 DCHECK(l->IsBound());
6878 int32_t offset_to_block = l->Position() - current_table_offset;
6879 assembler->AppendInt32(offset_to_block);
6880 }
6881 }
6882
6883 private:
6884 const HPackedSwitch* switch_instr_;
6885};
6886
Mark Mendellf55c3e02015-03-26 21:07:46 -04006887void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6888 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006889 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006890 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6891 // 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 -04006892 assembler->Align(4, 0);
6893 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006894
6895 // Populate any jump tables.
6896 for (auto jump_table : fixups_to_jump_tables_) {
6897 jump_table->CreateJumpTable();
6898 }
6899
6900 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006901 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006902 }
6903
6904 // And finish up.
6905 CodeGenerator::Finalize(allocator);
6906}
6907
Mark Mendellf55c3e02015-03-26 21:07:46 -04006908Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6909 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6910 return Address::RIP(fixup);
6911}
6912
6913Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6914 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6915 return Address::RIP(fixup);
6916}
6917
6918Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6919 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6920 return Address::RIP(fixup);
6921}
6922
6923Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6924 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6925 return Address::RIP(fixup);
6926}
6927
Andreas Gampe85b62f22015-09-09 13:15:38 -07006928// TODO: trg as memory.
6929void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6930 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006931 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006932 return;
6933 }
6934
6935 DCHECK_NE(type, Primitive::kPrimVoid);
6936
6937 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6938 if (trg.Equals(return_loc)) {
6939 return;
6940 }
6941
6942 // Let the parallel move resolver take care of all of this.
6943 HParallelMove parallel_move(GetGraph()->GetArena());
6944 parallel_move.AddMove(return_loc, trg, type, nullptr);
6945 GetMoveResolver()->EmitNativeCode(&parallel_move);
6946}
6947
Mark Mendell9c86b482015-09-18 13:36:07 -04006948Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6949 // Create a fixup to be used to create and address the jump table.
6950 JumpTableRIPFixup* table_fixup =
6951 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6952
6953 // We have to populate the jump tables.
6954 fixups_to_jump_tables_.push_back(table_fixup);
6955 return Address::RIP(table_fixup);
6956}
6957
Mark Mendellea5af682015-10-22 17:35:49 -04006958void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6959 const Address& addr_high,
6960 int64_t v,
6961 HInstruction* instruction) {
6962 if (IsInt<32>(v)) {
6963 int32_t v_32 = v;
6964 __ movq(addr_low, Immediate(v_32));
6965 MaybeRecordImplicitNullCheck(instruction);
6966 } else {
6967 // Didn't fit in a register. Do it in pieces.
6968 int32_t low_v = Low32Bits(v);
6969 int32_t high_v = High32Bits(v);
6970 __ movl(addr_low, Immediate(low_v));
6971 MaybeRecordImplicitNullCheck(instruction);
6972 __ movl(addr_high, Immediate(high_v));
6973 }
6974}
6975
Roland Levillain4d027112015-07-01 15:41:14 +01006976#undef __
6977
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006978} // namespace x86_64
6979} // namespace art