blob: bc5ffccb10704bad8309aef033dfd6e3a7406612 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Roland Levillain7cbd27f2016-08-11 23:53:33 +010054// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
55#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Andreas Gampe85b62f22015-09-09 13:15:38 -070058class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000060 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Alexandre Rames2ed20af2015-03-06 13:55:35 +000062 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000063 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000065 if (instruction_->CanThrowIntoCatchBlock()) {
66 // Live registers will be restored in the catch block if caught.
67 SaveLiveRegisters(codegen, instruction_->GetLocations());
68 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000069 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
70 instruction_,
71 instruction_->GetDexPc(),
72 this);
Roland Levillain888d0672015-11-23 18:53:50 +000073 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 }
75
Alexandre Rames8158f282015-08-07 10:26:17 +010076 bool IsFatal() const OVERRIDE { return true; }
77
Alexandre Rames9931f312015-06-19 14:47:01 +010078 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
79
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
82};
83
Andreas Gampe85b62f22015-09-09 13:15:38 -070084class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000085 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000086 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000089 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000091 if (instruction_->CanThrowIntoCatchBlock()) {
92 // Live registers will be restored in the catch block if caught.
93 SaveLiveRegisters(codegen, instruction_->GetLocations());
94 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000095 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
96 instruction_,
97 instruction_->GetDexPc(),
98 this);
Roland Levillain888d0672015-11-23 18:53:50 +000099 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000100 }
101
Alexandre Rames8158f282015-08-07 10:26:17 +0100102 bool IsFatal() const OVERRIDE { return true; }
103
Alexandre Rames9931f312015-06-19 14:47:01 +0100104 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
105
Calin Juravled0d48522014-11-04 16:40:20 +0000106 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
108};
109
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
113 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000114
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000116 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 if (is_div_) {
119 __ negl(cpu_reg_);
120 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400121 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 }
123
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 } else {
125 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negq(cpu_reg_);
128 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400129 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000130 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 }
Calin Juravled0d48522014-11-04 16:40:20 +0000132 __ jmp(GetExitLabel());
133 }
134
Alexandre Rames9931f312015-06-19 14:47:01 +0100135 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
136
Calin Juravled0d48522014-11-04 16:40:20 +0000137 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000139 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000140 const bool is_div_;
141 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000142};
143
Andreas Gampe85b62f22015-09-09 13:15:38 -0700144class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100146 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000150 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 __ Bind(GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000152 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
153 instruction_,
154 instruction_->GetDexPc(),
155 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000156 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 if (successor_ == nullptr) {
158 __ jmp(GetReturnLabel());
159 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000160 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162 }
163
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100164 Label* GetReturnLabel() {
165 DCHECK(successor_ == nullptr);
166 return &return_label_;
167 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100169 HBasicBlock* GetSuccessor() const {
170 return successor_;
171 }
172
Alexandre Rames9931f312015-06-19 14:47:01 +0100173 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
174
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 Label return_label_;
178
179 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
180};
181
Andreas Gampe85b62f22015-09-09 13:15:38 -0700182class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100184 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000185 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000187 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100188 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000189 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000191 if (instruction_->CanThrowIntoCatchBlock()) {
192 // Live registers will be restored in the catch block if caught.
193 SaveLiveRegisters(codegen, instruction_->GetLocations());
194 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400195 // Are we using an array length from memory?
196 HInstruction* array_length = instruction_->InputAt(1);
197 Location length_loc = locations->InAt(1);
198 InvokeRuntimeCallingConvention calling_convention;
199 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
200 // Load the array length into our temporary.
201 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
202 Location array_loc = array_length->GetLocations()->InAt(0);
203 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
204 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
205 // Check for conflicts with index.
206 if (length_loc.Equals(locations->InAt(0))) {
207 // We know we aren't using parameter 2.
208 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
209 }
210 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
211 }
212
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000213 // We're moving two locations to locations that could overlap, so we need a parallel
214 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000215 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100216 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000217 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100218 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400219 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100220 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
221 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100222 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
223 ? QUICK_ENTRY_POINT(pThrowStringBounds)
224 : QUICK_ENTRY_POINT(pThrowArrayBounds);
225 x86_64_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000226 instruction_,
227 instruction_->GetDexPc(),
228 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100229 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000230 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100231 }
232
Alexandre Rames8158f282015-08-07 10:26:17 +0100233 bool IsFatal() const OVERRIDE { return true; }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
236
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100237 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100238 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
239};
240
Andreas Gampe85b62f22015-09-09 13:15:38 -0700241class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100242 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 LoadClassSlowPathX86_64(HLoadClass* cls,
244 HInstruction* at,
245 uint32_t dex_pc,
246 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000247 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
249 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000253 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100254 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100258 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000260 x86_64_codegen->InvokeRuntime(do_clinit_ ?
261 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
262 QUICK_ENTRY_POINT(pInitializeType),
263 at_,
264 dex_pc_,
265 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000266 if (do_clinit_) {
267 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
268 } else {
269 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
270 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 if (out.IsValid()) {
275 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000276 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 }
278
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100280 __ jmp(GetExitLabel());
281 }
282
Alexandre Rames9931f312015-06-19 14:47:01 +0100283 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
284
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 // The class this slow path will load.
287 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000289 // The instruction where this slow path is happening.
290 // (Might be the load class or an initialization check).
291 HInstruction* const at_;
292
293 // The dex PC of `at_`.
294 const uint32_t dex_pc_;
295
296 // Whether to initialize the class.
297 const bool do_clinit_;
298
299 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100300};
301
Andreas Gampe85b62f22015-09-09 13:15:38 -0700302class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000303 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000304 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000305
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000307 LocationSummary* locations = instruction_->GetLocations();
308 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
309
Roland Levillain0d5a2812015-11-13 10:07:31 +0000310 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000311 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000313
314 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000315 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
316 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000317 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
318 instruction_,
319 instruction_->GetDexPc(),
320 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000321 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000322 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000323 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000324 __ jmp(GetExitLabel());
325 }
326
Alexandre Rames9931f312015-06-19 14:47:01 +0100327 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
328
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000329 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000330 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
331};
332
Andreas Gampe85b62f22015-09-09 13:15:38 -0700333class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000336 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000338 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100340 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
341 : locations->Out();
342 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 DCHECK(instruction_->IsCheckCast()
344 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
Roland Levillain0d5a2812015-11-13 10:07:31 +0000346 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000349 if (!is_fatal_) {
350 SaveLiveRegisters(codegen, locations);
351 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352
353 // We're moving two locations to locations that could overlap, so we need a parallel
354 // move resolver.
355 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000356 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100359 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000363
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000365 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
366 instruction_,
367 dex_pc,
368 this);
369 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700370 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000371 } else {
372 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000373 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
374 instruction_,
375 dex_pc,
376 this);
377 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000378 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000379
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000380 if (!is_fatal_) {
381 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000382 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000383 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000384
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 RestoreLiveRegisters(codegen, locations);
386 __ jmp(GetExitLabel());
387 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000388 }
389
Alexandre Rames9931f312015-06-19 14:47:01 +0100390 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
391
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000392 bool IsFatal() const OVERRIDE { return is_fatal_; }
393
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000394 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000395 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000396
397 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
398};
399
Andreas Gampe85b62f22015-09-09 13:15:38 -0700400class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700401 public:
Aart Bik42249c32016-01-07 15:33:50 -0800402 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000406 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000409 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800410 instruction_,
411 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000412 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000413 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700414 }
415
Alexandre Rames9931f312015-06-19 14:47:01 +0100416 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
417
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700418 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700419 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
420};
421
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422class ArraySetSlowPathX86_64 : public SlowPathCode {
423 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000424 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425
426 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
427 LocationSummary* locations = instruction_->GetLocations();
428 __ Bind(GetEntryLabel());
429 SaveLiveRegisters(codegen, locations);
430
431 InvokeRuntimeCallingConvention calling_convention;
432 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
433 parallel_move.AddMove(
434 locations->InAt(0),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
436 Primitive::kPrimNot,
437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(1),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
441 Primitive::kPrimInt,
442 nullptr);
443 parallel_move.AddMove(
444 locations->InAt(2),
445 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
446 Primitive::kPrimNot,
447 nullptr);
448 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
449
Roland Levillain0d5a2812015-11-13 10:07:31 +0000450 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
451 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
452 instruction_,
453 instruction_->GetDexPc(),
454 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000455 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100456 RestoreLiveRegisters(codegen, locations);
457 __ jmp(GetExitLabel());
458 }
459
460 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
461
462 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100463 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
464};
465
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000466// Slow path marking an object during a read barrier.
467class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
468 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000469 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location obj, bool unpoison)
470 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000471 DCHECK(kEmitCompilerReadBarrier);
472 }
473
474 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
475
476 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
477 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100478 Register reg = obj_.AsRegister<Register>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000479 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100480 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000481 DCHECK(instruction_->IsInstanceFieldGet() ||
482 instruction_->IsStaticFieldGet() ||
483 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100484 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000485 instruction_->IsLoadClass() ||
486 instruction_->IsLoadString() ||
487 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100488 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100489 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
490 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000491 << "Unexpected instruction in read barrier marking slow path: "
492 << instruction_->DebugName();
493
494 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000495 if (unpoison_) {
496 // Object* ref = ref_addr->AsMirrorPtr()
497 __ MaybeUnpoisonHeapReference(obj_.AsRegister<CpuRegister>());
498 }
Roland Levillain4359e612016-07-20 11:32:19 +0100499 // No need to save live registers; it's taken care of by the
500 // entrypoint. Also, there is no need to update the stack mask,
501 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000502 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100503 DCHECK_NE(reg, RSP);
504 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
505 // "Compact" slow path, saving two moves.
506 //
507 // Instead of using the standard runtime calling convention (input
508 // and output in R0):
509 //
510 // RDI <- obj
511 // RAX <- ReadBarrierMark(RDI)
512 // obj <- RAX
513 //
514 // we just use rX (the register holding `obj`) as input and output
515 // of a dedicated entrypoint:
516 //
517 // rX <- ReadBarrierMarkRegX(rX)
518 //
519 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700520 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100521 // This runtime call does not require a stack map.
522 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000523 __ jmp(GetExitLabel());
524 }
525
526 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000527 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000528 const bool unpoison_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000529
530 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
531};
532
Roland Levillain0d5a2812015-11-13 10:07:31 +0000533// Slow path generating a read barrier for a heap reference.
534class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
535 public:
536 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
537 Location out,
538 Location ref,
539 Location obj,
540 uint32_t offset,
541 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000542 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000543 out_(out),
544 ref_(ref),
545 obj_(obj),
546 offset_(offset),
547 index_(index) {
548 DCHECK(kEmitCompilerReadBarrier);
549 // If `obj` is equal to `out` or `ref`, it means the initial
550 // object has been overwritten by (or after) the heap object
551 // reference load to be instrumented, e.g.:
552 //
553 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000554 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000555 //
556 // In that case, we have lost the information about the original
557 // object, and the emitted read barrier cannot work properly.
558 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
559 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
560}
561
562 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
563 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
564 LocationSummary* locations = instruction_->GetLocations();
565 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
566 DCHECK(locations->CanCall());
567 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100568 DCHECK(instruction_->IsInstanceFieldGet() ||
569 instruction_->IsStaticFieldGet() ||
570 instruction_->IsArrayGet() ||
571 instruction_->IsInstanceOf() ||
572 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100573 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000574 << "Unexpected instruction in read barrier for heap reference slow path: "
575 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000576
577 __ Bind(GetEntryLabel());
578 SaveLiveRegisters(codegen, locations);
579
580 // We may have to change the index's value, but as `index_` is a
581 // constant member (like other "inputs" of this slow path),
582 // introduce a copy of it, `index`.
583 Location index = index_;
584 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100585 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000586 if (instruction_->IsArrayGet()) {
587 // Compute real offset and store it in index_.
588 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
589 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
590 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
591 // We are about to change the value of `index_reg` (see the
592 // calls to art::x86_64::X86_64Assembler::shll and
593 // art::x86_64::X86_64Assembler::AddImmediate below), but it
594 // has not been saved by the previous call to
595 // art::SlowPathCode::SaveLiveRegisters, as it is a
596 // callee-save register --
597 // art::SlowPathCode::SaveLiveRegisters does not consider
598 // callee-save registers, as it has been designed with the
599 // assumption that callee-save registers are supposed to be
600 // handled by the called function. So, as a callee-save
601 // register, `index_reg` _would_ eventually be saved onto
602 // the stack, but it would be too late: we would have
603 // changed its value earlier. Therefore, we manually save
604 // it here into another freely available register,
605 // `free_reg`, chosen of course among the caller-save
606 // registers (as a callee-save `free_reg` register would
607 // exhibit the same problem).
608 //
609 // Note we could have requested a temporary register from
610 // the register allocator instead; but we prefer not to, as
611 // this is a slow path, and we know we can find a
612 // caller-save register that is available.
613 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
614 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
615 index_reg = free_reg;
616 index = Location::RegisterLocation(index_reg);
617 } else {
618 // The initial register stored in `index_` has already been
619 // saved in the call to art::SlowPathCode::SaveLiveRegisters
620 // (as it is not a callee-save register), so we can freely
621 // use it.
622 }
623 // Shifting the index value contained in `index_reg` by the
624 // scale factor (2) cannot overflow in practice, as the
625 // runtime is unable to allocate object arrays with a size
626 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
627 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
628 static_assert(
629 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
630 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
631 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
632 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100633 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
634 // intrinsics, `index_` is not shifted by a scale factor of 2
635 // (as in the case of ArrayGet), as it is actually an offset
636 // to an object field within an object.
637 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000638 DCHECK(instruction_->GetLocations()->Intrinsified());
639 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
640 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
641 << instruction_->AsInvoke()->GetIntrinsic();
642 DCHECK_EQ(offset_, 0U);
643 DCHECK(index_.IsRegister());
644 }
645 }
646
647 // We're moving two or three locations to locations that could
648 // overlap, so we need a parallel move resolver.
649 InvokeRuntimeCallingConvention calling_convention;
650 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
651 parallel_move.AddMove(ref_,
652 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
653 Primitive::kPrimNot,
654 nullptr);
655 parallel_move.AddMove(obj_,
656 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
657 Primitive::kPrimNot,
658 nullptr);
659 if (index.IsValid()) {
660 parallel_move.AddMove(index,
661 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
662 Primitive::kPrimInt,
663 nullptr);
664 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
665 } else {
666 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
667 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
668 }
669 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
670 instruction_,
671 instruction_->GetDexPc(),
672 this);
673 CheckEntrypointTypes<
674 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
675 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
676
677 RestoreLiveRegisters(codegen, locations);
678 __ jmp(GetExitLabel());
679 }
680
681 const char* GetDescription() const OVERRIDE {
682 return "ReadBarrierForHeapReferenceSlowPathX86_64";
683 }
684
685 private:
686 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
687 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
688 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
689 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
690 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
691 return static_cast<CpuRegister>(i);
692 }
693 }
694 // We shall never fail to find a free caller-save register, as
695 // there are more than two core caller-save registers on x86-64
696 // (meaning it is possible to find one which is different from
697 // `ref` and `obj`).
698 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
699 LOG(FATAL) << "Could not find a free caller-save register";
700 UNREACHABLE();
701 }
702
Roland Levillain0d5a2812015-11-13 10:07:31 +0000703 const Location out_;
704 const Location ref_;
705 const Location obj_;
706 const uint32_t offset_;
707 // An additional location containing an index to an array.
708 // Only used for HArrayGet and the UnsafeGetObject &
709 // UnsafeGetObjectVolatile intrinsics.
710 const Location index_;
711
712 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
713};
714
715// Slow path generating a read barrier for a GC root.
716class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
717 public:
718 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000719 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000720 DCHECK(kEmitCompilerReadBarrier);
721 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000722
723 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
724 LocationSummary* locations = instruction_->GetLocations();
725 DCHECK(locations->CanCall());
726 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000727 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
728 << "Unexpected instruction in read barrier for GC root slow path: "
729 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000730
731 __ Bind(GetEntryLabel());
732 SaveLiveRegisters(codegen, locations);
733
734 InvokeRuntimeCallingConvention calling_convention;
735 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
736 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
737 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
738 instruction_,
739 instruction_->GetDexPc(),
740 this);
741 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
742 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
743
744 RestoreLiveRegisters(codegen, locations);
745 __ jmp(GetExitLabel());
746 }
747
748 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
749
750 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000751 const Location out_;
752 const Location root_;
753
754 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
755};
756
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100757#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100758// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
759#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100760
Roland Levillain4fa13f62015-07-06 18:11:54 +0100761inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700762 switch (cond) {
763 case kCondEQ: return kEqual;
764 case kCondNE: return kNotEqual;
765 case kCondLT: return kLess;
766 case kCondLE: return kLessEqual;
767 case kCondGT: return kGreater;
768 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700769 case kCondB: return kBelow;
770 case kCondBE: return kBelowEqual;
771 case kCondA: return kAbove;
772 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700773 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100774 LOG(FATAL) << "Unreachable";
775 UNREACHABLE();
776}
777
Aart Bike9f37602015-10-09 11:15:55 -0700778// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100779inline Condition X86_64FPCondition(IfCondition cond) {
780 switch (cond) {
781 case kCondEQ: return kEqual;
782 case kCondNE: return kNotEqual;
783 case kCondLT: return kBelow;
784 case kCondLE: return kBelowEqual;
785 case kCondGT: return kAbove;
786 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700787 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100788 };
789 LOG(FATAL) << "Unreachable";
790 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700791}
792
Vladimir Markodc151b22015-10-15 18:02:30 +0100793HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
794 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
795 MethodReference target_method ATTRIBUTE_UNUSED) {
796 switch (desired_dispatch_info.code_ptr_location) {
797 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
798 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
799 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
800 return HInvokeStaticOrDirect::DispatchInfo {
801 desired_dispatch_info.method_load_kind,
802 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
803 desired_dispatch_info.method_load_data,
804 0u
805 };
806 default:
807 return desired_dispatch_info;
808 }
809}
810
Serguei Katkov288c7a82016-05-16 11:53:15 +0600811Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
812 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800813 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000814 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
815 switch (invoke->GetMethodLoadKind()) {
816 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
817 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000818 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000819 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000820 break;
821 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000822 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000823 break;
824 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
825 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
826 break;
827 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
828 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
829 method_patches_.emplace_back(invoke->GetTargetMethod());
830 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
831 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000832 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000833 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000834 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000835 // Bind a new fixup label at the end of the "movl" insn.
836 uint32_t offset = invoke->GetDexCacheArrayOffset();
837 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000838 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000839 }
Vladimir Marko58155012015-08-19 12:49:41 +0000840 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000841 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000842 Register method_reg;
843 CpuRegister reg = temp.AsRegister<CpuRegister>();
844 if (current_method.IsRegister()) {
845 method_reg = current_method.AsRegister<Register>();
846 } else {
847 DCHECK(invoke->GetLocations()->Intrinsified());
848 DCHECK(!current_method.IsValid());
849 method_reg = reg.AsRegister();
850 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
851 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000852 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100853 __ movq(reg,
854 Address(CpuRegister(method_reg),
855 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100856 // temp = temp[index_in_cache];
857 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
858 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000859 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
860 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100861 }
Vladimir Marko58155012015-08-19 12:49:41 +0000862 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600863 return callee_method;
864}
865
866void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
867 Location temp) {
868 // All registers are assumed to be correctly set up.
869 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000870
871 switch (invoke->GetCodePtrLocation()) {
872 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
873 __ call(&frame_entry_label_);
874 break;
875 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
876 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
877 Label* label = &relative_call_patches_.back().label;
878 __ call(label); // Bind to the patch label, override at link time.
879 __ Bind(label); // Bind the label at the end of the "call" insn.
880 break;
881 }
882 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
883 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100884 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
885 LOG(FATAL) << "Unsupported";
886 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000887 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
888 // (callee_method + offset_of_quick_compiled_code)()
889 __ call(Address(callee_method.AsRegister<CpuRegister>(),
890 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700891 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000892 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000893 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800894
895 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800896}
897
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000898void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
899 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
900 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
901 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000902
903 // Use the calling convention instead of the location of the receiver, as
904 // intrinsics may have put the receiver in a different register. In the intrinsics
905 // slow path, the arguments have been moved to the right place, so here we are
906 // guaranteed that the receiver is the first register of the calling convention.
907 InvokeDexCallingConvention calling_convention;
908 Register receiver = calling_convention.GetRegisterAt(0);
909
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000910 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000911 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000912 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000913 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000914 // Instead of simply (possibly) unpoisoning `temp` here, we should
915 // emit a read barrier for the previous class reference load.
916 // However this is not required in practice, as this is an
917 // intermediate/temporary reference and because the current
918 // concurrent copying collector keeps the from-space memory
919 // intact/accessible until the end of the marking phase (the
920 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000921 __ MaybeUnpoisonHeapReference(temp);
922 // temp = temp->GetMethodAt(method_offset);
923 __ movq(temp, Address(temp, method_offset));
924 // call temp->GetEntryPoint();
925 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -0700926 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000927}
928
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000929void CodeGeneratorX86_64::RecordSimplePatch() {
930 if (GetCompilerOptions().GetIncludePatchInformation()) {
931 simple_patches_.emplace_back();
932 __ Bind(&simple_patches_.back());
933 }
934}
935
936void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
937 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
938 __ Bind(&string_patches_.back().label);
939}
940
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100941void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
942 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
943 __ Bind(&type_patches_.back().label);
944}
945
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000946Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
947 uint32_t element_offset) {
948 // Add a patch entry and return the label.
949 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
950 return &pc_relative_dex_cache_patches_.back().label;
951}
952
Vladimir Marko58155012015-08-19 12:49:41 +0000953void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
954 DCHECK(linker_patches->empty());
955 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000956 method_patches_.size() +
957 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000958 pc_relative_dex_cache_patches_.size() +
959 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100960 string_patches_.size() +
961 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000962 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000963 // The label points to the end of the "movl" insn but the literal offset for method
964 // patch needs to point to the embedded constant which occupies the last 4 bytes.
965 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000966 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000967 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000968 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
969 info.target_method.dex_file,
970 info.target_method.dex_method_index));
971 }
972 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000973 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000974 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
975 info.target_method.dex_file,
976 info.target_method.dex_method_index));
977 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000978 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
979 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000980 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
981 &info.target_dex_file,
982 info.label.Position(),
983 info.element_offset));
984 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000985 for (const Label& label : simple_patches_) {
986 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
987 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
988 }
989 for (const StringPatchInfo<Label>& info : string_patches_) {
990 // These are always PC-relative, see GetSupportedLoadStringKind().
991 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
992 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
993 &info.dex_file,
994 info.label.Position(),
995 info.string_index));
996 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100997 for (const TypePatchInfo<Label>& info : type_patches_) {
998 // These are always PC-relative, see GetSupportedLoadClassKind().
999 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1000 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
1001 &info.dex_file,
1002 info.label.Position(),
1003 info.type_index));
1004 }
Vladimir Marko58155012015-08-19 12:49:41 +00001005}
1006
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001008 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001009}
1010
1011void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001012 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013}
1014
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001015size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1016 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1017 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001018}
1019
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001020size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1021 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1022 return kX86_64WordSize;
1023}
1024
1025size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1026 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1027 return kX86_64WordSize;
1028}
1029
1030size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1031 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1032 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001033}
1034
Calin Juravle175dc732015-08-25 15:42:32 +01001035void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1036 HInstruction* instruction,
1037 uint32_t dex_pc,
1038 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001039 InvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001040 instruction,
1041 dex_pc,
1042 slow_path);
1043}
1044
1045void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +01001046 HInstruction* instruction,
1047 uint32_t dex_pc,
1048 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001049 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001050 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +01001051 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +01001052}
1053
Roland Levillaindec8f632016-07-22 17:10:06 +01001054void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1055 HInstruction* instruction,
1056 SlowPathCode* slow_path) {
1057 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1058 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1059}
1060
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001061static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001062// Use a fake return address register to mimic Quick.
1063static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001064CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001065 const X86_64InstructionSetFeatures& isa_features,
1066 const CompilerOptions& compiler_options,
1067 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001068 : CodeGenerator(graph,
1069 kNumberOfCpuRegisters,
1070 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001071 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001072 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1073 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001074 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001075 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1076 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001077 compiler_options,
1078 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001079 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001080 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001081 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001082 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001083 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001084 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001085 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001086 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1087 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001088 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001089 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1090 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001091 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001092 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001093 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1094}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001096InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1097 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001098 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001099 assembler_(codegen->GetAssembler()),
1100 codegen_(codegen) {}
1101
David Brazdil58282f42016-01-14 12:45:10 +00001102void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001104 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001106 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001107 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108}
1109
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001110static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001111 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001112}
David Srbecky9d8606d2015-04-12 09:35:32 +01001113
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001114static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001115 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001116}
1117
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001119 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001120 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001121 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001122 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001123 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001124
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001125 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001126 __ testq(CpuRegister(RAX), Address(
1127 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001128 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001129 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001130
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001131 if (HasEmptyFrame()) {
1132 return;
1133 }
1134
Nicolas Geoffray98893962015-01-21 12:32:32 +00001135 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001136 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001137 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001138 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001139 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1140 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001141 }
1142 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001143
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001144 int adjust = GetFrameSize() - GetCoreSpillSize();
1145 __ subq(CpuRegister(RSP), Immediate(adjust));
1146 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001147 uint32_t xmm_spill_location = GetFpuSpillStart();
1148 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001149
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001150 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1151 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001152 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1153 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1154 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001155 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001156 }
1157
Mathieu Chartiere401d142015-04-22 13:56:20 -07001158 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001159 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160}
1161
1162void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001163 __ cfi().RememberState();
1164 if (!HasEmptyFrame()) {
1165 uint32_t xmm_spill_location = GetFpuSpillStart();
1166 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1167 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1168 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1169 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1170 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1171 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1172 }
1173 }
1174
1175 int adjust = GetFrameSize() - GetCoreSpillSize();
1176 __ addq(CpuRegister(RSP), Immediate(adjust));
1177 __ cfi().AdjustCFAOffset(-adjust);
1178
1179 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1180 Register reg = kCoreCalleeSaves[i];
1181 if (allocated_registers_.ContainsCoreRegister(reg)) {
1182 __ popq(CpuRegister(reg));
1183 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1184 __ cfi().Restore(DWARFReg(reg));
1185 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001186 }
1187 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001188 __ ret();
1189 __ cfi().RestoreState();
1190 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191}
1192
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001193void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1194 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195}
1196
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001197void CodeGeneratorX86_64::Move(Location destination, Location source) {
1198 if (source.Equals(destination)) {
1199 return;
1200 }
1201 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001202 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001203 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001204 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001206 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001208 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1209 } else if (source.IsConstant()) {
1210 HConstant* constant = source.GetConstant();
1211 if (constant->IsLongConstant()) {
1212 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1213 } else {
1214 Load32BitValue(dest, GetInt32ValueOf(constant));
1215 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216 } else {
1217 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001218 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001219 }
1220 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001221 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001223 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001225 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1226 } else if (source.IsConstant()) {
1227 HConstant* constant = source.GetConstant();
1228 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1229 if (constant->IsFloatConstant()) {
1230 Load32BitValue(dest, static_cast<int32_t>(value));
1231 } else {
1232 Load64BitValue(dest, value);
1233 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001235 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 } else {
1237 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001238 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001239 }
1240 } else if (destination.IsStackSlot()) {
1241 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 } else if (source.IsFpuRegister()) {
1245 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001246 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001247 } else if (source.IsConstant()) {
1248 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001249 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001250 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001252 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001253 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1254 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001255 }
1256 } else {
1257 DCHECK(destination.IsDoubleStackSlot());
1258 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001260 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001261 } else if (source.IsFpuRegister()) {
1262 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001263 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001264 } else if (source.IsConstant()) {
1265 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001266 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001267 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001268 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001269 } else {
1270 DCHECK(constant->IsLongConstant());
1271 value = constant->AsLongConstant()->GetValue();
1272 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001273 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001274 } else {
1275 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001276 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1277 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001278 }
1279 }
1280}
1281
Calin Juravle175dc732015-08-25 15:42:32 +01001282void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1283 DCHECK(location.IsRegister());
1284 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1285}
1286
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287void CodeGeneratorX86_64::MoveLocation(
1288 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1289 Move(dst, src);
1290}
1291
1292void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1293 if (location.IsRegister()) {
1294 locations->AddTemp(location);
1295 } else {
1296 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1297 }
1298}
1299
David Brazdilfc6a86a2015-06-26 10:33:45 +00001300void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001301 DCHECK(!successor->IsExitBlock());
1302
1303 HBasicBlock* block = got->GetBlock();
1304 HInstruction* previous = got->GetPrevious();
1305
1306 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001307 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001308 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1309 return;
1310 }
1311
1312 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1313 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1314 }
1315 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316 __ jmp(codegen_->GetLabelOf(successor));
1317 }
1318}
1319
David Brazdilfc6a86a2015-06-26 10:33:45 +00001320void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1321 got->SetLocations(nullptr);
1322}
1323
1324void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1325 HandleGoto(got, got->GetSuccessor());
1326}
1327
1328void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1329 try_boundary->SetLocations(nullptr);
1330}
1331
1332void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1333 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1334 if (!successor->IsExitBlock()) {
1335 HandleGoto(try_boundary, successor);
1336 }
1337}
1338
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001339void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1340 exit->SetLocations(nullptr);
1341}
1342
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001343void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001344}
1345
Mark Mendell152408f2015-12-31 12:28:50 -05001346template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001347void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001348 LabelType* true_label,
1349 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001350 if (cond->IsFPConditionTrueIfNaN()) {
1351 __ j(kUnordered, true_label);
1352 } else if (cond->IsFPConditionFalseIfNaN()) {
1353 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001355 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001356}
1357
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001358void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001359 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001360
Mark Mendellc4701932015-04-10 13:18:51 -04001361 Location left = locations->InAt(0);
1362 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001363 Primitive::Type type = condition->InputAt(0)->GetType();
1364 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001365 case Primitive::kPrimBoolean:
1366 case Primitive::kPrimByte:
1367 case Primitive::kPrimChar:
1368 case Primitive::kPrimShort:
1369 case Primitive::kPrimInt:
1370 case Primitive::kPrimNot: {
1371 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1372 if (right.IsConstant()) {
1373 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1374 if (value == 0) {
1375 __ testl(left_reg, left_reg);
1376 } else {
1377 __ cmpl(left_reg, Immediate(value));
1378 }
1379 } else if (right.IsStackSlot()) {
1380 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1381 } else {
1382 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1383 }
1384 break;
1385 }
Mark Mendellc4701932015-04-10 13:18:51 -04001386 case Primitive::kPrimLong: {
1387 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1388 if (right.IsConstant()) {
1389 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001390 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001391 } else if (right.IsDoubleStackSlot()) {
1392 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1393 } else {
1394 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1395 }
Mark Mendellc4701932015-04-10 13:18:51 -04001396 break;
1397 }
1398 case Primitive::kPrimFloat: {
1399 if (right.IsFpuRegister()) {
1400 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1401 } else if (right.IsConstant()) {
1402 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1403 codegen_->LiteralFloatAddress(
1404 right.GetConstant()->AsFloatConstant()->GetValue()));
1405 } else {
1406 DCHECK(right.IsStackSlot());
1407 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1408 Address(CpuRegister(RSP), right.GetStackIndex()));
1409 }
Mark Mendellc4701932015-04-10 13:18:51 -04001410 break;
1411 }
1412 case Primitive::kPrimDouble: {
1413 if (right.IsFpuRegister()) {
1414 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1415 } else if (right.IsConstant()) {
1416 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1417 codegen_->LiteralDoubleAddress(
1418 right.GetConstant()->AsDoubleConstant()->GetValue()));
1419 } else {
1420 DCHECK(right.IsDoubleStackSlot());
1421 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1422 Address(CpuRegister(RSP), right.GetStackIndex()));
1423 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001424 break;
1425 }
1426 default:
1427 LOG(FATAL) << "Unexpected condition type " << type;
1428 }
1429}
1430
1431template<class LabelType>
1432void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1433 LabelType* true_target_in,
1434 LabelType* false_target_in) {
1435 // Generated branching requires both targets to be explicit. If either of the
1436 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1437 LabelType fallthrough_target;
1438 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1439 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1440
1441 // Generate the comparison to set the CC.
1442 GenerateCompareTest(condition);
1443
1444 // Now generate the correct jump(s).
1445 Primitive::Type type = condition->InputAt(0)->GetType();
1446 switch (type) {
1447 case Primitive::kPrimLong: {
1448 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1449 break;
1450 }
1451 case Primitive::kPrimFloat: {
1452 GenerateFPJumps(condition, true_target, false_target);
1453 break;
1454 }
1455 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001456 GenerateFPJumps(condition, true_target, false_target);
1457 break;
1458 }
1459 default:
1460 LOG(FATAL) << "Unexpected condition type " << type;
1461 }
1462
David Brazdil0debae72015-11-12 18:37:00 +00001463 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001464 __ jmp(false_target);
1465 }
David Brazdil0debae72015-11-12 18:37:00 +00001466
1467 if (fallthrough_target.IsLinked()) {
1468 __ Bind(&fallthrough_target);
1469 }
Mark Mendellc4701932015-04-10 13:18:51 -04001470}
1471
David Brazdil0debae72015-11-12 18:37:00 +00001472static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1473 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1474 // are set only strictly before `branch`. We can't use the eflags on long
1475 // conditions if they are materialized due to the complex branching.
1476 return cond->IsCondition() &&
1477 cond->GetNext() == branch &&
1478 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1479}
1480
Mark Mendell152408f2015-12-31 12:28:50 -05001481template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001482void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001483 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001484 LabelType* true_target,
1485 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001486 HInstruction* cond = instruction->InputAt(condition_input_index);
1487
1488 if (true_target == nullptr && false_target == nullptr) {
1489 // Nothing to do. The code always falls through.
1490 return;
1491 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001492 // Constant condition, statically compared against "true" (integer value 1).
1493 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001494 if (true_target != nullptr) {
1495 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001496 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001497 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001498 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001499 if (false_target != nullptr) {
1500 __ jmp(false_target);
1501 }
1502 }
1503 return;
1504 }
1505
1506 // The following code generates these patterns:
1507 // (1) true_target == nullptr && false_target != nullptr
1508 // - opposite condition true => branch to false_target
1509 // (2) true_target != nullptr && false_target == nullptr
1510 // - condition true => branch to true_target
1511 // (3) true_target != nullptr && false_target != nullptr
1512 // - condition true => branch to true_target
1513 // - branch to false_target
1514 if (IsBooleanValueOrMaterializedCondition(cond)) {
1515 if (AreEflagsSetFrom(cond, instruction)) {
1516 if (true_target == nullptr) {
1517 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1518 } else {
1519 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1520 }
1521 } else {
1522 // Materialized condition, compare against 0.
1523 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1524 if (lhs.IsRegister()) {
1525 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1526 } else {
1527 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1528 }
1529 if (true_target == nullptr) {
1530 __ j(kEqual, false_target);
1531 } else {
1532 __ j(kNotEqual, true_target);
1533 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001534 }
1535 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001536 // Condition has not been materialized, use its inputs as the
1537 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001538 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001539
David Brazdil0debae72015-11-12 18:37:00 +00001540 // If this is a long or FP comparison that has been folded into
1541 // the HCondition, generate the comparison directly.
1542 Primitive::Type type = condition->InputAt(0)->GetType();
1543 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1544 GenerateCompareTestAndBranch(condition, true_target, false_target);
1545 return;
1546 }
1547
1548 Location lhs = condition->GetLocations()->InAt(0);
1549 Location rhs = condition->GetLocations()->InAt(1);
1550 if (rhs.IsRegister()) {
1551 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1552 } else if (rhs.IsConstant()) {
1553 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001554 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001555 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001556 __ cmpl(lhs.AsRegister<CpuRegister>(),
1557 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1558 }
1559 if (true_target == nullptr) {
1560 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1561 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001562 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001563 }
Dave Allison20dfc792014-06-16 20:44:29 -07001564 }
David Brazdil0debae72015-11-12 18:37:00 +00001565
1566 // If neither branch falls through (case 3), the conditional branch to `true_target`
1567 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1568 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001569 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001570 }
1571}
1572
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001573void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001574 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1575 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001576 locations->SetInAt(0, Location::Any());
1577 }
1578}
1579
1580void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001581 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1582 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1583 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1584 nullptr : codegen_->GetLabelOf(true_successor);
1585 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1586 nullptr : codegen_->GetLabelOf(false_successor);
1587 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001588}
1589
1590void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1591 LocationSummary* locations = new (GetGraph()->GetArena())
1592 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001593 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001594 locations->SetInAt(0, Location::Any());
1595 }
1596}
1597
1598void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001599 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001600 GenerateTestAndBranch<Label>(deoptimize,
1601 /* condition_input_index */ 0,
1602 slow_path->GetEntryLabel(),
1603 /* false_target */ nullptr);
1604}
1605
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001606static bool SelectCanUseCMOV(HSelect* select) {
1607 // There are no conditional move instructions for XMMs.
1608 if (Primitive::IsFloatingPointType(select->GetType())) {
1609 return false;
1610 }
1611
1612 // A FP condition doesn't generate the single CC that we need.
1613 HInstruction* condition = select->GetCondition();
1614 if (condition->IsCondition() &&
1615 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1616 return false;
1617 }
1618
1619 // We can generate a CMOV for this Select.
1620 return true;
1621}
1622
David Brazdil74eb1b22015-12-14 11:44:01 +00001623void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1624 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1625 if (Primitive::IsFloatingPointType(select->GetType())) {
1626 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001627 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001628 } else {
1629 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001630 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001631 if (select->InputAt(1)->IsConstant()) {
1632 locations->SetInAt(1, Location::RequiresRegister());
1633 } else {
1634 locations->SetInAt(1, Location::Any());
1635 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001636 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001637 locations->SetInAt(1, Location::Any());
1638 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001639 }
1640 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1641 locations->SetInAt(2, Location::RequiresRegister());
1642 }
1643 locations->SetOut(Location::SameAsFirstInput());
1644}
1645
1646void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1647 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001648 if (SelectCanUseCMOV(select)) {
1649 // If both the condition and the source types are integer, we can generate
1650 // a CMOV to implement Select.
1651 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001652 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001653 DCHECK(locations->InAt(0).Equals(locations->Out()));
1654
1655 HInstruction* select_condition = select->GetCondition();
1656 Condition cond = kNotEqual;
1657
1658 // Figure out how to test the 'condition'.
1659 if (select_condition->IsCondition()) {
1660 HCondition* condition = select_condition->AsCondition();
1661 if (!condition->IsEmittedAtUseSite()) {
1662 // This was a previously materialized condition.
1663 // Can we use the existing condition code?
1664 if (AreEflagsSetFrom(condition, select)) {
1665 // Materialization was the previous instruction. Condition codes are right.
1666 cond = X86_64IntegerCondition(condition->GetCondition());
1667 } else {
1668 // No, we have to recreate the condition code.
1669 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1670 __ testl(cond_reg, cond_reg);
1671 }
1672 } else {
1673 GenerateCompareTest(condition);
1674 cond = X86_64IntegerCondition(condition->GetCondition());
1675 }
1676 } else {
1677 // Must be a boolean condition, which needs to be compared to 0.
1678 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1679 __ testl(cond_reg, cond_reg);
1680 }
1681
1682 // If the condition is true, overwrite the output, which already contains false.
1683 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001684 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1685 if (value_true_loc.IsRegister()) {
1686 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1687 } else {
1688 __ cmov(cond,
1689 value_false,
1690 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1691 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001692 } else {
1693 NearLabel false_target;
1694 GenerateTestAndBranch<NearLabel>(select,
1695 /* condition_input_index */ 2,
1696 /* true_target */ nullptr,
1697 &false_target);
1698 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1699 __ Bind(&false_target);
1700 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001701}
1702
David Srbecky0cf44932015-12-09 14:09:59 +00001703void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1704 new (GetGraph()->GetArena()) LocationSummary(info);
1705}
1706
David Srbeckyd28f4a02016-03-14 17:14:24 +00001707void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1708 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001709}
1710
1711void CodeGeneratorX86_64::GenerateNop() {
1712 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001713}
1714
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001715void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001716 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001717 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001718 // Handle the long/FP comparisons made in instruction simplification.
1719 switch (cond->InputAt(0)->GetType()) {
1720 case Primitive::kPrimLong:
1721 locations->SetInAt(0, Location::RequiresRegister());
1722 locations->SetInAt(1, Location::Any());
1723 break;
1724 case Primitive::kPrimFloat:
1725 case Primitive::kPrimDouble:
1726 locations->SetInAt(0, Location::RequiresFpuRegister());
1727 locations->SetInAt(1, Location::Any());
1728 break;
1729 default:
1730 locations->SetInAt(0, Location::RequiresRegister());
1731 locations->SetInAt(1, Location::Any());
1732 break;
1733 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001734 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001735 locations->SetOut(Location::RequiresRegister());
1736 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001737}
1738
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001739void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001740 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001741 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001742 }
Mark Mendellc4701932015-04-10 13:18:51 -04001743
1744 LocationSummary* locations = cond->GetLocations();
1745 Location lhs = locations->InAt(0);
1746 Location rhs = locations->InAt(1);
1747 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001748 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001749
1750 switch (cond->InputAt(0)->GetType()) {
1751 default:
1752 // Integer case.
1753
1754 // Clear output register: setcc only sets the low byte.
1755 __ xorl(reg, reg);
1756
1757 if (rhs.IsRegister()) {
1758 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1759 } else if (rhs.IsConstant()) {
1760 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001761 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001762 } else {
1763 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1764 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001765 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001766 return;
1767 case Primitive::kPrimLong:
1768 // Clear output register: setcc only sets the low byte.
1769 __ xorl(reg, reg);
1770
1771 if (rhs.IsRegister()) {
1772 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1773 } else if (rhs.IsConstant()) {
1774 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001775 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001776 } else {
1777 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1778 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001779 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001780 return;
1781 case Primitive::kPrimFloat: {
1782 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1783 if (rhs.IsConstant()) {
1784 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1785 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1786 } else if (rhs.IsStackSlot()) {
1787 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1788 } else {
1789 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1790 }
1791 GenerateFPJumps(cond, &true_label, &false_label);
1792 break;
1793 }
1794 case Primitive::kPrimDouble: {
1795 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1796 if (rhs.IsConstant()) {
1797 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1798 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1799 } else if (rhs.IsDoubleStackSlot()) {
1800 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1801 } else {
1802 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1803 }
1804 GenerateFPJumps(cond, &true_label, &false_label);
1805 break;
1806 }
1807 }
1808
1809 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001810 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001811
Roland Levillain4fa13f62015-07-06 18:11:54 +01001812 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001813 __ Bind(&false_label);
1814 __ xorl(reg, reg);
1815 __ jmp(&done_label);
1816
Roland Levillain4fa13f62015-07-06 18:11:54 +01001817 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001818 __ Bind(&true_label);
1819 __ movl(reg, Immediate(1));
1820 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001821}
1822
1823void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001825}
1826
1827void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001829}
1830
1831void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001833}
1834
1835void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001837}
1838
1839void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001841}
1842
1843void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001845}
1846
1847void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001848 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001849}
1850
1851void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001853}
1854
1855void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001856 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001857}
1858
1859void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001860 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001861}
1862
1863void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001864 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001865}
1866
1867void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001868 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869}
1870
Aart Bike9f37602015-10-09 11:15:55 -07001871void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001872 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001873}
1874
1875void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001876 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001877}
1878
1879void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001880 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001881}
1882
1883void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001884 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001885}
1886
1887void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001888 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001889}
1890
1891void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001893}
1894
1895void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001896 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001897}
1898
1899void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001900 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001901}
1902
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001903void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001904 LocationSummary* locations =
1905 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001906 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001907 case Primitive::kPrimBoolean:
1908 case Primitive::kPrimByte:
1909 case Primitive::kPrimShort:
1910 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001911 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001912 case Primitive::kPrimLong: {
1913 locations->SetInAt(0, Location::RequiresRegister());
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(), Location::kNoOutputOverlap);
1916 break;
1917 }
1918 case Primitive::kPrimFloat:
1919 case Primitive::kPrimDouble: {
1920 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001921 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001922 locations->SetOut(Location::RequiresRegister());
1923 break;
1924 }
1925 default:
1926 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1927 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001928}
1929
1930void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001931 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001932 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001933 Location left = locations->InAt(0);
1934 Location right = locations->InAt(1);
1935
Mark Mendell0c9497d2015-08-21 09:30:05 -04001936 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001937 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001938 Condition less_cond = kLess;
1939
Calin Juravleddb7df22014-11-25 20:56:51 +00001940 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001941 case Primitive::kPrimBoolean:
1942 case Primitive::kPrimByte:
1943 case Primitive::kPrimShort:
1944 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001945 case Primitive::kPrimInt: {
1946 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1947 if (right.IsConstant()) {
1948 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1949 codegen_->Compare32BitValue(left_reg, value);
1950 } else if (right.IsStackSlot()) {
1951 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1952 } else {
1953 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1954 }
1955 break;
1956 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001957 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001958 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1959 if (right.IsConstant()) {
1960 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001961 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001962 } else if (right.IsDoubleStackSlot()) {
1963 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001964 } else {
1965 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1966 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001967 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001968 }
1969 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001970 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1971 if (right.IsConstant()) {
1972 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1973 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1974 } else if (right.IsStackSlot()) {
1975 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1976 } else {
1977 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1978 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001979 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001980 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001981 break;
1982 }
1983 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001984 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1985 if (right.IsConstant()) {
1986 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1987 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1988 } else if (right.IsDoubleStackSlot()) {
1989 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1990 } else {
1991 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1992 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001993 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001994 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001995 break;
1996 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001997 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001998 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001999 }
Aart Bika19616e2016-02-01 18:57:58 -08002000
Calin Juravleddb7df22014-11-25 20:56:51 +00002001 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002002 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002003 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002004
Calin Juravle91debbc2014-11-26 19:01:09 +00002005 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002006 __ movl(out, Immediate(1));
2007 __ jmp(&done);
2008
2009 __ Bind(&less);
2010 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002011
2012 __ Bind(&done);
2013}
2014
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002015void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002016 LocationSummary* locations =
2017 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002018 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002019}
2020
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002021void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002022 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002023}
2024
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002025void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2026 LocationSummary* locations =
2027 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2028 locations->SetOut(Location::ConstantLocation(constant));
2029}
2030
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002031void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002032 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002033}
2034
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002035void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002036 LocationSummary* locations =
2037 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002038 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002039}
2040
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002041void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002042 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002043}
2044
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002045void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2046 LocationSummary* locations =
2047 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2048 locations->SetOut(Location::ConstantLocation(constant));
2049}
2050
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002051void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002052 // Will be generated at use site.
2053}
2054
2055void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2056 LocationSummary* locations =
2057 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2058 locations->SetOut(Location::ConstantLocation(constant));
2059}
2060
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002061void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2062 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002063 // Will be generated at use site.
2064}
2065
Calin Juravle27df7582015-04-17 19:12:31 +01002066void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2067 memory_barrier->SetLocations(nullptr);
2068}
2069
2070void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002071 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002072}
2073
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002074void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2075 ret->SetLocations(nullptr);
2076}
2077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002078void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002079 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002080}
2081
2082void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002083 LocationSummary* locations =
2084 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002085 switch (ret->InputAt(0)->GetType()) {
2086 case Primitive::kPrimBoolean:
2087 case Primitive::kPrimByte:
2088 case Primitive::kPrimChar:
2089 case Primitive::kPrimShort:
2090 case Primitive::kPrimInt:
2091 case Primitive::kPrimNot:
2092 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002093 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002094 break;
2095
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002096 case Primitive::kPrimFloat:
2097 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002098 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002099 break;
2100
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002101 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002102 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002104}
2105
2106void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2107 if (kIsDebugBuild) {
2108 switch (ret->InputAt(0)->GetType()) {
2109 case Primitive::kPrimBoolean:
2110 case Primitive::kPrimByte:
2111 case Primitive::kPrimChar:
2112 case Primitive::kPrimShort:
2113 case Primitive::kPrimInt:
2114 case Primitive::kPrimNot:
2115 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002116 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002117 break;
2118
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002119 case Primitive::kPrimFloat:
2120 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002122 XMM0);
2123 break;
2124
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002126 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002127 }
2128 }
2129 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002130}
2131
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002132Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2133 switch (type) {
2134 case Primitive::kPrimBoolean:
2135 case Primitive::kPrimByte:
2136 case Primitive::kPrimChar:
2137 case Primitive::kPrimShort:
2138 case Primitive::kPrimInt:
2139 case Primitive::kPrimNot:
2140 case Primitive::kPrimLong:
2141 return Location::RegisterLocation(RAX);
2142
2143 case Primitive::kPrimVoid:
2144 return Location::NoLocation();
2145
2146 case Primitive::kPrimDouble:
2147 case Primitive::kPrimFloat:
2148 return Location::FpuRegisterLocation(XMM0);
2149 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002150
2151 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002152}
2153
2154Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2155 return Location::RegisterLocation(kMethodRegisterArgument);
2156}
2157
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002158Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002159 switch (type) {
2160 case Primitive::kPrimBoolean:
2161 case Primitive::kPrimByte:
2162 case Primitive::kPrimChar:
2163 case Primitive::kPrimShort:
2164 case Primitive::kPrimInt:
2165 case Primitive::kPrimNot: {
2166 uint32_t index = gp_index_++;
2167 stack_index_++;
2168 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002169 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002170 } else {
2171 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2172 }
2173 }
2174
2175 case Primitive::kPrimLong: {
2176 uint32_t index = gp_index_;
2177 stack_index_ += 2;
2178 if (index < calling_convention.GetNumberOfRegisters()) {
2179 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002180 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002181 } else {
2182 gp_index_ += 2;
2183 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2184 }
2185 }
2186
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002187 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002188 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002189 stack_index_++;
2190 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002191 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002192 } else {
2193 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2194 }
2195 }
2196
2197 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002198 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002199 stack_index_ += 2;
2200 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002201 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002202 } else {
2203 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2204 }
2205 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002206
2207 case Primitive::kPrimVoid:
2208 LOG(FATAL) << "Unexpected parameter type " << type;
2209 break;
2210 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002211 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002212}
2213
Calin Juravle175dc732015-08-25 15:42:32 +01002214void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2215 // The trampoline uses the same calling convention as dex calling conventions,
2216 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2217 // the method_idx.
2218 HandleInvoke(invoke);
2219}
2220
2221void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2222 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2223}
2224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002225void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002226 // Explicit clinit checks triggered by static invokes must have been pruned by
2227 // art::PrepareForRegisterAllocation.
2228 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002229
Mark Mendellfb8d2792015-03-31 22:16:59 -04002230 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002231 if (intrinsic.TryDispatch(invoke)) {
2232 return;
2233 }
2234
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002235 HandleInvoke(invoke);
2236}
2237
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002238static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2239 if (invoke->GetLocations()->Intrinsified()) {
2240 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2241 intrinsic.Dispatch(invoke);
2242 return true;
2243 }
2244 return false;
2245}
2246
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002247void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002248 // Explicit clinit checks triggered by static invokes must have been pruned by
2249 // art::PrepareForRegisterAllocation.
2250 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002251
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002252 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2253 return;
2254 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002255
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002256 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002257 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002258 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002259 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002260}
2261
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002262void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002263 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002264 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002265}
2266
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002267void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002268 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002269 if (intrinsic.TryDispatch(invoke)) {
2270 return;
2271 }
2272
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273 HandleInvoke(invoke);
2274}
2275
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002276void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002277 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2278 return;
2279 }
2280
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002281 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002282 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002283 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002284}
2285
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002286void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2287 HandleInvoke(invoke);
2288 // Add the hidden argument.
2289 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2290}
2291
2292void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2293 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002294 LocationSummary* locations = invoke->GetLocations();
2295 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2296 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002297 Location receiver = locations->InAt(0);
2298 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2299
Roland Levillain0d5a2812015-11-13 10:07:31 +00002300 // Set the hidden argument. This is safe to do this here, as RAX
2301 // won't be modified thereafter, before the `call` instruction.
2302 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002303 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002304
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002305 if (receiver.IsStackSlot()) {
2306 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002307 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002308 __ movl(temp, Address(temp, class_offset));
2309 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002310 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002311 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002312 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002313 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002314 // Instead of simply (possibly) unpoisoning `temp` here, we should
2315 // emit a read barrier for the previous class reference load.
2316 // However this is not required in practice, as this is an
2317 // intermediate/temporary reference and because the current
2318 // concurrent copying collector keeps the from-space memory
2319 // intact/accessible until the end of the marking phase (the
2320 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002321 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002322 // temp = temp->GetAddressOfIMT()
2323 __ movq(temp,
2324 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2325 // temp = temp->GetImtEntryAt(method_offset);
2326 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002327 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002328 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002329 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002330 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002331 __ call(Address(
2332 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002333
2334 DCHECK(!codegen_->IsLeafMethod());
2335 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2336}
2337
Roland Levillain88cb1752014-10-20 16:36:47 +01002338void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2339 LocationSummary* locations =
2340 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2341 switch (neg->GetResultType()) {
2342 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002343 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002344 locations->SetInAt(0, Location::RequiresRegister());
2345 locations->SetOut(Location::SameAsFirstInput());
2346 break;
2347
Roland Levillain88cb1752014-10-20 16:36:47 +01002348 case Primitive::kPrimFloat:
2349 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002350 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002351 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002352 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002353 break;
2354
2355 default:
2356 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2357 }
2358}
2359
2360void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2361 LocationSummary* locations = neg->GetLocations();
2362 Location out = locations->Out();
2363 Location in = locations->InAt(0);
2364 switch (neg->GetResultType()) {
2365 case Primitive::kPrimInt:
2366 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002367 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002369 break;
2370
2371 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002372 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002373 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002374 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002375 break;
2376
Roland Levillain5368c212014-11-27 15:03:41 +00002377 case Primitive::kPrimFloat: {
2378 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002379 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002380 // Implement float negation with an exclusive or with value
2381 // 0x80000000 (mask for bit 31, representing the sign of a
2382 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002383 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002384 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002385 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002386 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002387
Roland Levillain5368c212014-11-27 15:03:41 +00002388 case Primitive::kPrimDouble: {
2389 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002390 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002391 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002392 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002393 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002394 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002395 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002396 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002397 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002398
2399 default:
2400 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2401 }
2402}
2403
Roland Levillaindff1f282014-11-05 14:15:05 +00002404void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2405 LocationSummary* locations =
2406 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2407 Primitive::Type result_type = conversion->GetResultType();
2408 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002409 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002410
David Brazdilb2bd1c52015-03-25 11:17:37 +00002411 // The Java language does not allow treating boolean as an integral type but
2412 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002413
Roland Levillaindff1f282014-11-05 14:15:05 +00002414 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002415 case Primitive::kPrimByte:
2416 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002417 case Primitive::kPrimLong:
2418 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002419 case Primitive::kPrimBoolean:
2420 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002421 case Primitive::kPrimShort:
2422 case Primitive::kPrimInt:
2423 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002424 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002425 locations->SetInAt(0, Location::Any());
2426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2427 break;
2428
2429 default:
2430 LOG(FATAL) << "Unexpected type conversion from " << input_type
2431 << " to " << result_type;
2432 }
2433 break;
2434
Roland Levillain01a8d712014-11-14 16:27:39 +00002435 case Primitive::kPrimShort:
2436 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002437 case Primitive::kPrimLong:
2438 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002439 case Primitive::kPrimBoolean:
2440 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002441 case Primitive::kPrimByte:
2442 case Primitive::kPrimInt:
2443 case Primitive::kPrimChar:
2444 // Processing a Dex `int-to-short' instruction.
2445 locations->SetInAt(0, Location::Any());
2446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2447 break;
2448
2449 default:
2450 LOG(FATAL) << "Unexpected type conversion from " << input_type
2451 << " to " << result_type;
2452 }
2453 break;
2454
Roland Levillain946e1432014-11-11 17:35:19 +00002455 case Primitive::kPrimInt:
2456 switch (input_type) {
2457 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002458 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002459 locations->SetInAt(0, Location::Any());
2460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2461 break;
2462
2463 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002464 // Processing a Dex `float-to-int' instruction.
2465 locations->SetInAt(0, Location::RequiresFpuRegister());
2466 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002467 break;
2468
Roland Levillain946e1432014-11-11 17:35:19 +00002469 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002470 // Processing a Dex `double-to-int' instruction.
2471 locations->SetInAt(0, Location::RequiresFpuRegister());
2472 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002473 break;
2474
2475 default:
2476 LOG(FATAL) << "Unexpected type conversion from " << input_type
2477 << " to " << result_type;
2478 }
2479 break;
2480
Roland Levillaindff1f282014-11-05 14:15:05 +00002481 case Primitive::kPrimLong:
2482 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002483 case Primitive::kPrimBoolean:
2484 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002485 case Primitive::kPrimByte:
2486 case Primitive::kPrimShort:
2487 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002488 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002489 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002490 // TODO: We would benefit from a (to-be-implemented)
2491 // Location::RegisterOrStackSlot requirement for this input.
2492 locations->SetInAt(0, Location::RequiresRegister());
2493 locations->SetOut(Location::RequiresRegister());
2494 break;
2495
2496 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002497 // Processing a Dex `float-to-long' instruction.
2498 locations->SetInAt(0, Location::RequiresFpuRegister());
2499 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002500 break;
2501
Roland Levillaindff1f282014-11-05 14:15:05 +00002502 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002503 // Processing a Dex `double-to-long' instruction.
2504 locations->SetInAt(0, Location::RequiresFpuRegister());
2505 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 break;
2507
2508 default:
2509 LOG(FATAL) << "Unexpected type conversion from " << input_type
2510 << " to " << result_type;
2511 }
2512 break;
2513
Roland Levillain981e4542014-11-14 11:47:14 +00002514 case Primitive::kPrimChar:
2515 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002516 case Primitive::kPrimLong:
2517 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002518 case Primitive::kPrimBoolean:
2519 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002520 case Primitive::kPrimByte:
2521 case Primitive::kPrimShort:
2522 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002523 // Processing a Dex `int-to-char' instruction.
2524 locations->SetInAt(0, Location::Any());
2525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2526 break;
2527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532 break;
2533
Roland Levillaindff1f282014-11-05 14:15:05 +00002534 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002535 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002536 case Primitive::kPrimBoolean:
2537 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002538 case Primitive::kPrimByte:
2539 case Primitive::kPrimShort:
2540 case Primitive::kPrimInt:
2541 case Primitive::kPrimChar:
2542 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002543 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002544 locations->SetOut(Location::RequiresFpuRegister());
2545 break;
2546
2547 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002548 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002549 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002550 locations->SetOut(Location::RequiresFpuRegister());
2551 break;
2552
Roland Levillaincff13742014-11-17 14:32:17 +00002553 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002554 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002555 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002556 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 };
2563 break;
2564
Roland Levillaindff1f282014-11-05 14:15:05 +00002565 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002566 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002567 case Primitive::kPrimBoolean:
2568 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002569 case Primitive::kPrimByte:
2570 case Primitive::kPrimShort:
2571 case Primitive::kPrimInt:
2572 case Primitive::kPrimChar:
2573 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002574 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002575 locations->SetOut(Location::RequiresFpuRegister());
2576 break;
2577
2578 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002579 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002580 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002581 locations->SetOut(Location::RequiresFpuRegister());
2582 break;
2583
Roland Levillaincff13742014-11-17 14:32:17 +00002584 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002585 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002586 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002587 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002588 break;
2589
2590 default:
2591 LOG(FATAL) << "Unexpected type conversion from " << input_type
2592 << " to " << result_type;
2593 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002594 break;
2595
2596 default:
2597 LOG(FATAL) << "Unexpected type conversion from " << input_type
2598 << " to " << result_type;
2599 }
2600}
2601
2602void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2603 LocationSummary* locations = conversion->GetLocations();
2604 Location out = locations->Out();
2605 Location in = locations->InAt(0);
2606 Primitive::Type result_type = conversion->GetResultType();
2607 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002608 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002609 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002610 case Primitive::kPrimByte:
2611 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002612 case Primitive::kPrimLong:
2613 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002614 case Primitive::kPrimBoolean:
2615 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002616 case Primitive::kPrimShort:
2617 case Primitive::kPrimInt:
2618 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002619 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002620 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002622 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002623 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002624 Address(CpuRegister(RSP), in.GetStackIndex()));
2625 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002627 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002628 }
2629 break;
2630
2631 default:
2632 LOG(FATAL) << "Unexpected type conversion from " << input_type
2633 << " to " << result_type;
2634 }
2635 break;
2636
Roland Levillain01a8d712014-11-14 16:27:39 +00002637 case Primitive::kPrimShort:
2638 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002639 case Primitive::kPrimLong:
2640 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002641 case Primitive::kPrimBoolean:
2642 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002643 case Primitive::kPrimByte:
2644 case Primitive::kPrimInt:
2645 case Primitive::kPrimChar:
2646 // Processing a Dex `int-to-short' instruction.
2647 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002649 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002650 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002651 Address(CpuRegister(RSP), in.GetStackIndex()));
2652 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002653 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002654 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002655 }
2656 break;
2657
2658 default:
2659 LOG(FATAL) << "Unexpected type conversion from " << input_type
2660 << " to " << result_type;
2661 }
2662 break;
2663
Roland Levillain946e1432014-11-11 17:35:19 +00002664 case Primitive::kPrimInt:
2665 switch (input_type) {
2666 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002667 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002668 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002669 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002670 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002671 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002672 Address(CpuRegister(RSP), in.GetStackIndex()));
2673 } else {
2674 DCHECK(in.IsConstant());
2675 DCHECK(in.GetConstant()->IsLongConstant());
2676 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002677 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002678 }
2679 break;
2680
Roland Levillain3f8f9362014-12-02 17:45:01 +00002681 case Primitive::kPrimFloat: {
2682 // Processing a Dex `float-to-int' instruction.
2683 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2684 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002685 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002686
2687 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002688 // if input >= (float)INT_MAX goto done
2689 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002690 __ j(kAboveEqual, &done);
2691 // if input == NaN goto nan
2692 __ j(kUnordered, &nan);
2693 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002694 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002695 __ jmp(&done);
2696 __ Bind(&nan);
2697 // output = 0
2698 __ xorl(output, output);
2699 __ Bind(&done);
2700 break;
2701 }
2702
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002703 case Primitive::kPrimDouble: {
2704 // Processing a Dex `double-to-int' instruction.
2705 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2706 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002707 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002708
2709 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002710 // if input >= (double)INT_MAX goto done
2711 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002712 __ j(kAboveEqual, &done);
2713 // if input == NaN goto nan
2714 __ j(kUnordered, &nan);
2715 // output = double-to-int-truncate(input)
2716 __ cvttsd2si(output, input);
2717 __ jmp(&done);
2718 __ Bind(&nan);
2719 // output = 0
2720 __ xorl(output, output);
2721 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002722 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002723 }
Roland Levillain946e1432014-11-11 17:35:19 +00002724
2725 default:
2726 LOG(FATAL) << "Unexpected type conversion from " << input_type
2727 << " to " << result_type;
2728 }
2729 break;
2730
Roland Levillaindff1f282014-11-05 14:15:05 +00002731 case Primitive::kPrimLong:
2732 switch (input_type) {
2733 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002734 case Primitive::kPrimBoolean:
2735 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002736 case Primitive::kPrimByte:
2737 case Primitive::kPrimShort:
2738 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002739 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002740 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002741 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002743 break;
2744
Roland Levillain624279f2014-12-04 11:54:28 +00002745 case Primitive::kPrimFloat: {
2746 // Processing a Dex `float-to-long' instruction.
2747 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2748 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002749 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002750
Mark Mendell92e83bf2015-05-07 11:25:03 -04002751 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002752 // if input >= (float)LONG_MAX goto done
2753 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002754 __ j(kAboveEqual, &done);
2755 // if input == NaN goto nan
2756 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002757 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002758 __ cvttss2si(output, input, true);
2759 __ jmp(&done);
2760 __ Bind(&nan);
2761 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002762 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002763 __ Bind(&done);
2764 break;
2765 }
2766
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002767 case Primitive::kPrimDouble: {
2768 // Processing a Dex `double-to-long' instruction.
2769 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2770 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002771 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002772
Mark Mendell92e83bf2015-05-07 11:25:03 -04002773 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002774 // if input >= (double)LONG_MAX goto done
2775 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002776 __ j(kAboveEqual, &done);
2777 // if input == NaN goto nan
2778 __ j(kUnordered, &nan);
2779 // output = double-to-long-truncate(input)
2780 __ cvttsd2si(output, input, true);
2781 __ jmp(&done);
2782 __ Bind(&nan);
2783 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002784 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002785 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002786 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002787 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002788
2789 default:
2790 LOG(FATAL) << "Unexpected type conversion from " << input_type
2791 << " to " << result_type;
2792 }
2793 break;
2794
Roland Levillain981e4542014-11-14 11:47:14 +00002795 case Primitive::kPrimChar:
2796 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002797 case Primitive::kPrimLong:
2798 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002799 case Primitive::kPrimBoolean:
2800 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002801 case Primitive::kPrimByte:
2802 case Primitive::kPrimShort:
2803 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002804 // Processing a Dex `int-to-char' instruction.
2805 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002806 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002807 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002809 Address(CpuRegister(RSP), in.GetStackIndex()));
2810 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002812 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002813 }
2814 break;
2815
2816 default:
2817 LOG(FATAL) << "Unexpected type conversion from " << input_type
2818 << " to " << result_type;
2819 }
2820 break;
2821
Roland Levillaindff1f282014-11-05 14:15:05 +00002822 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002823 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002824 case Primitive::kPrimBoolean:
2825 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002826 case Primitive::kPrimByte:
2827 case Primitive::kPrimShort:
2828 case Primitive::kPrimInt:
2829 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002830 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002831 if (in.IsRegister()) {
2832 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2833 } else if (in.IsConstant()) {
2834 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2835 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002836 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002837 } else {
2838 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2839 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2840 }
Roland Levillaincff13742014-11-17 14:32:17 +00002841 break;
2842
2843 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002844 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002845 if (in.IsRegister()) {
2846 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2847 } else if (in.IsConstant()) {
2848 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2849 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002850 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002851 } else {
2852 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2853 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2854 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002855 break;
2856
Roland Levillaincff13742014-11-17 14:32:17 +00002857 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002858 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002859 if (in.IsFpuRegister()) {
2860 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2861 } else if (in.IsConstant()) {
2862 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2863 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002864 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002865 } else {
2866 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2867 Address(CpuRegister(RSP), in.GetStackIndex()));
2868 }
Roland Levillaincff13742014-11-17 14:32:17 +00002869 break;
2870
2871 default:
2872 LOG(FATAL) << "Unexpected type conversion from " << input_type
2873 << " to " << result_type;
2874 };
2875 break;
2876
Roland Levillaindff1f282014-11-05 14:15:05 +00002877 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002878 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002879 case Primitive::kPrimBoolean:
2880 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002881 case Primitive::kPrimByte:
2882 case Primitive::kPrimShort:
2883 case Primitive::kPrimInt:
2884 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002885 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002886 if (in.IsRegister()) {
2887 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2888 } else if (in.IsConstant()) {
2889 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2890 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002891 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002892 } else {
2893 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2894 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2895 }
Roland Levillaincff13742014-11-17 14:32:17 +00002896 break;
2897
2898 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002899 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002900 if (in.IsRegister()) {
2901 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2902 } else if (in.IsConstant()) {
2903 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2904 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002905 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002906 } else {
2907 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2908 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2909 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002910 break;
2911
Roland Levillaincff13742014-11-17 14:32:17 +00002912 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002913 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002914 if (in.IsFpuRegister()) {
2915 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2916 } else if (in.IsConstant()) {
2917 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2918 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002919 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002920 } else {
2921 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2922 Address(CpuRegister(RSP), in.GetStackIndex()));
2923 }
Roland Levillaincff13742014-11-17 14:32:17 +00002924 break;
2925
2926 default:
2927 LOG(FATAL) << "Unexpected type conversion from " << input_type
2928 << " to " << result_type;
2929 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002930 break;
2931
2932 default:
2933 LOG(FATAL) << "Unexpected type conversion from " << input_type
2934 << " to " << result_type;
2935 }
2936}
2937
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002938void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002939 LocationSummary* locations =
2940 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002941 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002942 case Primitive::kPrimInt: {
2943 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002944 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002946 break;
2947 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002948
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002949 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002950 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002951 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002952 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002954 break;
2955 }
2956
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002957 case Primitive::kPrimDouble:
2958 case Primitive::kPrimFloat: {
2959 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002960 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002961 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002962 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002963 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002964
2965 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002966 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002967 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002968}
2969
2970void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2971 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002972 Location first = locations->InAt(0);
2973 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002974 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002975
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002976 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002977 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002979 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2980 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002981 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2982 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002983 } else {
2984 __ leal(out.AsRegister<CpuRegister>(), Address(
2985 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2986 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002988 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2989 __ addl(out.AsRegister<CpuRegister>(),
2990 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2991 } else {
2992 __ leal(out.AsRegister<CpuRegister>(), Address(
2993 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2994 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002995 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002996 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002997 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002998 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002999 break;
3000 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003001
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003002 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003003 if (second.IsRegister()) {
3004 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3005 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003006 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3007 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003008 } else {
3009 __ leaq(out.AsRegister<CpuRegister>(), Address(
3010 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3011 }
3012 } else {
3013 DCHECK(second.IsConstant());
3014 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3015 int32_t int32_value = Low32Bits(value);
3016 DCHECK_EQ(int32_value, value);
3017 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3018 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3019 } else {
3020 __ leaq(out.AsRegister<CpuRegister>(), Address(
3021 first.AsRegister<CpuRegister>(), int32_value));
3022 }
3023 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003024 break;
3025 }
3026
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003027 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003028 if (second.IsFpuRegister()) {
3029 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3030 } else if (second.IsConstant()) {
3031 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003032 codegen_->LiteralFloatAddress(
3033 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003034 } else {
3035 DCHECK(second.IsStackSlot());
3036 __ addss(first.AsFpuRegister<XmmRegister>(),
3037 Address(CpuRegister(RSP), second.GetStackIndex()));
3038 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003040 }
3041
3042 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003043 if (second.IsFpuRegister()) {
3044 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3045 } else if (second.IsConstant()) {
3046 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003047 codegen_->LiteralDoubleAddress(
3048 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003049 } else {
3050 DCHECK(second.IsDoubleStackSlot());
3051 __ addsd(first.AsFpuRegister<XmmRegister>(),
3052 Address(CpuRegister(RSP), second.GetStackIndex()));
3053 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003054 break;
3055 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003056
3057 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003059 }
3060}
3061
3062void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003063 LocationSummary* locations =
3064 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003065 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003066 case Primitive::kPrimInt: {
3067 locations->SetInAt(0, Location::RequiresRegister());
3068 locations->SetInAt(1, Location::Any());
3069 locations->SetOut(Location::SameAsFirstInput());
3070 break;
3071 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003072 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003073 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003074 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003075 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003076 break;
3077 }
Calin Juravle11351682014-10-23 15:38:15 +01003078 case Primitive::kPrimFloat:
3079 case Primitive::kPrimDouble: {
3080 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003081 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003082 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003083 break;
Calin Juravle11351682014-10-23 15:38:15 +01003084 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003085 default:
Calin Juravle11351682014-10-23 15:38:15 +01003086 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003087 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003088}
3089
3090void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3091 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003092 Location first = locations->InAt(0);
3093 Location second = locations->InAt(1);
3094 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003095 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003096 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003097 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003098 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003099 } else if (second.IsConstant()) {
3100 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003102 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003103 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003104 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003105 break;
3106 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003107 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003108 if (second.IsConstant()) {
3109 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3110 DCHECK(IsInt<32>(value));
3111 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3112 } else {
3113 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3114 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003115 break;
3116 }
3117
Calin Juravle11351682014-10-23 15:38:15 +01003118 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003119 if (second.IsFpuRegister()) {
3120 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3121 } else if (second.IsConstant()) {
3122 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003123 codegen_->LiteralFloatAddress(
3124 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003125 } else {
3126 DCHECK(second.IsStackSlot());
3127 __ subss(first.AsFpuRegister<XmmRegister>(),
3128 Address(CpuRegister(RSP), second.GetStackIndex()));
3129 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003130 break;
Calin Juravle11351682014-10-23 15:38:15 +01003131 }
3132
3133 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003134 if (second.IsFpuRegister()) {
3135 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3136 } else if (second.IsConstant()) {
3137 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003138 codegen_->LiteralDoubleAddress(
3139 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003140 } else {
3141 DCHECK(second.IsDoubleStackSlot());
3142 __ subsd(first.AsFpuRegister<XmmRegister>(),
3143 Address(CpuRegister(RSP), second.GetStackIndex()));
3144 }
Calin Juravle11351682014-10-23 15:38:15 +01003145 break;
3146 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003147
3148 default:
Calin Juravle11351682014-10-23 15:38:15 +01003149 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003150 }
3151}
3152
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3154 LocationSummary* locations =
3155 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3156 switch (mul->GetResultType()) {
3157 case Primitive::kPrimInt: {
3158 locations->SetInAt(0, Location::RequiresRegister());
3159 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003160 if (mul->InputAt(1)->IsIntConstant()) {
3161 // Can use 3 operand multiply.
3162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3163 } else {
3164 locations->SetOut(Location::SameAsFirstInput());
3165 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003166 break;
3167 }
3168 case Primitive::kPrimLong: {
3169 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003170 locations->SetInAt(1, Location::Any());
3171 if (mul->InputAt(1)->IsLongConstant() &&
3172 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003173 // Can use 3 operand multiply.
3174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3175 } else {
3176 locations->SetOut(Location::SameAsFirstInput());
3177 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 break;
3179 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003180 case Primitive::kPrimFloat:
3181 case Primitive::kPrimDouble: {
3182 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003183 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003184 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003185 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003186 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187
3188 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003189 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003190 }
3191}
3192
3193void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3194 LocationSummary* locations = mul->GetLocations();
3195 Location first = locations->InAt(0);
3196 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003197 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003198 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003199 case Primitive::kPrimInt:
3200 // The constant may have ended up in a register, so test explicitly to avoid
3201 // problems where the output may not be the same as the first operand.
3202 if (mul->InputAt(1)->IsIntConstant()) {
3203 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3204 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3205 } else if (second.IsRegister()) {
3206 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003208 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003209 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003210 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003211 __ imull(first.AsRegister<CpuRegister>(),
3212 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003213 }
3214 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003216 // The constant may have ended up in a register, so test explicitly to avoid
3217 // problems where the output may not be the same as the first operand.
3218 if (mul->InputAt(1)->IsLongConstant()) {
3219 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3220 if (IsInt<32>(value)) {
3221 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3222 Immediate(static_cast<int32_t>(value)));
3223 } else {
3224 // Have to use the constant area.
3225 DCHECK(first.Equals(out));
3226 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3227 }
3228 } else if (second.IsRegister()) {
3229 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003230 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003231 } else {
3232 DCHECK(second.IsDoubleStackSlot());
3233 DCHECK(first.Equals(out));
3234 __ imulq(first.AsRegister<CpuRegister>(),
3235 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003236 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237 break;
3238 }
3239
Calin Juravleb5bfa962014-10-21 18:02:24 +01003240 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003241 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003242 if (second.IsFpuRegister()) {
3243 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3244 } else if (second.IsConstant()) {
3245 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003246 codegen_->LiteralFloatAddress(
3247 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003248 } else {
3249 DCHECK(second.IsStackSlot());
3250 __ mulss(first.AsFpuRegister<XmmRegister>(),
3251 Address(CpuRegister(RSP), second.GetStackIndex()));
3252 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003254 }
3255
3256 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003257 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003258 if (second.IsFpuRegister()) {
3259 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3260 } else if (second.IsConstant()) {
3261 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003262 codegen_->LiteralDoubleAddress(
3263 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003264 } else {
3265 DCHECK(second.IsDoubleStackSlot());
3266 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3267 Address(CpuRegister(RSP), second.GetStackIndex()));
3268 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003269 break;
3270 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003271
3272 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003273 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003274 }
3275}
3276
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003277void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3278 uint32_t stack_adjustment, bool is_float) {
3279 if (source.IsStackSlot()) {
3280 DCHECK(is_float);
3281 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3282 } else if (source.IsDoubleStackSlot()) {
3283 DCHECK(!is_float);
3284 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3285 } else {
3286 // Write the value to the temporary location on the stack and load to FP stack.
3287 if (is_float) {
3288 Location stack_temp = Location::StackSlot(temp_offset);
3289 codegen_->Move(stack_temp, source);
3290 __ flds(Address(CpuRegister(RSP), temp_offset));
3291 } else {
3292 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3293 codegen_->Move(stack_temp, source);
3294 __ fldl(Address(CpuRegister(RSP), temp_offset));
3295 }
3296 }
3297}
3298
3299void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3300 Primitive::Type type = rem->GetResultType();
3301 bool is_float = type == Primitive::kPrimFloat;
3302 size_t elem_size = Primitive::ComponentSize(type);
3303 LocationSummary* locations = rem->GetLocations();
3304 Location first = locations->InAt(0);
3305 Location second = locations->InAt(1);
3306 Location out = locations->Out();
3307
3308 // Create stack space for 2 elements.
3309 // TODO: enhance register allocator to ask for stack temporaries.
3310 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3311
3312 // Load the values to the FP stack in reverse order, using temporaries if needed.
3313 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3314 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3315
3316 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003317 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003318 __ Bind(&retry);
3319 __ fprem();
3320
3321 // Move FP status to AX.
3322 __ fstsw();
3323
3324 // And see if the argument reduction is complete. This is signaled by the
3325 // C2 FPU flag bit set to 0.
3326 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3327 __ j(kNotEqual, &retry);
3328
3329 // We have settled on the final value. Retrieve it into an XMM register.
3330 // Store FP top of stack to real stack.
3331 if (is_float) {
3332 __ fsts(Address(CpuRegister(RSP), 0));
3333 } else {
3334 __ fstl(Address(CpuRegister(RSP), 0));
3335 }
3336
3337 // Pop the 2 items from the FP stack.
3338 __ fucompp();
3339
3340 // Load the value from the stack into an XMM register.
3341 DCHECK(out.IsFpuRegister()) << out;
3342 if (is_float) {
3343 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3344 } else {
3345 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3346 }
3347
3348 // And remove the temporary stack space we allocated.
3349 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3350}
3351
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3353 DCHECK(instruction->IsDiv() || instruction->IsRem());
3354
3355 LocationSummary* locations = instruction->GetLocations();
3356 Location second = locations->InAt(1);
3357 DCHECK(second.IsConstant());
3358
3359 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3360 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003361 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003362
3363 DCHECK(imm == 1 || imm == -1);
3364
3365 switch (instruction->GetResultType()) {
3366 case Primitive::kPrimInt: {
3367 if (instruction->IsRem()) {
3368 __ xorl(output_register, output_register);
3369 } else {
3370 __ movl(output_register, input_register);
3371 if (imm == -1) {
3372 __ negl(output_register);
3373 }
3374 }
3375 break;
3376 }
3377
3378 case Primitive::kPrimLong: {
3379 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003380 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003381 } else {
3382 __ movq(output_register, input_register);
3383 if (imm == -1) {
3384 __ negq(output_register);
3385 }
3386 }
3387 break;
3388 }
3389
3390 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003391 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003392 }
3393}
3394
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003395void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003396 LocationSummary* locations = instruction->GetLocations();
3397 Location second = locations->InAt(1);
3398
3399 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3400 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3401
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003402 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003403 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3404 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003405
3406 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3407
3408 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003409 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003410 __ testl(numerator, numerator);
3411 __ cmov(kGreaterEqual, tmp, numerator);
3412 int shift = CTZ(imm);
3413 __ sarl(tmp, Immediate(shift));
3414
3415 if (imm < 0) {
3416 __ negl(tmp);
3417 }
3418
3419 __ movl(output_register, tmp);
3420 } else {
3421 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3422 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3423
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003424 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425 __ addq(rdx, numerator);
3426 __ testq(numerator, numerator);
3427 __ cmov(kGreaterEqual, rdx, numerator);
3428 int shift = CTZ(imm);
3429 __ sarq(rdx, Immediate(shift));
3430
3431 if (imm < 0) {
3432 __ negq(rdx);
3433 }
3434
3435 __ movq(output_register, rdx);
3436 }
3437}
3438
3439void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3440 DCHECK(instruction->IsDiv() || instruction->IsRem());
3441
3442 LocationSummary* locations = instruction->GetLocations();
3443 Location second = locations->InAt(1);
3444
3445 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3446 : locations->GetTemp(0).AsRegister<CpuRegister>();
3447 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3448 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3449 : locations->Out().AsRegister<CpuRegister>();
3450 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3451
3452 DCHECK_EQ(RAX, eax.AsRegister());
3453 DCHECK_EQ(RDX, edx.AsRegister());
3454 if (instruction->IsDiv()) {
3455 DCHECK_EQ(RAX, out.AsRegister());
3456 } else {
3457 DCHECK_EQ(RDX, out.AsRegister());
3458 }
3459
3460 int64_t magic;
3461 int shift;
3462
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003463 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464 if (instruction->GetResultType() == Primitive::kPrimInt) {
3465 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3466
3467 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3468
3469 __ movl(numerator, eax);
3470
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003471 __ movl(eax, Immediate(magic));
3472 __ imull(numerator);
3473
3474 if (imm > 0 && magic < 0) {
3475 __ addl(edx, numerator);
3476 } else if (imm < 0 && magic > 0) {
3477 __ subl(edx, numerator);
3478 }
3479
3480 if (shift != 0) {
3481 __ sarl(edx, Immediate(shift));
3482 }
3483
3484 __ movl(eax, edx);
3485 __ shrl(edx, Immediate(31));
3486 __ addl(edx, eax);
3487
3488 if (instruction->IsRem()) {
3489 __ movl(eax, numerator);
3490 __ imull(edx, Immediate(imm));
3491 __ subl(eax, edx);
3492 __ movl(edx, eax);
3493 } else {
3494 __ movl(eax, edx);
3495 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003496 } else {
3497 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3498
3499 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3500
3501 CpuRegister rax = eax;
3502 CpuRegister rdx = edx;
3503
3504 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3505
3506 // Save the numerator.
3507 __ movq(numerator, rax);
3508
3509 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003510 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003511
3512 // RDX:RAX = magic * numerator
3513 __ imulq(numerator);
3514
3515 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003516 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517 __ addq(rdx, numerator);
3518 } else if (imm < 0 && magic > 0) {
3519 // RDX -= numerator
3520 __ subq(rdx, numerator);
3521 }
3522
3523 // Shift if needed.
3524 if (shift != 0) {
3525 __ sarq(rdx, Immediate(shift));
3526 }
3527
3528 // RDX += 1 if RDX < 0
3529 __ movq(rax, rdx);
3530 __ shrq(rdx, Immediate(63));
3531 __ addq(rdx, rax);
3532
3533 if (instruction->IsRem()) {
3534 __ movq(rax, numerator);
3535
3536 if (IsInt<32>(imm)) {
3537 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3538 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003539 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 }
3541
3542 __ subq(rax, rdx);
3543 __ movq(rdx, rax);
3544 } else {
3545 __ movq(rax, rdx);
3546 }
3547 }
3548}
3549
Calin Juravlebacfec32014-11-14 15:54:36 +00003550void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3551 DCHECK(instruction->IsDiv() || instruction->IsRem());
3552 Primitive::Type type = instruction->GetResultType();
3553 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3554
3555 bool is_div = instruction->IsDiv();
3556 LocationSummary* locations = instruction->GetLocations();
3557
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003558 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3559 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003560
Roland Levillain271ab9c2014-11-27 15:23:57 +00003561 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003562 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003563
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003565 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003566
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003567 if (imm == 0) {
3568 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3569 } else if (imm == 1 || imm == -1) {
3570 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003571 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003572 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003573 } else {
3574 DCHECK(imm <= -2 || imm >= 2);
3575 GenerateDivRemWithAnyConstant(instruction);
3576 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003577 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003578 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003579 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003580 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003582
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3584 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3585 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3586 // so it's safe to just use negl instead of more complex comparisons.
3587 if (type == Primitive::kPrimInt) {
3588 __ cmpl(second_reg, Immediate(-1));
3589 __ j(kEqual, slow_path->GetEntryLabel());
3590 // edx:eax <- sign-extended of eax
3591 __ cdq();
3592 // eax = quotient, edx = remainder
3593 __ idivl(second_reg);
3594 } else {
3595 __ cmpq(second_reg, Immediate(-1));
3596 __ j(kEqual, slow_path->GetEntryLabel());
3597 // rdx:rax <- sign-extended of rax
3598 __ cqo();
3599 // rax = quotient, rdx = remainder
3600 __ idivq(second_reg);
3601 }
3602 __ Bind(slow_path->GetExitLabel());
3603 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003604}
3605
Calin Juravle7c4954d2014-10-28 16:57:40 +00003606void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3607 LocationSummary* locations =
3608 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3609 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003610 case Primitive::kPrimInt:
3611 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003612 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003614 locations->SetOut(Location::SameAsFirstInput());
3615 // Intel uses edx:eax as the dividend.
3616 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003617 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3618 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3619 // output and request another temp.
3620 if (div->InputAt(1)->IsConstant()) {
3621 locations->AddTemp(Location::RequiresRegister());
3622 }
Calin Juravled0d48522014-11-04 16:40:20 +00003623 break;
3624 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003625
Calin Juravle7c4954d2014-10-28 16:57:40 +00003626 case Primitive::kPrimFloat:
3627 case Primitive::kPrimDouble: {
3628 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003629 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003630 locations->SetOut(Location::SameAsFirstInput());
3631 break;
3632 }
3633
3634 default:
3635 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3636 }
3637}
3638
3639void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3640 LocationSummary* locations = div->GetLocations();
3641 Location first = locations->InAt(0);
3642 Location second = locations->InAt(1);
3643 DCHECK(first.Equals(locations->Out()));
3644
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003645 Primitive::Type type = div->GetResultType();
3646 switch (type) {
3647 case Primitive::kPrimInt:
3648 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003649 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003650 break;
3651 }
3652
Calin Juravle7c4954d2014-10-28 16:57:40 +00003653 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003654 if (second.IsFpuRegister()) {
3655 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3656 } else if (second.IsConstant()) {
3657 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003658 codegen_->LiteralFloatAddress(
3659 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003660 } else {
3661 DCHECK(second.IsStackSlot());
3662 __ divss(first.AsFpuRegister<XmmRegister>(),
3663 Address(CpuRegister(RSP), second.GetStackIndex()));
3664 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003665 break;
3666 }
3667
3668 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003669 if (second.IsFpuRegister()) {
3670 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3671 } else if (second.IsConstant()) {
3672 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003673 codegen_->LiteralDoubleAddress(
3674 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003675 } else {
3676 DCHECK(second.IsDoubleStackSlot());
3677 __ divsd(first.AsFpuRegister<XmmRegister>(),
3678 Address(CpuRegister(RSP), second.GetStackIndex()));
3679 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003680 break;
3681 }
3682
3683 default:
3684 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3685 }
3686}
3687
Calin Juravlebacfec32014-11-14 15:54:36 +00003688void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003689 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003690 LocationSummary* locations =
3691 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003692
3693 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003694 case Primitive::kPrimInt:
3695 case Primitive::kPrimLong: {
3696 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003697 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003698 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3699 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003700 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3701 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3702 // output and request another temp.
3703 if (rem->InputAt(1)->IsConstant()) {
3704 locations->AddTemp(Location::RequiresRegister());
3705 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003706 break;
3707 }
3708
3709 case Primitive::kPrimFloat:
3710 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003711 locations->SetInAt(0, Location::Any());
3712 locations->SetInAt(1, Location::Any());
3713 locations->SetOut(Location::RequiresFpuRegister());
3714 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003715 break;
3716 }
3717
3718 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003719 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003720 }
3721}
3722
3723void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3724 Primitive::Type type = rem->GetResultType();
3725 switch (type) {
3726 case Primitive::kPrimInt:
3727 case Primitive::kPrimLong: {
3728 GenerateDivRemIntegral(rem);
3729 break;
3730 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003731 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003732 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003733 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003734 break;
3735 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003736 default:
3737 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3738 }
3739}
3740
Calin Juravled0d48522014-11-04 16:40:20 +00003741void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003742 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3743 ? LocationSummary::kCallOnSlowPath
3744 : LocationSummary::kNoCall;
3745 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003746 locations->SetInAt(0, Location::Any());
3747 if (instruction->HasUses()) {
3748 locations->SetOut(Location::SameAsFirstInput());
3749 }
3750}
3751
3752void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003753 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003754 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3755 codegen_->AddSlowPath(slow_path);
3756
3757 LocationSummary* locations = instruction->GetLocations();
3758 Location value = locations->InAt(0);
3759
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003760 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003761 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003762 case Primitive::kPrimByte:
3763 case Primitive::kPrimChar:
3764 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003765 case Primitive::kPrimInt: {
3766 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003767 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003768 __ j(kEqual, slow_path->GetEntryLabel());
3769 } else if (value.IsStackSlot()) {
3770 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3771 __ j(kEqual, slow_path->GetEntryLabel());
3772 } else {
3773 DCHECK(value.IsConstant()) << value;
3774 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3775 __ jmp(slow_path->GetEntryLabel());
3776 }
3777 }
3778 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003779 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003780 case Primitive::kPrimLong: {
3781 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003783 __ j(kEqual, slow_path->GetEntryLabel());
3784 } else if (value.IsDoubleStackSlot()) {
3785 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3786 __ j(kEqual, slow_path->GetEntryLabel());
3787 } else {
3788 DCHECK(value.IsConstant()) << value;
3789 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3790 __ jmp(slow_path->GetEntryLabel());
3791 }
3792 }
3793 break;
3794 }
3795 default:
3796 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003797 }
Calin Juravled0d48522014-11-04 16:40:20 +00003798}
3799
Calin Juravle9aec02f2014-11-18 23:06:35 +00003800void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3801 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3802
3803 LocationSummary* locations =
3804 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3805
3806 switch (op->GetResultType()) {
3807 case Primitive::kPrimInt:
3808 case Primitive::kPrimLong: {
3809 locations->SetInAt(0, Location::RequiresRegister());
3810 // The shift count needs to be in CL.
3811 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3812 locations->SetOut(Location::SameAsFirstInput());
3813 break;
3814 }
3815 default:
3816 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3817 }
3818}
3819
3820void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3821 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3822
3823 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003824 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003825 Location second = locations->InAt(1);
3826
3827 switch (op->GetResultType()) {
3828 case Primitive::kPrimInt: {
3829 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003830 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831 if (op->IsShl()) {
3832 __ shll(first_reg, second_reg);
3833 } else if (op->IsShr()) {
3834 __ sarl(first_reg, second_reg);
3835 } else {
3836 __ shrl(first_reg, second_reg);
3837 }
3838 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003839 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003840 if (op->IsShl()) {
3841 __ shll(first_reg, imm);
3842 } else if (op->IsShr()) {
3843 __ sarl(first_reg, imm);
3844 } else {
3845 __ shrl(first_reg, imm);
3846 }
3847 }
3848 break;
3849 }
3850 case Primitive::kPrimLong: {
3851 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003853 if (op->IsShl()) {
3854 __ shlq(first_reg, second_reg);
3855 } else if (op->IsShr()) {
3856 __ sarq(first_reg, second_reg);
3857 } else {
3858 __ shrq(first_reg, second_reg);
3859 }
3860 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003861 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003862 if (op->IsShl()) {
3863 __ shlq(first_reg, imm);
3864 } else if (op->IsShr()) {
3865 __ sarq(first_reg, imm);
3866 } else {
3867 __ shrq(first_reg, imm);
3868 }
3869 }
3870 break;
3871 }
3872 default:
3873 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003874 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003875 }
3876}
3877
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003878void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3879 LocationSummary* locations =
3880 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3881
3882 switch (ror->GetResultType()) {
3883 case Primitive::kPrimInt:
3884 case Primitive::kPrimLong: {
3885 locations->SetInAt(0, Location::RequiresRegister());
3886 // The shift count needs to be in CL (unless it is a constant).
3887 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3888 locations->SetOut(Location::SameAsFirstInput());
3889 break;
3890 }
3891 default:
3892 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3893 UNREACHABLE();
3894 }
3895}
3896
3897void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3898 LocationSummary* locations = ror->GetLocations();
3899 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3900 Location second = locations->InAt(1);
3901
3902 switch (ror->GetResultType()) {
3903 case Primitive::kPrimInt:
3904 if (second.IsRegister()) {
3905 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3906 __ rorl(first_reg, second_reg);
3907 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003908 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003909 __ rorl(first_reg, imm);
3910 }
3911 break;
3912 case Primitive::kPrimLong:
3913 if (second.IsRegister()) {
3914 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3915 __ rorq(first_reg, second_reg);
3916 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003917 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003918 __ rorq(first_reg, imm);
3919 }
3920 break;
3921 default:
3922 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3923 UNREACHABLE();
3924 }
3925}
3926
Calin Juravle9aec02f2014-11-18 23:06:35 +00003927void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3928 HandleShift(shl);
3929}
3930
3931void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3932 HandleShift(shl);
3933}
3934
3935void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3936 HandleShift(shr);
3937}
3938
3939void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3940 HandleShift(shr);
3941}
3942
3943void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3944 HandleShift(ushr);
3945}
3946
3947void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3948 HandleShift(ushr);
3949}
3950
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003951void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003952 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003954 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003955 if (instruction->IsStringAlloc()) {
3956 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3957 } else {
3958 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3959 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3960 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003961 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003962}
3963
3964void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003965 // Note: if heap poisoning is enabled, the entry point takes cares
3966 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003967 if (instruction->IsStringAlloc()) {
3968 // String is allocated through StringFactory. Call NewEmptyString entry point.
3969 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003970 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003971 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3972 __ call(Address(temp, code_offset.SizeValue()));
3973 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3974 } else {
3975 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3976 instruction,
3977 instruction->GetDexPc(),
3978 nullptr);
3979 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3980 DCHECK(!codegen_->IsLeafMethod());
3981 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003982}
3983
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003984void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3985 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987 InvokeRuntimeCallingConvention calling_convention;
3988 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003989 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003990 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003991 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003992}
3993
3994void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3995 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003996 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3997 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003998 // Note: if heap poisoning is enabled, the entry point takes cares
3999 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004000 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4001 instruction,
4002 instruction->GetDexPc(),
4003 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004004 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004005
4006 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004007}
4008
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004009void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004010 LocationSummary* locations =
4011 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004012 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4013 if (location.IsStackSlot()) {
4014 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4015 } else if (location.IsDoubleStackSlot()) {
4016 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4017 }
4018 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004019}
4020
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004021void InstructionCodeGeneratorX86_64::VisitParameterValue(
4022 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004023 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004024}
4025
4026void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4027 LocationSummary* locations =
4028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4029 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4030}
4031
4032void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4033 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4034 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004035}
4036
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004037void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4038 LocationSummary* locations =
4039 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4040 locations->SetInAt(0, Location::RequiresRegister());
4041 locations->SetOut(Location::RequiresRegister());
4042}
4043
4044void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4045 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004046 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004047 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004048 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004049 __ movq(locations->Out().AsRegister<CpuRegister>(),
4050 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004051 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004052 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004053 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004054 __ movq(locations->Out().AsRegister<CpuRegister>(),
4055 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4056 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004057 __ movq(locations->Out().AsRegister<CpuRegister>(),
4058 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004059 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004060}
4061
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004062void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004063 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004064 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004065 locations->SetInAt(0, Location::RequiresRegister());
4066 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004067}
4068
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004069void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4070 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4072 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004073 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004074 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004075 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004076 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004077 break;
4078
4079 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004080 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004081 break;
4082
4083 default:
4084 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4085 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004086}
4087
David Brazdil66d126e2015-04-03 16:02:44 +01004088void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4089 LocationSummary* locations =
4090 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4091 locations->SetInAt(0, Location::RequiresRegister());
4092 locations->SetOut(Location::SameAsFirstInput());
4093}
4094
4095void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004096 LocationSummary* locations = bool_not->GetLocations();
4097 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4098 locations->Out().AsRegister<CpuRegister>().AsRegister());
4099 Location out = locations->Out();
4100 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4101}
4102
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004103void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004104 LocationSummary* locations =
4105 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004106 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004107 locations->SetInAt(i, Location::Any());
4108 }
4109 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004110}
4111
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004112void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004113 LOG(FATAL) << "Unimplemented";
4114}
4115
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004116void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004117 /*
4118 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004119 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004120 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4121 */
4122 switch (kind) {
4123 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004124 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004125 break;
4126 }
4127 case MemBarrierKind::kAnyStore:
4128 case MemBarrierKind::kLoadAny:
4129 case MemBarrierKind::kStoreStore: {
4130 // nop
4131 break;
4132 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004133 case MemBarrierKind::kNTStoreStore:
4134 // Non-Temporal Store/Store needs an explicit fence.
4135 MemoryFence(/* non-temporal */ true);
4136 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004137 }
4138}
4139
4140void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4141 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4142
Roland Levillain0d5a2812015-11-13 10:07:31 +00004143 bool object_field_get_with_read_barrier =
4144 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004145 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004146 new (GetGraph()->GetArena()) LocationSummary(instruction,
4147 object_field_get_with_read_barrier ?
4148 LocationSummary::kCallOnSlowPath :
4149 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004150 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004151 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4152 locations->SetOut(Location::RequiresFpuRegister());
4153 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004154 // The output overlaps for an object field get when read barriers
4155 // are enabled: we do not want the move to overwrite the object's
4156 // location, as we need it to emit the read barrier.
4157 locations->SetOut(
4158 Location::RequiresRegister(),
4159 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004160 }
Calin Juravle52c48962014-12-16 17:02:57 +00004161}
4162
4163void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4164 const FieldInfo& field_info) {
4165 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4166
4167 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004168 Location base_loc = locations->InAt(0);
4169 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004170 Location out = locations->Out();
4171 bool is_volatile = field_info.IsVolatile();
4172 Primitive::Type field_type = field_info.GetFieldType();
4173 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4174
4175 switch (field_type) {
4176 case Primitive::kPrimBoolean: {
4177 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4178 break;
4179 }
4180
4181 case Primitive::kPrimByte: {
4182 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4183 break;
4184 }
4185
4186 case Primitive::kPrimShort: {
4187 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4188 break;
4189 }
4190
4191 case Primitive::kPrimChar: {
4192 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4193 break;
4194 }
4195
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004196 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004197 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4198 break;
4199 }
4200
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004201 case Primitive::kPrimNot: {
4202 // /* HeapReference<Object> */ out = *(base + offset)
4203 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004204 // Note that a potential implicit null check is handled in this
4205 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4206 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004207 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004208 if (is_volatile) {
4209 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4210 }
4211 } else {
4212 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4213 codegen_->MaybeRecordImplicitNullCheck(instruction);
4214 if (is_volatile) {
4215 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4216 }
4217 // If read barriers are enabled, emit read barriers other than
4218 // Baker's using a slow path (and also unpoison the loaded
4219 // reference, if heap poisoning is enabled).
4220 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4221 }
4222 break;
4223 }
4224
Calin Juravle52c48962014-12-16 17:02:57 +00004225 case Primitive::kPrimLong: {
4226 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4227 break;
4228 }
4229
4230 case Primitive::kPrimFloat: {
4231 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4232 break;
4233 }
4234
4235 case Primitive::kPrimDouble: {
4236 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4237 break;
4238 }
4239
4240 case Primitive::kPrimVoid:
4241 LOG(FATAL) << "Unreachable type " << field_type;
4242 UNREACHABLE();
4243 }
4244
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004245 if (field_type == Primitive::kPrimNot) {
4246 // Potential implicit null checks, in the case of reference
4247 // fields, are handled in the previous switch statement.
4248 } else {
4249 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004250 }
Roland Levillain4d027112015-07-01 15:41:14 +01004251
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004252 if (is_volatile) {
4253 if (field_type == Primitive::kPrimNot) {
4254 // Memory barriers, in the case of references, are also handled
4255 // in the previous switch statement.
4256 } else {
4257 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4258 }
Roland Levillain4d027112015-07-01 15:41:14 +01004259 }
Calin Juravle52c48962014-12-16 17:02:57 +00004260}
4261
4262void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4263 const FieldInfo& field_info) {
4264 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4265
4266 LocationSummary* locations =
4267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004268 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004269 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004270 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004271 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004272
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004273 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004274 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004275 if (is_volatile) {
4276 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4277 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4278 } else {
4279 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4280 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004281 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004282 if (is_volatile) {
4283 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4284 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4285 } else {
4286 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4287 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004288 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004289 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004290 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004291 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004292 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004293 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4294 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004295 locations->AddTemp(Location::RequiresRegister());
4296 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297}
4298
Calin Juravle52c48962014-12-16 17:02:57 +00004299void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004300 const FieldInfo& field_info,
4301 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004302 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4303
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004305 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4306 Location value = locations->InAt(1);
4307 bool is_volatile = field_info.IsVolatile();
4308 Primitive::Type field_type = field_info.GetFieldType();
4309 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4310
4311 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004312 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004313 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004314
Mark Mendellea5af682015-10-22 17:35:49 -04004315 bool maybe_record_implicit_null_check_done = false;
4316
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317 switch (field_type) {
4318 case Primitive::kPrimBoolean:
4319 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004320 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004321 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004322 __ movb(Address(base, offset), Immediate(v));
4323 } else {
4324 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4325 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004326 break;
4327 }
4328
4329 case Primitive::kPrimShort:
4330 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004331 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004332 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004333 __ movw(Address(base, offset), Immediate(v));
4334 } else {
4335 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4336 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004337 break;
4338 }
4339
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004342 if (value.IsConstant()) {
4343 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004344 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4345 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4346 // Note: if heap poisoning is enabled, no need to poison
4347 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004348 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004349 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004350 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4351 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4352 __ movl(temp, value.AsRegister<CpuRegister>());
4353 __ PoisonHeapReference(temp);
4354 __ movl(Address(base, offset), temp);
4355 } else {
4356 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4357 }
Mark Mendell40741f32015-04-20 22:10:34 -04004358 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359 break;
4360 }
4361
4362 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004363 if (value.IsConstant()) {
4364 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004365 codegen_->MoveInt64ToAddress(Address(base, offset),
4366 Address(base, offset + sizeof(int32_t)),
4367 v,
4368 instruction);
4369 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004370 } else {
4371 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4372 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373 break;
4374 }
4375
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004376 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004377 if (value.IsConstant()) {
4378 int32_t v =
4379 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4380 __ movl(Address(base, offset), Immediate(v));
4381 } else {
4382 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4383 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004384 break;
4385 }
4386
4387 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004388 if (value.IsConstant()) {
4389 int64_t v =
4390 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4391 codegen_->MoveInt64ToAddress(Address(base, offset),
4392 Address(base, offset + sizeof(int32_t)),
4393 v,
4394 instruction);
4395 maybe_record_implicit_null_check_done = true;
4396 } else {
4397 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4398 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004399 break;
4400 }
4401
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004402 case Primitive::kPrimVoid:
4403 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004404 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004405 }
Calin Juravle52c48962014-12-16 17:02:57 +00004406
Mark Mendellea5af682015-10-22 17:35:49 -04004407 if (!maybe_record_implicit_null_check_done) {
4408 codegen_->MaybeRecordImplicitNullCheck(instruction);
4409 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004410
4411 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4412 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4413 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004414 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004415 }
4416
Calin Juravle52c48962014-12-16 17:02:57 +00004417 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004418 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004419 }
4420}
4421
4422void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4423 HandleFieldSet(instruction, instruction->GetFieldInfo());
4424}
4425
4426void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004427 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428}
4429
4430void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004431 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432}
4433
4434void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004435 HandleFieldGet(instruction, instruction->GetFieldInfo());
4436}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004437
Calin Juravle52c48962014-12-16 17:02:57 +00004438void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4439 HandleFieldGet(instruction);
4440}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441
Calin Juravle52c48962014-12-16 17:02:57 +00004442void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4443 HandleFieldGet(instruction, instruction->GetFieldInfo());
4444}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445
Calin Juravle52c48962014-12-16 17:02:57 +00004446void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4447 HandleFieldSet(instruction, instruction->GetFieldInfo());
4448}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449
Calin Juravle52c48962014-12-16 17:02:57 +00004450void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004451 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004452}
4453
Calin Juravlee460d1d2015-09-29 04:52:17 +01004454void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4455 HUnresolvedInstanceFieldGet* instruction) {
4456 FieldAccessCallingConventionX86_64 calling_convention;
4457 codegen_->CreateUnresolvedFieldLocationSummary(
4458 instruction, instruction->GetFieldType(), calling_convention);
4459}
4460
4461void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4462 HUnresolvedInstanceFieldGet* instruction) {
4463 FieldAccessCallingConventionX86_64 calling_convention;
4464 codegen_->GenerateUnresolvedFieldAccess(instruction,
4465 instruction->GetFieldType(),
4466 instruction->GetFieldIndex(),
4467 instruction->GetDexPc(),
4468 calling_convention);
4469}
4470
4471void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4472 HUnresolvedInstanceFieldSet* instruction) {
4473 FieldAccessCallingConventionX86_64 calling_convention;
4474 codegen_->CreateUnresolvedFieldLocationSummary(
4475 instruction, instruction->GetFieldType(), calling_convention);
4476}
4477
4478void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4479 HUnresolvedInstanceFieldSet* instruction) {
4480 FieldAccessCallingConventionX86_64 calling_convention;
4481 codegen_->GenerateUnresolvedFieldAccess(instruction,
4482 instruction->GetFieldType(),
4483 instruction->GetFieldIndex(),
4484 instruction->GetDexPc(),
4485 calling_convention);
4486}
4487
4488void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4489 HUnresolvedStaticFieldGet* instruction) {
4490 FieldAccessCallingConventionX86_64 calling_convention;
4491 codegen_->CreateUnresolvedFieldLocationSummary(
4492 instruction, instruction->GetFieldType(), calling_convention);
4493}
4494
4495void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4496 HUnresolvedStaticFieldGet* instruction) {
4497 FieldAccessCallingConventionX86_64 calling_convention;
4498 codegen_->GenerateUnresolvedFieldAccess(instruction,
4499 instruction->GetFieldType(),
4500 instruction->GetFieldIndex(),
4501 instruction->GetDexPc(),
4502 calling_convention);
4503}
4504
4505void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4506 HUnresolvedStaticFieldSet* instruction) {
4507 FieldAccessCallingConventionX86_64 calling_convention;
4508 codegen_->CreateUnresolvedFieldLocationSummary(
4509 instruction, instruction->GetFieldType(), calling_convention);
4510}
4511
4512void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4513 HUnresolvedStaticFieldSet* instruction) {
4514 FieldAccessCallingConventionX86_64 calling_convention;
4515 codegen_->GenerateUnresolvedFieldAccess(instruction,
4516 instruction->GetFieldType(),
4517 instruction->GetFieldIndex(),
4518 instruction->GetDexPc(),
4519 calling_convention);
4520}
4521
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004523 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4524 ? LocationSummary::kCallOnSlowPath
4525 : LocationSummary::kNoCall;
4526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4527 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004528 ? Location::RequiresRegister()
4529 : Location::Any();
4530 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004531 if (instruction->HasUses()) {
4532 locations->SetOut(Location::SameAsFirstInput());
4533 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534}
4535
Calin Juravle2ae48182016-03-16 14:05:09 +00004536void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4537 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004538 return;
4539 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004540 LocationSummary* locations = instruction->GetLocations();
4541 Location obj = locations->InAt(0);
4542
4543 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004544 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004545}
4546
Calin Juravle2ae48182016-03-16 14:05:09 +00004547void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004548 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004549 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004550
4551 LocationSummary* locations = instruction->GetLocations();
4552 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553
4554 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004555 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004556 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004557 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004558 } else {
4559 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004560 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004561 __ jmp(slow_path->GetEntryLabel());
4562 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563 }
4564 __ j(kEqual, slow_path->GetEntryLabel());
4565}
4566
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004567void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004568 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004569}
4570
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004572 bool object_array_get_with_read_barrier =
4573 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004574 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004575 new (GetGraph()->GetArena()) LocationSummary(instruction,
4576 object_array_get_with_read_barrier ?
4577 LocationSummary::kCallOnSlowPath :
4578 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004579 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004580 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004581 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4582 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4583 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004584 // The output overlaps for an object array get when read barriers
4585 // are enabled: we do not want the move to overwrite the array's
4586 // location, as we need it to emit the read barrier.
4587 locations->SetOut(
4588 Location::RequiresRegister(),
4589 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004590 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591}
4592
4593void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4594 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004595 Location obj_loc = locations->InAt(0);
4596 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004598 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004599 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004601 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004602 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004603 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004604 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 if (index.IsConstant()) {
4606 __ movzxb(out, Address(obj,
4607 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4608 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004609 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 }
4611 break;
4612 }
4613
4614 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004615 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 if (index.IsConstant()) {
4617 __ movsxb(out, Address(obj,
4618 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4619 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004620 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621 }
4622 break;
4623 }
4624
4625 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004626 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 if (index.IsConstant()) {
4628 __ movsxw(out, Address(obj,
4629 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4630 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004631 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 }
4633 break;
4634 }
4635
4636 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004637 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 if (index.IsConstant()) {
4639 __ movzxw(out, Address(obj,
4640 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4641 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004642 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 }
4644 break;
4645 }
4646
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004647 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004648 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 if (index.IsConstant()) {
4650 __ movl(out, Address(obj,
4651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4652 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004653 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004654 }
4655 break;
4656 }
4657
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004658 case Primitive::kPrimNot: {
4659 static_assert(
4660 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4661 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004662 // /* HeapReference<Object> */ out =
4663 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4664 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004665 // Note that a potential implicit null check is handled in this
4666 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4667 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004668 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004669 } else {
4670 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4671 if (index.IsConstant()) {
4672 uint32_t offset =
4673 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4674 __ movl(out, Address(obj, offset));
4675 codegen_->MaybeRecordImplicitNullCheck(instruction);
4676 // If read barriers are enabled, emit read barriers other than
4677 // Baker's using a slow path (and also unpoison the loaded
4678 // reference, if heap poisoning is enabled).
4679 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4680 } else {
4681 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4682 codegen_->MaybeRecordImplicitNullCheck(instruction);
4683 // If read barriers are enabled, emit read barriers other than
4684 // Baker's using a slow path (and also unpoison the loaded
4685 // reference, if heap poisoning is enabled).
4686 codegen_->MaybeGenerateReadBarrierSlow(
4687 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4688 }
4689 }
4690 break;
4691 }
4692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004693 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004694 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695 if (index.IsConstant()) {
4696 __ movq(out, Address(obj,
4697 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4698 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004699 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700 }
4701 break;
4702 }
4703
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004704 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004705 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004706 if (index.IsConstant()) {
4707 __ movss(out, Address(obj,
4708 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4709 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004710 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004711 }
4712 break;
4713 }
4714
4715 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004716 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004717 if (index.IsConstant()) {
4718 __ movsd(out, Address(obj,
4719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4720 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004721 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004722 }
4723 break;
4724 }
4725
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004726 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004727 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004728 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729 }
Roland Levillain4d027112015-07-01 15:41:14 +01004730
4731 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004732 // Potential implicit null checks, in the case of reference
4733 // arrays, are handled in the previous switch statement.
4734 } else {
4735 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004736 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737}
4738
4739void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004740 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004741
4742 bool needs_write_barrier =
4743 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004744 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004745 bool object_array_set_with_read_barrier =
4746 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004747
Nicolas Geoffray39468442014-09-02 15:17:15 +01004748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004749 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004750 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004751 LocationSummary::kCallOnSlowPath :
4752 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004753
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004754 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004755 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4756 if (Primitive::IsFloatingPointType(value_type)) {
4757 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004758 } else {
4759 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4760 }
4761
4762 if (needs_write_barrier) {
4763 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004764 // These registers may be used for Baker read barriers too.
4765 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004766 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004767 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768}
4769
4770void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4771 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004772 Location array_loc = locations->InAt(0);
4773 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004775 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004776 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004777 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004778 bool needs_write_barrier =
4779 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004780 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4781 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4782 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783
4784 switch (value_type) {
4785 case Primitive::kPrimBoolean:
4786 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004787 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4788 Address address = index.IsConstant()
4789 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4790 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4791 if (value.IsRegister()) {
4792 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004793 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004795 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004796 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797 break;
4798 }
4799
4800 case Primitive::kPrimShort:
4801 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004802 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4803 Address address = index.IsConstant()
4804 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4805 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4806 if (value.IsRegister()) {
4807 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004808 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004809 DCHECK(value.IsConstant()) << value;
4810 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004811 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004812 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004813 break;
4814 }
4815
4816 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004817 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4818 Address address = index.IsConstant()
4819 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4820 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004821
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004822 if (!value.IsRegister()) {
4823 // Just setting null.
4824 DCHECK(instruction->InputAt(2)->IsNullConstant());
4825 DCHECK(value.IsConstant()) << value;
4826 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004827 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004828 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004829 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004830 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004831 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832
4833 DCHECK(needs_write_barrier);
4834 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004835 // We cannot use a NearLabel for `done`, as its range may be too
4836 // short when Baker read barriers are enabled.
4837 Label done;
4838 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004839 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004840 Location temp_loc = locations->GetTemp(0);
4841 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004842 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004843 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4844 codegen_->AddSlowPath(slow_path);
4845 if (instruction->GetValueCanBeNull()) {
4846 __ testl(register_value, register_value);
4847 __ j(kNotEqual, &not_null);
4848 __ movl(address, Immediate(0));
4849 codegen_->MaybeRecordImplicitNullCheck(instruction);
4850 __ jmp(&done);
4851 __ Bind(&not_null);
4852 }
4853
Roland Levillain0d5a2812015-11-13 10:07:31 +00004854 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01004855 if (!kUseBakerReadBarrier) {
4856 // When (non-Baker) read barriers are enabled, the type
4857 // checking instrumentation requires two read barriers
4858 // generated by CodeGeneratorX86_64::GenerateReadBarrierSlow:
4859 //
4860 // __ movl(temp2, temp);
4861 // // /* HeapReference<Class> */ temp = temp->component_type_
4862 // __ movl(temp, Address(temp, component_offset));
4863 // codegen_->GenerateReadBarrierSlow(
4864 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4865 //
4866 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4867 // __ movl(temp2, Address(register_value, class_offset));
4868 // codegen_->GenerateReadBarrierSlow(
4869 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4870 //
4871 // __ cmpl(temp, temp2);
4872 //
4873 // However, the second read barrier may trash `temp`, as it
4874 // is a temporary register, and as such would not be saved
4875 // along with live registers before calling the runtime (nor
4876 // restored afterwards). So in this case, we bail out and
4877 // delegate the work to the array set slow path.
4878 //
4879 // TODO: Extend the register allocator to support a new
4880 // "(locally) live temp" location so as to avoid always
4881 // going into the slow path when read barriers are enabled?
4882 //
4883 // There is no such problem with Baker read barriers (see below).
4884 __ jmp(slow_path->GetEntryLabel());
4885 } else {
4886 Location temp2_loc = locations->GetTemp(1);
4887 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
4888 // /* HeapReference<Class> */ temp = array->klass_
4889 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4890 instruction, temp_loc, array, class_offset, /* needs_null_check */ true);
4891
4892 // /* HeapReference<Class> */ temp = temp->component_type_
4893 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4894 instruction, temp_loc, temp, component_offset, /* needs_null_check */ false);
4895 // Register `temp` is not trashed by the read barrier
4896 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
4897 // as that method produces a call to a ReadBarrierMarkRegX
4898 // entry point, which saves all potentially live registers,
4899 // including temporaries such a `temp`.
4900 // /* HeapReference<Class> */ temp2 = register_value->klass_
4901 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4902 instruction, temp2_loc, register_value, class_offset, /* needs_null_check */ false);
4903 // If heap poisoning is enabled, `temp` and `temp2` have
4904 // been unpoisoned by the the previous calls to
4905 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4906 __ cmpl(temp, temp2);
4907
4908 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4909 __ j(kEqual, &do_put);
4910 // We do not need to emit a read barrier for the
4911 // following heap reference load, as `temp` is only used
4912 // in a comparison with null below, and this reference
4913 // is not kept afterwards. Also, if heap poisoning is
4914 // enabled, there is no need to unpoison that heap
4915 // reference for the same reason (comparison with null).
4916 __ cmpl(Address(temp, super_offset), Immediate(0));
4917 __ j(kNotEqual, slow_path->GetEntryLabel());
4918 __ Bind(&do_put);
4919 } else {
4920 __ j(kNotEqual, slow_path->GetEntryLabel());
4921 }
4922 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004923 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01004924 // Non read barrier code.
4925
Roland Levillain0d5a2812015-11-13 10:07:31 +00004926 // /* HeapReference<Class> */ temp = array->klass_
4927 __ movl(temp, Address(array, class_offset));
4928 codegen_->MaybeRecordImplicitNullCheck(instruction);
4929 __ MaybeUnpoisonHeapReference(temp);
4930
4931 // /* HeapReference<Class> */ temp = temp->component_type_
4932 __ movl(temp, Address(temp, component_offset));
4933 // If heap poisoning is enabled, no need to unpoison `temp`
4934 // nor the object reference in `register_value->klass`, as
4935 // we are comparing two poisoned references.
4936 __ cmpl(temp, Address(register_value, class_offset));
4937
4938 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4939 __ j(kEqual, &do_put);
4940 // If heap poisoning is enabled, the `temp` reference has
4941 // not been unpoisoned yet; unpoison it now.
4942 __ MaybeUnpoisonHeapReference(temp);
4943
Roland Levillain16d9f942016-08-25 17:27:56 +01004944 // If heap poisoning is enabled, no need to unpoison the
4945 // heap reference loaded below, as it is only used for a
4946 // comparison with null.
4947 __ cmpl(Address(temp, super_offset), Immediate(0));
Roland Levillain0d5a2812015-11-13 10:07:31 +00004948 __ j(kNotEqual, slow_path->GetEntryLabel());
4949 __ Bind(&do_put);
4950 } else {
4951 __ j(kNotEqual, slow_path->GetEntryLabel());
4952 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004953 }
4954 }
4955
4956 if (kPoisonHeapReferences) {
4957 __ movl(temp, register_value);
4958 __ PoisonHeapReference(temp);
4959 __ movl(address, temp);
4960 } else {
4961 __ movl(address, register_value);
4962 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004963 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004964 codegen_->MaybeRecordImplicitNullCheck(instruction);
4965 }
4966
4967 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4968 codegen_->MarkGCCard(
4969 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4970 __ Bind(&done);
4971
4972 if (slow_path != nullptr) {
4973 __ Bind(slow_path->GetExitLabel());
4974 }
4975
4976 break;
4977 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004978
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004979 case Primitive::kPrimInt: {
4980 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4981 Address address = index.IsConstant()
4982 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4983 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4984 if (value.IsRegister()) {
4985 __ movl(address, value.AsRegister<CpuRegister>());
4986 } else {
4987 DCHECK(value.IsConstant()) << value;
4988 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4989 __ movl(address, Immediate(v));
4990 }
4991 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992 break;
4993 }
4994
4995 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004996 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4997 Address address = index.IsConstant()
4998 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4999 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
5000 if (value.IsRegister()) {
5001 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005002 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005004 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04005005 Address address_high = index.IsConstant()
5006 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5007 offset + sizeof(int32_t))
5008 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5009 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005010 }
5011 break;
5012 }
5013
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005014 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005015 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5016 Address address = index.IsConstant()
5017 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5018 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005019 if (value.IsFpuRegister()) {
5020 __ movss(address, value.AsFpuRegister<XmmRegister>());
5021 } else {
5022 DCHECK(value.IsConstant());
5023 int32_t v =
5024 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5025 __ movl(address, Immediate(v));
5026 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005028 break;
5029 }
5030
5031 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005032 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5033 Address address = index.IsConstant()
5034 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5035 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005036 if (value.IsFpuRegister()) {
5037 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5038 codegen_->MaybeRecordImplicitNullCheck(instruction);
5039 } else {
5040 int64_t v =
5041 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5042 Address address_high = index.IsConstant()
5043 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5044 offset + sizeof(int32_t))
5045 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5046 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5047 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005048 break;
5049 }
5050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051 case Primitive::kPrimVoid:
5052 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005053 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054 }
5055}
5056
5057void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005058 LocationSummary* locations =
5059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005060 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005061 if (!instruction->IsEmittedAtUseSite()) {
5062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5063 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064}
5065
5066void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005067 if (instruction->IsEmittedAtUseSite()) {
5068 return;
5069 }
5070
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005072 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005073 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5074 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005076 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005077}
5078
5079void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005080 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5081 ? LocationSummary::kCallOnSlowPath
5082 : LocationSummary::kNoCall;
5083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005084 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005085 HInstruction* length = instruction->InputAt(1);
5086 if (!length->IsEmittedAtUseSite()) {
5087 locations->SetInAt(1, Location::RegisterOrConstant(length));
5088 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005089 if (instruction->HasUses()) {
5090 locations->SetOut(Location::SameAsFirstInput());
5091 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092}
5093
5094void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5095 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005096 Location index_loc = locations->InAt(0);
5097 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005098 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099
Mark Mendell99dbd682015-04-22 16:18:52 -04005100 if (length_loc.IsConstant()) {
5101 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5102 if (index_loc.IsConstant()) {
5103 // BCE will remove the bounds check if we are guarenteed to pass.
5104 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5105 if (index < 0 || index >= length) {
5106 codegen_->AddSlowPath(slow_path);
5107 __ jmp(slow_path->GetEntryLabel());
5108 } else {
5109 // Some optimization after BCE may have generated this, and we should not
5110 // generate a bounds check if it is a valid range.
5111 }
5112 return;
5113 }
5114
5115 // We have to reverse the jump condition because the length is the constant.
5116 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5117 __ cmpl(index_reg, Immediate(length));
5118 codegen_->AddSlowPath(slow_path);
5119 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005120 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005121 HInstruction* array_length = instruction->InputAt(1);
5122 if (array_length->IsEmittedAtUseSite()) {
5123 // Address the length field in the array.
5124 DCHECK(array_length->IsArrayLength());
5125 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5126 Location array_loc = array_length->GetLocations()->InAt(0);
5127 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
5128 if (index_loc.IsConstant()) {
5129 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5130 __ cmpl(array_len, Immediate(value));
5131 } else {
5132 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5133 }
5134 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005135 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005136 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5137 if (index_loc.IsConstant()) {
5138 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5139 __ cmpl(length, Immediate(value));
5140 } else {
5141 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5142 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005143 }
5144 codegen_->AddSlowPath(slow_path);
5145 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005146 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147}
5148
5149void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5150 CpuRegister card,
5151 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005152 CpuRegister value,
5153 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005154 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005155 if (value_can_be_null) {
5156 __ testl(value, value);
5157 __ j(kEqual, &is_null);
5158 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005159 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005160 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 __ movq(temp, object);
5162 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005163 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005164 if (value_can_be_null) {
5165 __ Bind(&is_null);
5166 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167}
5168
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005169void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005170 LOG(FATAL) << "Unimplemented";
5171}
5172
5173void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005174 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5175}
5176
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005177void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5178 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5179}
5180
5181void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005182 HBasicBlock* block = instruction->GetBlock();
5183 if (block->GetLoopInformation() != nullptr) {
5184 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5185 // The back edge will generate the suspend check.
5186 return;
5187 }
5188 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5189 // The goto will generate the suspend check.
5190 return;
5191 }
5192 GenerateSuspendCheck(instruction, nullptr);
5193}
5194
5195void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5196 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005197 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005198 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5199 if (slow_path == nullptr) {
5200 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5201 instruction->SetSlowPath(slow_path);
5202 codegen_->AddSlowPath(slow_path);
5203 if (successor != nullptr) {
5204 DCHECK(successor->IsLoopHeader());
5205 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5206 }
5207 } else {
5208 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5209 }
5210
Andreas Gampe542451c2016-07-26 09:02:02 -07005211 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005212 /* no_rip */ true),
5213 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005214 if (successor == nullptr) {
5215 __ j(kNotEqual, slow_path->GetEntryLabel());
5216 __ Bind(slow_path->GetReturnLabel());
5217 } else {
5218 __ j(kEqual, codegen_->GetLabelOf(successor));
5219 __ jmp(slow_path->GetEntryLabel());
5220 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005221}
5222
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005223X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5224 return codegen_->GetAssembler();
5225}
5226
5227void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005228 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005229 Location source = move->GetSource();
5230 Location destination = move->GetDestination();
5231
5232 if (source.IsRegister()) {
5233 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005234 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005235 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005236 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005237 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005238 } else {
5239 DCHECK(destination.IsDoubleStackSlot());
5240 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005241 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005242 }
5243 } else if (source.IsStackSlot()) {
5244 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005245 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005246 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005247 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005248 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005249 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005250 } else {
5251 DCHECK(destination.IsStackSlot());
5252 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5253 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5254 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005255 } else if (source.IsDoubleStackSlot()) {
5256 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005257 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005258 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005259 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005260 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5261 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005262 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005263 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005264 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5265 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5266 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005267 } else if (source.IsConstant()) {
5268 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005269 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5270 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005271 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005272 if (value == 0) {
5273 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5274 } else {
5275 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5276 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005277 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005278 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005279 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005280 }
5281 } else if (constant->IsLongConstant()) {
5282 int64_t value = constant->AsLongConstant()->GetValue();
5283 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005284 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005285 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005286 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005287 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005288 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005289 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005290 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005291 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005292 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005293 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 } else {
5295 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005296 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005297 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5298 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005299 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005300 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005301 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005302 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005303 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005304 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005305 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005306 } else {
5307 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005308 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005309 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005310 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 } else if (source.IsFpuRegister()) {
5312 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005313 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 } else if (destination.IsStackSlot()) {
5315 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005316 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005317 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005318 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005319 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005320 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005321 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005322 }
5323}
5324
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005325void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005326 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005327 __ movl(Address(CpuRegister(RSP), mem), reg);
5328 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005329}
5330
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005331void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005332 ScratchRegisterScope ensure_scratch(
5333 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5334
5335 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5336 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5337 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5338 Address(CpuRegister(RSP), mem2 + stack_offset));
5339 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5340 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5341 CpuRegister(ensure_scratch.GetRegister()));
5342}
5343
Mark Mendell8a1c7282015-06-29 15:41:28 -04005344void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5345 __ movq(CpuRegister(TMP), reg1);
5346 __ movq(reg1, reg2);
5347 __ movq(reg2, CpuRegister(TMP));
5348}
5349
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005350void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5351 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5352 __ movq(Address(CpuRegister(RSP), mem), reg);
5353 __ movq(reg, CpuRegister(TMP));
5354}
5355
5356void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5357 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005358 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005359
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005360 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5361 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5362 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5363 Address(CpuRegister(RSP), mem2 + stack_offset));
5364 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5365 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5366 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005367}
5368
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005369void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5370 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5371 __ movss(Address(CpuRegister(RSP), mem), reg);
5372 __ movd(reg, CpuRegister(TMP));
5373}
5374
5375void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5376 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5377 __ movsd(Address(CpuRegister(RSP), mem), reg);
5378 __ movd(reg, CpuRegister(TMP));
5379}
5380
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005381void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005382 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005383 Location source = move->GetSource();
5384 Location destination = move->GetDestination();
5385
5386 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005387 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005388 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005389 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005390 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005391 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005392 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005393 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5394 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005395 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005396 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005397 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005398 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5399 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005400 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005401 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5402 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5403 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005404 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005405 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005406 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005407 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005408 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005409 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005410 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005411 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005412 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005413 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005414 }
5415}
5416
5417
5418void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5419 __ pushq(CpuRegister(reg));
5420}
5421
5422
5423void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5424 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005425}
5426
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005427void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005428 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005429 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5430 Immediate(mirror::Class::kStatusInitialized));
5431 __ j(kLess, slow_path->GetEntryLabel());
5432 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005433 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005434}
5435
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005436HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5437 HLoadClass::LoadKind desired_class_load_kind) {
5438 if (kEmitCompilerReadBarrier) {
5439 switch (desired_class_load_kind) {
5440 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5441 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5442 case HLoadClass::LoadKind::kBootImageAddress:
5443 // TODO: Implement for read barrier.
5444 return HLoadClass::LoadKind::kDexCacheViaMethod;
5445 default:
5446 break;
5447 }
5448 }
5449 switch (desired_class_load_kind) {
5450 case HLoadClass::LoadKind::kReferrersClass:
5451 break;
5452 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5453 DCHECK(!GetCompilerOptions().GetCompilePic());
5454 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5455 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5456 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5457 DCHECK(GetCompilerOptions().GetCompilePic());
5458 break;
5459 case HLoadClass::LoadKind::kBootImageAddress:
5460 break;
5461 case HLoadClass::LoadKind::kDexCacheAddress:
5462 DCHECK(Runtime::Current()->UseJitCompilation());
5463 break;
5464 case HLoadClass::LoadKind::kDexCachePcRelative:
5465 DCHECK(!Runtime::Current()->UseJitCompilation());
5466 break;
5467 case HLoadClass::LoadKind::kDexCacheViaMethod:
5468 break;
5469 }
5470 return desired_class_load_kind;
5471}
5472
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005473void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005474 if (cls->NeedsAccessCheck()) {
5475 InvokeRuntimeCallingConvention calling_convention;
5476 CodeGenerator::CreateLoadClassLocationSummary(
5477 cls,
5478 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5479 Location::RegisterLocation(RAX),
5480 /* code_generator_supports_read_barrier */ true);
5481 return;
5482 }
5483
5484 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5485 ? LocationSummary::kCallOnSlowPath
5486 : LocationSummary::kNoCall;
5487 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5488 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5489 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5490 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5491 locations->SetInAt(0, Location::RequiresRegister());
5492 }
5493 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005494}
5495
5496void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005497 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005498 if (cls->NeedsAccessCheck()) {
5499 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5500 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5501 cls,
5502 cls->GetDexPc(),
5503 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005504 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005505 return;
5506 }
5507
Roland Levillain0d5a2812015-11-13 10:07:31 +00005508 Location out_loc = locations->Out();
5509 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005510
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005511 bool generate_null_check = false;
5512 switch (cls->GetLoadKind()) {
5513 case HLoadClass::LoadKind::kReferrersClass: {
5514 DCHECK(!cls->CanCallRuntime());
5515 DCHECK(!cls->MustGenerateClinitCheck());
5516 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5517 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5518 GenerateGcRootFieldLoad(
5519 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5520 break;
5521 }
5522 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5523 DCHECK(!kEmitCompilerReadBarrier);
5524 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5525 codegen_->RecordTypePatch(cls);
5526 break;
5527 case HLoadClass::LoadKind::kBootImageAddress: {
5528 DCHECK(!kEmitCompilerReadBarrier);
5529 DCHECK_NE(cls->GetAddress(), 0u);
5530 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5531 __ movl(out, Immediate(address)); // Zero-extended.
5532 codegen_->RecordSimplePatch();
5533 break;
5534 }
5535 case HLoadClass::LoadKind::kDexCacheAddress: {
5536 DCHECK_NE(cls->GetAddress(), 0u);
5537 // /* GcRoot<mirror::Class> */ out = *address
5538 if (IsUint<32>(cls->GetAddress())) {
5539 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5540 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005541 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005542 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5543 __ movq(out, Immediate(cls->GetAddress()));
5544 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005545 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005546 generate_null_check = !cls->IsInDexCache();
5547 break;
5548 }
5549 case HLoadClass::LoadKind::kDexCachePcRelative: {
5550 uint32_t offset = cls->GetDexCacheElementOffset();
5551 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5552 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5553 /* no_rip */ false);
5554 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5555 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5556 generate_null_check = !cls->IsInDexCache();
5557 break;
5558 }
5559 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5560 // /* GcRoot<mirror::Class>[] */ out =
5561 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5562 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5563 __ movq(out,
5564 Address(current_method,
5565 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5566 // /* GcRoot<mirror::Class> */ out = out[type_index]
5567 GenerateGcRootFieldLoad(
5568 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5569 generate_null_check = !cls->IsInDexCache();
5570 break;
5571 }
5572 default:
5573 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5574 UNREACHABLE();
5575 }
5576
5577 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5578 DCHECK(cls->CanCallRuntime());
5579 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5580 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5581 codegen_->AddSlowPath(slow_path);
5582 if (generate_null_check) {
5583 __ testl(out, out);
5584 __ j(kEqual, slow_path->GetEntryLabel());
5585 }
5586 if (cls->MustGenerateClinitCheck()) {
5587 GenerateClassInitializationCheck(slow_path, out);
5588 } else {
5589 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005590 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005591 }
5592}
5593
5594void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5595 LocationSummary* locations =
5596 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5597 locations->SetInAt(0, Location::RequiresRegister());
5598 if (check->HasUses()) {
5599 locations->SetOut(Location::SameAsFirstInput());
5600 }
5601}
5602
5603void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005604 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005605 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005606 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005608 GenerateClassInitializationCheck(slow_path,
5609 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005610}
5611
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005612HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5613 HLoadString::LoadKind desired_string_load_kind) {
5614 if (kEmitCompilerReadBarrier) {
5615 switch (desired_string_load_kind) {
5616 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5617 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5618 case HLoadString::LoadKind::kBootImageAddress:
5619 // TODO: Implement for read barrier.
5620 return HLoadString::LoadKind::kDexCacheViaMethod;
5621 default:
5622 break;
5623 }
5624 }
5625 switch (desired_string_load_kind) {
5626 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5627 DCHECK(!GetCompilerOptions().GetCompilePic());
5628 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5629 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5630 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5631 DCHECK(GetCompilerOptions().GetCompilePic());
5632 break;
5633 case HLoadString::LoadKind::kBootImageAddress:
5634 break;
5635 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005636 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005637 break;
5638 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005639 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005640 break;
5641 case HLoadString::LoadKind::kDexCacheViaMethod:
5642 break;
5643 }
5644 return desired_string_load_kind;
5645}
5646
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005647void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005648 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005649 ? LocationSummary::kCallOnSlowPath
5650 : LocationSummary::kNoCall;
5651 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005652 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5653 locations->SetInAt(0, Location::RequiresRegister());
5654 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005655 locations->SetOut(Location::RequiresRegister());
5656}
5657
5658void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005659 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005660 Location out_loc = locations->Out();
5661 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005662
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005663 switch (load->GetLoadKind()) {
5664 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5665 DCHECK(!kEmitCompilerReadBarrier);
5666 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5667 codegen_->RecordStringPatch(load);
5668 return; // No dex cache slow path.
5669 }
5670 case HLoadString::LoadKind::kBootImageAddress: {
5671 DCHECK(!kEmitCompilerReadBarrier);
5672 DCHECK_NE(load->GetAddress(), 0u);
5673 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5674 __ movl(out, Immediate(address)); // Zero-extended.
5675 codegen_->RecordSimplePatch();
5676 return; // No dex cache slow path.
5677 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005678 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005679 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005680 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005681
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005682 // TODO: Re-add the compiler code to do string dex cache lookup again.
5683 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5684 codegen_->AddSlowPath(slow_path);
5685 __ jmp(slow_path->GetEntryLabel());
5686 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005687}
5688
David Brazdilcb1c0552015-08-04 16:22:25 +01005689static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005690 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005691 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005692}
5693
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005694void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5695 LocationSummary* locations =
5696 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5697 locations->SetOut(Location::RequiresRegister());
5698}
5699
5700void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005701 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5702}
5703
5704void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5705 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5706}
5707
5708void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5709 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005710}
5711
5712void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5713 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005715 InvokeRuntimeCallingConvention calling_convention;
5716 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5717}
5718
5719void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005720 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5721 instruction,
5722 instruction->GetDexPc(),
5723 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005724 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005725}
5726
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005727static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5728 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005729 !kUseBakerReadBarrier &&
5730 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005731 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5732 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5733}
5734
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005735void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005736 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005737 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5738 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005739 case TypeCheckKind::kExactCheck:
5740 case TypeCheckKind::kAbstractClassCheck:
5741 case TypeCheckKind::kClassHierarchyCheck:
5742 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743 call_kind =
5744 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005745 break;
5746 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005747 case TypeCheckKind::kUnresolvedCheck:
5748 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005749 call_kind = LocationSummary::kCallOnSlowPath;
5750 break;
5751 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005752
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005753 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005754 locations->SetInAt(0, Location::RequiresRegister());
5755 locations->SetInAt(1, Location::Any());
5756 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5757 locations->SetOut(Location::RequiresRegister());
5758 // When read barriers are enabled, we need a temporary register for
5759 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005760 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005761 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005762 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005763}
5764
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005765void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005766 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005767 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005768 Location obj_loc = locations->InAt(0);
5769 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005770 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005771 Location out_loc = locations->Out();
5772 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005773 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005774 locations->GetTemp(0) :
5775 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005776 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5778 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5779 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005780 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005782
5783 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005784 // Avoid null check if we know obj is not null.
5785 if (instruction->MustDoNullCheck()) {
5786 __ testl(obj, obj);
5787 __ j(kEqual, &zero);
5788 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005789
Roland Levillain0d5a2812015-11-13 10:07:31 +00005790 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005791 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005792
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005793 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 case TypeCheckKind::kExactCheck: {
5795 if (cls.IsRegister()) {
5796 __ cmpl(out, cls.AsRegister<CpuRegister>());
5797 } else {
5798 DCHECK(cls.IsStackSlot()) << cls;
5799 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5800 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005801 if (zero.IsLinked()) {
5802 // Classes must be equal for the instanceof to succeed.
5803 __ j(kNotEqual, &zero);
5804 __ movl(out, Immediate(1));
5805 __ jmp(&done);
5806 } else {
5807 __ setcc(kEqual, out);
5808 // setcc only sets the low byte.
5809 __ andl(out, Immediate(1));
5810 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005811 break;
5812 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005813
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 case TypeCheckKind::kAbstractClassCheck: {
5815 // If the class is abstract, we eagerly fetch the super class of the
5816 // object to avoid doing a comparison we know will fail.
5817 NearLabel loop, success;
5818 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005820 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005821 __ testl(out, out);
5822 // If `out` is null, we use it for the result, and jump to `done`.
5823 __ j(kEqual, &done);
5824 if (cls.IsRegister()) {
5825 __ cmpl(out, cls.AsRegister<CpuRegister>());
5826 } else {
5827 DCHECK(cls.IsStackSlot()) << cls;
5828 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5829 }
5830 __ j(kNotEqual, &loop);
5831 __ movl(out, Immediate(1));
5832 if (zero.IsLinked()) {
5833 __ jmp(&done);
5834 }
5835 break;
5836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 case TypeCheckKind::kClassHierarchyCheck: {
5839 // Walk over the class hierarchy to find a match.
5840 NearLabel loop, success;
5841 __ Bind(&loop);
5842 if (cls.IsRegister()) {
5843 __ cmpl(out, cls.AsRegister<CpuRegister>());
5844 } else {
5845 DCHECK(cls.IsStackSlot()) << cls;
5846 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5847 }
5848 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005849 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005850 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 __ testl(out, out);
5852 __ j(kNotEqual, &loop);
5853 // If `out` is null, we use it for the result, and jump to `done`.
5854 __ jmp(&done);
5855 __ Bind(&success);
5856 __ movl(out, Immediate(1));
5857 if (zero.IsLinked()) {
5858 __ jmp(&done);
5859 }
5860 break;
5861 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005864 // Do an exact check.
5865 NearLabel exact_check;
5866 if (cls.IsRegister()) {
5867 __ cmpl(out, cls.AsRegister<CpuRegister>());
5868 } else {
5869 DCHECK(cls.IsStackSlot()) << cls;
5870 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5871 }
5872 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005873 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005874 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005875 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 __ testl(out, out);
5877 // If `out` is null, we use it for the result, and jump to `done`.
5878 __ j(kEqual, &done);
5879 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5880 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005881 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 __ movl(out, Immediate(1));
5883 __ jmp(&done);
5884 break;
5885 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005886
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005887 case TypeCheckKind::kArrayCheck: {
5888 if (cls.IsRegister()) {
5889 __ cmpl(out, cls.AsRegister<CpuRegister>());
5890 } else {
5891 DCHECK(cls.IsStackSlot()) << cls;
5892 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5893 }
5894 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005895 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5896 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005897 codegen_->AddSlowPath(slow_path);
5898 __ j(kNotEqual, slow_path->GetEntryLabel());
5899 __ movl(out, Immediate(1));
5900 if (zero.IsLinked()) {
5901 __ jmp(&done);
5902 }
5903 break;
5904 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005905
Calin Juravle98893e12015-10-02 21:05:03 +01005906 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005907 case TypeCheckKind::kInterfaceCheck: {
5908 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005909 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005910 // cases.
5911 //
5912 // We cannot directly call the InstanceofNonTrivial runtime
5913 // entry point without resorting to a type checking slow path
5914 // here (i.e. by calling InvokeRuntime directly), as it would
5915 // require to assign fixed registers for the inputs of this
5916 // HInstanceOf instruction (following the runtime calling
5917 // convention), which might be cluttered by the potential first
5918 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005919 //
5920 // TODO: Introduce a new runtime entry point taking the object
5921 // to test (instead of its class) as argument, and let it deal
5922 // with the read barrier issues. This will let us refactor this
5923 // case of the `switch` code as it was previously (with a direct
5924 // call to the runtime not using a type checking slow path).
5925 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005926 DCHECK(locations->OnlyCallsOnSlowPath());
5927 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5928 /* is_fatal */ false);
5929 codegen_->AddSlowPath(slow_path);
5930 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 if (zero.IsLinked()) {
5932 __ jmp(&done);
5933 }
5934 break;
5935 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005936 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005937
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005939 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 __ xorl(out, out);
5941 }
5942
5943 if (done.IsLinked()) {
5944 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005945 }
5946
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005947 if (slow_path != nullptr) {
5948 __ Bind(slow_path->GetExitLabel());
5949 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005950}
5951
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005952void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5954 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005955 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5956 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 case TypeCheckKind::kExactCheck:
5958 case TypeCheckKind::kAbstractClassCheck:
5959 case TypeCheckKind::kClassHierarchyCheck:
5960 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005961 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5962 LocationSummary::kCallOnSlowPath :
5963 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005964 break;
5965 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005966 case TypeCheckKind::kUnresolvedCheck:
5967 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 call_kind = LocationSummary::kCallOnSlowPath;
5969 break;
5970 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5972 locations->SetInAt(0, Location::RequiresRegister());
5973 locations->SetInAt(1, Location::Any());
5974 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5975 locations->AddTemp(Location::RequiresRegister());
5976 // When read barriers are enabled, we need an additional temporary
5977 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005978 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005979 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005981}
5982
5983void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005984 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005985 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986 Location obj_loc = locations->InAt(0);
5987 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005988 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005989 Location temp_loc = locations->GetTemp(0);
5990 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005991 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005992 locations->GetTemp(1) :
5993 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005994 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5995 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5996 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5997 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005998
Roland Levillain0d5a2812015-11-13 10:07:31 +00005999 bool is_type_check_slow_path_fatal =
6000 (type_check_kind == TypeCheckKind::kExactCheck ||
6001 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6002 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6003 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6004 !instruction->CanThrowIntoCatchBlock();
6005 SlowPathCode* type_check_slow_path =
6006 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6007 is_type_check_slow_path_fatal);
6008 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006009
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 case TypeCheckKind::kExactCheck:
6012 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006013 NearLabel done;
6014 // Avoid null check if we know obj is not null.
6015 if (instruction->MustDoNullCheck()) {
6016 __ testl(obj, obj);
6017 __ j(kEqual, &done);
6018 }
6019
6020 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006021 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006022
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006023 if (cls.IsRegister()) {
6024 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6025 } else {
6026 DCHECK(cls.IsStackSlot()) << cls;
6027 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6028 }
6029 // Jump to slow path for throwing the exception or doing a
6030 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006032 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 break;
6034 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006035
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006037 NearLabel done;
6038 // Avoid null check if we know obj is not null.
6039 if (instruction->MustDoNullCheck()) {
6040 __ testl(obj, obj);
6041 __ j(kEqual, &done);
6042 }
6043
6044 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006045 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006046
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_
Vladimir Marko953437b2016-08-24 08:30:46 +00006065 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006066 __ jmp(type_check_slow_path->GetEntryLabel());
6067
6068 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006069 if (cls.IsRegister()) {
6070 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6071 } else {
6072 DCHECK(cls.IsStackSlot()) << cls;
6073 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6074 }
6075 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006076 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006077 break;
6078 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006081 NearLabel done;
6082 // Avoid null check if we know obj is not null.
6083 if (instruction->MustDoNullCheck()) {
6084 __ testl(obj, obj);
6085 __ j(kEqual, &done);
6086 }
6087
6088 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006089 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006090
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006091 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006092 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006093 __ Bind(&loop);
6094 if (cls.IsRegister()) {
6095 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6096 } else {
6097 DCHECK(cls.IsStackSlot()) << cls;
6098 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6099 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006100 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006101
Roland Levillain0d5a2812015-11-13 10:07:31 +00006102 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006103 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104
6105 // If the class reference currently in `temp` is not null, jump
6106 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006107 __ testl(temp, temp);
6108 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006109 // Otherwise, jump to the slow path to throw the exception.
6110 //
6111 // But before, move back the object's class into `temp` before
6112 // going into the slow path, as it has been overwritten in the
6113 // meantime.
6114 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006115 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006116 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006117 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006118 break;
6119 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006120
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006121 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006122 // We cannot use a NearLabel here, as its range might be too
6123 // short in some cases when read barriers are enabled. This has
6124 // been observed for instance when the code emitted for this
6125 // case uses high x86-64 registers (R8-R15).
6126 Label done;
6127 // Avoid null check if we know obj is not null.
6128 if (instruction->MustDoNullCheck()) {
6129 __ testl(obj, obj);
6130 __ j(kEqual, &done);
6131 }
6132
6133 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006134 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006135
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006136 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006138 if (cls.IsRegister()) {
6139 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6140 } else {
6141 DCHECK(cls.IsStackSlot()) << cls;
6142 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6143 }
6144 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006145
6146 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006147 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006148 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149
6150 // If the component type is not null (i.e. the object is indeed
6151 // an array), jump to label `check_non_primitive_component_type`
6152 // to further check that this component type is not a primitive
6153 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 __ j(kNotEqual, &check_non_primitive_component_type);
6156 // Otherwise, jump to the slow path to throw the exception.
6157 //
6158 // But before, move back the object's class into `temp` before
6159 // going into the slow path, as it has been overwritten in the
6160 // meantime.
6161 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006162 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006163 __ jmp(type_check_slow_path->GetEntryLabel());
6164
6165 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006167 __ j(kEqual, &done);
6168 // Same comment as above regarding `temp` and the slow path.
6169 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006170 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006171 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006172 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006173 break;
6174 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006175
Calin Juravle98893e12015-10-02 21:05:03 +01006176 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006177 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006178 NearLabel done;
6179 // Avoid null check if we know obj is not null.
6180 if (instruction->MustDoNullCheck()) {
6181 __ testl(obj, obj);
6182 __ j(kEqual, &done);
6183 }
6184
6185 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006186 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006187
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006188 // We always go into the type check slow path for the unresolved
6189 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006190 //
6191 // We cannot directly call the CheckCast runtime entry point
6192 // without resorting to a type checking slow path here (i.e. by
6193 // calling InvokeRuntime directly), as it would require to
6194 // assign fixed registers for the inputs of this HInstanceOf
6195 // instruction (following the runtime calling convention), which
6196 // might be cluttered by the potential first read barrier
6197 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006198 //
6199 // TODO: Introduce a new runtime entry point taking the object
6200 // to test (instead of its class) as argument, and let it deal
6201 // with the read barrier issues. This will let us refactor this
6202 // case of the `switch` code as it was previously (with a direct
6203 // call to the runtime not using a type checking slow path).
6204 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006206 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 break;
6208 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209
Roland Levillain0d5a2812015-11-13 10:07:31 +00006210 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006211}
6212
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006213void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6214 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006216 InvokeRuntimeCallingConvention calling_convention;
6217 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6218}
6219
6220void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006221 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6222 : QUICK_ENTRY_POINT(pUnlockObject),
6223 instruction,
6224 instruction->GetDexPc(),
6225 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006226 if (instruction->IsEnter()) {
6227 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6228 } else {
6229 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6230 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006231}
6232
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006233void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6234void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6235void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6236
6237void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6238 LocationSummary* locations =
6239 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6240 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6241 || instruction->GetResultType() == Primitive::kPrimLong);
6242 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006243 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006244 locations->SetOut(Location::SameAsFirstInput());
6245}
6246
6247void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6248 HandleBitwiseOperation(instruction);
6249}
6250
6251void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6252 HandleBitwiseOperation(instruction);
6253}
6254
6255void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6256 HandleBitwiseOperation(instruction);
6257}
6258
6259void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6260 LocationSummary* locations = instruction->GetLocations();
6261 Location first = locations->InAt(0);
6262 Location second = locations->InAt(1);
6263 DCHECK(first.Equals(locations->Out()));
6264
6265 if (instruction->GetResultType() == Primitive::kPrimInt) {
6266 if (second.IsRegister()) {
6267 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006268 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006269 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006270 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006271 } else {
6272 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006273 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006274 }
6275 } else if (second.IsConstant()) {
6276 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6277 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006278 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006279 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006280 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006281 } else {
6282 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006283 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006284 }
6285 } else {
6286 Address address(CpuRegister(RSP), second.GetStackIndex());
6287 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006288 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006289 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006290 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006291 } else {
6292 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006293 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006294 }
6295 }
6296 } else {
6297 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006298 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6299 bool second_is_constant = false;
6300 int64_t value = 0;
6301 if (second.IsConstant()) {
6302 second_is_constant = true;
6303 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006304 }
Mark Mendell40741f32015-04-20 22:10:34 -04006305 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006306
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006307 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006308 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006309 if (is_int32_value) {
6310 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6311 } else {
6312 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6313 }
6314 } else if (second.IsDoubleStackSlot()) {
6315 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006316 } else {
6317 __ andq(first_reg, second.AsRegister<CpuRegister>());
6318 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006319 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006320 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006321 if (is_int32_value) {
6322 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6323 } else {
6324 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6325 }
6326 } else if (second.IsDoubleStackSlot()) {
6327 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006328 } else {
6329 __ orq(first_reg, second.AsRegister<CpuRegister>());
6330 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006331 } else {
6332 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006333 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006334 if (is_int32_value) {
6335 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6336 } else {
6337 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6338 }
6339 } else if (second.IsDoubleStackSlot()) {
6340 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006341 } else {
6342 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6343 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006344 }
6345 }
6346}
6347
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006348void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6349 Location out,
6350 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006351 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006352 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6353 if (kEmitCompilerReadBarrier) {
6354 if (kUseBakerReadBarrier) {
6355 // Load with fast path based Baker's read barrier.
6356 // /* HeapReference<Object> */ out = *(out + offset)
6357 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006358 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006359 } else {
6360 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006361 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006362 // in the following move operation, as we will need it for the
6363 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006364 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006365 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006366 // /* HeapReference<Object> */ out = *(out + offset)
6367 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006368 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006369 }
6370 } else {
6371 // Plain load with no read barrier.
6372 // /* HeapReference<Object> */ out = *(out + offset)
6373 __ movl(out_reg, Address(out_reg, offset));
6374 __ MaybeUnpoisonHeapReference(out_reg);
6375 }
6376}
6377
6378void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6379 Location out,
6380 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006381 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006382 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6383 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6384 if (kEmitCompilerReadBarrier) {
6385 if (kUseBakerReadBarrier) {
6386 // Load with fast path based Baker's read barrier.
6387 // /* HeapReference<Object> */ out = *(obj + offset)
6388 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006389 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006390 } else {
6391 // Load with slow path based read barrier.
6392 // /* HeapReference<Object> */ out = *(obj + offset)
6393 __ movl(out_reg, Address(obj_reg, offset));
6394 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6395 }
6396 } else {
6397 // Plain load with no read barrier.
6398 // /* HeapReference<Object> */ out = *(obj + offset)
6399 __ movl(out_reg, Address(obj_reg, offset));
6400 __ MaybeUnpoisonHeapReference(out_reg);
6401 }
6402}
6403
6404void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6405 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006406 const Address& address,
6407 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006408 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6409 if (kEmitCompilerReadBarrier) {
6410 if (kUseBakerReadBarrier) {
6411 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6412 // Baker's read barrier are used:
6413 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006414 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006415 // if (Thread::Current()->GetIsGcMarking()) {
6416 // root = ReadBarrier::Mark(root)
6417 // }
6418
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006419 // /* GcRoot<mirror::Object> */ root = *address
6420 __ movl(root_reg, address);
6421 if (fixup_label != nullptr) {
6422 __ Bind(fixup_label);
6423 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006424 static_assert(
6425 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6426 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6427 "have different sizes.");
6428 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6429 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6430 "have different sizes.");
6431
Vladimir Marko953437b2016-08-24 08:30:46 +00006432 // Slow path marking the GC root `root`.
6433 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6434 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006435 codegen_->AddSlowPath(slow_path);
6436
Andreas Gampe542451c2016-07-26 09:02:02 -07006437 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006438 /* no_rip */ true),
6439 Immediate(0));
6440 __ j(kNotEqual, slow_path->GetEntryLabel());
6441 __ Bind(slow_path->GetExitLabel());
6442 } else {
6443 // GC root loaded through a slow path for read barriers other
6444 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006445 // /* GcRoot<mirror::Object>* */ root = address
6446 __ leaq(root_reg, address);
6447 if (fixup_label != nullptr) {
6448 __ Bind(fixup_label);
6449 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006450 // /* mirror::Object* */ root = root->Read()
6451 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6452 }
6453 } else {
6454 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006455 // /* GcRoot<mirror::Object> */ root = *address
6456 __ movl(root_reg, address);
6457 if (fixup_label != nullptr) {
6458 __ Bind(fixup_label);
6459 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006460 // Note that GC roots are not affected by heap poisoning, thus we
6461 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006462 }
6463}
6464
6465void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6466 Location ref,
6467 CpuRegister obj,
6468 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006469 bool needs_null_check) {
6470 DCHECK(kEmitCompilerReadBarrier);
6471 DCHECK(kUseBakerReadBarrier);
6472
6473 // /* HeapReference<Object> */ ref = *(obj + offset)
6474 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006475 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006476}
6477
6478void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6479 Location ref,
6480 CpuRegister obj,
6481 uint32_t data_offset,
6482 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006483 bool needs_null_check) {
6484 DCHECK(kEmitCompilerReadBarrier);
6485 DCHECK(kUseBakerReadBarrier);
6486
Roland Levillain3d312422016-06-23 13:53:42 +01006487 static_assert(
6488 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6489 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006490 // /* HeapReference<Object> */ ref =
6491 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6492 Address src = index.IsConstant() ?
6493 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6494 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006495 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006496}
6497
6498void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6499 Location ref,
6500 CpuRegister obj,
6501 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006502 bool needs_null_check) {
6503 DCHECK(kEmitCompilerReadBarrier);
6504 DCHECK(kUseBakerReadBarrier);
6505
6506 // In slow path based read barriers, the read barrier call is
6507 // inserted after the original load. However, in fast path based
6508 // Baker's read barriers, we need to perform the load of
6509 // mirror::Object::monitor_ *before* the original reference load.
6510 // This load-load ordering is required by the read barrier.
6511 // The fast path/slow path (for Baker's algorithm) should look like:
6512 //
6513 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6514 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6515 // HeapReference<Object> ref = *src; // Original reference load.
6516 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6517 // if (is_gray) {
6518 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6519 // }
6520 //
6521 // Note: the original implementation in ReadBarrier::Barrier is
6522 // slightly more complex as:
6523 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006524 // the high-bits of rb_state, which are expected to be all zeroes
6525 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6526 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006527 // - it performs additional checks that we do not do here for
6528 // performance reasons.
6529
6530 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006531 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6532
Vladimir Marko953437b2016-08-24 08:30:46 +00006533 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6534 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6535 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6536 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6537 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6538 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6539 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6540
6541 // if (rb_state == ReadBarrier::gray_ptr_)
6542 // ref = ReadBarrier::Mark(ref);
6543 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6544 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006545 if (needs_null_check) {
6546 MaybeRecordImplicitNullCheck(instruction);
6547 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006548
6549 // Load fence to prevent load-load reordering.
6550 // Note that this is a no-op, thanks to the x86-64 memory model.
6551 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6552
6553 // The actual reference load.
6554 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006555 __ movl(ref_reg, src); // Flags are unaffected.
6556
6557 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6558 // Slow path marking the object `ref` when it is gray.
6559 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6560 instruction, ref, /* unpoison */ true);
6561 AddSlowPath(slow_path);
6562
6563 // We have done the "if" of the gray bit check above, now branch based on the flags.
6564 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006565
6566 // Object* ref = ref_addr->AsMirrorPtr()
6567 __ MaybeUnpoisonHeapReference(ref_reg);
6568
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006569 __ Bind(slow_path->GetExitLabel());
6570}
6571
6572void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6573 Location out,
6574 Location ref,
6575 Location obj,
6576 uint32_t offset,
6577 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006578 DCHECK(kEmitCompilerReadBarrier);
6579
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006580 // Insert a slow path based read barrier *after* the reference load.
6581 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582 // If heap poisoning is enabled, the unpoisoning of the loaded
6583 // reference will be carried out by the runtime within the slow
6584 // path.
6585 //
6586 // Note that `ref` currently does not get unpoisoned (when heap
6587 // poisoning is enabled), which is alright as the `ref` argument is
6588 // not used by the artReadBarrierSlow entry point.
6589 //
6590 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6591 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6592 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6593 AddSlowPath(slow_path);
6594
Roland Levillain0d5a2812015-11-13 10:07:31 +00006595 __ jmp(slow_path->GetEntryLabel());
6596 __ Bind(slow_path->GetExitLabel());
6597}
6598
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006599void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6600 Location out,
6601 Location ref,
6602 Location obj,
6603 uint32_t offset,
6604 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006605 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006606 // Baker's read barriers shall be handled by the fast path
6607 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6608 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006609 // If heap poisoning is enabled, unpoisoning will be taken care of
6610 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006611 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006612 } else if (kPoisonHeapReferences) {
6613 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6614 }
6615}
6616
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006617void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6618 Location out,
6619 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620 DCHECK(kEmitCompilerReadBarrier);
6621
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006622 // Insert a slow path based read barrier *after* the GC root load.
6623 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624 // Note that GC roots are not affected by heap poisoning, so we do
6625 // not need to do anything special for this here.
6626 SlowPathCode* slow_path =
6627 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6628 AddSlowPath(slow_path);
6629
Roland Levillain0d5a2812015-11-13 10:07:31 +00006630 __ jmp(slow_path->GetEntryLabel());
6631 __ Bind(slow_path->GetExitLabel());
6632}
6633
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006634void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006635 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006636 LOG(FATAL) << "Unreachable";
6637}
6638
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006639void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006640 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006641 LOG(FATAL) << "Unreachable";
6642}
6643
Mark Mendellfe57faa2015-09-18 09:26:15 -04006644// Simple implementation of packed switch - generate cascaded compare/jumps.
6645void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6646 LocationSummary* locations =
6647 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6648 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006649 locations->AddTemp(Location::RequiresRegister());
6650 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006651}
6652
6653void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6654 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006655 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006656 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006657 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6658 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6659 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006660 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6661
6662 // Should we generate smaller inline compare/jumps?
6663 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6664 // Figure out the correct compare values and jump conditions.
6665 // Handle the first compare/branch as a special case because it might
6666 // jump to the default case.
6667 DCHECK_GT(num_entries, 2u);
6668 Condition first_condition;
6669 uint32_t index;
6670 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6671 if (lower_bound != 0) {
6672 first_condition = kLess;
6673 __ cmpl(value_reg_in, Immediate(lower_bound));
6674 __ j(first_condition, codegen_->GetLabelOf(default_block));
6675 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6676
6677 index = 1;
6678 } else {
6679 // Handle all the compare/jumps below.
6680 first_condition = kBelow;
6681 index = 0;
6682 }
6683
6684 // Handle the rest of the compare/jumps.
6685 for (; index + 1 < num_entries; index += 2) {
6686 int32_t compare_to_value = lower_bound + index + 1;
6687 __ cmpl(value_reg_in, Immediate(compare_to_value));
6688 // Jump to successors[index] if value < case_value[index].
6689 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6690 // Jump to successors[index + 1] if value == case_value[index + 1].
6691 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6692 }
6693
6694 if (index != num_entries) {
6695 // There are an odd number of entries. Handle the last one.
6696 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006697 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006698 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6699 }
6700
6701 // And the default for any other value.
6702 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6703 __ jmp(codegen_->GetLabelOf(default_block));
6704 }
6705 return;
6706 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006707
6708 // Remove the bias, if needed.
6709 Register value_reg_out = value_reg_in.AsRegister();
6710 if (lower_bound != 0) {
6711 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6712 value_reg_out = temp_reg.AsRegister();
6713 }
6714 CpuRegister value_reg(value_reg_out);
6715
6716 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006717 __ cmpl(value_reg, Immediate(num_entries - 1));
6718 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006719
Mark Mendell9c86b482015-09-18 13:36:07 -04006720 // We are in the range of the table.
6721 // Load the address of the jump table in the constant area.
6722 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006723
Mark Mendell9c86b482015-09-18 13:36:07 -04006724 // Load the (signed) offset from the jump table.
6725 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6726
6727 // Add the offset to the address of the table base.
6728 __ addq(temp_reg, base_reg);
6729
6730 // And jump.
6731 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006732}
6733
Aart Bikc5d47542016-01-27 17:00:35 -08006734void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6735 if (value == 0) {
6736 __ xorl(dest, dest);
6737 } else {
6738 __ movl(dest, Immediate(value));
6739 }
6740}
6741
Mark Mendell92e83bf2015-05-07 11:25:03 -04006742void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6743 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006744 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006745 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006746 } else if (IsUint<32>(value)) {
6747 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006748 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6749 } else {
6750 __ movq(dest, Immediate(value));
6751 }
6752}
6753
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006754void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6755 if (value == 0) {
6756 __ xorps(dest, dest);
6757 } else {
6758 __ movss(dest, LiteralInt32Address(value));
6759 }
6760}
6761
6762void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6763 if (value == 0) {
6764 __ xorpd(dest, dest);
6765 } else {
6766 __ movsd(dest, LiteralInt64Address(value));
6767 }
6768}
6769
6770void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6771 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6772}
6773
6774void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6775 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6776}
6777
Aart Bika19616e2016-02-01 18:57:58 -08006778void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6779 if (value == 0) {
6780 __ testl(dest, dest);
6781 } else {
6782 __ cmpl(dest, Immediate(value));
6783 }
6784}
6785
6786void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6787 if (IsInt<32>(value)) {
6788 if (value == 0) {
6789 __ testq(dest, dest);
6790 } else {
6791 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6792 }
6793 } else {
6794 // Value won't fit in an int.
6795 __ cmpq(dest, LiteralInt64Address(value));
6796 }
6797}
6798
Mark Mendellcfa410b2015-05-25 16:02:44 -04006799void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6800 DCHECK(dest.IsDoubleStackSlot());
6801 if (IsInt<32>(value)) {
6802 // Can move directly as an int32 constant.
6803 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6804 Immediate(static_cast<int32_t>(value)));
6805 } else {
6806 Load64BitValue(CpuRegister(TMP), value);
6807 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6808 }
6809}
6810
Mark Mendell9c86b482015-09-18 13:36:07 -04006811/**
6812 * Class to handle late fixup of offsets into constant area.
6813 */
6814class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6815 public:
6816 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6817 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6818
6819 protected:
6820 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6821
6822 CodeGeneratorX86_64* codegen_;
6823
6824 private:
6825 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6826 // Patch the correct offset for the instruction. We use the address of the
6827 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6828 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6829 int32_t relative_position = constant_offset - pos;
6830
6831 // Patch in the right value.
6832 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6833 }
6834
6835 // Location in constant area that the fixup refers to.
6836 size_t offset_into_constant_area_;
6837};
6838
6839/**
6840 t * Class to handle late fixup of offsets to a jump table that will be created in the
6841 * constant area.
6842 */
6843class JumpTableRIPFixup : public RIPFixup {
6844 public:
6845 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6846 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6847
6848 void CreateJumpTable() {
6849 X86_64Assembler* assembler = codegen_->GetAssembler();
6850
6851 // Ensure that the reference to the jump table has the correct offset.
6852 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6853 SetOffset(offset_in_constant_table);
6854
6855 // Compute the offset from the start of the function to this jump table.
6856 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6857
6858 // Populate the jump table with the correct values for the jump table.
6859 int32_t num_entries = switch_instr_->GetNumEntries();
6860 HBasicBlock* block = switch_instr_->GetBlock();
6861 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6862 // The value that we want is the target offset - the position of the table.
6863 for (int32_t i = 0; i < num_entries; i++) {
6864 HBasicBlock* b = successors[i];
6865 Label* l = codegen_->GetLabelOf(b);
6866 DCHECK(l->IsBound());
6867 int32_t offset_to_block = l->Position() - current_table_offset;
6868 assembler->AppendInt32(offset_to_block);
6869 }
6870 }
6871
6872 private:
6873 const HPackedSwitch* switch_instr_;
6874};
6875
Mark Mendellf55c3e02015-03-26 21:07:46 -04006876void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6877 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006878 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006879 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6880 // 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 -04006881 assembler->Align(4, 0);
6882 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006883
6884 // Populate any jump tables.
6885 for (auto jump_table : fixups_to_jump_tables_) {
6886 jump_table->CreateJumpTable();
6887 }
6888
6889 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006890 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006891 }
6892
6893 // And finish up.
6894 CodeGenerator::Finalize(allocator);
6895}
6896
Mark Mendellf55c3e02015-03-26 21:07:46 -04006897Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6898 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6899 return Address::RIP(fixup);
6900}
6901
6902Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6903 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6904 return Address::RIP(fixup);
6905}
6906
6907Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6908 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6909 return Address::RIP(fixup);
6910}
6911
6912Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6913 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6914 return Address::RIP(fixup);
6915}
6916
Andreas Gampe85b62f22015-09-09 13:15:38 -07006917// TODO: trg as memory.
6918void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6919 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006920 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006921 return;
6922 }
6923
6924 DCHECK_NE(type, Primitive::kPrimVoid);
6925
6926 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6927 if (trg.Equals(return_loc)) {
6928 return;
6929 }
6930
6931 // Let the parallel move resolver take care of all of this.
6932 HParallelMove parallel_move(GetGraph()->GetArena());
6933 parallel_move.AddMove(return_loc, trg, type, nullptr);
6934 GetMoveResolver()->EmitNativeCode(&parallel_move);
6935}
6936
Mark Mendell9c86b482015-09-18 13:36:07 -04006937Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6938 // Create a fixup to be used to create and address the jump table.
6939 JumpTableRIPFixup* table_fixup =
6940 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6941
6942 // We have to populate the jump tables.
6943 fixups_to_jump_tables_.push_back(table_fixup);
6944 return Address::RIP(table_fixup);
6945}
6946
Mark Mendellea5af682015-10-22 17:35:49 -04006947void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6948 const Address& addr_high,
6949 int64_t v,
6950 HInstruction* instruction) {
6951 if (IsInt<32>(v)) {
6952 int32_t v_32 = v;
6953 __ movq(addr_low, Immediate(v_32));
6954 MaybeRecordImplicitNullCheck(instruction);
6955 } else {
6956 // Didn't fit in a register. Do it in pieces.
6957 int32_t low_v = Low32Bits(v);
6958 int32_t high_v = High32Bits(v);
6959 __ movl(addr_low, Immediate(low_v));
6960 MaybeRecordImplicitNullCheck(instruction);
6961 __ movl(addr_high, Immediate(high_v));
6962 }
6963}
6964
Roland Levillain4d027112015-07-01 15:41:14 +01006965#undef __
6966
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006967} // namespace x86_64
6968} // namespace art